-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLogic.cc
276 lines (230 loc) · 6.52 KB
/
GameLogic.cc
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
#include "GameLogic.h"
#include "log.h"
#include "utils.h"
#include <cassert>
#include <stdexcept>
GameLogic::GameLogic(): _mine_field(0), _state(GameState::STOPPED),
_num_mines(0), _first_move(false)
{
}
GameLogic::~GameLogic()
{
delete _mine_field;
}
bool GameLogic::loadGame(const std::string& filename)
{
delete _mine_field;
_mine_field = new MineField(2, 2, 1);
if (!_mine_field->loadFromFile(filename)) return false;
_num_mines = 0;
for (int i = 0; i < _mine_field->getRows(); i++)
for (int j = 0; j < _mine_field->getCols(); j++)
if (this->getCell(i, j).getValue() == Cell::MINE) _num_mines++;
printf("GameLogic::loadGame() loaded %dx%dx%d game\n",
getRows(), getCols(), _num_mines);
setState(GameState::RUNNING);
return true;
}
void GameLogic::newGame(int row, int col, int num)
{
if (_mine_field) delete _mine_field;
this->_num_mines = num;
_mine_field = new MineField(row, col, num);
_first_move = true;
setState(GameState::RUNNING);
}
Cell& GameLogic::getCell(const Location& loc)
{
if (!_mine_field) throw std::runtime_error("Board has not been constructed yet");
return _mine_field->getCell(loc);
}
Cell& GameLogic::getCell(int i, int j)
{
if (!_mine_field) throw std::runtime_error("Board has not been constructed yet");
return _mine_field->getCell(i, j);
}
GameState GameLogic::getState() const
{
return _state;
}
void GameLogic::setState(GameState new_state)
{
_state = new_state;
}
int GameLogic::getNumMines() const
{
return _num_mines;
}
std::string GameLogic::toString()
{
if (this->getState() == GameState::RUNNING)
return _mine_field->toString();
else
return "";
}
void GameLogic::floodFillDig(int i, int j)
{
// printf("GameLogic::floodFillDig(%d,%d)\n", i, j);
if (getState() != GameState::RUNNING)
{
fprintf(stderr, "Game is not running");
return;
}
else if (!_mine_field->isValidCoordinate(i, j))
{
fprintf(stderr, "Invalid arguments");
return;
}
Neighbours lst = _mine_field->getNeighbours(i, j);
_mine_field->getCell(i, j).setState(Cell::KNOWN);
for (size_t i = 0; i < lst.size(); i++)
{
Location& p = lst[i];
Cell& c = _mine_field->getCell(p.row, p.col);
if (c.getState() != Cell::UNKNOWN) continue;
if (c.getValue() == 0)
floodFillDig(p.row, p.col);
else
dig(p.row, p.col);
}
}
void GameLogic::revealAll()
{
for (int i = 0; i < _mine_field->getRows(); i++)
for (int j = 0; j < _mine_field->getCols(); j++)
this->getCell(i, j).setState(Cell::KNOWN);
}
Neighbours GameLogic::getNeighbours(int i, int j)
{
return this->_mine_field->getNeighbours(i, j);
}
void GameLogic::dig(int i, int j)
{
Log::print(Log::DEBUG, "GameLogic::dig(%d,%d)\n", i, j);
// need to check win & lose after calling this function
if (getState() != GameState::RUNNING)
throw std::logic_error("Game is not running");
else if (!_mine_field->isValidCoordinate(i, j))
throw std::invalid_argument("Invalid arguments");
bool bomb_hit = false;
while (true)
{
bomb_hit = _mine_field->getCell(i, j).getValue() == Cell::MINE;
if (bomb_hit)
{
if (_first_move)
{
_mine_field->randomlyFillMines(_num_mines);
continue;
}
Log::print(Log::VERBOSE, "GameLogic::dig(%d,%d) BOOOM!!!\n", i, j);
}
break;
}
Cell& c = _mine_field->getCell(i, j);
if (c.getState() != Cell::UNKNOWN)
fprintf(stderr, "Can only dig UNKNOWN cells\n");
else
{
if (c.getValue() == 0)
this->floodFillDig(i, j);
else
c.setState(Cell::KNOWN);
}
if (this->checkWin())
{
this->setState(GameState::WON);
gameWon();
}
else if (bomb_hit) // || this->checkLose())
{
this->revealAll();
this->setState(GameState::LOST);
gameLost();
}
_first_move = false;
}
void GameLogic::mark(int i, int j)
{
// printf("GameLogic::mark(%d,%d)\n", i, j);
// need to check win after calling this function
if (getState() != GameState::RUNNING)
throw std::logic_error("Game is not running");
else if (!_mine_field->isValidCoordinate(i, j))
throw std::invalid_argument("Invalid arguments");
Cell& c = _mine_field->getCell(i, j);
if (c.getState() != Cell::UNKNOWN)
{
fprintf(stderr, "Can only mark UNKNOWN cells\n");
exit(1);
}
else
{
if (c.getValue() != Cell::MINE ||
c.getState() == Cell::FLAGGED)
{
fprintf(stderr, "AUTO SWEEP MEGA ERROR 0x433211: Attempted to flag a cell with no mine!\n");
exit(1);
}
c.setState(Cell::FLAGGED);
}
if (this->checkWin())
{
this->setState(GameState::WON);
gameWon();;
}
}
void GameLogic::unmark(int i, int j)
{
if (getState() != GameState::RUNNING)
throw std::logic_error("Game is not running");
else if (!_mine_field->isValidCoordinate(i, j))
throw std::invalid_argument("Invalid arguments");
Cell& c = _mine_field->getCell(i, j);
if (c.getState() != Cell::FLAGGED)
fprintf(stderr, "Can only unmark FLAGGED cells\n");
else
c.setState(Cell::UNKNOWN);
}
bool GameLogic::checkWin()
{
// all mine are FLAGGED, the rest are KNOWN
if (!_mine_field) return false;
for (int i = 0; i < _mine_field->getRows(); i++)
{
for (int j = 0; j < _mine_field->getCols(); j++)
{
const Cell& c = _mine_field->getCell(i, j);
if (c.getValue() == Cell::MINE)
{
if (c.getState() != Cell::FLAGGED)
return false;
}
else
{
if (c.getState() != Cell::KNOWN)
return false;
}
}
}
return true;
}
bool GameLogic::checkLose()
{
// some MINE is KNOWN
if (!_mine_field) return false;
for (int i = 0; i < _mine_field->getRows(); i++)
{
for (int j = 0; j < _mine_field->getCols(); j++)
{
const Cell& c = _mine_field->getCell(i, j);
if (c.getValue() == Cell::MINE
&& c.getState() == Cell::KNOWN)
{
// printf("Pang! at %d,%d\n", i, j);
return true;
}
}
}
return false;
}