-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxField.cpp
executable file
·334 lines (285 loc) · 9.18 KB
/
boxField.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
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
#include "boxField.hpp"
#include <random>
namespace {
// FIXME: not the best way to do this
const char * mines_to_text(unsigned int mines)
{
switch(mines) {
case 8:
return "8";
case 7:
return "7";
case 6:
return "6";
case 5:
return "5";
case 4:
return "4";
case 3:
return "3";
case 2:
return "2";
case 1:
return "1";
case 0:
return "";
default:
return "x";
}
}
}
sf::Text BoxField::Box::boxDigit(const std::array<sf::Color, 9> & colors,
const sf::Font & font) const
{
// FIXME: set default font size somewhere
sf::Text text(mines_to_text(mines_touching), font, 20);
if (mines_touching < colors.size()) {
text.setColor(colors[mines_touching]);
}
return text;
}
BoxField::BoxField(unsigned int mines, unsigned int w, unsigned int h)
: mines_amount(mines)
{
if (!box_texture.loadFromFile("textures/box.png")) {
throw std::invalid_argument("Couldn't open the box texture");
}
if (!pressed_box_texture.loadFromFile("textures/box_pressed.png")) {
throw std::invalid_argument("Couldn't open the pressed box "
"texture");
}
if (!mine_texture.loadFromFile("textures/mine.png")) {
throw std::invalid_argument("Couldn't open the mine texture");
}
if (!pressed_mine_texture.loadFromFile("textures/mine_pressed.png")) {
throw std::invalid_argument("Couldn't open the pressed mine texture");
}
if (!flagged_mine_texture.loadFromFile("textures/mine_flagged.png")) {
throw std::invalid_argument("Couldn't open the flagged mine texture");
}
if (!flag_texture.loadFromFile("textures/flag.png")) {
throw std::invalid_argument("Couldn't open the flag texture");
}
if (!digit_font.loadFromFile("textures/arial.ttf")) {
throw std::invalid_argument("Couldn't open the font");
}
box_sprite.setTexture(box_texture);
pressed_box_sprite.setTexture(pressed_box_texture);
mine_sprite.setTexture(mine_texture);
pressed_mine_sprite.setTexture(pressed_mine_texture);
flagged_mine_sprite.setTexture(flagged_mine_texture);
flag_sprite.setTexture(flag_texture);
// FIXME: remove scaling
box_sprite.setScale(sf::Vector2f(.2f, .2f));
pressed_box_sprite.setScale(sf::Vector2f(.2f, .2f));
mine_sprite.setScale(sf::Vector2f(.2f, .2f));
pressed_mine_sprite.setScale(sf::Vector2f(.2f, .2f));
flagged_mine_sprite.setScale(sf::Vector2f(.2f, .2f));
flag_sprite.setScale(sf::Vector2f(.2f, .2f));
box_height = box_sprite.getGlobalBounds().height;
box_width = box_sprite.getGlobalBounds().width;
width = w / box_width;
height = h / box_height;
for (unsigned int i = 0; i < width; i++) {
std::vector<Box> row;
for (unsigned int j = 0; j < height; j++) {
row.push_back(Box(i, j));
}
field.push_back(row);
}
}
void BoxField::setupMines()
{
std::default_random_engine generator(std::random_device{}());
std::uniform_int_distribution<int> distribution(0,1);
std::uniform_int_distribution<int> distribution_per_row(0,3);
unsigned int mines_set = 0;
/* Try to divide roughly same amount of mines for each row so first rows are
* not so packed. */
unsigned int mines_per_row = mines_amount / field.size()
+ (mines_amount % field.size() ? 1 : 0);
for (auto & row : field) {
unsigned int mines_set_per_row = 0;
/* Random variable when setting mines per row so player cannot assume
* that every row has same amount of mines.
*/
unsigned int mines_per_row_rand = mines_per_row
+ distribution_per_row(generator);
for (auto & box : row) {
if (box.pressed || mines_set_per_row == mines_per_row_rand) {
continue;
}
bool is_mine = mines_set != mines_amount ?
distribution(generator) : false;
if (is_mine) {
box.mine = true;
mines_set++;
mines_set_per_row++;
}
}
}
is_mines_set = true;
}
void BoxField::calculateTouchingMines()
{
for (unsigned int i = 0; i < width; i++) {
for (unsigned int j = 0; j < height; j++) {
auto & box = field[i][j];
if (box.mine) {
continue;
}
for (int x = -1; x <= 1; x++) {
if ((i == 0 && x == -1 ) || i + x >= width) {
continue;
}
for (int y = -1; y <= 1; y++) {
if ((j == 0 && y == - 1) || j + y >= height) {
continue;
}
if (field[i + x][j + y].mine) {
box.mines_touching++;
}
}
}
}
}
}
void BoxField::draw(sf::RenderWindow & window)
{
for (auto & row : field) {
for (auto & box : row) {
sf::Vector2f pos(box.position.x * box_width, box.position.y * box_height);
sf::Sprite sprite;
if (box.mine && box.pressed) {
sprite = pressed_mine_sprite;
} else if (box.pressed) {
sprite = pressed_box_sprite;
} else if (box.flagged && !box.pressed) {
sprite = flag_sprite;
} else {
sprite = box_sprite;
}
// If game is over, show all mines
if (game_over) {
if (box.mine && !box.pressed) {
if (box.flagged) {
sprite = flagged_mine_sprite;
} else {
sprite = mine_sprite;
}
}
}
sprite.setPosition(pos);
window.draw(sprite);
if (box.pressed && !box.mine) {
sf::Text text = box.boxDigit(digit_colors, digit_font);
// Text is not quite centered so it has to be moved a little
text.setPosition(pos - sf::Vector2f(-4, 3));
window.draw(text);
}
}
}
}
const sf::Vector2u BoxField::calculatePressedBox(const sf::Vector2u & position) const
{
/**
* Calculate to which box is pressed with given position. E.g. if position.x
* is somewhere between [0,1), then pressed box should be in first column.
*/
unsigned int x = position.x / box_width
+ (position.x % box_width ? 1 : 0)
- 1;
unsigned int y = position.y / box_height
+ (position.y % box_height ? 1 : 0)
- 1;
return sf::Vector2u(x, y);
}
void BoxField::mark(const sf::Vector2u & position)
{
if (!game_over) {
const sf::Vector2u & box_pos = calculatePressedBox(position);
// This shouldn't be possible
if (box_pos.x >= width || box_pos.y >= height) {
throw std::invalid_argument("Invalid press location");
}
auto & box = field[box_pos.x][box_pos.y];
box.flagged = !box.flagged;
}
}
void BoxField::pressAdjacent(int x, int y)
{
if (x < 0 || y < 0 || (unsigned int)x >= width || (unsigned int)y >= height) {
return;
}
auto & box = field[x][y];
if (box.mine || box.pressed) {
return;
}
box.pressed = true;
/* Try to press all adjacent boxes, but not diagonally adjacent. */
pressAdjacent(x - 1, y);
pressAdjacent(x, y - 1);
pressAdjacent(x + 1, y);
pressAdjacent(x, y + 1);
}
void BoxField::press(const sf::Vector2u & position)
{
if (!game_over) {
const sf::Vector2u & box_pos = calculatePressedBox(position);
if (box_pos.x >= width || box_pos.y >= height) {
return;
}
auto & box = field[box_pos.x][box_pos.y];
if (!is_mines_set) {
/* If mines are not set, this is the first press so we can setup the
* mines now. If we would have done it earlier, game could be over
* on first press which isn't very nice.
*/
// FIXME: hack
box.pressed = true;
setupMines();
box.pressed = false;
calculateTouchingMines();
}
if (!box.mine) {
pressAdjacent(box_pos.x, box_pos.y);
} else {
box.pressed = true;
game_over = true;
}
}
}
bool BoxField::checkIfWon()
{
for (auto & row : field) {
for (auto & box : row) {
if (box.mine && !box.flagged) {
return false;
}
if (!box.mine && !box.pressed) {
return false;
}
}
}
game_over = true;
return true;
}
void BoxField::reset()
{
if (game_over) {
for (auto & row : field) {
for (auto & box : row) {
box.pressed = false;
box.flagged = false;
box.mine = false;
box.mines_touching = 0;
}
}
is_mines_set = false;
game_over = false;
}
}
bool BoxField::isOver() const
{
return game_over;
}