-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
428 lines (361 loc) · 11.1 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//Andrew Hoyle
//implementation of Board class
#include "MSEnum.h"
#include "Board.h"
#include "Tile.h"
Board::~Board()
{
//goes through and clears dynamic grid of vector<vector<int> >
//get those columns
for(int i=0; i<Row; ++i)
{
this->grid[i].clear();
}
//get the vector of rows
this->grid.clear();
//other data members, (not that important)
//make sure win check ocurrs AFTER destructor is called b/c
// the unClickedCount is set to 0
unClickedCount=0;
Row=0;
Column=0;
mineCount=0;
}
Board::Board(const Board& b)
{
//get data members from b
this->Row=b.Row;
this->Column=b.Column;
this->mineCount=b.mineCount;
this->unClickedCount=b.unClickedCount;
//resize the Board to the dim. given
this->grid.resize(Row);
for( int i = 0; i < Row; ++i)
{
this->grid[i].resize(Column);
}
//copy Tile values over
for(int i=0; i<Row; ++i)
{
for(int j=0; j<Column; ++j)
{
//uses overloaded = in Tile
grid[i][j]=b.grid[i][j];
}
}
}
Board::Board(int mc, int row, int col)
{
//resize the Board to the dim. given
this->grid.resize(row);
for( int i = 0; i < row; ++i)
{
this->grid[i].resize(col);
}
//get data members from params
this->Row=row;
this->Column=col;
this->mineCount=mc;
//account for edges and mines
//when this value is 0 after subtracting mineCount,
// winCheck will be true
this->unClickedCount=row*col-(2*row)-(2*col)+4;
//discovered, question, flag, edge, mine == false for all,
for(int i=0; i<Row; ++i)
{
for(int j=0; j<Column; ++j)
{
this->grid[i][j].flag=false;
this->grid[i][j].question=false;
this->grid[i][j].discovered=false;
this->grid[i][j].edge=false;
this->grid[i][j].mine=false;
this->grid[i][j].visited=false;
}
}
//make edges=true, and discovered=true, ALL EDGES ARE discovered!!!
//top/bottom rows
for(int j=0; j<col; ++j)
{
this->grid[0][j].edge=true;
this->grid[0][j].discovered=true;
this->grid[row-1][j].edge=true;
this->grid[row-1][j].discovered=true;
}
//left/right cols
for(int i=0; i<row; ++i)
{
this->grid[i][0].edge=true;
this->grid[i][0].discovered=true;
this->grid[i][col-1].edge=true;
this->grid[i][col-1].discovered=true;
}
//---mines
//for mine selection
std::srand(std::time(NULL));
//add mines to the grid, change mine to true IF not already mine,
// AND if not edge
for(int count=0; count < this->mineCount;)
{
//randomly pick mine
int randI = std::rand() % row;
int randJ = std::rand() % col;
if(this->grid[randI][randJ].mine==false && this->grid[randI][randJ].edge==false)
{
this->grid[randI][randJ].mine=true;
++count;
}
}
//update the Tiles' adjacent matrices
this->updateAllTiles();
}
void Board::updateAllTiles()
{
for(int i=0; i<Row; ++i)
{
for(int j=0; j<Column; ++j)
{
if(this->grid[i][j].edge != true)
{
//should only be done by constructor?
this->grid[i][j].adjMines=countMines(i,j);
this->grid[i][j].adjQuestions=countQuestions(i,j);
this->grid[i][j].adjFlags=countFlags(i,j);
this->grid[i][j].adjUnknowns=countUnknowns(i,j);
}
}
}
}
int Board::countMines(int i, int j)
{
int tempMineCount=0;
//looks at 8 surrounding Tiles
for(int I=i-1; I<i-1+3; ++I)
{
for(int J=j-1; J<j-1+3; ++J)
{
if(this->grid[I][J].mine==true && !(i==I && j==J) )
++tempMineCount;
}
}
return tempMineCount;
}
int Board::countQuestions(int i, int j)
{
int tempQCount=0;
//looks at 8 surrounding Tiles by making 3x3 grid and ignoring itself
for(int I=i-1; I<i-1+3; ++I)
{
for(int J=j-1; J<j-1+3; ++J)
{
//don't count yourself!
if(this->grid[I][J].question==true && !(i==I && j==J) )
++tempQCount;
}
}
return tempQCount;
}
int Board::countFlags(int i, int j)
{
int tempFlagCount=0;
//looks at 8 surrounding Tiles
for(int I=i-1; I<i-1+3; ++I)
{
for(int J=j-1; J<j-1+3; ++J)
{
if(this->grid[I][J].flag==true && !(i==I && j==J) )
++tempFlagCount;
}
}
return tempFlagCount;
}
int Board::countUnknowns(int i, int j)
{
int tempUndiscoveredCount=0;
//looks at 8 surrounding Tiles
for(int I=i-1; I<i-1+3; ++I)
{
for(int J=j-1; J<j-1+3; ++J)
{
//cannot include flags in the unknowns count
if(this->grid[I][J].discovered==false &&
this->grid[I][J].flag==false &&
!(i==I && j==J) )
++tempUndiscoveredCount;
}
}
return tempUndiscoveredCount;
}
void Board::displayBoard()
{
//things to display
char EDGE = '#';
char UNCLICKED = '+';
char FLAG = 'F';
char QUEST = '?';
char MINE = '*';
//the grid and row numbers
for(int i=0; i<Row; ++i)
{
for(int j=0; j<Column; ++j)
{
if(this->grid[i][j].edge==true)
{
std::cout << EDGE;
continue;
}
//updated to not print 0s, just spaces
else if(this->grid[i][j].discovered==true && this->grid[i][j].adjMines!=0)
{
std::cout << this->grid[i][j].adjMines;
continue;
}
else if(this->grid[i][j].discovered==true && this->grid[i][j].adjMines==0)
{
std::cout << ' ';
continue;
}
else if(this->grid[i][j].flag==true)
{
std::cout << FLAG;
continue;
}
else if(this->grid[i][j].question==true)
{
std::cout << QUEST;
continue;
}
else
std::cout << UNCLICKED;
}
std::cout << std::endl;
}
}
void Board::displayBoardCheat()
{
//things to display
char EDGE = '#';
char UNCLICKED = '+';
char FLAG = 'F';
char QUEST = '?';
char MINE = '*';
//the grid and row numbers
for(int i=0; i<Row; ++i)
{
for(int j=0; j<Column; ++j)
{
if(this->grid[i][j].edge==true)
{
std::cout << EDGE;
continue;
}
//updated to not print 0s, just spaces
else if(this->grid[i][j].adjMines!=0 && this->grid[i][j].mine==false)
{
std::cout << this->grid[i][j].adjMines;
continue;
}
else if(this->grid[i][j].adjMines==0 && this->grid[i][j].mine==false)
{
std::cout << ' ';
continue;
}
else
{
std::cout << MINE;
continue;
}
}
std::cout << std::endl;
}
}
void Board::deathChecker(bool& DC, int& curI, int& curJ, const clickType& curCL)
{
if(this->grid[curI][curJ].mine==true && curCL==click)
DC=true;
}
void Board::winChecker(bool& WC)
{
//update the unClickedCount
this->updateUnClickedCount();
if( (this->unClickedCount-this->mineCount)==0 )
WC=true;
}
//you DON'T need to subtract edges because they are .discovered=true
void Board::updateUnClickedCount()
{
//reset value
this->unClickedCount=0;
for(int i=0; i<Row; ++i)
{
for(int j=0; j<Column; ++j)
{
if(this->grid[i][j].discovered==false)
++this->unClickedCount;
}
}
}
//exp bool set to true for algorithms' analyses
void Board::updateClickedTile(int& curI, int& curJ, const clickType& curCL)
{
//if Tile was clicked on
if(curCL==click)
{
//uncover the Tile's mask, decrement unClickedCount
this->grid[curI][curJ].discovered=true;
//call expansion
if(this->grid[curI][curJ].adjMines==0)
{
this->expandZeros(curI, curJ);
}
}
//if flag clickType and not flag
else if(curCL==flag && this->grid[curI][curJ].flag==false)
this->grid[curI][curJ].flag=true;
//if flag clickType and flag, cancel flag
else if(curCL==flag && this->grid[curI][curJ].flag==true)
this->grid[curI][curJ].flag=false;
//if ?, should not happen///////////////////////////////////////////////////
else
this->grid[curI][curJ].question=true;
}
//ALL edges are .discovered==true
//.visited is used for recursion help
void Board::expandZeros(int curI, int curJ)
{
//mark .visted=true
//prevents infinite loop so it doesn't go back to itself
this->grid[curI][curJ].visited=true;
//up
this->grid[curI-1][curJ].discovered=true;
if(this->grid[curI-1][curJ].adjMines==0 && this->grid[curI-1][curJ].edge==false && this->grid[curI-1][curJ].visited!=true)
this->expandZeros(curI-1, curJ);
//up right
this->grid[curI-1][curJ+1].discovered=true;
if(this->grid[curI-1][curJ+1].adjMines==0 && this->grid[curI-1][curJ+1].edge==false && this->grid[curI-1][curJ+1].visited!=true)
this->expandZeros(curI-1, curJ+1);
//right
this->grid[curI][curJ+1].discovered=true;
if(this->grid[curI][curJ+1].adjMines==0 && this->grid[curI][curJ+1].edge==false && this->grid[curI][curJ+1].visited!=true)
this->expandZeros(curI, curJ+1);
//down right
this->grid[curI+1][curJ+1].discovered=true;
if(this->grid[curI+1][curJ+1].adjMines==0 && this->grid[curI+1][curJ+1].edge==false && this->grid[curI+1][curJ+1].visited!=true)
this->expandZeros(curI+1, curJ+1);
//down
this->grid[curI+1][curJ].discovered=true;
if(this->grid[curI+1][curJ].adjMines==0 && this->grid[curI+1][curJ].edge==false && this->grid[curI+1][curJ].visited!=true)
this->expandZeros(curI+1, curJ);
//down left
this->grid[curI+1][curJ-1].discovered=true;
if(this->grid[curI+1][curJ-1].adjMines==0 && this->grid[curI+1][curJ-1].edge==false && this->grid[curI+1][curJ-1].visited!=true)
this->expandZeros(curI+1, curJ-1);
//left
this->grid[curI][curJ-1].discovered=true;
if(this->grid[curI][curJ-1].adjMines==0 && this->grid[curI][curJ-1].edge==false && this->grid[curI][curJ-1].visited!=true)
this->expandZeros(curI, curJ-1);
//up left
this->grid[curI-1][curJ-1].discovered=true;
if(this->grid[curI-1][curJ-1].adjMines==0 && this->grid[curI-1][curJ-1].edge==false && this->grid[curI-1][curJ-1].visited!=true)
this->expandZeros(curI-1, curJ-1);
}