-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.cpp
323 lines (322 loc) · 8.66 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
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
/**
*
* Solution to course project # 7
* Introduction to programming course
* Faculty of Mathematics and Informatics of Sofia University
* Winter semester 2022/2023
*
* @author Desislava Shunina
* @idnumber 6MI0600163
* @compiler VC
*
* <file with the game>
*
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
const unsigned N = 10;//max size of field
bool isValidInputOfSize(size_t size)
{
return size <= 10 && size >= 3;
}
bool isValidInputOfMines(size_t size, unsigned numOfMines)
{
return numOfMines >= 1 && numOfMines <= 3 * size;
}
void GiveAllTheSameValue(char field[N][N], char value)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
field[i][j] = value;
}
}
bool isMarked(char fieldCell)
{
return fieldCell == '*';
}
void GetValidCoordinatesOfMine(int& Xcoordinate, int& Ycoordinate, size_t sizeOfField)
{
while (Xcoordinate >= sizeOfField)
{
Xcoordinate = rand() % 10;
}
while (Ycoordinate >= sizeOfField)
{
Ycoordinate = rand() % 10;
}
}
void CreateMines(char field[N][N], size_t size, unsigned numOfMines)
{
srand(time(0));
for (int i = 1; i <= numOfMines; i++)
{
int Xcoordinate = rand() % size;
int Ycoordinate = rand() % size;
GetValidCoordinatesOfMine(Xcoordinate, Ycoordinate, size);
if (isMarked(field[Xcoordinate][Ycoordinate]))
{
i--;
}
else
{
field[Xcoordinate][Ycoordinate] = '*';
}
}
}
void GetCountOfMinesInCurrentCell(char field[N][N], size_t size, int x, int y)
{
char count = '0';
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && j >= 0 && i < size && j < size)
{
if (i == x && j == y)
continue;
if (field[i][j] == '*')
count++;
}
}
}
field[x][y] = count;
}
void GetCountOfMinesInEveryCell(char field[N][N], size_t size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (field[i][j] != '*')
GetCountOfMinesInCurrentCell(field, size, i, j);
}
}
}
void printField(const char field[N][N], size_t size)
{
std::cout << std::endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
std::cout << "[" << field[i][j] << "]";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void OpenAllNeighboursWhenZero(const char field[N][N], char RevealedField[N][N], int x, int y, size_t size, unsigned& forRevealing)
{
if (RevealedField[x][y] == ' ')
{
RevealedField[x][y] = field[x][y];
forRevealing--;
}
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && j >= 0 && i < size && j < size)
{
if (i == x && j == y)
continue;
if (RevealedField[i][j] == ' ' && field[i][j] != '*')
{
RevealedField[i][j] = field[i][j];
forRevealing--;
if (RevealedField[i][j] == '0')
{
OpenAllNeighboursWhenZero(field, RevealedField, i, j, size, forRevealing);
}
}
}
}
}
}
int myStrcmp(const char* first, const char* second)
{
if (first == nullptr || second == nullptr)
return -2;
while (*first != '\0' && *second != '\0')
{
if (*first < *second)
return -1;
if (*first > *second)
return 1;
first++;
second++;
}
if (*first == '\0' && *second == '\0')
return 0;
return *first == '\0' ? -1 : 1;
}
bool isValidUserInput(int x, int y, char* operation, size_t size)
{
return (myStrcmp(operation, "open") == 0 || myStrcmp(operation, "mark") == 0 || myStrcmp(operation, "unmark") == 0)
&& x >= 0 && x < size&& y >= 0 && y < size;
}
void OpenCellCommand(char RevealedField[N][N], const char field[N][N], size_t size, unsigned& forRevealing, int x, int y, bool& GameOver)
{
if (RevealedField[x][y] == '!')
{
std::cout << "Cell has been marked! Unmark first! " << std::endl;
}
else if (RevealedField[x][y] != ' ')
{
std::cout << "Cell has already been opened! " << std::endl;
}
else
{
if (field[x][y] == '*')
{
RevealedField[x][y] = field[x][y];
printField(RevealedField, size);
std::cout << "You stepped on a mine! Game Over :(" << std::endl;
printField(field, size);
GameOver = true;
}
else if (field[x][y] != '0')
{
RevealedField[x][y] = field[x][y];
forRevealing--;
}
else
{
OpenAllNeighboursWhenZero(field, RevealedField, x, y, size, forRevealing);
}
}
}
void MarkCellCommand(char RevealedField[N][N], const char field[N][N], int& countOfMarks, unsigned& numOfMines, int x, int y)
{
if (countOfMarks != 0)
{
if (RevealedField[x][y] == '!')
std::cout << "Cell has already been marked!" << std::endl;
else if (RevealedField[x][y] != ' ')
std::cout << "Invalid input - cell has already been opened!" << std::endl;
else
{
RevealedField[x][y] = '!';
if (RevealedField[x][y] == '!')
{
countOfMarks--;
if (field[x][y] == '*')
numOfMines--;
}
}
}
else
{
std::cout << "All available marks have been placed! " << std::endl;
}
}
void UnmarkCellCommand(char RevealedField[N][N], const char field[N][N], unsigned& numOfMines, int& countOfMarks, int x, int y)
{
if (RevealedField[x][y] != ' ' && RevealedField[x][y] != '!')
std::cout << "Invalid input - cell has already been opened!" << std::endl;
else
{
if (RevealedField[x][y] != '!')
std::cout << "Cell has not been marked yet! Mark first!" << std::endl;
else
{
RevealedField[x][y] = ' ';
countOfMarks++;
if (RevealedField[x][y] == ' ' && field[x][y] == '*')
numOfMines++;
}
}
}
bool WinnerCheck(unsigned numOfMines, unsigned forRevealing)
{
return numOfMines == 0 && forRevealing == 0;
}
void PlayGame(char RevealedField[N][N], const char field[N][N], size_t size, unsigned numOfMines, unsigned forRevealing)
{
char operation[7];//longest is unmark
int x, y;
int countOfMarks = numOfMines;
printField(RevealedField, size);
bool GameOver = false;
do
{
std::cout << "Please enter operation (open, mark or unmark) and valid coordinates of field cell: " << std::endl;
std::cin >> operation >> x >> y;
if (isValidUserInput(x, y, operation, size))
{
if (myStrcmp(operation, "open") == 0)
{
OpenCellCommand(RevealedField, field, size, forRevealing, x, y, GameOver);
if (GameOver)
break;
}
else if (myStrcmp(operation, "mark") == 0)
{
MarkCellCommand(RevealedField, field, countOfMarks, numOfMines, x, y);
}
else if (myStrcmp(operation, "unmark") == 0)
{
UnmarkCellCommand(RevealedField, field, numOfMines, countOfMarks, x, y);
}
printField(RevealedField, size);
if (WinnerCheck(numOfMines, forRevealing))
{
std::cout << "Congratulations! You won! " << (char)1 << std::endl;
break;
}
}
else
{
std::cout << "Invalid input!" << std::endl;
}
} while (true);
}
void PrintRules()
{
std::cout << "~~~~~~~Minesweeper~~~~~~~" << std::endl;
std::cout << "RULES OF THE GAME:" << std::endl;
std::cout << "1. Choose valid size of the field (from 3 to 10) and valid\nnumber of mines (from 1 to 3*size of the field) to start playing;" << std::endl;
std::cout << "2. Choose valid command and coordinates of a cell form the field." << std::endl;
std::cout << "*Note: Valid commands are:\n - open;\n - mark;\n - unmark." << std::endl;
std::cout << "3. If you step on a mine, you lose." << std::endl;
std::cout << "4. If you mark all mines and open all of the other cells, you win." << std::endl;
std::cout << "Have fun and enjoy!" << (char)1 << std::endl;
std::cout << std::endl;
}
void ValidateUserInput(size_t& size, unsigned& numOfMines)
{
std::cout << "Choose size of field (from 3 to 10): ";
std::cin >> size;
while (!isValidInputOfSize(size))
{
std::cout << "Invalid input! Please enter valid data to play!" << std::endl;
std::cout << "Choose size of field (from 3 to 10): ";
std::cin >> size;
}
std::cout << "Choose number of mines (from 1 to " << 3 * size << "): ";
std::cin >> numOfMines;
while (!isValidInputOfMines(size, numOfMines))
{
std::cout << "Invalid input! Please enter valid data to play!" << std::endl;
std::cout << "Choose number of mines (from 1 to " << 3 * size << "): ";
std::cin >> numOfMines;
}
}
int main()
{
PrintRules();
size_t size;
unsigned numOfMines;
ValidateUserInput(size, numOfMines);
unsigned forRevealing = size * size - numOfMines;
char field[N][N];
char RevealedField[N][N];
GiveAllTheSameValue(field, ' ');
GiveAllTheSameValue(RevealedField, ' ');
CreateMines(field, size, numOfMines);
GetCountOfMinesInEveryCell(field, size);
PlayGame(RevealedField, field, size, numOfMines, forRevealing);
return 0;
}