-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmines.c
611 lines (526 loc) · 17.3 KB
/
mines.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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#define _POSIX_C_SOURCE 2
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#define MINE_STATE_NORMAL 0
#define MINE_STATE_MINED 1
#define MINE_STATE_FLAGGED 2
#define MINE_STATE_UNKNOWN 3
typedef enum minefield_status {
MINEFIELD_STATUS_EMPTY = 0,
MINEFIELD_STATUS_GENERATED = 1,
MINEFIELD_STATUS_VICTORY = 2,
MINEFIELD_STATUS_LOST = 3,
} minefield_status_t;
#define MINEFIELD_STATUS_IS_GAME_OVER(status) (((status) | 1) == MINEFIELD_STATUS_LOST)
typedef struct mine {
uint8_t is_mine : 1;
uint8_t state : 2;
uint8_t _unused : 1;
uint8_t nearby : 4;
} mine_t;
typedef struct minefield {
unsigned width;
unsigned height;
unsigned mine_count;
minefield_status_t status;
unsigned flag_count;
unsigned unmined_count;
unsigned changed_x0;
unsigned changed_y0;
unsigned changed_x1;
unsigned changed_y1;
mine_t data[/* width * height */];
} minefield_t;
void minefield_reset(minefield_t *field)
{
field->status = MINEFIELD_STATUS_EMPTY;
field->flag_count = 0;
field->unmined_count = field->width * field->height - field->mine_count;
field->changed_x0 = 0;
field->changed_y0 = 0;
field->changed_x1 = field->width;
field->changed_y1 = field->height;
}
minefield_t *minefield_recreate(minefield_t *field, unsigned width, unsigned height, unsigned mine_count)
{
minefield_t *new_field = realloc(field, sizeof(minefield_t) + width * height * sizeof(mine_t));
if (new_field == NULL)
{
free(field);
return NULL;
}
field = new_field;
field->width = width;
field->height = height;
field->mine_count = mine_count;
minefield_reset(field);
return field;
}
minefield_t *minefield_create(unsigned width, unsigned height, unsigned mine_count)
{
return minefield_recreate(NULL, width, height, mine_count);
}
void minefield_destroy(minefield_t *field)
{
free(field);
}
void minefield_generate(minefield_t *field, unsigned x, unsigned y)
{
assert(field->status == MINEFIELD_STATUS_EMPTY);
assert(x < field->width && y < field->height);
assert(field->mine_count <= field->width * field->height - 3*3);
field->status = MINEFIELD_STATUS_GENERATED;
memset(field->data, 0, field->width * field->height * sizeof(mine_t));
unsigned safe_x0 = x == 0 ? 0 : x - 1;
unsigned safe_y0 = y == 0 ? 0 : y - 1;
unsigned safe_x1 = x == field->width - 1 ? field->width - 1 : x + 1;
unsigned safe_y1 = y == field->height - 1 ? field->height - 1 : y + 1;
unsigned safe_area = (safe_x1 - safe_x0 + 1) * (safe_y1 - safe_y0 + 1);
for (unsigned i = 0; i < field->mine_count; i++) {
unsigned r = rand();
unsigned pos = r % (field->width * field->height - safe_area - i);
for (unsigned j = 0; j <= pos; j++) {
unsigned x = j % field->width;
unsigned y = j / field->width;
if ((safe_x0 <= x && x <= safe_x1 && safe_y0 <= y && y <= safe_y1) || field->data[j].is_mine)
pos++;
}
unsigned pos_x = pos % field->width;
unsigned pos_y = pos / field->width;
field->data[pos].is_mine = 1;
(void)pos_x; (void)pos_y;
unsigned x0 = pos_x == 0 ? 0 : pos_x - 1;
unsigned y0 = pos_y == 0 ? 0 : pos_y - 1;
unsigned x1 = pos_x == field->width - 1 ? field->width - 1 : pos_x + 1;
unsigned y1 = pos_y == field->height - 1 ? field->height - 1 : pos_y + 1;
for (unsigned y = y0; y <= y1; y++) {
for (unsigned x = x0; x <= x1; x++) {
field->data[y * field->width + x].nearby++;
}
}
}
}
mine_t *minefield_get_mine_ptr(minefield_t *field, unsigned x, unsigned y)
{
assert(x < field->width && y < field->height);
return &field->data[y * field->width + x];
}
mine_t minefield_get_mine(minefield_t *field, unsigned x, unsigned y)
{
return *minefield_get_mine_ptr(field, x, y);
}
void _minefield_invalidate_at(minefield_t *field, unsigned x, unsigned y)
{
if (field->changed_x0 > x) field->changed_x0 = x;
if (field->changed_y0 > y) field->changed_y0 = y;
if (field->changed_x1 <= x) field->changed_x1 = x + 1;
if (field->changed_y1 <= y) field->changed_y1 = y + 1;
}
bool _minefield_mine_around(minefield_t *field, unsigned x, unsigned y);
bool _minefield_mine(minefield_t *field, unsigned x, unsigned y, bool is_user_input)
{
// invalidate the area even if no change happened just because
_minefield_invalidate_at(field, x, y);
mine_t *m = minefield_get_mine_ptr(field, x, y);
switch (m->state) {
case MINE_STATE_NORMAL:
m->state = MINE_STATE_MINED;
if (m->is_mine) {
field->status = MINEFIELD_STATUS_LOST;
return false;
}
field->unmined_count--;
if (field->unmined_count == 0) {
field->status = MINEFIELD_STATUS_VICTORY;
}
if (m->nearby == 0) {
return _minefield_mine_around(field, x, y);
}
case MINE_STATE_FLAGGED:
case MINE_STATE_UNKNOWN:
break;
case MINE_STATE_MINED:
if (is_user_input) {
unsigned nearby_flags = 0;
unsigned x0 = x == 0 ? 0 : x - 1;
unsigned y0 = y == 0 ? 0 : y - 1;
unsigned x1 = x == field->width - 1 ? field->width - 1 : x + 1;
unsigned y1 = y == field->height - 1 ? field->height - 1 : y + 1;
for (unsigned y = y0; y <= y1; y++) {
for (unsigned x = x0; x <= x1; x++) {
nearby_flags += minefield_get_mine(field, x, y).state == MINE_STATE_FLAGGED;
}
}
// NOTE: This expects m->state != FLAGGED
if (nearby_flags == m->nearby)
return _minefield_mine_around(field, x, y);
}
break;
}
return true;
}
bool _minefield_mine_around(minefield_t *field, unsigned x, unsigned y)
{
unsigned x0 = x == 0 ? 0 : x - 1;
unsigned y0 = y == 0 ? 0 : y - 1;
unsigned x1 = x == field->width - 1 ? field->width - 1 : x + 1;
unsigned y1 = y == field->height - 1 ? field->height - 1 : y + 1;
for (unsigned y = y0; y <= y1; y++) {
for (unsigned x = x0; x <= x1; x++) {
if (!_minefield_mine(field, x, y, false))
return false;
}
}
return true;
}
void minefield_mine(minefield_t *field, unsigned x, unsigned y)
{
assert(x < field->width && y < field->height);
if (MINEFIELD_STATUS_IS_GAME_OVER(field->status))
return;
if (field->status == MINEFIELD_STATUS_EMPTY)
minefield_generate(field, x, y);
_minefield_mine(field, x, y, true);
}
void minefield_flag(minefield_t *field, unsigned x, unsigned y)
{
assert(x < field->width && y < field->height);
if (field->status != MINEFIELD_STATUS_GENERATED)
return;
// invalidate the area even if no change happened just because
_minefield_invalidate_at(field, x, y);
mine_t *m = &field->data[y * field->width + x];
switch (m->state) {
case MINE_STATE_NORMAL:
m->state = MINE_STATE_FLAGGED;
field->flag_count++;
break;
case MINE_STATE_FLAGGED:
m->state = MINE_STATE_UNKNOWN;
field->flag_count--;
break;
case MINE_STATE_UNKNOWN:
m->state = MINE_STATE_NORMAL;
break;
case MINE_STATE_MINED:
break;
}
}
bool terminal_init(struct termios *old)
{
struct termios new;
if (tcgetattr(STDIN_FILENO, &new) < 0)
return false;
*old = new;
new.c_lflag &= ~ICANON;
new.c_lflag &= ~ECHO;
new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &new) < 0)
return false;
return true;
}
bool terminal_restore(struct termios *old)
{
return tcsetattr(STDIN_FILENO, TCSANOW, old) >= 0;
}
bool terminal_try_restore(struct termios *old)
{
if (!terminal_restore(old)) {
printf("Failed to restore terminal attributes!\n");
return false;
}
return true;
}
void print_status_line(minefield_t *field, unsigned x, unsigned y)
{
printf("\033[%dH", field->height + 1); // move cursor to status line
printf("\033[J"); // clear rest of the screen
printf("\nP %u,%u M %u/%u C %u", x + 1, y + 1, field->mine_count - field->flag_count, field->mine_count, field->unmined_count);
switch (field->status) {
case MINEFIELD_STATUS_EMPTY:
printf(" --empty--");
break;
case MINEFIELD_STATUS_GENERATED:
printf(" ---generated---");
break;
case MINEFIELD_STATUS_VICTORY:
printf(" You win!");
break;
case MINEFIELD_STATUS_LOST:
printf(" You lose!");
break;
}
}
unsigned char minefield_get_mine_visual(minefield_t *field, unsigned x, unsigned y)
{
mine_t mine = minefield_get_mine(field, x, y);
if (field->status == MINEFIELD_STATUS_EMPTY || mine.state == MINE_STATE_NORMAL)
return '.';
else if (mine.state == MINE_STATE_FLAGGED)
return 'f';
else if (mine.state == MINE_STATE_UNKNOWN)
return '?';
else // MINE_STATE_MINED
{
if (mine.is_mine)
return 'x';
else
return '0' + mine.nearby;
}
}
// This thing takes 2 kB on 64-bit!
const char *const _mine_visual_colored_map[UCHAR_MAX] = {
['.'] = "\033[97m.",
['f'] = "\033[1;36mf", // bold, cyan
['?'] = "\033[1;34m?", // bold, blue
['x'] = "\033[1;31mx", // bold, red
['0'] = " ",
['1'] = "\033[1;32m1", // bold, green
['2'] = "\033[32m2", // green
['3'] = "\033[1;33m3", // bold, yellow
['4'] = "\033[33m4", // yellow
['5'] = "\033[1;31m5", // bold, red
['6'] = "\033[31m6", // red
['7'] = "\033[35m7", // magenta
['8'] = "\033[1;35m8", // bold, magenta
};
void _minefield_draw_mine(minefield_t *field, unsigned cursor_x, unsigned cursor_y, unsigned x, unsigned y)
{
if (x == cursor_x && y == cursor_y)
printf("\033[7m"); // reverse video
unsigned char c = minefield_get_mine_visual(field, x, y);
assert(_mine_visual_colored_map[c]);
printf("%s ", _mine_visual_colored_map[c]);
printf("\033[m"); // reset attributes
}
void minefield_draw_region(minefield_t *field, unsigned cursor_x, unsigned cursor_y, unsigned x0, unsigned x1, unsigned y0, unsigned y1)
{
for (unsigned y = y0; y < y1; y++) {
printf("\033[%d;%dH", y + 1, x0 * 2 + 1);
for (unsigned x = x0; x < x1; x++) {
_minefield_draw_mine(field, cursor_x, cursor_y, x, y);
}
}
}
void minefield_draw_at(minefield_t *field, unsigned cursor_x, unsigned cursor_y, unsigned x, unsigned y)
{
printf("\033[%d;%dH", y + 1, x * 2 + 1); // Set cursor position
_minefield_draw_mine(field, cursor_x, cursor_y, x, y);
}
void minefield_draw_changed(minefield_t *field, unsigned cursor_x, unsigned cursor_y)
{
minefield_draw_region(field, cursor_x, cursor_y, field->changed_x0, field->changed_x1, field->changed_y0, field->changed_y1);
}
void game_loop(minefield_t **field_ptr)
{
minefield_t *field = *field_ptr;
char c;
#define read_char() if (read(0, &c, 1) != 1) return
unsigned old_cursor_x = 0;
unsigned old_cursor_y = 0;
unsigned cursor_x = 0;
unsigned cursor_y = 0;
for (;;) {
print_status_line(*field_ptr, cursor_x, cursor_y);
printf("\033[H");
if (field->changed_x0 != UINT_MAX) {
minefield_draw_changed(field, cursor_x, cursor_y);
field->changed_x0 = UINT_MAX;
field->changed_x1 = 0;
field->changed_y0 = UINT_MAX;
field->changed_y1 = 0;
} else if (old_cursor_x != cursor_x || old_cursor_y != cursor_y) {
minefield_draw_at(field, cursor_x, cursor_y, old_cursor_x, old_cursor_y);
minefield_draw_at(field, cursor_x, cursor_y, cursor_x, cursor_y);
}
old_cursor_x = cursor_x;
old_cursor_y = cursor_y;
printf("\033[%d;%dH", cursor_y + 1, cursor_x * 2 + 1); // Set cursor position
fflush(stdout);
read_char();
switch (c) {
case 'w':
if (cursor_y == 0)
cursor_y = field->height;
cursor_y--;
break;
case 's':
cursor_y++;
if (cursor_y == field->height)
cursor_y = 0;
break;
case 'a':
if (cursor_x == 0)
cursor_x = field->width;
cursor_x--;
break;
case 'd':
cursor_x++;
if (cursor_x == field->width)
cursor_x = 0;
break;
case 'e':
minefield_mine(field, cursor_x, cursor_y);
break;
case 'f':
minefield_flag(field, cursor_x, cursor_y);
break;
case 'n':
minefield_reset(field);
break;
case 'q':
return;
default: break;
};
}
}
unsigned parse_uint(const char *restrict str)
{
char * endptr;
long value = strtol(str, &endptr, 0);
if (str + strlen(str) != endptr)
return UINT_MAX;
if (value < 0)
return UINT_MAX;
return (unsigned)value;
}
int main(int argc, char **argv)
{
srand(0);
(void)argc;
(void)argv;
unsigned difficulty = UINT_MAX;
unsigned width = UINT_MAX;
unsigned height = UINT_MAX;
unsigned mine_count = UINT_MAX;
bool fullscreen = false;
int c;
while ((c = getopt(argc, argv, "d:w:h:m:fF")) != -1) {
switch (c) {
case 'd':
switch (optarg[0]) {
case 'e':
difficulty = 0;
break;
case 'm':
case 'n':
difficulty = 1;
break;
case 'h':
difficulty = 2;
break;
default:
printf("Unknown difficulty \"%s\"!\n", optarg);
return 1;
}
break;
case 'w':
width = parse_uint(optarg);
if (width == UINT_MAX) {
printf("Unable to parse width \"%s\"!\n", optarg);
return 1;
}
break;
case 'h':
height = parse_uint(optarg);
if (height == UINT_MAX) {
printf("Unable to parse height \"%s\"!\n", optarg);
return 1;
}
break;
case 'm':
mine_count = parse_uint(optarg);
if (mine_count == UINT_MAX) {
printf("Unable to parse mine_count \"%s\"!\n", optarg);
return 1;
}
break;
case 'f':
fullscreen = true;
break;
case 'F':
fullscreen = false;
break;
default:
return 1;
}
}
if (difficulty == 0) {
width = 10;
height = 10;
mine_count = 10;
} else if (difficulty == 1) {
width = 16;
height = 16;
mine_count = 40;
} else if (difficulty == 2) {
width = 30;
height = 16;
mine_count = 99;
}
if (width == UINT_MAX || height == UINT_MAX || mine_count == UINT_MAX) {
printf("You must set a difficulty, or width, height and mine count!\n");
printf("\n");
printf("Usage:\n");
printf("mines -d difficulty [-f]\n");
printf("mines -w width -h height -m mines [-f]\n");
printf("\n");
printf("Options:\n");
printf("-d difficulty\n");
printf(" Available difficulties:\n");
printf(" 10x10 10 easy\n");
printf(" 16x16 40 normal medium\n");
printf(" 30x16 99 hard\n");
printf("\n");
printf("-w width\n");
printf("-h height\n");
printf("-m mines\n");
printf(" Sets the width, height and mine count of the game field.\n");
printf("\n");
printf("-f Enables fullscreen mode.\n");
printf(" \n");
printf(" Sets width and height to fill the terminal and\n");
printf(" adjusts mine count while keeping the same density.\n");
printf("\n");
printf("-F Disables fullscreen mode.\n");
return 1;
}
if (fullscreen) {
double mine_density = (double)mine_count / width / height;
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
return 1;
width = ws.ws_col / 2;
height = ws.ws_row - 2;
mine_count = mine_density * width * height;
}
struct termios old;
if (!terminal_init(&old)) {
printf("Failed to initialize terminal!\n");
return 1;
}
minefield_t *field = minefield_create(width, height, mine_count);
if (!field) {
terminal_try_restore(&old);
printf("Failed to create minefield!\n");
return 1;
}
printf("\033[?1049h"); // enable alternative screen buffer
printf("\033[3J\033[H\033[J"); // clear the screen, move cursor to top left, clear again
printf("\033[?25l"); // hide cursor
game_loop(&field);
printf("\033[?25h"); // show cursor
printf("\033[?1049l"); // disable alternative screen buffer
minefield_destroy(field);
terminal_try_restore(&old);
}