-
Notifications
You must be signed in to change notification settings - Fork 1
/
MineSweeperStateBase.cpp
281 lines (247 loc) · 8.22 KB
/
MineSweeperStateBase.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
// Copyright 2014, flackbash
#include <stdio.h>
#include <unistd.h>
#include <ncurses.h>
#include <time.h>
#include <stdlib.h>
#include "./MineSweeperStateBase.h"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define WHITE "\x1b[37m"
#define RESET "\x1b[0m"
// _____________________________________________________________________________
MineSweeperState::MineSweeperState() {
// initialize screen
initscr();
nodelay(stdscr, true);
noecho();
cbreak();
curs_set(false);
keypad(stdscr, true);
}
// _____________________________________________________________________________
MineSweeperState::~MineSweeperState() {
for (int i = 0; i < _numRows; i++) { delete[] _mineField[i]; }
delete[] _mineField;
endwin();
}
// _____________________________________________________________________________
void MineSweeperState::initialize(size_t numRows, size_t numCols,
size_t numMines) {
_numRows = numRows;
_numCols = numCols;
_numMines = numMines;
_numRevealed = 0;
_numMarked = 0;
_status = ONGOING;
// initialize two-dimensional _mineField with UNREVEALED
_mineField = new CellInfo* [_numRows];
for (int h = 0; h < _numRows; h++) {
_mineField[h] = new CellInfo[_numCols];
for (int k = 0; k < _numCols; k++) {
_mineField[h][k] = UNREVEALED;
}
}
// set alignment
setAlignment();
}
// _____________________________________________________________________________
void MineSweeperState::setAlignment() {
// get window size and adjust alignment
int winWidth, winHeight;
getmaxyx(stdscr, winHeight, winWidth);
_xAl = (winWidth / 2) - (_numCols);
_yAl = 8;
// check whether the window is big enough to display the field
// end program if it's not
if (winWidth <= _numCols * 2 || winHeight <= _numRows * 2 + 10) {
printf("\x1b[%d;%dH", _yAl - 7, _xAl + _numCols - 16);
printf("Please increase your windows size.");
fflush(stdout);
usleep(2 * 1000 * 1000);
endwin();
exit(1);
}
}
// _____________________________________________________________________________
void MineSweeperState::drawField() {
for (int i = 0; i < _numRows; i++) {
for (int j = 0; j < _numCols; j++) {
printf("%s\x1b[%d;%dH", RESET, i * 2 + _yAl + 1, j * 2 + _xAl + 1);
CellInfo cellInfo = getCellInfo(i, j);
// draw '? if cell is unrevealed and not a mine
if (cellInfo == UNREVEALED) printf("?");
// draw '?' if cell is unrevealed (mine) and 'x' when the game is lost
else if (cellInfo == UNREVEALED_MINE) {
if (_status == LOST) {
printf("%sx", RED);
} else {
printf("?");
}
// draw 'X' if cell is marked (wrongly) and '-' when the game is lost
} else if (cellInfo == MARKED_WRONG) {
if (_status == LOST) {
printf("%s-", RED);
} else {
printf("%sX", RED);
}
// draw 'X' if cell is marked (correctly)
} else if (cellInfo == MARKED_CORRECT) {
printf("%sX", RED);
// draw '*' if cell is a revealed mine
} else if (cellInfo == REVEALED_MINE) {
printf("%s*", RED);
// draw number of surrounding mines in diff. colors if cell is revealed
} else if (cellInfo >= 0) {
switch (cellInfo) {
case(REVEALED_ZERO):
printf("%s", WHITE);
break;
case(REVEALED_ONE):
printf("%s", CYAN);
break;
case(REVEALED_TWO):
printf("%s", GREEN);
break;
case(REVEALED_THREE):
printf("%s", MAGENTA);
break;
case(REVEALED_FOUR):
printf("%s", BLUE);
break;
case(REVEALED_FIVE):
printf("%s", YELLOW);
break;
case(REVEALED_SIX):
printf("%s", CYAN);
break;
case(REVEALED_SEVEN):
printf("%s", GREEN);
break;
case(REVEALED_EIGHT):
printf("%s", RED);
break;
}
printf("%d", cellInfo);
}
}
}
printf("%s", RESET);
}
// _____________________________________________________________________________
void MineSweeperState::setMines() {
int fieldIndex;
int mineCount = 0;
int row;
int col;
unsigned int seed = (unsigned int)(time(NULL));
while (mineCount < _numMines) {
fieldIndex = rand_r(&seed) % (_numRows * _numCols);
row = fieldIndex / _numCols;
col = fieldIndex % _numCols;
if (_mineField[row][col] == UNREVEALED) {
_mineField[row][col] = UNREVEALED_MINE;
mineCount++;
}
}
}
// _____________________________________________________________________________
MineSweeperState::CellInfo MineSweeperState::getCellInfo(size_t row,
size_t col) const {
// make sure the given index is within range
if (row >= 0 && row < _numRows && col >= 0 && col < _numCols) {
return _mineField[row][col];
}
}
// _____________________________________________________________________________
void MineSweeperState::revealCell(size_t row, size_t col) {
int mineCount = 0;
// the game is lost if an unrevealed mine is revealed
if (getCellInfo(row, col) == UNREVEALED_MINE) {
_status = LOST;
_mineField[row][col] = REVEALED_MINE;
}
// get number of mines in surrounding cells
// do if the field is unrevealed and not a mine or marked as one
if (getCellInfo(row, col) == UNREVEALED) {
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (getCellInfo(row + i, col + j) <= MARKED_CORRECT) mineCount++;
}
}
_mineField[row][col] = CellInfo(mineCount);
_numRevealed++;
// reveal cells recursively if there are no mines in surrounding cells
if (mineCount == 0) {
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
revealCell(row + i, col + j);
}
}
}
// check whether the game is won already
if (_numMarked + _numRevealed == _numCols * _numRows) _status = WON;
}
}
// _____________________________________________________________________________
void MineSweeperState::markCell(size_t row, size_t col) {
// set CellInfo to marked (correctly/ẃrongly) if cell was unrevealed before
if (getCellInfo(row, col) == UNREVEALED_MINE) {
_mineField[row][col] = MARKED_CORRECT;
_numMarked++;
} else if (getCellInfo(row, col) == UNREVEALED) {
_mineField[row][col] = MARKED_WRONG;
// set CellInfo to unrevealed (no mine/mine) if cell was marked before
} else if (getCellInfo(row, col) == MARKED_CORRECT) {
_mineField[row][col] = UNREVEALED_MINE;
_numMarked--;
} else if (getCellInfo(row, col) == MARKED_WRONG) {
_mineField[row][col] = UNREVEALED;
}
// check whether the game is won already
if (_numMarked + _numRevealed == _numCols * _numRows) _status = WON;
}
// _____________________________________________________________________________
void MineSweeperState::reset() {
for (int i = 0; i < _numRows; i++) { delete[] _mineField[i]; }
delete[] _mineField;
clear();
initialize(_numRows, _numCols, _numMines);
setMines();
}
// _____________________________________________________________________________
MineSweeperState::GameStatus MineSweeperState::status() const {
return _status;
}
// _____________________________________________________________________________
size_t MineSweeperState::numRows() const {
return _numRows;
}
// _____________________________________________________________________________
size_t MineSweeperState::numCols() const {
return _numCols;
}
// _____________________________________________________________________________
size_t MineSweeperState::numMines() const {
return _numMines;
}
// _____________________________________________________________________________
size_t MineSweeperState::numRevealed() const {
return _numRevealed;
}
// _____________________________________________________________________________
size_t MineSweeperState::numMarked() const {
return _numMarked;
}
// _____________________________________________________________________________
size_t MineSweeperState::xAl() const {
return _xAl;
}
// _____________________________________________________________________________
size_t MineSweeperState::yAl() const {
return _yAl;
}