-
Notifications
You must be signed in to change notification settings - Fork 0
/
minefield.h
354 lines (290 loc) · 10.7 KB
/
minefield.h
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
#include <iostream>
#include <algorithm>
#include <QPushButton>
#include <QTimer>
#include <Mine.h>
#include <QByteArray>
#include <QBuffer>
#include <QPixmap>
#include <random>
#include <chrono>
#include "util.h"
#ifndef MINEFIELD_H
#define MINEFIELD_H
class MineField{
public:
int w, h, num_active;
std::vector<int> data;
std::vector<Mine*> buttons;
std::vector<int> flags;
std::vector<int> clicked_spaces;
Rsrc rsrc;
QPushButton *smiley;
QTimer* timer;
Digit digitLeft, digitRight;
int time = 0;
bool mines_set = false;
bool lost = false;
bool win = false;
MineField(){}
MineField(int w, int h, int num_active){
this->w = w;
this->h = h;
this->rsrc = Rsrc();
this->num_active = num_active;
this->flags = std::vector<int>(w*h, 0);
this->clicked_spaces = std::vector<int>(w*h, 0);
this->data = std::vector<int>(w*h, 0);
this->timer = new QTimer();
}
int operator () (int x, int y) const{
return data[y * w + x];
}
/* Resets the game to the initial state. */
void reset(){
win = false;
lost = false;
flags = std::vector<int>(w *h, 0);
mines_set = false;
for(auto b : buttons){
b->setIcon(rsrc.buttons[0]);
b->setStyleSheet("QPushButton { border: 1px solid black; }"
"QPushButton:hover { background-color: yellow; }");
}
clicked_spaces = std::vector<int>(w*h, 0);
smiley->setIcon(rsrc.face[0]);
time = 0;
digitRight.update(0, rsrc);
digitLeft.update(num_active, rsrc);
timer->start();
}
/* Sets difficulty to the selected level. */
void set_difficulty(int diff){
switch(diff){
case 0:
num_active = 10;
w = 9;
h = 9;
break;
case 1:
num_active = 40;
w = 16;
h = 16;
break;
case 2:
num_active = 99;
w = 30;
h = 16;
break;
case 3:
num_active = 10;
w = 10;
h = 10;
break;
}
reset();
}
/* Overload for custom difficulty. */
void set_difficulty(int x, int y, int m){
w = x;
h = y;
num_active = std::min((int)(x * y / 2), m); //the maximum amount of mines can be x*y / 2
reset();
}
/* Function that calculates the number of mines in each spaces (from adjacent mines). */
void calculate_field(){
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
int sum = 0;
if(data[i * w + j] == -1){ //skip mines
continue;
}
for (int di = -1; di <= 1; ++di) {
for (int dj = -1; dj <= 1; ++dj) {
if (di == 0 && dj == 0) { //skip self
continue;
}
int new_i = i + di; //neighbour i
int new_j = j + dj; //neighbour j
if (new_i >= 0 && new_i < h && new_j >= 0 && new_j < w && data[new_i * w + new_j] == -1) { //is in range
sum++;
}
}
}
data[i * w + j] = sum;
}
}
}
/* Checks if guessing is needed (check for neighbouring mines), not guaranteed. */
void check_guessing(int x, int y) {
bool guessing = true;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (data[i * w + j] != -1) {
// if current cell is not a mine
bool is_mine_neighbor = false;
for (int di = -1; di <= 1; ++di) {
for (int dj = -1; dj <= 1; ++dj) {
int new_i = i + di;
int new_j = j + dj;
if (new_i >= 0 && new_i < h && new_j >= 0 && new_j < w && data[new_i * w + new_j] == -1) {
// if there is a mine as neighbor
is_mine_neighbor = true;
break;
}
}
if (is_mine_neighbor) {
break;
}
}
if (!is_mine_neighbor) {
guessing = false;
break;
}
}
}
if (!guessing) {
break;
}
}
if (guessing) {
// rearrange mines
set_mines(x, y, num_active);
}
}
/* Function that fills the minefield with the specified amount of mines, then randomizes the field so the entry-point
* space is not a mine and invokes a click method on that field.
*/
void set_mines(int x, int y, int num_mines){
data = std::vector<int>();
for(int i = 0; i < w*h; ++i){
data.push_back(i < num_mines ? -1 : 0);
}
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine rng(seed);
std::shuffle(data.begin(), data.end(), rng);
while(data[y*w+x] != 0){
std::shuffle(data.begin(), data.end(), rng);
}
calculate_field();
this->field_click(x, y);
}
/* Clicks on a mine. */
void field_click(int x, int y){
if(!mines_set){ //execute only once at the start of the game
mines_set = true;
set_mines(x, y, num_active);
check_guessing(x, y);
buttons[y * w + x]->setStyleSheet("QPushButton { border: none; }"
"QPushButton:hover { background-color: none; }");
clicked_spaces[y * w + x] = 1;
return;
}
if(clicked_spaces[y * w + x]){
return;
}
if(data[y * w + x] == 0){ //recursively click on mines
buttons[y * w + x]->setStyleSheet("QPushButton { border: none; }"
"QPushButton:hover { background-color: none; }");
clicked_spaces[y * w + x] = 1;
if (data[y * w + x] == 0) {
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
int new_x = x + j;
int new_y = y + i;
if (i == 0 && j == 0) { //skip self
continue;
}
if (new_x >= 0 && new_x < w && new_y >= 0 && new_y < h){
flags[y * w + x] = 0;
update_flag_count();
buttons[y * w + x]->setIcon(rsrc.buttons[0]);
field_click(new_x, new_y);
}
}
}
}
if(check_win()){
disable_all();
smiley->setIcon(rsrc.face[2]);
}
}
else if(data[y * w + x] == -1){
buttons[y * w + x]->setStyleSheet("QPushButton { border: none; }"
"QPushButton:hover { background-color: none; }");
clicked_spaces[y * w + x] = 1;
buttons[y * w + x]->setIcon(rsrc.buttons[11]);
if(lost){
return;
}
lost = true;
disable_all();
smiley->setIcon(rsrc.face[1]);
}
else{
buttons[y * w + x]->setStyleSheet("QPushButton { border: none; }"
"QPushButton:hover { background-color: none; }");
clicked_spaces[y * w + x] = 1;
flags[y * w + x] = 0;
update_flag_count();
buttons[y * w + x]->setIcon(rsrc.buttons[data[y * w + x]]);
if(check_win()){
disable_all();
smiley->setIcon(rsrc.face[2]);
}
}
}
/* Check if all mines have been flagged correctly, if yes, win the game. */
bool check_win(){
int sum = 0;
int flag_sum = 0;
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
flag_sum += flags[i * w + j] * data[i * w + j]; //only count flag spaces
sum += clicked_spaces[i * w + j]; //count all clicked spaces
}
}
return (sum == w * h - num_active) && (flag_sum == -num_active);
}
/* Function that destructively disables all spaces, so they are not clickable. */
void disable_all(){
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
if(!clicked_spaces[i * w + j]){
clicked_spaces[i * w + j] = 1;
buttons[i * w + j]->setStyleSheet("QPushButton { border: none; }"
"QPushButton:hover { background-color: none; }");
buttons[i * w + j]->setIcon(data[i * w + j] >= 0 ? rsrc.buttons[data[i * w + j]] : rsrc.buttons[10]);
if((data[i * w + j] != -1) && flags[i * w + j]){ //misflagged
buttons[i * w + j]->setStyleSheet("QPushButton { border: none; }"
"QPushButton:hover { background-color: none; }");
buttons[i * w + j]->setIcon(rsrc.buttons[12]);
}
}
}
}
timer->stop();
}
/* Draws a flag icon atop of a mine (right click). */
void draw_flag(int x, int y){
buttons[y * w + x]->setIcon(flags[y * w + x] ? rsrc.buttons[9] : rsrc.buttons[0]);
}
/* Updates the left mine counter. */
void update_flag_count(){
int sum = 0;
for(auto f : flags){ sum += f; }
digitLeft.update(num_active - sum, rsrc);
}
/* Updates the flag on certain position. */
void update_flag(int x, int y){
flags[y * w + x] = !flags[y * w + x];
draw_flag(x, y);
update_flag_count();
if(check_win()){
disable_all();
smiley->setIcon(rsrc.face[2]);
}
}
private:
};
#endif // MINEFIELD_H