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
|
#include <renderer/RenderGroup.hpp>
#include <games/minesweeper/Minesweeper.hpp>
#include <algorithm>
#include <random>
// Todo: draw a difficulty selection menu at top-mid.
// Note: How many mines?
// - Beginner: 8x8 or 9x9 grid with 10 mines.
// - Intermediate: 16x16 grid with 40 mines.
// - Expert: 30x16 grid with 99 mines.
Minesweeper::Minesweeper()
{
float map_width = static_cast<float>(m_MapWidth);
float map_height = static_cast<float>(m_MapHeight);
float cell_size = 0.8f * std::min(m_WorldHeight / MAX_MAP_HEIGHT, m_WorldWidth / MAX_MAP_WIDTH);
float cell_size_without_border = 0.8f * cell_size;
m_MapViewPos = {
0.1f * cell_size_without_border + (m_WorldWidth - cell_size * map_width) / 2,
0.1f * cell_size_without_border + (m_WorldHeight - cell_size * map_height) / 2
};
m_CellOuterViewSize = {cell_size, cell_size};
m_CellInnerViewSize = {cell_size_without_border, cell_size_without_border};
// Todo: assert various stuff
Reinit();
}
void Minesweeper::Reinit() {
int32_t mine_count = 40;
memset(m_IsCoveredBitmap, 0xff, sizeof(m_IsCoveredBitmap));
memset(m_IsFlaggedBitmap, 0 , sizeof(m_IsFlaggedBitmap));
InitIsMineBitmap(mine_count);
InitAdjacentMineCounters();
}
void Minesweeper::InitIsMineBitmap(int32_t mine_count) {
assert(mine_count < m_MapWidth * m_MapHeight);
memset(m_IsMineBitmap, 0 , sizeof(m_IsMineBitmap));
std::mt19937 rng((std::random_device()()));
std::uniform_int_distribution<int32_t> dist(0, m_MapWidth * m_MapHeight - 1);
while (mine_count) {
int32_t random_pos = dist(rng);
int32_t x = random_pos / m_MapWidth;
int32_t y = random_pos % m_MapWidth;
if (!IsMine(x, y)) {
m_IsMineBitmap[y] |= 1 << x;
mine_count--;
}
}
}
void Minesweeper::InitAdjacentMineCounters() {
for (int32_t y = 0; y < m_MapHeight; y++) {
int32_t y0 = y > 0 ? y-1 : y;
int32_t y1 = y < m_MapHeight-1 ? y+1 : y;
for (int32_t x = 0; x < m_MapHeight; x++) {
int32_t x0 = x > 0 ? x-1 : x;
int32_t x1 = x < m_MapWidth-1 ? x+1 : x;
int32_t adjacent_mine_counter = 0;
for (int32_t inner_y = y0; inner_y <= y1; inner_y++) {
for (int32_t inner_x = x0; inner_x <= x1; inner_x++) {
if (IsMine(inner_x, inner_y)) {
adjacent_mine_counter++;
}
}
}
if (IsMine(x, y)) {
adjacent_mine_counter = -1;
}
m_AdjacentMineCounters[y * m_MapWidth + x] = adjacent_mine_counter;
}
}
}
bool Minesweeper::Update(std::vector<SDL_Event> &events, RenderGroup &render_group) {
V3F32 clear_color = {0.3f, 0.2f, 0.3f};
render_group.SetCameraSize(4.0f, 3.0f);
render_group.Clear(clear_color);
if (m_RunState == MinesweeperRunState::Restart) {
Reinit();
m_RunState = MinesweeperRunState::Resume;
}
for (SDL_Event &event : events) {
if (m_RunState == MinesweeperRunState::Exit) {
return false;
}
else if (m_RunState == MinesweeperRunState::Pause) {
ProcessEventDuringPause(event, render_group);
}
else if (m_RunState == MinesweeperRunState::Resume) {
ProcessEventDuringResume(event, render_group);
}
}
if (m_RunState == MinesweeperRunState::Pause) {
DrawPauseMenu(render_group);
}
else if (m_RunState == MinesweeperRunState::GameOver) {
DrawGameOverMenu(render_group);
}
DrawBoard(render_group);
bool keep_running = m_RunState != MinesweeperRunState::Exit;
return keep_running;
}
void Minesweeper::ProcessEventDuringPause(SDL_Event &event, RenderGroup &render_group) {
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
if (event.key.key == SDLK_ESCAPE) {
m_RunState = MinesweeperRunState::Resume;
}
} break;
default:;
}
}
void Minesweeper::ProcessEventDuringResume(SDL_Event &event, RenderGroup &render_group) {
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
if (event.key.key == SDLK_ESCAPE) {
m_RunState = MinesweeperRunState::Pause;
}
} break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
V2F32 click_screen_pos = {event.button.x, (float)render_group.m_ScreenHeight -1 - event.button.y};
V2F32 click_view_pos = ScreenPosToViewPos(click_screen_pos, render_group);
float x_adjusted = click_view_pos.x - m_MapViewPos.x;
float y_adjusted = click_view_pos.y - m_MapViewPos.y;
if (x_adjusted < 0.0f) {
break;
}
if (y_adjusted < 0.0f) {
break;
}
int32_t x = (int32_t)(x_adjusted / m_CellOuterViewSize.x);
int32_t y = (int32_t)(y_adjusted / m_CellOuterViewSize.y);
if (x >= m_MapWidth) {
break;
}
if (y >= m_MapHeight) {
break;
}
if (event.button.button == 1) {
if (IsCovered(x, y)) {
if (IsMine(x, y)) {
m_IsCoveredBitmap[y] &= ~(1 << x);
m_RunState = MinesweeperRunState::GameOver;
}
else {
Uncover(x, y);
}
}
}
else if (event.button.button == 3) {
if (IsCovered(x, y)) {
ToggleFlag(x ,y);
}
}
} break;
default:;
}
}
// Todo: maybe find a more efficient non-naive solution
void Minesweeper::Uncover(int32_t x, int32_t y) {
if (x < 0) return;
if (x >= m_MapWidth) return;
if (y < 0) return;
if (y >= m_MapHeight) return;
if (!IsCovered(x, y)) return;
m_IsCoveredBitmap[y] &= ~(1 << x);
if (IsFlagged(x, y)) {
ToggleFlag(x, y);
}
if (m_AdjacentMineCounters[y*m_MapWidth + x] > 0) {
return;
}
Uncover(x-1, y-1);
Uncover(x , y-1);
Uncover(x+1, y-1);
Uncover(x-1, y);
Uncover(x+1, y);
Uncover(x-1, y+1);
Uncover(x , y+1);
Uncover(x+1, y+1);
}
void Minesweeper::ToggleFlag(int32_t x, int32_t y) {
m_IsFlaggedBitmap[y] ^= (1 << x);
}
bool Minesweeper::IsCovered(int32_t x, int32_t y) {
bool is_covered = m_IsCoveredBitmap[y] & 1 << x;
return is_covered;
}
bool Minesweeper::IsFlagged(int32_t x, int32_t y) {
bool is_flagged = m_IsFlaggedBitmap[y] & 1 << x;
return is_flagged;
}
bool Minesweeper::IsMine(int32_t x, int32_t y) {
bool is_mine = m_IsMineBitmap[y] & 1 << x;
return is_mine;
}
V2F32 Minesweeper::ScreenPosToViewPos(V2F32 screen_pos, RenderGroup &render_group) {
// e.g. [0, 1024] -> [0, 1] -> [0, 4]
// e.g. [0, 768] -> [0, 1] -> [0, 3]
float screen_width = (float)render_group.m_ScreenWidth;
float screen_height = (float)render_group.m_ScreenHeight;
V2F32 view_pos;
view_pos.x = (screen_pos.x / screen_width) * m_WorldWidth;
view_pos.y = (screen_pos.y / screen_height) * m_WorldHeight;
return view_pos;
}
void Minesweeper::DrawPauseMenu(RenderGroup &render_group) {
ImGui::Begin("MinesweeperPause");
if (ImGui::Button("Resume")) {
m_RunState = MinesweeperRunState::Resume;
}
if (ImGui::Button("Exit")) {
m_RunState = MinesweeperRunState::Exit;
}
ImGui::End();
}
void Minesweeper::DrawGameOverMenu(RenderGroup &render_group) {
ImGui::Begin("MinesweeperGameOver");
ImGui::Text("Score = ???");
if (ImGui::Button("Restart")) {
m_RunState = MinesweeperRunState::Restart;
}
if (ImGui::Button("Exit")) {
m_RunState = MinesweeperRunState::Exit;
}
ImGui::End();
}
void Minesweeper::DrawBoard(RenderGroup &render_group) {
V3F32 covered_cell_color = V3F32(0.4f, 0.4f, 0.4f);
V3F32 uncovered_cell_color = V3F32(0.2f, 0.2f, 0.2f);
V3F32 flag_color = {0.6f, 0.3f, 03.f};
V3F32 mine_color = {0.8f, 0.2f, 0.2f};
V2F32 flag_draw_size = {m_CellInnerViewSize.x * 0.5f, m_CellInnerViewSize.y * 0.5f};
V2F32 flag_draw_offset = {
(m_CellInnerViewSize.x - flag_draw_size.x) / 2,
(m_CellInnerViewSize.y - flag_draw_size.y) / 2
};
// Todo: avoid if-statement by having them in separate contiguous locations?
for (int32_t y = 0; y < m_MapHeight; y++) {
for (int32_t x = 0; x < m_MapWidth; x++) {
V3F32 world_pos = {
m_MapViewPos.x + (float)x * m_CellOuterViewSize.x,
m_MapViewPos.y + (float)y * m_CellOuterViewSize.y,
0.0f
};
bool is_covered = IsCovered(x, y);
bool is_flagged = IsFlagged(x, y);
bool is_mine = IsMine(x, y);
if (is_covered) {
render_group.PushRectangle(world_pos, m_CellInnerViewSize, covered_cell_color);
}
if (is_covered && is_flagged) {
assert(IsCovered(x ,y));
V3F32 flag_world_pos = {
world_pos.x + flag_draw_offset.x,
world_pos.y + flag_draw_offset.y,
1.0f
};
render_group.PushRectangle(flag_world_pos, flag_draw_size, flag_color);
}
if (!is_covered && !is_mine) {
render_group.PushRectangle(world_pos, m_CellInnerViewSize, uncovered_cell_color);
}
if (!is_covered && is_mine) {
V3F32 mine_world_pos = {
world_pos.x,
world_pos.y,
2.0f
};
render_group.PushRectangle(mine_world_pos, m_CellInnerViewSize, mine_color);
}
}
}
}
|