-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheMinerGame.cpp
83 lines (69 loc) · 1.78 KB
/
TheMinerGame.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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <cstdio>
using std::cout,
std::cin,
std::endl;
const int SIZE {5};
const int MINES {5};
char board[SIZE][SIZE];
bool revealed[SIZE][SIZE] = {false};
void initBoard() {
for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j)
board[i][j] = '.';
int minesPlaced = 0;
while (minesPlaced < MINES) {
int x = rand() % SIZE;
int y = rand() % SIZE;
if (board[x][y] != '*') {
board[x][y] = '*';
minesPlaced++;
}
}
}
void displayBoard() {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (revealed[i][j]) printf("%c ", board[i][j]);
else printf(". ");
}
printf("\n");
}
}
int main() {
srand(time(0));
initBoard();
int x, y;
while (true) {
displayBoard();
printf("Enter the coordinates (x y): ");
scanf("%d %d", &x, &y);
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
puts("Incorrect coordinates! Try again.");
continue;
}
if (board[x][y] == '*') {
puts("You've stepped on a landmine! The game is over.");
revealed[x][y] = true;
displayBoard();
break;
}
revealed[x][y] = true;
board[x][y] = ' ';
bool win = true;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (board[i][j] != '*' && !revealed[i][j]) {
win = false;
break;
}
}
if (!win) break;
}
if (win) {puts("Congratulations! You've won!"); displayBoard(); break;}
}
return 0;
}