-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoat.cpp
More file actions
216 lines (198 loc) · 3.35 KB
/
Copy pathBoat.cpp
File metadata and controls
216 lines (198 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "Boat.h"
Boat::Boat()
{
m_x = 0;
m_y = 0;
m_ascii = 0;
m_size = 0;
m_position[2][5] = { };
m_player = 0;
m_name = " ";
counter = 0;
}
Boat::~Boat()
{
if (m_player == 1)
{
cout << "You sank their " << getName() << endl;
}
else
{
cout << "They sank your " << getName() << endl;
}
}
//METHODS
void Boat::gotoXY(int x, int y)
{
HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x, y };
SetConsoleCursorPosition(ConsoleHandle, position);
CONSOLE_CURSOR_INFO CursorInfo;
CursorInfo.bVisible = false;
CursorInfo.dwSize = 10;
SetConsoleCursorInfo(ConsoleHandle, &CursorInfo);
}
void Boat::generateShip()
{
int orient = rand() % 10;
int boundary = 10- m_size;
if (m_player == 1)
{
m_x = rand() % boundary + 15;
m_y = rand() % boundary + 2;
m_xprev = m_x - 15;
m_yprev = m_y - 2;
if (orient % 2 == 0) //HORIZONTAL
{
m_ascii = 205;
for (int i = 0; i < m_size; i++)
{
m_position[0][i] = m_x - 15;
m_position[1][i] = m_y - 2;
m_x++;
}
}
else //VERTICAL
{
m_ascii = 186;
for (int i = 0; i < m_size; i++)
{
m_position[0][i] = m_x - 15;
m_position[1][i] = m_y - 2;
m_y++;
}
}
}
else
{
m_x = rand() % boundary + 15;
m_y = rand() % boundary + 15;
m_xprev = m_x - 15;
m_yprev = m_y - 15;
if (orient % 2 == 0) //HORIZONTAL
{
m_ascii = 205;
for (int i = 0; i < m_size; i++)
{
m_position[0][i] = m_x - 15;
m_position[1][i] = m_y - 15;
m_x++;
}
}
else //VERTICAL
{
m_ascii = 186;
for (int i = 0; i < m_size; i++)
{
m_position[0][i] = m_x - 15;
m_position[1][i] = m_y - 15;
m_y++;
}
}
}
}
void Boat::drawShip()
{
if (m_player == 1)
{
for (int i = 0; i < m_size; i++)
{
gotoXY((m_position[0][i] + 15), (m_position[1][i] + 2));
cout << (char)m_ascii;
}
}
else
{
for (int i = 0; i < m_size; i++)
{
gotoXY((m_position[0][i] + 15), (m_position[1][i] + 15));
cout << (char)m_ascii;
}
}
}
//VIRTUAL METHOD (outputs sound when hit)
void AircraftCarrier::sound()
{
cout << "BOOOOOOOM!" << endl;
}
void Battleship::sound()
{
cout << "BANG!" << endl;
}
void Submarine::sound()
{
cout << "BLUBBADUBDUB!" << endl;
}
void Cruiser::sound()
{
cout << "PLOP!" << endl;
}
void Destroyer::sound()
{
cout << "KERPLUNK!" << endl;
}
#pragma region GETTERS
int Boat::getX()
{
return m_x;
}
int Boat::getY()
{
return m_y;
}
int Boat::getSize()
{
return m_size;
}
string Boat::getName()
{
return m_name;
}
int Boat::getPosition(int index, int index2)
{
return m_position[index][index2];
}
#pragma endregion
#pragma region SETTERS
void Boat::setX(int x)
{
m_x = x;
}
void Boat::setY(int y)
{
m_y = y;
}
void Boat::setPlayer(int player)
{
m_player = player;
}
void Boat::setPosition(int index, int index2, int value)
{
m_position[index][index2] = value;
}
#pragma endregion
AircraftCarrier::AircraftCarrier()
{
m_size = 5;
m_name = "Aircraft Carrier";
}
Battleship::Battleship()
{
m_size = 4;
m_name = "Battleship";
}
Submarine::Submarine()
{
m_size = 3;
m_name = "Submarine";
}
Cruiser::Cruiser()
{
m_size = 3;
m_name = "Cruiser";
}
Destroyer::Destroyer()
{
m_size = 2;
m_name = "Destroyer";
}