-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
312 lines (294 loc) · 8.59 KB
/
Board.cpp
File metadata and controls
312 lines (294 loc) · 8.59 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
#include "Board.h"
Board::Board(int height, int width, int mines) {
this->width = width;
this->height = height;
this->mines = mines;
this->flags = 0;
}
void Board::NewBoard() {
//Resetting Board
this->tiles.clear();
this->gameOver = false;
this->victory = false;
//Creating board of empty tiles
Tile newTile;
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
sf::Vector2f coordinates = sf::Vector2f((float)(j) * 32, float(i) * 32);
//Creating a single tile at designated xy coordinates, empty tile for now
newTile = Tile(coordinates, "tile_hidden", 0);
this->tiles.push_back(newTile);
}
}
//Generating list of mine locations
vector<int> mineLocations;
mineLocations.push_back(Random::Int(0, (this->height * this->width) - 1));
while (mineLocations.size() < this->mines) {
int newMine = Random::Int(0, (this->height * this->width) - 1);
bool duplicateMine = false;
for (int k = 0; k < mineLocations.size(); k++) {
if (newMine == mineLocations[k]) {
duplicateMine = true;
break;
}
}
if (duplicateMine == false) {
mineLocations.push_back(newMine);
}
}
//Setting mineNumber as 9, isMine to true
for (int l = 0; l < mineLocations.size(); l++) {
this->tiles[mineLocations[l]].mineNumber = 9;
this->tiles[mineLocations[l]].isMine = true;
if (this->inDebug) {
this->tiles[mineLocations[l]].SetRevealedTile("hidden");
}
}
TileNeighbors();
}
void Board::TestBoard(vector<char> testTiles) {
//Resetting Board
this->tiles.clear();
this->gameOver = false;
this->victory = false;
this->mines = 0;
Tile newTile;
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
sf::Vector2f newCoordinates = sf::Vector2f((float)(j) * 32, (float)(i) * 32);
int index = (this->width * i) + j;
if (testTiles[index] == '1') {
if (this->inDebug) {
newTile = Tile(newCoordinates, "mine", 9);
newTile.SetRevealedTile("tile_hidden");
}
else {
newTile = Tile(newCoordinates, "tile_hidden", 9);
}
this->mines += 1;
}
else {
newTile = Tile(newCoordinates, "tile_hidden", 0);
}
tiles.push_back(newTile);
}
}
TileNeighbors();
}
Tile& Board::GetTile(unsigned int tileIndex) {
return this->tiles[(tileIndex)];
}
int Board::GetWidth() {
return this->width;
}
int Board::GetHeight() {
return this->height;
}
int Board::GetMines() {
return this->mines;
}
int Board::GetFlags() {
int flagCount = 0;
for (int i = 0; i < height * width; i++) {
if (tiles.at(i).isFlag == true) {
flagCount += 1;
}
}
this->flags = flagCount;
return this->flags;
}
void Board::CheckGameOver() {
if (this->gameOver == true) {
this->victory = false;
for (int i = 0; i < this->tiles.size(); i++) {
this->tiles[i].canClick = false;
this->tiles[i].isRevealed = true;
if (this->tiles[i].isMine == true) {
this->tiles[i].SetImage("mine");
if(this->tiles[i].isFlag){
this->tiles[i].SetDebugFlag("flag");
}
}
}
}
else {
this->gameOver == false;
}
}
void Board::CheckVictory() {
int revealedTiles = 0;
//checking and counting if all tiles that are not mines are revealed
for (int i = 0; i < tiles.size(); i++) {
if (this->tiles[i].isRevealed == true) {
revealedTiles += 1;
}
}
//If all tiles are revealed besides mines which must be flagged, the player has won
if (this->tiles.size() - revealedTiles - GetFlags() == 0) {
for (int i = 0; i < this->tiles.size(); i++) {
if (this->tiles[i].isMine == true && this->tiles[i].isFlag == false) {
this->victory = false;
break;
}
else if (this->tiles[i].isMine == false && this->tiles[i].isFlag == true) {
this->victory = false;
break;
}
else {
this->victory = true;
}
}
}
else {
this->victory = false;
}
if (this->victory == true) {
for (int i = 0; i < this->tiles.size(); i++) {
tiles[i].canClick = false;
tiles[i].isRevealed = true;
}
}
}
void Board::DebugMode() {
if (inDebug == false) {
inDebug = true;
for (int i = 0; i < tiles.size(); i++) {
if (tiles[i].isMine == true && tiles[i].isFlag == true) {
tiles[i].SetDebugFlag("flag");
tiles[i].SetImage("mine");
}
else if(tiles[i].isMine == true && tiles[i].isFlag == false) {
tiles[i].SetDebugFlag("tile_hidden");
tiles[i].SetImage("mine");
}
}
}
else {
inDebug = false;
for (int i = 0; i < tiles.size(); i++) {
if (tiles[i].isMine == true && tiles[i].isFlag == true) {
tiles[i].SetDebugFlag("tile_hidden");
tiles[i].SetImage("flag");
}
else if (tiles[i].isMine == true && tiles[i].isFlag == false) {
tiles[i].SetRevealedTile("tile_hidden");
tiles[i].SetImage("tile_hidden");
}
}
}
}
void Board::TileNeighbors() {
//Going thru all tiles and adding their neighbors
for (int i = 0; i < tiles.size(); i++) {
//Setting top left corner tile's neighbors
if (i == 0) {
this->tiles[i].newNeighbor(&tiles[1]);
this->tiles[i].newNeighbor(&tiles[width]);
this->tiles[i].newNeighbor(&tiles[width + 1]);
}
//Setting bottom left corner tile's neighbors
else if (i == (width * height) - width) {
this->tiles[i].newNeighbor(&tiles[i - width]);
this->tiles[i].newNeighbor(&tiles[i - width + 1]);
this->tiles[i].newNeighbor(&tiles[i + 1]);
}
//Setting top right corner tile's neighbors
else if (i == width - 1) {
this->tiles[i].newNeighbor(&tiles[i - 2]);
this->tiles[i].newNeighbor(&tiles[i * 2]);
this->tiles[i].newNeighbor(&tiles[(i * 2) + 1]);
}
//Setting bottom right corner tile's neighbors
else if (i == (width * height) - 1) {
this->tiles[i].newNeighbor(&tiles[i - width]);
this->tiles[i].newNeighbor(&tiles[i - width - 1]);
this->tiles[i].newNeighbor(&tiles[i - 1]);
}
//Setting left edge tiles
else if (i % width == 0) {
this->tiles[i].newNeighbor(&tiles[i + 1]);
this->tiles[i].newNeighbor(&tiles[i + width]);
this->tiles[i].newNeighbor(&tiles[i - width]);
this->tiles[i].newNeighbor(&tiles[i - width + 1]);
this->tiles[i].newNeighbor(&tiles[i + width + 1]);
}
//Setting top edge tiles
else if (i > 0 && i < width - 1) {
this->tiles[i].newNeighbor(&tiles[i - 1]);
this->tiles[i].newNeighbor(&tiles[i + 1]);
this->tiles[i].newNeighbor(&tiles[i + width]);
this->tiles[i].newNeighbor(&tiles[i + width + 1]);
this->tiles[i].newNeighbor(&tiles[i + width - 1]);
}
//Setting right edge tiles
else if ((i + 1) % width == 0) {
this->tiles[i].newNeighbor(&tiles[i - 1]);
this->tiles[i].newNeighbor(&tiles[i + width]);
this->tiles[i].newNeighbor(&tiles[i + width - 1]);
this->tiles[i].newNeighbor(&tiles[i - width]);
this->tiles[i].newNeighbor(&tiles[i - width - 1]);
}
//Setting bottom edge tiles
else if (i > (width * height) - width && i < (width * height) - 1) {
this->tiles[i].newNeighbor(&tiles[i + 1]);
this->tiles[i].newNeighbor(&tiles[i - 1]);
this->tiles[i].newNeighbor(&tiles[i - width]);
this->tiles[i].newNeighbor(&tiles[i - width + 1]);
this->tiles[i].newNeighbor(&tiles[i - width - 1]);
}
//Setting "middle" tiles
else {
this->tiles[i].newNeighbor(&tiles[i + 1]);
this->tiles[i].newNeighbor(&tiles[i - 1]);
this->tiles[i].newNeighbor(&tiles[i + width]);
this->tiles[i].newNeighbor(&tiles[i - width]);
this->tiles[i].newNeighbor(&tiles[i + width + 1]);
this->tiles[i].newNeighbor(&tiles[i - width + 1]);
this->tiles[i].newNeighbor(&tiles[i + width - 1]);
this->tiles[i].newNeighbor(&tiles[i - width - 1]);
}
}
//Setting numbers for neighbor tiles if necessary
int nearbyMines = 0;
for (int j = 0; j < this->tiles.size(); j++) {
//Checking if mine
if (this->tiles[j].isMine == false) {
nearbyMines = 0;
for (int k = 0; k < this->tiles[j].neighborTiles.size(); k++) {
Tile* currentTile = this->tiles[j].neighborTiles[k];
if (currentTile->isMine == true) {
nearbyMines += 1;
}
}
switch (nearbyMines) {
case 0:
this->tiles[j].mineNumber = 0;
break;
case 1:
this->tiles[j].mineNumber = 1;
break;
case 2:
this->tiles[j].mineNumber = 2;
break;
case 3:
this->tiles[j].mineNumber = 3;
break;
case 4:
this->tiles[j].mineNumber = 4;
break;
case 5:
this->tiles[j].mineNumber = 5;
break;
case 6:
this->tiles[j].mineNumber = 6;
break;
case 7:
this->tiles[j].mineNumber = 7;
break;
case 8:
this->tiles[j].mineNumber = 8;
break;
}
}
}
}