]> git.gir.st - minesVIiper.git/blob - mines_2017.c
fix "\e is non-ISO-standard escape sequence"
[minesVIiper.git] / mines_2017.c
1 /*******************************************************************************
2 minesviiper 0.3.145926
3 By Tobias Girstmair, 2015 - 2018
4
5 ./minesviiper 16x16x40
6 (see ./minesviiper -h for full list of options)
7
8 MOUSE MODE: - left click to open and choord
9 - right click to flag/unflag
10 VI MODE: - hjkl to move cursor left/down/up/right
11 - o/space to open and choord
12 - i to flag/unflag
13 - (see `./minesviiper -h' for all keybindings)
14
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
17
18
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
20 #include <ctype.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/ioctl.h>
25 #include <sys/time.h>
26 #include <termios.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "schemes.h"
31
32 #define LINE_OFFSET 3
33 #define COL_OFFSET 2
34 #define BIG_MOVE 5
35
36 #define MIN(a,b) (a>b?b:a)
37 #define MAX(a,b) (a>b?a:b)
38 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
39 #define printm(n, s) for (int _loop = 0; _loop < n; _loop++) fputs (s, stdout)
40 #define print(str) fputs (str?str:"", stdout)
41 #define EMOT(e) op.scheme->emoticons[EMOT_ ## e]
42 #define BORDER(l, c) op.scheme->border[B_ ## l][B_ ## c]
43
44 struct minecell {
45 unsigned m:2; /* mine?1:killmine?2:0 */
46 unsigned o:1; /* open?1:0 */
47 unsigned f:2; /* flagged?1:questioned?2:0 */
48 unsigned n:4; /* 0<= neighbours <=8 */
49 };
50 struct minefield {
51 struct minecell **c;
52 int w; /* width */
53 int h; /* height */
54 int m; /* number of mines */
55
56 int f; /* flags counter */
57 int t; /* time of game start */
58 int p[2]; /* cursor position {line, col} */
59 int s; /* space mode */
60 int o; /* mode */
61 } f;
62
63 struct opt {
64 struct minescheme* scheme;
65 int mode; /* allow flags? quesm? */
66 } op;
67
68 struct line_col {
69 int l;
70 int c;
71 };
72
73 void fill_minefield (int, int);
74 void move_ph (int, int);
75 void move_hi (int, int);
76 void to_next_boundary (int l, int c, char direction);
77 int getch (unsigned char*);
78 int getctrlseq (unsigned char*);
79 int everything_opened (void);
80 int wait_mouse_up (int, int);
81 void partial_show_minefield (int, int, int);
82 void show_minefield (int);
83 int get_neighbours (int, int, int);
84 int uncover_square (int, int);
85 void flag_square (int, int);
86 void quesm_square (int, int);
87 int choord_square (int, int);
88 int do_uncover (int*);
89 struct minecell** alloc_array (int, int);
90 void free_field (void);
91 char* get_emoticon(void);
92 int screen2field_l (int);
93 int screen2field_c (int);
94 int field2screen_c (int);
95 int clicked_emoticon (unsigned char*);
96 void quit(void);
97 void signal_handler (int signum);
98 void timer_setup (int);
99
100 enum modes {
101 NORMAL,
102 REDUCED,
103 SHOWMINES,
104 HIGHLIGHT,
105 };
106 enum flagtypes {
107 NOFLAG,
108 FLAG,
109 QUESM,
110 };
111 enum fieldopenstates {
112 CLOSED,
113 OPENED,
114 };
115 enum game_states {
116 GAME_INPROGRESS,
117 GAME_NEW,
118 GAME_WON,
119 GAME_LOST,
120 };
121 enum space_modes {
122 MODE_OPEN,
123 MODE_FLAG,
124 MODE_QUESM,
125 };
126 enum event {
127 /* for getctrlseq() */
128 CTRSEQ_NULL = 0,
129 CTRSEQ_EOF = -1,
130 CTRSEQ_INVALID = -2,
131 CTRSEQ_MOUSE = -3,
132 /* for getch() */
133 CTRSEQ_MOUSE_LEFT = -4,
134 CTRSEQ_MOUSE_MIDDLE = -5,
135 CTRSEQ_MOUSE_RIGHT = -6,
136 CTRSEQ_CURSOR_LEFT = -7,
137 CTRSEQ_CURSOR_DOWN = -8,
138 CTRSEQ_CURSOR_UP = -9,
139 CTRSEQ_CURSOR_RIGHT = -10,
140 };
141 enum mine_types {
142 NO_MINE,
143 STD_MINE,
144 DEATH_MINE,
145 };
146
147 #define CW op.scheme->cell_width
148 void signal_handler (int signum) {
149 int dtime;
150 switch (signum) {
151 case SIGALRM:
152 dtime = difftime (time(NULL), f.t);
153 move_ph (1, f.w*CW-(CW%2)-3-(dtime>999));
154 printf ("[%03d]", f.t?dtime:0);
155 break;
156 case SIGINT:
157 exit(128+SIGINT);
158 }
159 }
160 #undef CW
161
162 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
163 struct termios saved_term_mode;
164 struct termios set_raw_term_mode() {
165 struct termios cur_term_mode, raw_term_mode;
166
167 tcgetattr(STDIN_FILENO, &cur_term_mode);
168 raw_term_mode = cur_term_mode;
169 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
170 raw_term_mode.c_cc[VMIN] = 1 ;
171 raw_term_mode.c_cc[VTIME] = 0;
172 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
173
174 return cur_term_mode;
175 }
176 void restore_term_mode(struct termios saved_term_mode) {
177 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
178 }
179
180
181 int main (int argc, char** argv) {
182 struct sigaction saction;
183
184 saction.sa_handler = signal_handler;
185 sigemptyset(&saction.sa_mask);
186 saction.sa_flags = 0;
187 if (sigaction(SIGALRM, &saction, NULL) < 0 ) {
188 perror("SIGALRM");
189 exit(1);
190 }
191
192 if (sigaction(SIGINT, &saction, NULL) < 0 ) {
193 perror ("SIGINT");
194 exit (1);
195 }
196 /* end screen setup */
197
198 op.scheme = &symbols_mono;
199 op.mode = FLAG;
200 /* end defaults */
201 /* parse options */
202 int optget;
203 opterr = 0; /* don't print message on unrecognized option */
204 while ((optget = getopt (argc, argv, "+hnfqcdx")) != -1) {
205 switch (optget) {
206 case 'n': op.mode = NOFLAG; break;
207 case 'f': op.mode = FLAG; break; /*default*/
208 case 'q': op.mode = QUESM; break;
209 case 'c': op.scheme = &symbols_col1; break;
210 case 'd': op.scheme = &symbols_doublewidth; break;
211 case 'h':
212 default:
213 fprintf (stderr, "%s [OPTIONS] [FIELDSPEC]\n"
214 "OPTIONS:\n"
215 " -n(o flagging)\n"
216 " -f(lagging)\n"
217 " -q(uestion marks)\n"
218 " -c(olored symbols)\n"
219 " -d(ec charset symbols)\n"
220 "FIELDSPEC:\n"
221 " WxH[xM] (width 'x' height 'x' mines)\n"
222 " defaults to 30x16x99; mines default to ~20%%\n"
223 "\n"
224 "Keybindings:\n"
225 " hjkl: move left/down/up/right\n"
226 " bduw: move to next boundary\n"
227 " ^Gg$: move to the left/bottom/top/right\n"
228 " z: center cursor on minefield\n"
229 " o: open/choord\n"
230 " i: flag/unflag\n"
231 " space:modeful cursor (either open or flag)\n"
232 " a: toggle mode for space (open/flag)\n"
233 " mX: set a mark for letter X\n"
234 " `X: move to mark X (aliased to ')\n"
235 " r: start a new game\n"
236 " q: quit\n", argv[0]);
237 _exit(optget=='h'?0:1);
238 }
239 }
240 /* end parse options*/
241 if (optind < argc) { /* parse Fieldspec */
242 int n = sscanf (argv[optind], "%dx%dx%d", &f.w, &f.h, &f.m);
243 struct winsize w;
244 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
245 #define CW op.scheme->cell_width
246 #define WW w.ws_col /*window width */
247 #define WH w.ws_row /*window height */
248 #define FW f.w /* field width */
249 #define FH f.h /* field height */
250 #define MM 231 /* mouse maximum*/
251 #define LB 2 /* left border */
252 #define RB 2 /* right border */
253 #define TB 3 /* top border */
254 #define BB 2 /*bottom border */
255
256 if (LB + FW*CW + RB > WW) FW = WW/CW - (LB+RB);
257 if (TB + FH + BB > WH) FH = WH - (TB+BB);
258 if (LB + FW*CW > MM) FW = MM/CW - LB;
259 if (TB + FH > MM) FH = MM - TB;
260
261 if (n < 2) {
262 fprintf (stderr, "FIELDSPEC: WxH[xM]"
263 " (width 'x' height 'x' mines)\n");
264 return 1;
265 } else if (n == 2) {
266 if (f.w < 30) f.m = f.w*f.h*.15625;
267 else f.m = f.w*f.h*.20625;
268 }
269 #undef CW
270 #undef WW
271 #undef WH
272 #undef FW
273 #undef FH
274 #undef MM
275 #undef LB
276 #undef RB
277 #undef TB
278 #undef BB
279 } else { /* use defaults */
280 f.w = 30;
281 f.h = 16;
282 f.m = 99;
283 }
284 /* check boundaries */
285 if (f.m > (f.w-1) * (f.h-1)) {
286 f.m = (f.w-1) * (f.h-1);
287 fprintf (stderr, "too many mines. reduced to %d.\r\n", f.m);
288 }
289 /* end check */
290
291 newgame:
292 f.c = alloc_array (f.h, f.w);
293
294 f.f = 0;
295 f.t = 0;
296 f.p[0] = 0;
297 f.p[1] = 0;
298
299 int is_newgame = 1;
300 int cheatmode = 0;
301 f.s = MODE_OPEN;
302 f.o = GAME_NEW;
303 struct line_col markers[26];
304 for (int i=26; i; markers[--i].l = -1);
305
306 /* setup the screen: */
307 saved_term_mode = set_raw_term_mode();
308 atexit (*quit);
309 /* save cursor and switch to alternate screen */
310 printf ("\033[s\033[?47h");
311 /* reset cursor, clear screen */
312 printf ("\033[H\033[J");
313
314 /* swich charset, if necessary */
315 if (op.scheme->init_seq != NULL) print (op.scheme->init_seq);
316
317 show_minefield (NORMAL);
318
319 /* enable mouse, hide cursor */
320 printf ("\033[?1000h\033[?25l");
321
322 while (1) {
323 int action;
324 unsigned char mouse[3];
325
326 action = getch(mouse);
327 switch (action) {
328 case ' ':
329 if (f.s == MODE_OPEN ||
330 f.c[f.p[0]][f.p[1]].o == OPENED) {
331 switch (do_uncover(&is_newgame)) {
332 case GAME_LOST: goto lose;
333 case GAME_WON: goto win;
334 }
335 } else if (f.s == MODE_FLAG) {
336 flag_square (f.p[0], f.p[1]);
337 } else if (f.s == MODE_QUESM) {
338 quesm_square (f.p[0], f.p[1]);
339 }
340 break;
341 case 'a':
342 f.s = (f.s+1)%(op.mode+1);
343 show_minefield (cheatmode?SHOWMINES:NORMAL);
344 break;
345 case CTRSEQ_MOUSE_LEFT:
346 if (clicked_emoticon(mouse)) {
347 free_field ();
348 goto newgame;
349 }
350 if (screen2field_c (mouse[1]) < 0 ||
351 screen2field_c (mouse[1]) >= f.w ||
352 screen2field_l (mouse[2]) < 0 ||
353 screen2field_l (mouse[2]) >= f.h) break;
354 f.p[0] = screen2field_l (mouse[2]);
355 f.p[1] = screen2field_c (mouse[1]);
356 /* fallthrough */
357 case 'o':
358 switch (do_uncover(&is_newgame)) {
359 case GAME_LOST: goto lose;
360 case GAME_WON: goto win;
361 }
362 break;
363 case CTRSEQ_MOUSE_RIGHT:
364 if (screen2field_c (mouse[1]) < 0 ||
365 screen2field_c (mouse[1]) >= f.w ||
366 screen2field_l (mouse[2]) < 0 ||
367 screen2field_l (mouse[2]) >= f.h) break;
368 f.p[0] = screen2field_l (mouse[2]);
369 f.p[1] = screen2field_c (mouse[1]);
370 /* fallthrough */
371 case 'i': flag_square (f.p[0], f.p[1]); break;
372 case '?':quesm_square (f.p[0], f.p[1]); break;
373 case CTRSEQ_CURSOR_LEFT:
374 case 'h': move_hi (f.p[0], f.p[1]-1 ); break;
375 case CTRSEQ_CURSOR_DOWN:
376 case 'j': move_hi (f.p[0]+1, f.p[1] ); break;
377 case CTRSEQ_CURSOR_UP:
378 case 'k': move_hi (f.p[0]-1, f.p[1] ); break;
379 case CTRSEQ_CURSOR_RIGHT:
380 case 'l': move_hi (f.p[0], f.p[1]+1 ); break;
381 case 'w': to_next_boundary (f.p[0], f.p[1], '>'); break;
382 case 'b': to_next_boundary (f.p[0], f.p[1], '<'); break;
383 case 'u': to_next_boundary (f.p[0], f.p[1], '^'); break;
384 case 'd': to_next_boundary (f.p[0], f.p[1], 'v'); break;
385 case '0': /* fallthrough */
386 case '^': move_hi (f.p[0], 0 ); break;
387 case '$': move_hi (f.p[0], f.w-1 ); break;
388 case 'g': move_hi (0, f.p[1] ); break;
389 case 'G': move_hi (f.h-1, f.p[1] ); break;
390 case 'z': move_hi (f.h/2, f.w/2); break;
391 case 'm':
392 action = tolower(getch(mouse));
393 if (action < 'a' || action > 'z') break;/*out of bound*/
394 markers[action-'a'].l = f.p[0];
395 markers[action-'a'].c = f.p[1];
396 break;
397 case'\'': /* fallthrough */
398 case '`':
399 action = tolower(getch(mouse));
400 if (action < 'a' || action > 'z' /* out of bound or */
401 || markers[action-'a'].l == -1) break; /* unset */
402 move_hi (markers[action-'a'].l, markers[action-'a'].c);
403 break;
404 case 'r': /* start a new game */
405 free_field ();
406 goto newgame;
407 case 'q':
408 goto quit;
409 case '\014': /* Ctrl-L -- redraw */
410 show_minefield (NORMAL);
411 break;
412 case '\\':
413 if (is_newgame) {
414 is_newgame = 0;
415 fill_minefield (-1, -1);
416 timer_setup(1);
417 }
418 show_minefield (cheatmode?NORMAL:SHOWMINES);
419 cheatmode = !cheatmode;
420 break;
421 }
422 }
423
424 win: f.o = GAME_WON; goto endgame;
425 lose: f.o = GAME_LOST; goto endgame;
426 endgame:
427 timer_setup(0); /* stop timer */
428 show_minefield (SHOWMINES);
429 int gotaction;
430 do {
431 unsigned char mouse[3];
432 gotaction = getch(mouse);
433 if (gotaction==CTRSEQ_MOUSE_LEFT && clicked_emoticon(mouse)) {
434 free_field ();
435 goto newgame;
436 } else if (gotaction == 'r') {
437 free_field ();
438 goto newgame;
439 } else if (gotaction == 'q') {
440 goto quit;
441 }
442 } while (1);
443
444 quit:
445 return 0;
446 }
447
448 void quit (void) {
449 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
450 print (op.scheme->reset_seq); /* reset charset, if necessary */
451 printf ("\033[?47l"); /* revert to primary screen */
452 printf ("\033[u"); /* restore cursor position */
453 free_field ();
454 restore_term_mode(saved_term_mode);
455 }
456
457 /* I haven't won as long as a cell exists, that
458 - I haven't opened, and
459 - is not a mine */
460 int everything_opened (void) {
461 for (int row = 0; row < f.h; row++)
462 for (int col = 0; col < f.w; col++)
463 if (f.c[row][col].o == CLOSED &&
464 f.c[row][col].m == NO_MINE ) return 0;
465 return 1;
466 }
467
468 int wait_mouse_up (int l, int c) {
469 unsigned char mouse2[3];
470 int level = 1;
471 int l2, c2;
472
473 /* show :o face */
474 move_ph (1, field2screen_c (f.w/2)-1); print (EMOT(OHH));
475
476 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
477 /* show a pushed-in button if cursor is on minefield */
478 move_ph (l+LINE_OFFSET, field2screen_c(c));
479 fputs (op.scheme->mouse_highlight, stdout);
480 }
481
482 while (level > 0) {
483 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
484 /* ignore mouse wheel events: */
485 if (mouse2[0] & 0x40) continue;
486
487 else if (mouse2[0]&3 == 3) level--; /* release event */
488 else level++; /* another button pressed */
489 }
490 }
491
492 move_ph (1, field2screen_c (f.w/2)-1); print (get_emoticon());
493
494 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
495 partial_show_minefield (l, c, NORMAL);
496 }
497 c2 = screen2field_c(mouse2[1]);
498 l2 = screen2field_l(mouse2[2]);
499 return ((l2 == l) && (c2 == c));
500 }
501
502 int choord_square (int line, int col) {
503 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
504 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
505 if (f.c[l][c].f != FLAG) {
506 if (uncover_square (l, c))
507 return 1;
508 }
509 }
510 }
511
512 return 0;
513 }
514
515 int uncover_square (int l, int c) {
516 f.c[l][c].o = OPENED;
517 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
518 partial_show_minefield (l, c, NORMAL);
519
520 if (f.c[l][c].m) {
521 f.c[l][c].m = DEATH_MINE;
522 return 1;
523 }
524
525 /* check for chording */
526 if (f.c[l][c].n == 0) {
527 for (int choord_l = -1; choord_l <= 1; choord_l++) {
528 for (int choord_c = -1; choord_c <= 1; choord_c++) {
529 int newl = l + choord_l;
530 int newc = c + choord_c;
531 if (newl >= 0 && newl < f.h &&
532 newc >= 0 && newc < f.w &&
533 f.c[newl][newc].o == CLOSED &&
534 uncover_square (newl, newc)) {
535 return 1;
536 }
537 }
538 }
539 }
540
541 return 0;
542 }
543
544 void flag_square (int l, int c) {
545 static char modechar[] = {'*', '!', '?'};
546
547 if (f.c[l][c].o != CLOSED) return;
548 /* cycle through flag/quesm/noflag: */
549 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
550 if (f.c[l][c].f==FLAG) f.f++;
551 else if ((op.mode==FLAG && f.c[l][c].f==NOFLAG) ||
552 (op.mode==QUESM && f.c[l][c].f==QUESM)) f.f--;
553 partial_show_minefield (l, c, NORMAL);
554 move_ph (1, op.scheme->cell_width);
555 printf ("[%03d%c]", f.m - f.f, modechar[f.s]);
556 }
557
558 void quesm_square (int l, int c) {
559 /* toggle question mark / none. won't turn flags into question marks.
560 unlike flag_square, this function doesn't respect `-q'. */
561 if (f.c[l][c].o != CLOSED) return;
562 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
563 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
564 partial_show_minefield (l, c, NORMAL);
565 }
566
567 int do_uncover (int* is_newgame) {
568 if (*is_newgame) {
569 *is_newgame = 0;
570 fill_minefield (f.p[0], f.p[1]);
571 timer_setup(1);
572 }
573
574 if (f.c[f.p[0]][f.p[1]].f == FLAG ) return GAME_INPROGRESS;
575 if (f.c[f.p[0]][f.p[1]].o == CLOSED) {
576 if (uncover_square (f.p[0], f.p[1])) return GAME_LOST;
577 } else if (get_neighbours (f.p[0], f.p[1], 1) == 0) {
578 if (choord_square (f.p[0], f.p[1])) return GAME_LOST;
579 }
580 if (everything_opened()) return GAME_WON;
581
582 return GAME_INPROGRESS;
583 }
584
585 void fill_minefield (int l, int c) {
586 srand (time(0));
587 int mines_set = f.m;
588 while (mines_set) {
589 int line = rand() % f.h;
590 int col = rand() % f.w;
591
592 if (f.c[line][col].m) {
593 /* skip if field already has a mine */
594 continue;
595 } else if ((line == l) && (col == c)) {
596 /* don't put a mine on the already opened (first click) field */
597 continue;
598 } else {
599 mines_set--;
600 f.c[line][col].m = STD_MINE;
601 }
602 }
603
604 /* precalculate neighbours */
605 for (int l=0; l < f.h; l++)
606 for (int c=0; c < f.w; c++)
607 f.c[l][c].n = get_neighbours (l, c, NORMAL);
608 }
609
610 void move_ph (int line, int col) {
611 /* move printhead to zero-indexed position */
612 printf ("\033[%d;%dH", line+1, col+1);
613 }
614
615 void move_hi (int l, int c) {
616 /* move cursor and highlight to absolute coordinates */
617
618 partial_show_minefield (f.p[0], f.p[1], NORMAL);
619 /* update f.p */
620 f.p[0] = CLAMP(l, 0, f.h-1);
621 f.p[1] = CLAMP(c, 0, f.w-1);
622 move_ph (f.p[0]+LINE_OFFSET, field2screen_c(f.p[1]));
623
624 print("\033[7m"); /* reverse video */
625 partial_show_minefield (f.p[0], f.p[1], HIGHLIGHT);
626 print("\033[0m"); /* un-invert */
627 }
628
629 /* to_next_boundary(): move into the supplied direction until a change in open-
630 state or flag-state is found and move there. falls back to BIG_MOVE. */
631 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
632 new_ ## X OP ## = BIG_MOVE;\
633 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
634 if (((f.c[L ][C ].o<<2) + f.c[L ][C ].f) \
635 != ((f.c[L1][C1].o<<2) + f.c[L1][C1].f)) {\
636 new_ ## X = i OP 1;\
637 break;\
638 }\
639 } while(0)
640 void to_next_boundary (int l, int c, char direction) {
641 int new_l = l;
642 int new_c = c;
643 switch (direction) {
644 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
645 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
646 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
647 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
648 }
649
650 move_hi (new_l, new_c);
651 }
652 #undef FIND_NEXT
653
654 char* cell2schema (int l, int c, int mode) {
655 struct minecell cell = f.c[l][c];
656
657 if (mode == SHOWMINES) return (
658 cell.f == FLAG && cell.m ? op.scheme->field_flagged:
659 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf:
660 cell.m == STD_MINE ? op.scheme->mine_normal:
661 cell.m == DEATH_MINE ? op.scheme->mine_death:
662 cell.o == CLOSED ? op.scheme->field_closed:
663 /*.......................*/ op.scheme->number[f.c[l][c].n]);
664 else return (
665 cell.f == FLAG ? op.scheme->field_flagged:
666 cell.f == QUESM ? op.scheme->field_question:
667 cell.o == CLOSED ? op.scheme->field_closed:
668 cell.m == STD_MINE ? op.scheme->mine_normal:
669 cell.m == DEATH_MINE ? op.scheme->mine_death:
670 /*.......................*/ op.scheme->number[f.c[l][c].n]);
671 }
672
673 void partial_show_minefield (int l, int c, int mode) {
674 move_ph (l+LINE_OFFSET, field2screen_c(c));
675
676 print (cell2schema(l, c, mode));
677 }
678
679 char* get_emoticon(void) {
680 return f.o==GAME_WON ? EMOT(WON):
681 f.o==GAME_LOST? EMOT(DEAD):
682 EMOT(SMILE);
683 }
684
685 /* https://zserge.com/blog/c-for-loop-tricks.html */
686 #define print_line(which) \
687 for (int _break = (printf("%s", BORDER(which,LEFT)), 1); _break; \
688 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
689 #define print_border(which, width) \
690 print_line(which) printm (width, BORDER(which,MIDDLE))
691 void show_minefield (int mode) {
692 int dtime = difftime (time(NULL), f.t)*!!f.t;
693 int half_spaces = f.w*op.scheme->cell_width/2;
694 int left_spaces = MAX(0,half_spaces-7-(f.m-f.f>999));
695 int right_spaces = MAX(0,half_spaces-6-(dtime>999));
696 static char modechar[] = {'*', '!', '?'};
697
698 move_ph (0,0);
699
700 print_border(TOP, f.w);
701 print_line(STATUS) {
702 printf("[%03d%c]%*s%s%*s[%03d]",
703 /* [ */ f.m - f.f, modechar[f.s], /* ] */
704 left_spaces,"", get_emoticon(), right_spaces,"",
705 /* [ */ dtime /* ] */);
706 }
707 print_border(DIVIDER, f.w);
708 /* main body */
709 for (int l = 0; l < f.h; l++) print_line(FIELD)
710 printm (f.w, cell2schema(l, _loop, mode));
711 print_border(BOTTOM, f.w);
712 }
713
714 int get_neighbours (int line, int col, int reduced_mode) {
715 /* counts mines surrounding a square
716 modes: 0=normal; 1=reduced */
717
718 int count = 0;
719
720 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
721 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
722 if (!l && !c) continue;
723
724 count += !!f.c[l][c].m;
725 count -= reduced_mode * f.c[l][c].f==FLAG;
726 }
727 }
728 return count;
729 }
730
731 struct minecell** alloc_array (int lines, int cols) {
732 struct minecell** a = malloc (lines * sizeof(struct minecell*));
733 if (a == NULL) return NULL;
734 for (int l = 0; l < lines; l++) {
735 a[l] = calloc (cols, sizeof(struct minecell));
736 if (a[l] == NULL) goto unalloc;
737 }
738
739 return a;
740 unalloc:
741 for (int l = 0; l < lines; l++)
742 free (a[l]);
743 return NULL;
744 }
745
746 void free_field (void) {
747 if (f.c == NULL) return;
748 for (int l = 0; l < f.h; l++) {
749 free (f.c[l]);
750 }
751
752 free (f.c);
753 f.c = NULL;
754 }
755
756 #define CW op.scheme->cell_width
757 int screen2field_l (int l) {
758 return (l-LINE_OFFSET) - 1;
759 }
760 int screen2field_c (int c) {
761 /* this depends on the cell width and only works when it is 1 or 2. */
762 return (c-COL_OFFSET+1 - 2*(CW%2))/2 - CW/2;
763 }
764 int field2screen_c (int c) {
765 return (CW*c+COL_OFFSET - (CW%2));
766 }
767 int clicked_emoticon (unsigned char* mouse) {
768 return (mouse[2] == LINE_OFFSET-1 && (
769 mouse[1] == f.w+COL_OFFSET ||
770 mouse[1] == f.w+COL_OFFSET+1));
771 }
772 #undef CW
773
774 enum esc_states {
775 START,
776 ESC_SENT,
777 CSI_SENT,
778 MOUSE_EVENT,
779 };
780 int getctrlseq (unsigned char* buf) {
781 int c;
782 int state = START;
783 int offset = 0x20; /* never sends control chars as data */
784 while ((c = getchar()) != EOF) {
785 switch (state) {
786 case START:
787 switch (c) {
788 case '\033': state=ESC_SENT; break;
789 default: return c;
790 }
791 break;
792 case ESC_SENT:
793 switch (c) {
794 case '[': state=CSI_SENT; break;
795 default: return CTRSEQ_INVALID;
796 }
797 break;
798 case CSI_SENT:
799 switch (c) {
800 case 'A': return CTRSEQ_CURSOR_UP;
801 case 'B': return CTRSEQ_CURSOR_DOWN;
802 case 'C': return CTRSEQ_CURSOR_RIGHT;
803 case 'D': return CTRSEQ_CURSOR_LEFT;
804 case 'M': state=MOUSE_EVENT; break;
805 default: return CTRSEQ_INVALID;
806 }
807 break;
808 case MOUSE_EVENT:
809 buf[0] = c - offset;
810 buf[1] = getchar() - offset;
811 buf[2] = getchar() - offset;
812 return CTRSEQ_MOUSE;
813 default:
814 return CTRSEQ_INVALID;
815 }
816 }
817 return 2;
818 }
819
820 int getch(unsigned char* buf) {
821 /* returns a character, EOF, or constant for an escape/control sequence - NOT
822 compatible with the ncurses implementation of same name */
823 int action = getctrlseq(buf);
824 int l, c;
825 switch (action) {
826 case CTRSEQ_MOUSE:
827 l = screen2field_l (buf[2]);
828 c = screen2field_c (buf[1]);
829
830 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
831 int success = wait_mouse_up(l, c);
832
833 /* mouse moved while pressed: */
834 if (!success) return CTRSEQ_INVALID;
835
836 switch (buf[0]) {
837 case 0: return CTRSEQ_MOUSE_LEFT;
838 case 1: return CTRSEQ_MOUSE_MIDDLE;
839 case 2: return CTRSEQ_MOUSE_RIGHT;
840 }
841 }
842
843 return action;
844 }
845
846 void timer_setup (int enable) {
847 static struct itimerval tbuf;
848 tbuf.it_interval.tv_sec = 1;
849 tbuf.it_interval.tv_usec = 0;
850
851 if (enable) {
852 f.t = time(NULL);
853 tbuf.it_value.tv_sec = 1;
854 tbuf.it_value.tv_usec = 0;
855 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
856 perror("setitimer");
857 exit(1);
858 }
859 } else {
860 tbuf.it_value.tv_sec = 0;
861 tbuf.it_value.tv_usec = 0;
862 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
863 perror("setitimer");
864 exit(1);
865 }
866 }
867
868 }
Imprint / Impressum