-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
264 lines (238 loc) · 4.97 KB
/
main.cpp
File metadata and controls
264 lines (238 loc) · 4.97 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
#include <iostream>
#include <vector>
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define PURPUR "\033[35m"
#define LIGHT_BLUE "\033[36m"
#define WHITE "\033[37m"
#define BLACK_BG "\033[40m"
#define RED_BG "\033[41m"
#define GREEN_BG "\033[42m"
#define YELLOW_BG "\033[43m"
#define BLUE_BG "\033[44m"
#define PURPUR_BG "\033[45m"
#define LIGHT_BLUE_BG "\033[26m"
#define WHITE_BG "\033[47m"
using namespace std;
class Cell {
private:
int number;
bool hasBomb, _isOpen;
public:
Cell(int number, bool hasBomb) : hasBomb(hasBomb), number(number), _isOpen(false) {
}
int getNumber() {
return number;
}
bool isBomb() {
return hasBomb;
}
bool isOpen() {
return _isOpen;
}
void setNumber(int num) {
number = num;
}
void setBomb(bool state) {
hasBomb = state;
}
void open(int& openedCells) {
if (!_isOpen) openedCells++;
_isOpen = true;
}
};
class Board {
private:
vector<vector<Cell>> playground;
const int bombs;
const int rows, columns;
bool isDead, firstMove;
int openedCells;
public:
Board(int rows, int columns, int bombs): rows(rows), columns(columns), bombs(bombs), isDead(false), playground(rows, vector<Cell>(columns, Cell(0, false))), openedCells(0), firstMove(true)
{
genPlayground();
}
void genPlayground() {
for (int i = 0; i < bombs; i++)
{
int x = rand() % rows;
int y = rand() % columns;
while (playground[x][y].isBomb())
{
x = rand() % rows;
y = rand() % columns;
}
playground[x][y].setBomb(true);
playground[x][y].setNumber(9);
}
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < columns; y++)
{
int num = 0;
if (playground[x][y].isBomb()) continue;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (x + i >= 0 && x + i < rows && y + j >= 0 && y + j < columns && playground[x + i][y + j].isBomb())
num++;
playground[x][y].setNumber(num);
}
}
}
void openCells(int x, int y) {
if (x < 0 || x >= rows || y < 0 || y >= columns) return; // Check boundary
Cell& cell = playground[x][y];
if (cell.getNumber() == 0 && !cell.isOpen()) {
cell.open(openedCells);
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
openCells(x + i, y + j);
}
}
}
else if (cell.getNumber() != 9 && !cell.isOpen()) {
cell.open(openedCells);
}
}
void openBombs() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
{
if (playground[i][j].isBomb())
playground[i][j].open(openedCells);
}
}
}
int open(int x, int y)
{
if (x < 0 || y < 0 || x >= rows || y >= columns)
return -1;
Cell& cell = playground[x][y];
if (cell.isBomb())
{
isDead = true;
openBombs();
return -2;
}
else if (cell.isOpen())
{
return 1;
}
else
{
openCells(x, y);
}
if (openedCells == columns * rows - bombs && !isDead) {
return 2;
}
return 0;
}
void draw(bool __isOpen, string text)
{
system("cls");
cout << " 0 1 2 3 4 5 6 7 8 9\n"
<< " ----------------------------------------\n";
for (int i = 0; i < rows; i++)
{
if ( i < 10) cout << " " << i << "| ";
else cout << i << "| ";
for (int j = 0; j < columns; j++)
{
if (__isOpen || playground[i][j].isOpen())
{
switch (playground[i][j].getNumber())
{
case 0:
cout << WHITE_BG << WHITE << (char)254u << BLACK_BG << WHITE;
break;
case 1:
cout << LIGHT_BLUE << 1 << WHITE;
break;
case 2:
cout << GREEN << 2 << WHITE;
break;
case 3:
cout << RED << 3 << WHITE;
break;
case 4:
cout << BLUE << 4 << WHITE;
break;
case 5:
cout << PURPUR << 5 << WHITE;
break;
case 6:
cout << WHITE_BG << BLACK << 6 << BLACK_BG << WHITE;
break;
case 7:
cout << WHITE_BG << BLACK << 7 << BLACK_BG << WHITE;
break;
case 8:
cout << BLUE << BLACK << 8 << BLACK_BG << WHITE;
break;
case 9:
cout << RED_BG << BLACK << "X" << BLACK_BG << WHITE;
break;
default:
cout << playground[i][j].getNumber();
break;
}
}
else {
cout << (char)254u;
}
cout << " ";
}
cout << "\n |\n";
}
cout << text;
}
};
int main() {
srand(time(NULL));
Board board(10, 10, 20);
bool run = 1;
board.draw(true, "");
string text;
while (run)
{
board.draw(false, text);
text = "";
int x;
int y;
std::cout << "\nEnter move (x y) : ";
std::cin >> y;
std::cin >> x;
if (x == -1 && y == -1)
{
run = 0;
break;
}
int res = board.open(x, y);
if (res != 0)
{
switch (res)
{
case -1:
text = "Incorrect input!";
break;
case -2:
board.draw(false, RED + string("Game Over!") + WHITE);
run = 0;
break;
case 1:
text = "Cell now opened!";
break;
case 2:
text = GREEN + string("You win!");
board.draw(true, text);
run = 0;
break;
}
}
}
}