-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
375 lines (305 loc) · 8.31 KB
/
Board.cpp
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
369
370
371
372
373
374
375
//
// Board.cpp
// battleeee
//
// Created by Ingy on 12/29/14.
// Copyright (c) 2014 Ingy. All rights reserved.
//
#include "Board.h"
Board::Board() // Constructor
{
SH[0].setSize(4);// Battleship
SH[1].setSize(3);// Cruiser
SH[2].setSize(3);// Cruiser
SH[3].setSize(2);// Destroyer
SH[4].setSize(2);// Destroyer
SH[5].setSize(2);// Destroyer
for (int i=6; i<10;i++)
SH[i].setSize(1);// Submarine
bool comp=true;
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
this->B[i][j].setPosition(j, i, comp);
}
Board::Board(bool comp) // Constructor
{
SH[0].setSize(4);// Battleship
SH[1].setSize(3);// Cruiser
SH[2].setSize(3);// Cruiser
SH[3].setSize(2);// Destroyer
SH[4].setSize(2);// Destroyer
SH[5].setSize(2);// Destroyer
for (int i=6; i<10;i++)
SH[i].setSize(1);// Submarine
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
this->B[i][j].setPosition(j, i, comp);
}
Board::~Board() // Destructor
{
}
bool Board :: placeShip(int row, int col, int num)
{
Ship * p;
p= & SH[num];
bool valid = true;
if (p->isH())
{
for(int i=-1; i<2; i++)
for(int j=-1; j<SH[num].getSize()+1; j++)
{
if(row+i >= 0 && row+i < 10 && col+j >= 0 && col+j < 10)
{
if(B[row+i][col+j].hasShip())
valid=false;
}
else
{
//valid= false;
}
}
}
else
for(int i=-1; i<SH[num].getSize()+1; i++)
for(int j=-1; j<2; j++)
{
if(row+i >= 0 && row+i < 10 && col+j >= 0 && col+j < 10)
{
if(B[row+i][col+j].hasShip())
valid=false;
}
else
{
//valid = false;
}
}
if(valid)
{
if(p->isH())
for (int i=0; i<SH[num].getSize();i++)
{
p->setPosition(row, col);
B[row][col+i].placeShip(p);
}
else
for (int i=0; i<SH[num].getSize();i++)
{
p->setPosition(row, col);
B[row+i][col].placeShip(p);
}
p->setPosition(row, col);
}
else cout << "CANNOT PLACE SHIP";
return valid;
}
bool Board::changePos(int row, int col, int toRow, int toCol)
{
Ship * p= this->getShip(row, col);
bool valid=true;
if(p->isH())
{
for(int i=-1; i<2; i++)
for(int j=-1; j<p->getSize()+1; j++)
if(B[toRow+i][toCol+j].hasShip())
valid=false;
}
else
for(int i=-1; i<p->getSize()+1; i++)
for(int j=-1; j<2; j++)
if(B[toRow+i][toCol+j].hasShip())
valid=false;
if(!B[toRow][toCol].hasShip())
{
B[row][col].removeShip();
B[toRow][toCol].placeShip(p);
}
return valid;
}// change the position of a ship from row, col to toRow, toCol
void Board :: initializeR() // initializes the board with ships randomly placed
{
int r,c;
bool valid; bool h;
for(int w=0; w<10; w++)
{
do
{
valid =true;
h= rand()%2;
if (h)
{
r =rand ()%(10);
c =rand()%(10-SH[w].getSize()+1);
}
else
{
r =rand ()%(10-SH[w].getSize()+1);
c =rand()%(10);
}
SH[w].setOrientation(h);
valid=placeShip(r, c, w);
} while (!valid);
SH[w].setPosition(r, c);
}
}
bool Board :: isFinished() // checks if all the ships are hit.
{
bool f=true;
int i=0;
while(f && i<10)
{
for(int j=0; j<10; j++)
if(B[i][j].hasShip() && !B[i][j].shipSunk())
f=false;
i++;
}
return f;
}
bool Board ::isHit (int row, int col) // checks if a cell has been hit knowing the x(col) and y(row)position
{
return B[row][col].isHit();
}
bool Board ::hasShip (int row, int col) // checks if a cell has been hit knowing the x(col) and y(row)position
{
return B[row][col].hasShip();
}
void Board :: attack (int row, int col) // attacks a cell with the row and column.
{
B[row][col].hitCell();
if(B[row][col].shipSunk())
{
Ship * ship;
ship=B[row][col].getShip();
row = (ship->getPosition()).first.first;
col = (ship->getPosition()).first.second;
if (ship->isH())
{
for (int i=-1; i<2; i++)
for (int j=-1; j<ship->getSize()+1; j++)
if(withinBoundaries( row + i, col + j))
//if(!B[row+i][col+j].isHit())
B[row+i][col+j].hitCell();
}
else
{
for (int i=-1; i<ship->getSize()+1; i++)
for (int j=-1; j<2; j++)
if(withinBoundaries(row + i, col + j))
//if(!B[row+i][col+j].isHit())
B[row+i][col+j].hitCell();
}
}
}
bool Board :: withinBoundaries(int i, int j)
{
return(i>=0 && i<=9 && j>=0 && j<= 9);
}
void Board:: attack (Cell * p)
{
B[p->getPosition().second][p->getPosition().first].hitCell();
if(B[p->getPosition().second][p->getPosition().first].shipSunk())
{
Ship * ship;
ship=B[p->getPosition().second][p->getPosition().first].getShip();
int row = (ship->getPosition()).first.first;
int col = (ship->getPosition()).first.second;
if (ship->isH())
{
for (int i=-1; i<2; i++)
for (int j=-1; j<ship->getSize()+1; j++)
if(withinBoundaries( row + i, col + j))
//if(!B[row+i][col+j].isHit())
B[row+i][col+j].hitCell();
}
else
{
for (int i=-1; i<ship->getSize()+1; i++)
for (int j=-1; j<2; j++)
if(withinBoundaries(row + i, col + j))
//if(!B[row+i][col+j].isHit())
B[row+i][col+j].hitCell();
}
}
}// attacks a cell
Cell* Board:: getCell (int r, int c)
{
Cell * y;
y=&B[r][c];
return y;
}// retrieve a cell by its row and column
Ship* Board:: getShip (int r, int c)
{
Ship * y;
y=B[r][c].getShip();
return y;
}// retrieve a ship by its row and column
Cell* Board::upCell(Cell * p)
{
if(p->getPosition().second-1 >= 0){
Cell * n = p;
n = &B[p->getPosition().second-1][p->getPosition().first];
return n;
}
else
return NULL;
}
Cell* Board::downCell(Cell * p)
{
if(p->getPosition().second +1 < 10)
{
Cell * n = p;
n = &B[p->getPosition().second +1][p->getPosition().first];
return n;
}
else
return NULL;
}
Cell* Board::rightCell(Cell * p)
{
if(p->getPosition().first+1 < 10)
{
Cell * n = p;
n = & B[p->getPosition().second][p->getPosition().first+1];
return n;
}
else
return NULL;
}
Cell* Board::leftCell(Cell * p)
{
if(p->getPosition().first-1 >= 0)
{
Cell * n = p;
n = & B[p->getPosition().second][p->getPosition().first-1];
return n;
}
else
return NULL;
}
void Board:: drawB(RenderWindow * n, bool comp)
{
for (int i=0; i<10; i++)
SH[i].drawShip(n, comp);
for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
if(B[i][j].isHit())B[i][j].drawC(n);
}
void Board :: unDraw()
{
for (int i=0; i<10; i++)
SH[i].unDrawn();
}
void Board:: debug()
{
cout <<"--------------------------------------------------"<<endl;
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
if(B[i][j].hasShip())
cout << 'X';
else cout << '$';
}
cout <<endl;
}
cout <<"--------------------------------------------------"<<endl;
}