-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minesweeper.cpp
250 lines (237 loc) · 8.13 KB
/
Minesweeper.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
//
// Minesweeper.cpp
//
// Created by Cindy Lee on 9/22/17.
// Copyright © 2017 Cindy Lee. All rights reserved.
//
// Purpose: Simulate a minesweeper game for the user to interact with
#include <vector>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
struct Cell {
int numberBomb = 0;
bool bomb = false;
bool visible = false;
};
class Minesweeper {
public:
//Minesweeper (vector <vector <Cell>>& boarder, int row, int col) :
//board (boarder), rows (row), cols (col){
Minesweeper (){
rows = 16;
cols = 30;
for (int row = 0; row < rows+2; ++row) {
vector <Cell> aRow;
for (int column = 0; column < cols+2; ++column){
Cell cell;
aRow.push_back (cell);
}
board.push_back(aRow);
}
srand(time (NULL));
for (size_t indexRow = 1; indexRow < rows+1; ++indexRow){
for (size_t indexCol = 1; indexCol < board [indexRow].size()-1; ++indexCol){
if (rand () % 100 < 10) {
board [indexRow] [indexCol].bomb = true;
//size_t surround = indexRow - 1;
//size_t surrC = indexCol - 1;
board [indexRow + 1] [indexCol-1].numberBomb += 1;
board [indexRow + 1] [indexCol].numberBomb += 1;
board [indexRow + 1] [indexCol+1].numberBomb += 1;
board [indexRow - 1] [indexCol+1].numberBomb += 1;
board [indexRow - 1] [indexCol].numberBomb += 1;
board [indexRow - 1] [indexCol-1].numberBomb += 1;
board [indexRow] [indexCol - 1].numberBomb += 1;
board [indexRow] [indexCol + 1].numberBomb += 1;
//for (size_t count = indexRow - 1; count < indexRow + 2; ++count){
// for (size_t counter = indexCol - 1; counter< indexCol + 2; ++ count) {
// if ((count > 0) && (count < rows - 1)) {
// if ((counter > 0) && (counter < cols - 1)){
// if (board [surround] [surrC].bomb == true){
// board[indexRow][indexCol].bomb += 1;
// }
// }
// }
}
}
}
}
void display (bool x) {
if (x) {
for (size_t indexRow = 1; indexRow < rows+1; ++indexRow){
for (size_t indexCol = 1; indexCol < board [indexRow].size()-1; ++indexCol){
if (board [indexRow][indexCol].bomb == true) {
board[indexRow][indexCol].visible = true;
cout << 'X';
}
else if (board [indexRow][indexCol].visible == true) {
if (board [indexRow][indexCol].numberBomb == 0){
cout << ' ';
}
else{
cout << board[indexRow][indexCol].numberBomb;
}
}
else{
cout << '-';
}
}
cout << '\n';
}
}
else {
for (size_t indexRow = 1; indexRow < rows+1; ++indexRow){
for (size_t indexCol = 1; indexCol < board [indexRow].size()-1; ++indexCol){
if (board[indexRow][indexCol].visible == true) {
if (board [indexRow][indexCol].numberBomb == 0){
cout << ' ';
}
else{
cout << board[indexRow][indexCol].numberBomb;
}
}
else{
cout << '-';
}
}
cout << '\n';
}
}
}
bool validRow (int inputRow) const {
if ((inputRow > 0) && (inputRow < rows+1)){
return true;
}
return false;
}
bool validCol (int inputCol) const {
if ((inputCol > 0) && (inputCol < cols + 1)){
return true;
}
return false;
}
bool isVisible (int row, int col) const {
if (board [row][col].visible == true) {
return true;
}
return false;
}
bool done () const {
if (donester == true){
return true;
}
for (size_t indexRow = 1; indexRow < rows+1; ++indexRow){
for (size_t indexCol = 1; indexCol < board [indexRow].size()-1; ++indexCol){
if ((board [indexRow] [indexCol].visible == false) &&
(board[indexRow][indexCol].bomb == false)) {
return false;
}
}
}
return true;
}
bool play (int row, int col){
//cout << row << col<< endl;
if (board [row][col].bomb == true) {
donester = true;
board[row][col].visible = true;
return false;
}
else if (board [row] [col].numberBomb > 0 ){
board[row][col].visible = true;
}
else {
board[row][col].visible = true;
if (row + 1 < rows + 1){
if (!isVisible (row + 1, col)){
play (row + 1, col);
}
if (col - 1 > 0) {
if (!isVisible (row + 1, col-1)){
play (row + 1, col - 1);
}
}
if (col + 1 < cols + 1){
if (!isVisible (row + 1, col+1)){
play (row + 1, col + 1);
}
}
}
if (row - 1 > 0){
if (!isVisible (row - 1, col)){
play (row - 1, col);
}
if (col - 1 > 0) {
if (!isVisible (row - 1, col-1)){
play (row - 1, col - 1);
}
}
if (col + 1 < cols + 1){
if (!isVisible (row - 1, col+1)){
play (row - 1, col +1);
}
}
}
if (col - 1 > 0) {
if (!isVisible (row, col-1)){
play (row, col -1);
}
}
if (col + 1 < cols + 1) {
if (!isVisible (row, col+1)){
play (row, col + 1);
}
}
}
return true;
}
private:
int rows;
int cols;
bool donester;
vector <vector <Cell>> board;
};
//Professor's Test Code below
int main() {
Minesweeper sweeper;
// Continue until the only invisible cells are bombs
while (!sweeper.done()) {
sweeper.display(false); // display the board without bombs
int row_sel = -1, col_sel = -1;
while (row_sel == -1) {
// Get a valid move
int r, c;
cout << "row? ";
cin >> r;
if (!sweeper.validRow(r)) {
cout << "Row out of bounds\n";
continue;
}
cout << "col? ";
cin >> c;
if (!sweeper.validCol(c)) {
cout << "Column out of bounds\n";
continue;
}
if (sweeper.isVisible(r,c)) {
cout << "Square already visible\n";
continue;
}
row_sel = r;
col_sel = c;
}
// Set selected square to be visible. May effect other cells.
if (!sweeper.play(row_sel,col_sel)) {
cout << "BOOM!!!\n";
sweeper.display(true);
// We're done! Should consider ending more "cleanly",
// eg. Ask user to play another game.
exit(0);
}
}
// Ah! All invisible cells are bombs, so you won!
cout << "You won!!!!\n";
sweeper.display(true); // Final board with bombs shown
}