-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.c
272 lines (253 loc) · 6.84 KB
/
minesweeper.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
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "minesweeper.h"
#ifdef _WIN32
#include <windows.h>
#endif
typedef struct
{
int mines;
int tiles;
} clipboard;
const int MAX_SIZE = 99;
int length, height;
enum GameState status;
GameInfo info;
clipboard inspection(tile field[length][height], int x, int y);
void cascade(tile field[length][height], int x, int y);
void gameOver(tile field[length][height], int bombX, int bombY);
int commission(int order);
int main(int argc, char *argv[])
{
srand(time(NULL));
// Check for custom settings, set 8x8 field by default
int orderedMines = -1;
switch (argc)
{
case 1:
length = 8, height = 8;
break;
case 2:
length = atoi(argv[1]);
height = atoi(argv[1]);
break;
case 4:
orderedMines = atoi(argv[3]);
// Fallthrough
case 3:
length = atoi(argv[1]);
height = atoi(argv[2]);
break;
default:
printf("whoah there buddy\n");
return 1;
}
if (length < 4 || height < 4)
{
printf("bad values, mate\n");
return 1;
}
if (length > MAX_SIZE || height > MAX_SIZE)
{
printf("triple digits are out of our budget\n");
return 1;
}
tile field[length][height];
setup(field, orderedMines, -1);
// Render the game (graphics.c)
render(field);
}
void setup(tile field[length][height], int clickX, int clickY)
{
/* Ok this part's a little funky so bear with me. Basically when the function is called with -1 as the last parameter
(sentinel value), clickX will be the ordered mines dude, and the function will just initialize the field and get the
quota. When called with 2 extra parameters, clickX will be the x position of the starting click and clickY the y
position, and the mines will be placed.*/
static int quota = 0;
if (clickY == -1)
{
quota = commission(clickX);
// Initialize the tiles
for (int x = 0; x < length; x++)
{
for (int y = 0; y < height; y++)
{
field[x][y].mine = false;
field[x][y].blown = false;
field[x][y].revealed = false;
field[x][y].flagged = false;
field[x][y].reserved = false;
}
}
return;
}
// Initialize time
info.gameTime = 0;
// Reserve the space around initial click
for (int xOffset = -1; xOffset < 2; xOffset++)
{
for (int yOffset = -1; yOffset < 2; yOffset++)
{
if ((clickX + xOffset >= 0 && clickX + xOffset <= length - 1) &&
(clickY + yOffset >= 0 && clickY + yOffset <= height - 1))
{
field[clickX + xOffset][clickY + yOffset].reserved = true;
}
}
}
// Plant the bombs
while (info.totalMines < quota)
{
for (int x = 0; x < length && info.totalMines < quota; x++)
{
for (int y = 0; y < height && info.totalMines < quota; y++)
{
if (!field[x][y].mine && !field[x][y].reserved)
{
bool BOMB = !(rand() % 6);
field[x][y].mine = BOMB;
info.totalMines += BOMB;
}
}
}
}
// Check for invalid arrangements and assign numbers
for (int x = 0; x < length; x++)
{
for (int y = 0; y < height; y++)
{
clipboard report = inspection(field, x, y);
if (report.mines == report.tiles)
{
// Don't tell the commissioner
field[x][y].mine = false;
info.totalMines--;
// Ninesweeper.....
report.mines--;
}
field[x][y].threats = report.mines;
}
}
status = ACTIVE;
}
clipboard inspection(tile field[length][height], int x, int y)
{
int mines = 0;
int count = 0;
for (int xOffset = -1; xOffset < 2; xOffset++)
{
for (int yOffset = -1; yOffset < 2; yOffset++)
{
if ((x + xOffset >= 0 && x + xOffset <= length - 1) &&
(y + yOffset >= 0 && y + yOffset <= height - 1))
{
mines += field[x + xOffset][y + yOffset].mine;
count++;
}
}
}
clipboard report;
report.mines = mines;
report.tiles = count;
return report;
}
// Declared in header
void dig(tile field[length][height], int x, int y)
{
field[x][y].revealed = true;
info.tilesDug++;
if (field[x][y].mine)
{
// BOMB
gameOver(field, x, y);
}
else if (field[x][y].threats == 0)
{
cascade(field, x, y);
}
if (status == ACTIVE && info.tilesDug == length * height - info.totalMines)
{
status = WON;
}
}
void cascade(tile field[length][height], int x, int y)
{
for (int xOffset = -1; xOffset < 2; xOffset++)
{
for (int yOffset = -1; yOffset < 2; yOffset++)
{
if (x + xOffset >= 0 && x + xOffset <= length - 1 &&
y + yOffset >= 0 && y + yOffset <= height - 1 &&
!field[x + xOffset][y + yOffset].revealed)
{
dig(field, x + xOffset, y + yOffset);
}
}
}
}
void gameOver(tile field[length][height], int bombX, int bombY)
{
status = LOST;
field[bombX][bombY].blown = true;
}
int commission(int order)
{
int quota;
int fieldSize = length * height;
if (0 < order && order <= fieldSize / 2 && order <= fieldSize - 9)
{
quota = order;
}
else
{
if (order == 0 || order > fieldSize / 2)
{
printf("We'll take it from here, buddy\n");
}
if (fieldSize <= 100)
{
quota = round(sqrt(fieldSize));
}
else if (100 < fieldSize && fieldSize <= 256)
{
quota = 10 + ((fieldSize - 100) * (40 - 10)) / (256 - 100);
}
else
{
quota = 40 + ((fieldSize - 256) * (99 - 40)) / (480 - 256);
}
}
return quota;
}
#ifdef __unix__
int endTimer()
{
static bool started;
static struct timespec startTime;
struct timespec endTime;
if (!started)
{
clock_gettime(CLOCK_REALTIME, &startTime);
started = true;
}
clock_gettime(CLOCK_REALTIME, &endTime);
return (endTime.tv_sec - startTime.tv_sec) + (endTime.tv_nsec - startTime.tv_nsec) / 1e9;
}
#elif _WIN32
int endTimer()
{
static bool started;
static DWORD startTime;
DWORD endTime;
if (!started)
{
startTime = GetTickCount();
started = true;
}
endTime = GetTickCount();
return (endTime - startTime) / 1000;
}
#endif