-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinesweeper.cpp
More file actions
212 lines (167 loc) · 5.61 KB
/
Minesweeper.cpp
File metadata and controls
212 lines (167 loc) · 5.61 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
#include "Minesweeper.h"
Minesweeper::Minesweeper() {
proceed = true;
while (proceed == true) {
beginningSettings();
mainBoard = new Board(rows, cols, mines);
proceed = mainGame();
}
}
Minesweeper::~Minesweeper() {
delete mainBoard;
}
int Minesweeper::difficultyChoice(string ss) {
if (ss == "Easy" || ss == "easy") {
rows = cols = mines = 10;
return 0;
} else if (ss == "Medium" || ss == "medium") {
rows = cols = 16;
mines = 40;
return 0;
} else if (ss == "Hard" || ss == "hard") {
rows = 30;
cols = 16;
mines = 99;
return 0;
} else {
cout << "Error! Invalid difficulty!" << endl;
return 1;
}
}
void Minesweeper::beginningSettings() {
string response;
int flag = 1;
while (flag == 1) {
cout << "What difficulty would you like to play? Easy, Medium, or Hard: ";
cin >> response;
flag = difficultyChoice(response);
}
cout << "You will play on a " << rows << "x" << cols << " board, with " << mines << " mines." << endl;
// game official starts
gameState = true;
}
void Minesweeper::chooseTile(int & idx, char & mode) {
int x = -1;
int y = -1;
char md = 'z';
bool flag = false;
do {
x = y = -1;
md = 'z';
flag = false;
mainBoard->printBoard();
cout << "----------------------------------------------" << endl;
cout << "Reveal(R) or Flag(F) a tile, in a \"x y <R/F>\" format. For example: \"5 2 R\": " << endl;
cin >> x >> y >> md;
// if the tile is invalid
if (mainBoard->invalidTile(x-1, y-1) == true) {
// flag remains false
// if we are to reveal a tile
} else if (md == 'R' || md == 'r') {
// check if the tile is already revealed
if (mainBoard->exposedError(mainBoard->index(x-1,y-1)) == true) {
flag = false;
} else {
cout << endl << "Revealing the tile at " << x << " " << y << "." << endl;
idx = mainBoard->index(x-1,y-1);
mode = 'R';
flag = true;
}
} else if (md == 'F' || md == 'f') {
if (mainBoard->flagError(mainBoard->index(x-1,y-1)) == true) {
flag = false;
} else if (mainBoard->visualBoard[mainBoard->index(x,y)] == -4) {
cout << endl << "Unflagging the tile at " << x << " " << y << "." << endl;
idx = mainBoard->index(x-1,y-1);
mode = 'F';
flag = true;
} else {
cout << endl << "Flagging the tile at " << x << " " << y << "." << endl;
idx = mainBoard->index(x-1,y-1);
mode = 'F';
flag = true;
}
} else {
cout << endl << "Incorrect mode! Please choose 'R' or 'F'!" << endl;
// flag remains false
}
} while (flag == false);
}
void Minesweeper::changeTile(int idx, char mode) {
// error checking is done prior, so just need to change tiles
// reveal tile
if (mode == 'R') {
mainBoard->visualBoard[idx] = mainBoard->valueBoard[idx];
// flag / unflag tile
} else {
if (mainBoard->visualBoard[idx] == -4) {
mainBoard->visualBoard[idx] = -3;
} else {
mainBoard->visualBoard[idx] = -4;
}
}
}
int Minesweeper::endGameCheck(int idx) {
if (mainBoard->visualBoard[idx] == -2) {
// game over -> bomb detonated
return -1;
} else {
for (int i = 0; i < (rows * cols); i++) {
if (mainBoard->valueBoard[i] != -2 && mainBoard->visualBoard[i] == -3) {
// game continues -> still unexposed blanks
return 0;
}
}
// game over -> all non-bomb tiles have been exposed
return 1;
}
}
bool Minesweeper::mainGame() {
int idx = -99;
char mode = 'z';
int endCheck = -99;
char response = 'z';
mainBoard->printBoard();
while (gameState == true) {
idx = -1;
mode = 'z';
// IF YOU WANT TO TURN ON THE SOLVER, UNCOMMENT LINE 162 AND COMMENT 165 + 166
// mineSweeperSolver(mode);
chooseTile(idx, mode);
changeTile(idx, mode);
if (mode == 'R') {
endCheck = endGameCheck(idx);
if (endCheck == -1) {
cout << endl << "----------------------------------------------" << endl;
cout << "Game over! You detonated a mine!" << endl;
mainBoard->printBoardExposed();
gameState = false;
} else if (endCheck == 1) {
cout << "----------------------------------------------" << endl;
cout << "Congratulations! You have avoided all the mines!" << endl;
mainBoard->printBoardExposed();
gameState = false;
}
}
}
while (response != 'Y' && response != 'y' && response != 'N' && response != 'n') {
cout << "Would you like to play again? Y/N" << endl;
cin >> response;
}
if (response == 'Y' || response == 'y') {
return true;
} else {
cout << "Thanks for playing! Have a good one! :^)" << endl;
return false;
}
}
void Minesweeper::mineSweeperSolver(char & mode) {
mode = 'R';
for (int i = 0; i < (rows * cols); i++) {
if (mainBoard->valueBoard[i] != -2) {
changeTile(i, 'R');
} else if (mainBoard->valueBoard[i] == -2) {
changeTile(i, 'F');
}
}
}