forked from Atrix256/Thirteen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
463 lines (402 loc) · 14.2 KB
/
main.cpp
File metadata and controls
463 lines (402 loc) · 14.2 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#define _CRT_SECURE_NO_WARNINGS // for STB
#include "../../thirteen.h"
#include <stdio.h>
#include <random>
#include <vector>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../stb/stb_image_write.h"
static const unsigned int c_width = 768;
static const unsigned int c_height = 768;
static const bool c_fullscreen = false;
static constexpr unsigned int c_boardSize[2] = { 16, 16 };
static const unsigned int c_numMines = 40;
class Board
{
public:
static void Initialize()
{
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> distX(0, c_boardSize[0] - 1);
std::uniform_int_distribution<int> distY(0, c_boardSize[1] - 1);
memset(s_mines, 0, sizeof(s_mines));
memset(s_revealed, 0, sizeof(s_revealed));
memset(s_flagged, 0, sizeof(s_flagged));
int minesPlaced = 0;
while (minesPlaced < c_numMines)
{
int x = distX(rng);
int y = distY(rng);
if (s_mines[y * c_boardSize[0] + x])
continue;
s_mines[y * c_boardSize[0] + x] = true;
minesPlaced++;
}
s_initialized = true;
}
static bool Revealed(int x, int y)
{
return s_revealed[y * c_boardSize[0] + x];
}
static bool Flagged(int x, int y)
{
return s_flagged[y * c_boardSize[0] + x];
}
static bool Mine(int x, int y)
{
return s_mines[y * c_boardSize[0] + x];
}
enum class GameResult
{
Undecided,
Win,
Lose
};
static GameResult GetGameResult()
{
// If any mines are revealed, that's a loss
for (size_t i = 0; i < c_boardSize[0] * c_boardSize[1]; ++i)
{
if (s_revealed[i] && s_mines[i])
return GameResult::Lose;
}
// If there is a square that isn't revealed, and it isn't a mine, the game is still going.
for (size_t i = 0; i < c_boardSize[0] * c_boardSize[1]; ++i)
{
if (!s_revealed[i] && !s_mines[i])
return GameResult::Undecided;
}
// Otherwise the game was won
return GameResult::Win;
}
static void OnLeftClick(int x, int y)
{
s_revealed[y * c_boardSize[0] + x] = true;
if (Mine(x, y))
return;
std::vector<int> checkForReveal;
checkForReveal.push_back(y * c_boardSize[0] + x);
while (!checkForReveal.empty())
{
int checkIndex = checkForReveal.back();
checkForReveal.pop_back();
int checkX = checkIndex % c_boardSize[0];
int checkY = checkIndex / c_boardSize[0];
if (NumNeighbors(checkX, checkY) == 0)
{
for (int iy = -1; iy <= 1; ++iy)
{
for (int ix = -1; ix <= 1; ++ix)
{
if (ix == 0 && iy == 0)
continue;
int newX = checkX + ix;
int newY = checkY + iy;
if (newX < 0 || newX >= (int)c_boardSize[0] || newY < 0 || newY >= (int)c_boardSize[1])
continue;
if (!Revealed(newX, newY))
{
s_revealed[newY * c_boardSize[0] + newX] = true;
checkForReveal.push_back(newY * c_boardSize[0] + newX);
}
}
}
}
}
}
static void OnRightClick(int x, int y)
{
s_flagged[y * c_boardSize[0] + x] = !s_flagged[y * c_boardSize[0] + x];
}
static void ScreenPosToBoardPos(int screenX, int screenY, int& boardX, int& boardY)
{
float percentX = (float(screenX) + 0.5f) / float(c_width);
float percentY = (float(screenY) + 0.5f) / float(c_height);
boardX = int(percentX * c_boardSize[0]);
boardY = int(percentY * c_boardSize[1]);
}
static int NumNeighbors(int cellX, int cellY)
{
int ret = 0;
for (int iy = -1; iy <= 1; ++iy)
{
for (int ix = -1; ix <= 1; ++ix)
{
if (ix == 0 && iy == 0)
continue;
int x = cellX + ix;
int y = cellY + iy;
if (x < 0 || x >= (int)c_boardSize[0] || y < 0 || y >= (int)c_boardSize[1])
continue;
if (Mine(x, y))
ret++;
}
}
return ret;
}
static bool DrawCircle(size_t x, size_t y, size_t centerX, size_t centerY, size_t radius)
{
float dx = float(x) - float(centerX);
float dy = float(y) - float(centerY);
float dist = std::sqrt(dx * dx + dy * dy);
return dist < float(radius);
}
static void DrawPixel(int x, int y, unsigned char* outColor, Board::GameResult gameResult)
{
outColor[3] = 255;
int cellX, cellY;
ScreenPosToBoardPos(x, y, cellX, cellY);
size_t cellWidth = c_width / c_boardSize[0];
size_t cellHeight = c_height / c_boardSize[1];
int relX = x % cellWidth;
int relY = y % cellHeight;
// Draw unrevealed cell
if (!Board::Revealed(cellX, cellY))
{
if (relX < 2 || relY < 2)
{
outColor[0] = 255;
outColor[1] = 255;
outColor[2] = 255;
}
else if (relX >= cellWidth - 2 || relY >= cellHeight - 2)
{
outColor[0] = 128;
outColor[1] = 128;
outColor[2] = 128;
}
else
{
outColor[0] = 192;
outColor[1] = 192;
outColor[2] = 192;
}
if (Board::Flagged(cellX, cellY))
{
if (DrawCircle(relX, relY, cellWidth / 2, cellHeight / 2, cellWidth / 4))
{
outColor[0] = 0;
outColor[1] = 255;
outColor[2] = 0;
}
}
}
// Draw exposed mine
else if (Board::Mine(cellX, cellY))
{
if (DrawCircle(relX, relY, cellWidth / 2, cellHeight / 2, cellWidth / 3))
{
outColor[0] = 255;
outColor[1] = 0;
outColor[2] = 0;
}
else
{
outColor[0] = 255;
outColor[1] = 128;
outColor[2] = 128;
}
}
// Draw exposed empty cell, with the number of adjacent mines
else
{
if (relX < 1 || relY < 1 || relX >= cellWidth - 1 || relY >= cellHeight - 1)
{
outColor[0] = 100;
outColor[1] = 100;
outColor[2] = 100;
}
else
{
outColor[0] = 164;
outColor[1] = 164;
outColor[2] = 164;
}
int numNeighbors = NumNeighbors(cellX, cellY);
// center dot
if (numNeighbors == 1 || numNeighbors == 3 || numNeighbors == 5 || numNeighbors == 7)
{
if (DrawCircle(relX, relY, cellWidth / 2, cellHeight / 2, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// upper left dot
if (numNeighbors == 2 || numNeighbors == 3 || numNeighbors == 4 || numNeighbors == 5 || numNeighbors == 6 || numNeighbors == 7 || numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 1 / 4, cellHeight * 1 / 4, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// lower right dot
if (numNeighbors == 2 || numNeighbors == 3 || numNeighbors == 4 || numNeighbors == 5 || numNeighbors == 6 || numNeighbors == 7 || numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 3 / 4, cellHeight * 3 / 4, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// lower left dot
if (numNeighbors == 4 || numNeighbors == 5 || numNeighbors == 6 || numNeighbors == 7 || numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 1 / 4, cellHeight * 3 / 4, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// upper right dot
if (numNeighbors == 4 || numNeighbors == 5 || numNeighbors == 6 || numNeighbors == 7 || numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 3 / 4, cellHeight * 1 / 4, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// left center dot
if (numNeighbors == 6 || numNeighbors == 7 || numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 1 / 4, cellHeight * 1 / 2, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// right center dot
if (numNeighbors == 6 || numNeighbors == 7 || numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 3 / 4, cellHeight * 1 / 2, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// center top dot
if (numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 1 / 2, cellHeight * 1 / 4, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
// center bottom dot
if (numNeighbors == 8)
{
if (DrawCircle(relX, relY, cellWidth * 1 / 2, cellHeight * 3 / 4, cellWidth / 8))
{
outColor[0] = 64;
outColor[1] = 64;
outColor[2] = 64;
}
}
}
if (gameResult == GameResult::Win)
{
outColor[1] = 255;
}
else if (gameResult == GameResult::Lose)
{
outColor[1] /= 2;
outColor[2] /= 2;
}
}
static bool s_mines[c_boardSize[0] * c_boardSize[1]];
static bool s_revealed[c_boardSize[0] * c_boardSize[1]];
static bool s_flagged[c_boardSize[0] * c_boardSize[1]];
static bool s_initialized;
};
bool Board::s_mines[c_boardSize[0] * c_boardSize[1]] = {};
bool Board::s_revealed[c_boardSize[0] * c_boardSize[1]] = {};
bool Board::s_flagged[c_boardSize[0] * c_boardSize[1]] = {};
bool Board::s_initialized = false;
int main(int argc, char** argv)
{
Thirteen::SetApplicationName("Thirteen Demo - Minesweeper");
unsigned char* pixels = Thirteen::Init(c_width, c_height, c_fullscreen);
if (!pixels)
{
printf("Could not initialize Thirteen\n");
return 1;
}
static const float c_aspectRatio = float(c_width) / float(c_height);
bool dirty = true;
Board::GameResult lastGameResult = Board::GameResult::Undecided;
do
{
// V to toggle vsync
if (Thirteen::GetKey('V') && !Thirteen::GetKeyLastFrame('V'))
Thirteen::SetVSync(!Thirteen::GetVSync());
// F to toggle full screen
if (Thirteen::GetKey('F') && !Thirteen::GetKeyLastFrame('F'))
Thirteen::SetFullscreen(!Thirteen::GetFullscreen());
// S to save a screenshot
if (Thirteen::GetKey('S') && !Thirteen::GetKeyLastFrame('S'))
stbi_write_png("screenshot.png", c_width, c_height, 4, pixels, 0);
// Initialize the board if we need to, or if space key was pressed to reset it
if (!Board::s_initialized || (Thirteen::GetKey(VK_SPACE) && !Thirteen::GetKeyLastFrame(VK_SPACE)))
{
Board::Initialize();
dirty = true;
}
// If the game is not yet over:
// Left click to reveal. Right click to toggle flag.
Board::GameResult gameResult = Board::GetGameResult();
if (gameResult == Board::GameResult::Undecided)
{
if (Thirteen::GetMouseButton(0) && !Thirteen::GetMouseButtonLastFrame(0))
{
int clickPosX, clickPosY;
Thirteen::GetMousePosition(clickPosX, clickPosY);
int cellX, cellY;
Board::ScreenPosToBoardPos(clickPosX, clickPosY, cellX, cellY);
Board::OnLeftClick(cellX, cellY);
dirty = true;
}
if (Thirteen::GetMouseButton(1) && !Thirteen::GetMouseButtonLastFrame(1))
{
int clickPosX, clickPosY;
Thirteen::GetMousePosition(clickPosX, clickPosY);
int cellX, cellY;
Board::ScreenPosToBoardPos(clickPosX, clickPosY, cellX, cellY);
Board::OnRightClick(cellX, cellY);
dirty = true;
}
}
// redraw the board if the game result changed
if (lastGameResult != gameResult)
{
lastGameResult = gameResult;
dirty = true;
}
// Draw the board, only when dirty
if (dirty)
{
for (int iy = 0; iy < c_height; ++iy)
{
for (int ix = 0; ix < c_width; ++ix)
{
size_t i = (iy * c_width + ix) * 4;
Board::DrawPixel(ix, iy, &pixels[i], gameResult);
}
}
dirty = false;
}
}
while (Thirteen::Render() && !Thirteen::GetKey(VK_ESCAPE));
Thirteen::Shutdown();
return 0;
}