-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyBoard.cpp
More file actions
368 lines (302 loc) · 10.1 KB
/
Copy pathEnemyBoard.cpp
File metadata and controls
368 lines (302 loc) · 10.1 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// Created by Sptrp on 24.04.21.
//
#include "EnemyBoard.h"
#include <array>
#include <iostream>
/**
* Play board constructor
* @param multiplier the board will be created with multiplier x multiplier size
*/
EnemyBoard::EnemyBoard(const int multiplier) {
board = std::vector< std::vector<bool> >(
multiplier,std::vector<bool>(multiplier, false));
// Flags
shipCounter = 0;
shipDestroyed = false;
goForth = false;
rotation = false;
}
/**
* Play board destructor
*/
EnemyBoard::~EnemyBoard() {
for (int x = 0; x < board.size(); x++) {
// Use swap() to make sure memory was cleared from vectors
std::vector<bool>().swap(board[x]);
}
std::vector< std::vector<bool> >().swap(board);
std::vector< std::vector<int> >().swap(blacklist);
}
/**
* Check if field is in blacklist
* @param col
* @param row
* @return true if yes, otherwise false
*/
bool EnemyBoard::IsInBlacklist(int col, int row) {
if (!blacklist.empty()) {
// Iterate through blacklist and check if coordinate already in there
for (int ptr = 0; ptr < blacklist.size(); ptr++) {
int blCol = blacklist[ptr][0];
// Check first vector 1 (columns)
if (col == blCol) {
int blRow = blacklist[ptr][1];
// Check row only if column match for shorter runtime
if (row == blRow)
return true;
}
}
}
return false;
}
/**
* Start new attack
* @param continueAttack true if the attack should be continued
*/
void EnemyBoard::StartAttacking(bool continueAttack) {
std::cout << shipCounter << std::flush;
std::cout << " ships destroyed!" << std::endl;
if (shipCounter > 5) { return; }
// Start new attack if continue flag is false
if (!continueAttack) {
std::cout << "Starting new attack" << std::endl;
std::vector<int> newCoord = RandomizeCoordinate();
if (IsInBlacklist(newCoord[0], newCoord[1])) {
StartAttacking(false);
} else {
bool hit = AttackField(newCoord[0], newCoord[1]);
if (shipDestroyed) {
shipCounter++;
// Escape attacking program if ship limit reached TODO: change to 6
if (shipCounter > 5) { return; }
std::cout << shipCounter << std::flush;
std::cout << " ships destroyed!" << std::endl;
// Otherwise start new attack
StartAttacking(false);
return;
}
// If ship met, save field in cash and continue attack from this point
if (hit) {
coordinateCash = newCoord;
StartAttacking(true);
return;
} else {
// If miss, return
return;
}
}
// Otherwise continue attack from last saved point
} else {
ContinueAttacking(coordinateCash[0], coordinateCash[1]);
}
}
/**
* Try to attack field and set goForth if point need to be saved
* @param col
* @param row
* @return
*/
bool EnemyBoard::AttackField(int col, int row) {
std::vector<int> coord;
coord.push_back(col);
coord.push_back(row);
// TODO Send hit on NAO
if (!IsInBlacklist(col, row)) {
std::cout << col << std::flush;
std::cout << " : " << std::flush;
std::cout << row << std::endl;
// TODO: input for hit result
int shipMet;
std::cout << "Ship met? : ";
std::cin >> shipMet;
if (shipMet == 1) {
// TODO: input if ship met
std::cout << "Hit!" << std::endl;
board[col][row] = true;
// TODO: input if ship destroyed
int destroyed;
std::cout << "Ship destroyed? : ";
std::cin >> destroyed;
if (destroyed == 1) {
goForth = false;
shipDestroyed = true;
blacklist.push_back(coord);
return true;
}
std::cout << "Hit but not destroyed!" << std::endl;
goForth = true;
shipDestroyed = false;
blacklist.push_back(coord);
return true;
}
std::cout << "Missed!" << std::endl;
std::cout << "Miss from Attack" << std::endl;
shipDestroyed = false;
blacklist.push_back(coord);
return false;
}
return false;
}
/**
* Continue attack from last saved point
* Current rotation will be checked
* If Rotation true then go vertical, otherwise horizontal
*/
void EnemyBoard::ContinueAttacking(int col, int row) {
int attack;
std::cout << "Continue attack from: " << std::flush;
std::cout << coordinateCash[0] << std::flush;
std::cout << " : " << std::flush;
std::cout << coordinateCash[1] << std::endl;
// Change column if horizontal, otherwise change row
attack = !rotation ? AttackRow(col + 1, row, true) : AttackColumn(col, row + 1, true);
switch (attack) {
// Ship destroyed - start new attack
case 3:
shipCounter++;
StartAttacking(false);
// Miss - jump out
case 2:
return;
// Out of bounds or blacklist - try other direction
case 1:
// Change column reversed if horizontal, otherwise change row reversed
attack = !rotation ? AttackRow(col - 1, row, false) : AttackColumn(col, row - 1, false);
switch (attack) {
// Ship destroyed
case 3:
shipCounter++;
StartAttacking(false);
// Miss
case 2:
return;
// Out of bounds or blacklist - try again other rotation
case 1:
rotation = !rotation;
ContinueAttacking(col, row);
default: return;
}
default: return;
}
}
/**
* Attacking column, so long the ship not destroyed or reached end of playing area
* @param col
* @param row
* @param downwards to switch directions
* @return 3 if ship destroyed, 2 if attack missed, 1 if out of bounds or field is blacklisted
*/
int EnemyBoard::AttackColumn(int col, int row, bool downwards) {
std::cout << "Starting attack at column" << std::endl;
while (row >= 0) {
// Check if within grid and if field is in blacklist and start attacking next fields
if (IsWithinGrid(col, row) && !IsInBlacklist(col, row)) {
std::vector<int> coord;
coord.push_back(col);
coord.push_back(row);
std::cout << "Attacking :" << std::endl;
std::cout << col << std::flush;
std::cout << " : " << std::flush;
std::cout << row << std::endl;
int hit = AttackField(col, row);
if (!shipDestroyed && hit) {
blacklist.push_back(coord);
if (downwards) {
row++;
} else {
row--;
}
} else if (shipDestroyed && hit) {
// Start new attack and exit the loop if ship destroyed
return 3; // Start new Attack if ship destroyed
} else if (!hit) {
std::cout << "Miss from AttackColumn" << std::endl;
return 2;
}
} else {
std::cout << "AttackColumn Not within Grid os in blacklist" << std::endl;
return 1;
}
}
}
/**
* Attacking row, so long the ship not destroyed or reached end of playing area
* @param col
* @param row
* @param forwards to switch directions
* @return 3 if ship destroyed, 2 if attack missed, 1 if out of bounds or field is blacklisted
*/
int EnemyBoard::AttackRow(int col, int row, bool forwards) {
std::cout << "Starting attack at row" << std::endl;
while (col >= 0) {
// Check if within grid and if field is in blacklist and start attacking next fields
if (IsWithinGrid(col, row) && !IsInBlacklist(col, row)) {
std::vector<int> coord;
coord.push_back(col);
coord.push_back(row);
std::cout << "Attacking :" << std::endl;
std::cout << col << std::flush;
std::cout << " : " << std::flush;
std::cout << row << std::endl;
int hit = AttackField(col, row);
if (!shipDestroyed && hit) {
blacklist.push_back(coord);
if (forwards) {
col++;
} else {
col--;
}
} else if (shipDestroyed && hit) {
// Start new attack and exit the loop if ship destroyed
return 3; // Start new Attack if ship destroyed
} else if (!hit) {
std::cout << "Miss from AttackRow" << std::endl;
return 2;
}
} else {
std::cout << "AttackRow Not within Grid or in blacklist" << std::endl;
return 1;
}
}
}
/**
* Check if field is still in bounds of play board
* @param col
* @param row
* @return true if yes, otherwise false
*/
bool EnemyBoard::IsWithinGrid(int col, int row) {
// Is false if out of bounds
if ((col < 0) || (row < 0) || (col > 6) || (row > 6)) {
return false;
}
return true;
}
/**
* Create random coordinate
* @return coordinate
*/
std::vector<int> EnemyBoard::RandomizeCoordinate() {
std::vector<int> coord;
coord.push_back(rand() % 7);
coord.push_back(rand() % 7);
return coord;
}
/**
* Print out current enemy board
*/
void EnemyBoard::PrintBoard() {
std::cout << "!! ENEMY BOARD !!" << std::endl;
for (int row = 0; row < board.size(); row++) {
for (int col = 0; col < board.size(); col++) {
if (col != board.size() - 1) {
// Print values 0 - 6 of each row
board[col][row] ? std::cout << " X " << std::flush : std::cout << " _ " << std::flush;
} else {
// New line if 7
board[col][row] ? std::cout << " X " << std::endl : std::cout << " _ " << std::endl;
}
}
}
}