Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
408 lines (358 loc) · 7.61 KB
/
Copy pathmain.cpp
File metadata and controls
408 lines (358 loc) · 7.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
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
#include <iostream>
#include <cstring>
#include <unistd.h>
#define CLRSCR "\e[2J\e[H";
#define RED "\x1b[31m";
#define BLUE "\x1b[34m";
#define MAGENTA "\x1b[35m";
#define WHITE "\x1b[37m";
#define GREEN "\x1b[32m";
#define T "\t\t\t\t\t\t\t";
using namespace std;
int SIDE;
int MINES;
void Intro();
void Rules();
void Exit();
void getUserInput(int *x, int *y)
{
cout<<"Enter the coordinates (Row(x), Column(y)): ";
cin>>*x;
cin>>*y;
cout<<CLRSCR;
}
void printBoard(char myBoard[][25])
{
cout<<MAGENTA;
cout<<"Board: "<<endl<<endl;
cout<<WHITE;
cout<<" ";
for(int i = 0; i < SIDE; i++)
{
cout<<BLUE;
cout<<" "<<i;
cout<<WHITE;
}
cout<<endl;
for (int i = 0; i < SIDE; i++)
{
cout<<BLUE;
cout<<" "<<i;
cout<<WHITE;
for(int j = 0; j < SIDE; j++)
{
if(myBoard[i][j] == '*')
{
cout<<RED;
}
else if(myBoard[i][j] == '-')
{
cout<<WHITE;
}
else
{
cout<<GREEN;
}
cout<<" "<<myBoard[i][j];
cout<<WHITE;
}
cout<<endl;
}
cout<<endl;
}
bool isValid(int row, int col)
{
return ((row >= 0)&&(row < SIDE) && (col >= 0)&&(col < SIDE));
}
bool isMine(int row, int col, char board[][25])
{
if (board[row][col] == '*')
return true;
else
return false;
}
int countMines(int row, int col, int mines[][2], char realBoard[][25])
{
int count = 0;
if(isValid(row-1, col) && (isMine(row-1, col, realBoard)))
{
count++;
}
if(isValid(row+1, col) && (isMine(row+1, col, realBoard)))
{
count++;
}
if(isValid(row, col+1) && (isMine(row, col+1, realBoard)))
{
count++;
}
if(isValid(row, col-1) && (isMine(row, col-1, realBoard)))
{
count++;
}
if(isValid(row-1, col+1) && (isMine(row-1, col+1, realBoard)))
{
count++;
}
if(isValid(row-1, col-1) && (isMine(row-1, col-1, realBoard)))
{
count++;
}
if(isValid(row+1, col+1) && (isMine(row+1, col+1, realBoard)))
{
count++;
}
if(isValid(row+1, col-1) && (isMine(row+1, col-1, realBoard)))
{
count++;
}
return count;
}
bool playUntil(char myBoard[][25], char realBoard[][25], int mines[][2], int row, int col, int *movesLeft)
{
if (myBoard[row][col] != '-')
return false;
if (realBoard[row][col] == '*')
{
myBoard[row][col]='*';
for (int i = 0; i < MINES; i++)
{
myBoard[mines[i][0]][mines[i][1]] = '*';
}
printBoard(myBoard);
cout<<RED;
cout<<"OH NO! You stepped on a MINE! "<<endl;
cout<<"\t\tGAME OVER! "<<endl;
cout<<WHITE;
return true;
}
else
{
int count = countMines(row, col, mines, realBoard);
(*movesLeft)--;
myBoard[row][col] = count + '0';
if(!count)
{
if(isValid(row-1, col) && (!isMine(row-1, col, realBoard)))
{
playUntil(myBoard, realBoard, mines, row-1, col, movesLeft);
}
if(isValid(row+1, col) && (!isMine(row+1, col, realBoard)))
{
playUntil(myBoard, realBoard, mines, row+1, col, movesLeft);
}
if(isValid(row, col+1) && (!isMine(row, col+1, realBoard)))
{
playUntil(myBoard, realBoard, mines, row, col+1, movesLeft);
}
if(isValid(row, col-1) && (!isMine(row, col-1, realBoard)))
{
playUntil(myBoard, realBoard, mines, row, col-1, movesLeft);
}
if(isValid(row-1, col+1) && (!isMine(row-1, col+1, realBoard)))
{
playUntil(myBoard, realBoard, mines, row-1, col+1, movesLeft);
}
if(isValid(row-1, col-1) && (!isMine(row-1, col-1, realBoard)))
{
playUntil(myBoard, realBoard, mines, row-1, col-1, movesLeft);
}
if(isValid(row+1, col+1) && (!isMine(row+1, col+1, realBoard)))
{
playUntil(myBoard, realBoard, mines, row+1, col+1, movesLeft);
}
if(isValid(row+1, col-1) && (!isMine(row+1, col-1, realBoard)))
{
playUntil(myBoard, realBoard, mines, row+1, col-1, movesLeft);
}
}
return false;
}
}
void placeMines(int mines[][2], char realBoard[][25])
{
bool mark[25 * 25];
memset(mark, false, sizeof (mark));
for (int i = 0; i < MINES; )
{
int ran = rand() % (SIDE*SIDE);
int x = ran / SIDE;
int y = ran % SIDE;
if (!mark[ran])
{
mines[i][0]= x;
mines[i][1] = y;
realBoard[mines[i][0]][mines[i][1]] = '*';
mark[ran] = true;
i++;
}
}
}
void drawBoard(char realBoard[][25], char myBoard[][25])
{
srand(time(NULL));
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++)
{
myBoard[i][j] = realBoard[i][j] = '-';
}
}
}
void replaceMine(int row, int col, char board[][25])
{
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++)
{
if (board[i][j] != '*')
{
board[i][j] = '*';
board[row][col] = '-';
return;
}
}
}
}
void playGame()
{
bool gameOver = false;
char realBoard[25][25], myBoard[25][25];
int movesLeft = SIDE * SIDE - MINES, x, y;
int mines[99][2];
drawBoard(realBoard, myBoard);
placeMines(mines, realBoard);
int moveCount = 0;
while(!gameOver)
{
printBoard(myBoard);
getUserInput(&x, &y);
if(moveCount == 0)
{
if(isMine (x, y, realBoard))
{
replaceMine (x, y, realBoard);
}
}
moveCount++;
gameOver = playUntil(myBoard, realBoard, mines, x, y, &movesLeft);
if ((!gameOver) && (movesLeft == 0))
{
printBoard(myBoard);
cout<<GREEN;
cout<<"******************** YOU WON! ********************"<<endl;
cout<<WHITE;
gameOver = true;
}
}
}
void getSides()
{
cout<<CLRSCR;
cout<<"Enter number of sides for board: ";
cin>>SIDE;
cout<<"Enter number of mines: ";
cin>>MINES;
cout<<endl;
cout<<CLRSCR;
}
void Rules()
{
char back;
cout<<CLRSCR;
cout<<"RULES: "<<endl<<endl;
cout<<"1) The board is divided into cells, with mines randomly distributed. "<<endl;
cout<<"2) To win, you need to open all the cells. "<<endl;
cout<<"3) The number on a cell indicates the number of mines adjacent to it. "<<endl;
cout<<"4) Using this you can determine which cells are safe and which contain mines. "<<endl<<endl;
cout<<"Press B to go back: ";
cin>>back;
if(back == 'b' || back == 'B')
{
Intro();
}
else
{
cout<<RED;
cout<<"Please press b to go to the main menu! "<<endl<<WHITE;
sleep(2);
Rules();
}
}
void Exit()
{
char choice;
cout<<endl<<RED;
cout<<"\t\t\t\t Do you want to exit the game (Y/N): ";
cin>>choice;
if(choice == 'y' || choice == 'Y')
{
cout<<RED;
cout<<T;cout<<"\tBYE!"<<endl<<WHITE;
sleep(2);
return;
}
else if(choice == 'n' || choice == 'N')
{
Intro();
}
else
{
cout<<RED;
cout<<"Please press Y for Yes or N for No! "<<endl<<WHITE;
sleep(2);
Exit();
}
}
void Intro()
{
int choice;
while(true)
{
cout<<CLRSCR;
cout<<MAGENTA;
cout<<" _"<<endl;
cout<<" ____ ___ (_)____ ___ _____ _ __ ___ ___ ____ ___ _____"<<endl;
cout<<" / __ `__ \\ / // __ \\ / _ \\ / ___/| | /\\ / // _ \\ / _ \\ / __ \\ / _ \\ / ___/"<<endl;
cout<<" / / / / / // // / / // __/(__ ) | |/ \\/ // __// __// /_/ // __// /"<<endl;
cout<<"/_/ /_/ /_//_//_/ /_/ \\___/ \\___/ \\__/\\__/ \\___/ \\___// .___/ \\___//_/"<<endl;
cout<<" /_/"<<endl<<endl;
cout<<GREEN;
cout<<T;cout<<" 0 0 1 1 1"<<endl;
cout<<T;cout<<" 0 0 1 ";cout<<RED;cout<<"*"<<GREEN;cout<<" 1"<<endl;
cout<<T;cout<<" 1 1 2 2 2"<<endl<<RED;
cout<<T;cout<<" *"<<GREEN;cout<<" 1 1 "<<RED;cout<<"*"<<GREEN;cout<<" 2"<<endl;
cout<<T;cout<<" 1 1 1 2 "<<RED;cout<<"*"<<endl<<endl<<WHITE;
cout<<T;cout<<" 1) PLAY "<<endl;
cout<<T;cout<<" 2) RULES "<<endl;
cout<<T;cout<<" 3) EXIT "<<endl<<endl;
cout<<"\t\t\t\t\t\t Enter your Input: ";
cin>>choice;
if(choice == 1)
{
getSides();
playGame();
break;
}
else if(choice == 2)
{
Rules();
break;
}
else if(choice == 3)
{
Exit();
break;
}
else
{
cout<<RED;
cout<<"\t\t\t\t\t Please Enter a valid Choice! "<<WHITE;
sleep(2);
}
}
}
int main()
{
Intro();
}