-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
319 lines (264 loc) · 6.34 KB
/
main.c
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
// Raylib Minesweeper
// Andrew Hamel Codes
// https://github.com/AndrewHamel111/raylib-minesweeper
// https://www.youtube.com/watch?v=3-EYrPRdjp4
#include <stdlib.h>
#include <time.h>
#include "raylib.h"
#include "raymath.h"
#define COLS 15
#define ROWS 15
const int screenWidth = 600;
const int screenHeight = 600;
const int cellWidth = screenWidth / COLS;
const int cellHeight = screenHeight / ROWS;
const char* youLose = "YOU LOSE!";
const char* youWin = "YOU WIN!";
const char* pressRToRestart = "Press 'r' to play again!";
typedef struct Cell
{
int i;
int j;
bool containsMine;
bool revealed;
bool flagged;
int nearbyMines;
} Cell;
Cell grid[COLS][ROWS];
Texture2D flagSprite;
int tilesRevealed;
int minesPresent;
typedef enum GameState
{
PLAYING,
LOSE,
WIN
} GameState;
GameState state;
float timeGameStarted;
float timeGameEnded;
void CellDraw(Cell);
bool IndexIsValid(int, int);
void CellReveal(int, int);
void CellFlag(int, int);
int CellCountMines(int, int);
void GridInit(void);
void GridFloodClearFrom(int, int);
void GameInit(void);
int main()
{
srand(time(0));
InitWindow(screenWidth, screenHeight, "Raylib Minesweeper by Andrew Hamel");
flagSprite = LoadTexture("resources/flag.png");
GameInit();
while(!WindowShouldClose())
{
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Vector2 mPos = GetMousePosition();
int indexI = mPos.x / cellWidth;
int indexJ = mPos.y / cellHeight;
if (state == PLAYING && IndexIsValid(indexI, indexJ))
{
CellReveal(indexI, indexJ);
}
}
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
{
Vector2 mPos = GetMousePosition();
int indexI = mPos.x / cellWidth;
int indexJ = mPos.y / cellHeight;
if (state == PLAYING && IndexIsValid(indexI, indexJ))
{
CellFlag(indexI, indexJ);
}
}
if (IsKeyPressed(KEY_R))
{
GameInit();
}
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < COLS; i++)
{
for (int j = 0; j < ROWS; j++)
{
CellDraw(grid[i][j]);
}
}
if (state == LOSE)
{
DrawRectangle(0, 0, screenWidth,screenHeight, Fade(WHITE, 0.8f));
DrawText(youLose, screenWidth / 2 - MeasureText(youLose, 40) / 2, screenHeight / 2 - 10, 40, DARKGRAY);
DrawText(pressRToRestart, screenWidth / 2 - MeasureText(pressRToRestart, 20) / 2, screenHeight * 0.75f - 10, 20, DARKGRAY);
int minutes = (int)(timeGameEnded - timeGameStarted) / 60;
int seconds = (int)(timeGameEnded - timeGameStarted) % 60;
DrawText(TextFormat("Time played: %d minutes, %d seconds.", minutes, seconds), 20, screenHeight - 40, 20, DARKGRAY);
}
if (state == WIN)
{
DrawRectangle(0, 0, screenWidth,screenHeight, Fade(WHITE, 0.8f));
DrawText(youWin, screenWidth / 2 - MeasureText(youWin, 40) / 2, screenHeight / 2 - 10, 40, DARKGRAY);
DrawText(pressRToRestart, screenWidth / 2 - MeasureText(pressRToRestart, 20) / 2, screenHeight * 0.75f - 10, 20, DARKGRAY);
int minutes = (int)(timeGameEnded - timeGameStarted) / 60;
int seconds = (int)(timeGameEnded - timeGameStarted) % 60;
DrawText(TextFormat("Time played: %d minutes, %d seconds.", minutes, seconds), 20, screenHeight - 40, 20, DARKGRAY);
}
EndDrawing();
}
CloseWindow();
return 0;
}
void CellDraw(Cell cell)
{
if (cell.revealed)
{
if (cell.containsMine)
{
DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, RED);
}
else
{
DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, LIGHTGRAY);
if (cell.nearbyMines > 0)
{
DrawText(TextFormat("%d", cell.nearbyMines), cell.i * cellWidth + 12, cell.j * cellHeight + 4, cellHeight - 8, DARKGRAY);
}
}
}
else if (cell.flagged)
{
// draw flag
Rectangle source = {0, 0, flagSprite.width, flagSprite.height};
Rectangle dest = {cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight};
Vector2 origin = {0, 0};
DrawTexturePro(flagSprite, source, dest, origin, 0.0f, Fade(WHITE, 0.5f));
}
DrawRectangleLines(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, BLACK);
}
bool IndexIsValid(int i, int j)
{
return i >= 0 && i < COLS && j >= 0 && j < ROWS;
}
void CellReveal(int i, int j)
{
if (grid[i][j].flagged || grid[i][j].revealed)
{
return;
}
grid[i][j].revealed = true;
if (grid[i][j].containsMine)
{
state = LOSE;
timeGameEnded = GetTime();
}
else
{
if (grid[i][j].nearbyMines == 0)
{
GridFloodClearFrom(i, j);
}
tilesRevealed++;
if (tilesRevealed >= ROWS * COLS - minesPresent)
{
state = WIN;
timeGameEnded = GetTime();
}
}
}
void CellFlag(int i, int j)
{
if (grid[i][j].revealed)
{
return;
}
grid[i][j].flagged = !grid[i][j].flagged;
}
int CellCountMines(int i, int j)
{
int count = 0;
for (int iOff = -1; iOff <= 1; iOff++)
{
for (int jOff = -1; jOff <= 1; jOff++)
{
if (iOff == 0 && jOff == 0)
{
continue;
}
if (!IndexIsValid(i + iOff, j + jOff))
{
continue;
}
if (grid[i + iOff][j + jOff].containsMine)
{
count++;
}
}
}
return count;
}
void GridInit(void)
{
for (int i = 0; i < COLS; i++)
{
for (int j = 0; j < ROWS; j++)
{
grid[i][j] = (Cell)
{
.i = i,
.j = j,
.containsMine = false,
.revealed = false,
.flagged = false,
.nearbyMines = -1
};
}
}
minesPresent = (int)(ROWS * COLS * 0.1f);
int minesToPlace = minesPresent;
while (minesToPlace > 0)
{
int i = rand() % COLS;
int j = rand() % ROWS;
if (!grid[i][j].containsMine)
{
grid[i][j].containsMine = true;
minesToPlace--;
}
}
for (int i = 0; i < COLS; i++)
{
for (int j = 0; j < ROWS; j++)
{
if (!grid[i][j].containsMine)
{
grid[i][j].nearbyMines = CellCountMines(i, j);
}
}
}
}
void GridFloodClearFrom(int i, int j)
{
for (int iOff = -1; iOff <= 1; iOff++)
{
for (int jOff = -1; jOff <= 1; jOff++)
{
if (iOff == 0 && jOff == 0)
{
continue;
}
if (!IndexIsValid(i + iOff, j + jOff))
{
continue;
}
CellReveal(i + iOff, j + jOff);
}
}
}
void GameInit(void)
{
GridInit();
state = PLAYING;
tilesRevealed = 0;
timeGameStarted = GetTime();
}