]> git.gir.st - minesVIiper.git/blob - mines_2017.c
new flag for monoscheme
[minesVIiper.git] / mines_2017.c
1 /*******************************************************************************
2 minesviiper 0.3.14592
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 /* switch to alternate screen */
310 printf ("\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 l, c;
324 int action;
325 unsigned char mouse[3];
326
327 action = getch(mouse);
328 switch (action) {
329 case ' ':
330 if (f.s == MODE_OPEN ||
331 f.c[f.p[0]][f.p[1]].o == OPENED) {
332 switch (do_uncover(&is_newgame)) {
333 case GAME_LOST: goto lose;
334 case GAME_WON: goto win;
335 }
336 } else if (f.s == MODE_FLAG) {
337 flag_square (f.p[0], f.p[1]);
338 } else if (f.s == MODE_QUESM) {
339 quesm_square (f.p[0], f.p[1]);
340 }
341 break;
342 case 'a':
343 f.s = (f.s+1)%(op.mode+1);
344 show_minefield (cheatmode?SHOWMINES:NORMAL);
345 break;
346 case CTRSEQ_MOUSE_LEFT:
347 if (clicked_emoticon(mouse)) {
348 free_field ();
349 goto newgame;
350 }
351 if (screen2field_c (mouse[1]) < 0 ||
352 screen2field_c (mouse[1]) >= f.w ||
353 screen2field_l (mouse[2]) < 0 ||
354 screen2field_l (mouse[2]) >= f.h) break;
355 f.p[0] = screen2field_l (mouse[2]);
356 f.p[1] = screen2field_c (mouse[1]);
357 /* fallthrough */
358 case 'o':
359 switch (do_uncover(&is_newgame)) {
360 case GAME_LOST: goto lose;
361 case GAME_WON: goto win;
362 }
363 break;
364 case CTRSEQ_MOUSE_RIGHT:
365 if (screen2field_c (mouse[1]) < 0 ||
366 screen2field_c (mouse[1]) >= f.w ||
367 screen2field_l (mouse[2]) < 0 ||
368 screen2field_l (mouse[2]) >= f.h) break;
369 f.p[0] = screen2field_l (mouse[2]);
370 f.p[1] = screen2field_c (mouse[1]);
371 /* fallthrough */
372 case 'i': flag_square (f.p[0], f.p[1]); break;
373 case '?':quesm_square (f.p[0], f.p[1]); break;
374 case CTRSEQ_CURSOR_LEFT:
375 case 'h': move_hi (f.p[0], f.p[1]-1 ); break;
376 case CTRSEQ_CURSOR_DOWN:
377 case 'j': move_hi (f.p[0]+1, f.p[1] ); break;
378 case CTRSEQ_CURSOR_UP:
379 case 'k': move_hi (f.p[0]-1, f.p[1] ); break;
380 case CTRSEQ_CURSOR_RIGHT:
381 case 'l': move_hi (f.p[0], f.p[1]+1 ); break;
382 case 'w': to_next_boundary (f.p[0], f.p[1], '>'); break;
383 case 'b': to_next_boundary (f.p[0], f.p[1], '<'); break;
384 case 'u': to_next_boundary (f.p[0], f.p[1], '^'); break;
385 case 'd': to_next_boundary (f.p[0], f.p[1], 'v'); break;
386 case '0': /* fallthrough */
387 case '^': move_hi (f.p[0], 0 ); break;
388 case '$': move_hi (f.p[0], f.w-1 ); break;
389 case 'g': move_hi (0, f.p[1] ); break;
390 case 'G': move_hi (f.h-1, f.p[1] ); break;
391 case 'z': move_hi (f.h/2, f.w/2); break;
392 case 'm':
393 action = tolower(getch(mouse));
394 if (action < 'a' || action > 'z') break;/*out of bound*/
395 markers[action-'a'].l = f.p[0];
396 markers[action-'a'].c = f.p[1];
397 break;
398 case'\'': /* fallthrough */
399 case '`':
400 action = tolower(getch(mouse));
401 if (action < 'a' || action > 'z' /* out of bound or */
402 || markers[action-'a'].l == -1) break; /* unset */
403 move_hi (markers[action-'a'].l, markers[action-'a'].c);
404 break;
405 case 'r': /* start a new game */
406 free_field ();
407 goto newgame;
408 case 'q':
409 goto quit;
410 case '\014': /* Ctrl-L -- redraw */
411 show_minefield (NORMAL);
412 break;
413 case '\\':
414 if (is_newgame) {
415 is_newgame = 0;
416 fill_minefield (-1, -1);
417 timer_setup(1);
418 }
419 show_minefield (cheatmode?NORMAL:SHOWMINES);
420 cheatmode = !cheatmode;
421 break;
422 }
423 }
424
425 win: f.o = GAME_WON; goto endgame;
426 lose: f.o = GAME_LOST; goto endgame;
427 endgame:
428 timer_setup(0); /* stop timer */
429 show_minefield (SHOWMINES);
430 int gotaction;
431 do {
432 unsigned char mouse[3];
433 gotaction = getch(mouse);
434 if (gotaction==CTRSEQ_MOUSE_LEFT && clicked_emoticon(mouse)) {
435 free_field ();
436 goto newgame;
437 } else if (gotaction == 'r') {
438 free_field ();
439 goto newgame;
440 } else if (gotaction == 'q') {
441 goto quit;
442 }
443 } while (1);
444
445 quit:
446 return 0;
447 }
448
449 void quit (void) {
450 move_ph(0,0);
451 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
452 print (op.scheme->reset_seq); /* reset charset, if necessary */
453 printf ("\033[?47l"); /* revert to primary screen */
454 free_field ();
455 restore_term_mode(saved_term_mode);
456 }
457
458 /* I haven't won as long as a cell exists, that
459 - I haven't opened, and
460 - is not a mine */
461 int everything_opened (void) {
462 for (int row = 0; row < f.h; row++)
463 for (int col = 0; col < f.w; col++)
464 if (f.c[row][col].o == CLOSED &&
465 f.c[row][col].m == NO_MINE ) return 0;
466 return 1;
467 }
468
469 int wait_mouse_up (int l, int c) {
470 unsigned char mouse2[3];
471 int level = 1;
472 int l2, c2;
473
474 /* show :o face */
475 move_ph (1, field2screen_c (f.w/2)-1); print (EMOT(OHH));
476
477 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
478 /* show a pushed-in button if cursor is on minefield */
479 move_ph (l+LINE_OFFSET, field2screen_c(c));
480 fputs (op.scheme->mouse_highlight, stdout);
481 }
482
483 while (level > 0) {
484 if (getctrlseq (mouse2) == CTRSEQ_MOUSE) {
485 /* ignore mouse wheel events: */
486 if (mouse2[0] & 0x40) continue;
487
488 else if (mouse2[0]&3 == 3) level--; /* release event */
489 else level++; /* another button pressed */
490 }
491 }
492
493 move_ph (1, field2screen_c (f.w/2)-1); print (get_emoticon());
494
495 if (!(l < 0 || l >= f.h || c < 0 || c >= f.w)) {
496 partial_show_minefield (l, c, NORMAL);
497 }
498 c2 = screen2field_c(mouse2[1]);
499 l2 = screen2field_l(mouse2[2]);
500 return ((l2 == l) && (c2 == c));
501 }
502
503 int choord_square (int line, int col) {
504 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
505 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
506 if (f.c[l][c].f != FLAG) {
507 if (uncover_square (l, c))
508 return 1;
509 }
510 }
511 }
512
513 return 0;
514 }
515
516 int uncover_square (int l, int c) {
517 f.c[l][c].o = OPENED;
518 f.c[l][c].f = NOFLAG; /*must not be QUESM, otherwise rendering issues*/
519 partial_show_minefield (l, c, NORMAL);
520
521 if (f.c[l][c].m) {
522 f.c[l][c].m = DEATH_MINE;
523 return 1;
524 }
525
526 /* check for chording */
527 if (f.c[l][c].n == 0) {
528 for (int choord_l = -1; choord_l <= 1; choord_l++) {
529 for (int choord_c = -1; choord_c <= 1; choord_c++) {
530 int newl = l + choord_l;
531 int newc = c + choord_c;
532 if (newl >= 0 && newl < f.h &&
533 newc >= 0 && newc < f.w &&
534 f.c[newl][newc].o == CLOSED &&
535 uncover_square (newl, newc)) {
536 return 1;
537 }
538 }
539 }
540 }
541
542 return 0;
543 }
544
545 void flag_square (int l, int c) {
546 static char modechar[] = {'*', '!', '?'};
547
548 if (f.c[l][c].o != CLOSED) return;
549 /* cycle through flag/quesm/noflag: */
550 f.c[l][c].f = (f.c[l][c].f + 1) % (op.mode + 1);
551 if (f.c[l][c].f==FLAG) f.f++;
552 else if ((op.mode==FLAG && f.c[l][c].f==NOFLAG) ||
553 (op.mode==QUESM && f.c[l][c].f==QUESM)) f.f--;
554 partial_show_minefield (l, c, NORMAL);
555 move_ph (1, op.scheme->cell_width);
556 printf ("[%03d%c]", f.m - f.f, modechar[f.s]);
557 }
558
559 void quesm_square (int l, int c) {
560 /* toggle question mark / none. won't turn flags into question marks.
561 unlike flag_square, this function doesn't respect `-q'. */
562 if (f.c[l][c].o != CLOSED) return;
563 else if (f.c[l][c].f == NOFLAG) f.c[l][c].f = QUESM;
564 else if (f.c[l][c].f == QUESM) f.c[l][c].f = NOFLAG;
565 partial_show_minefield (l, c, NORMAL);
566 }
567
568 int do_uncover (int* is_newgame) {
569 if (*is_newgame) {
570 *is_newgame = 0;
571 fill_minefield (f.p[0], f.p[1]);
572 timer_setup(1);
573 }
574
575 if (f.c[f.p[0]][f.p[1]].f == FLAG ) return GAME_INPROGRESS;
576 if (f.c[f.p[0]][f.p[1]].o == CLOSED) {
577 if (uncover_square (f.p[0], f.p[1])) return GAME_LOST;
578 } else if (get_neighbours (f.p[0], f.p[1], 1) == 0) {
579 if (choord_square (f.p[0], f.p[1])) return GAME_LOST;
580 }
581 if (everything_opened()) return GAME_WON;
582
583 return GAME_INPROGRESS;
584 }
585
586 void fill_minefield (int l, int c) {
587 srand (time(0));
588 int mines_set = f.m;
589 while (mines_set) {
590 int line = rand() % f.h;
591 int col = rand() % f.w;
592
593 if (f.c[line][col].m) {
594 /* skip if field already has a mine */
595 continue;
596 } else if ((line == l) && (col == c)) {
597 /* don't put a mine on the already opened (first click) field */
598 continue;
599 } else {
600 mines_set--;
601 f.c[line][col].m = STD_MINE;
602 }
603 }
604
605 /* precalculate neighbours */
606 for (int l=0; l < f.h; l++)
607 for (int c=0; c < f.w; c++)
608 f.c[l][c].n = get_neighbours (l, c, NORMAL);
609 }
610
611 void move_ph (int line, int col) {
612 /* move printhead to zero-indexed position */
613 printf ("\033[%d;%dH", line+1, col+1);
614 }
615
616 void move_hi (int l, int c) {
617 /* move cursor and highlight to absolute coordinates */
618
619 partial_show_minefield (f.p[0], f.p[1], NORMAL);
620 /* update f.p */
621 f.p[0] = CLAMP(l, 0, f.h-1);
622 f.p[1] = CLAMP(c, 0, f.w-1);
623 move_ph (f.p[0]+LINE_OFFSET, field2screen_c(f.p[1]));
624
625 /* invert unless ! or ? (and flag_offset is used in the scheme): */
626 if (!f.c[f.p[0]][f.p[1]].f || !op.scheme->flag_offset)
627 print("\033[7m");
628 partial_show_minefield (f.p[0], f.p[1], HIGHLIGHT);
629 print("\033[0m"); /* un-invert */
630 }
631
632 /* to_next_boundary(): move into the supplied direction until a change in open-
633 state or flag-state is found and move there. falls back to BIG_MOVE. */
634 #define FIND_NEXT(X, L, C, L1, C1, MAX, OP) do {\
635 new_ ## X OP ## = BIG_MOVE;\
636 for (int i = X OP 1; i > 0 && i < f.MAX-1; i OP ## OP)\
637 if ((f.c[L ][C ].o<<2 + f.c[L ][C ].f) \
638 != (f.c[L1][C1].o<<2 + f.c[L1][C1].f)) {\
639 new_ ## X = i OP 1;\
640 break;\
641 }\
642 } while(0)
643 void to_next_boundary (int l, int c, char direction) {
644 int new_l = l;
645 int new_c = c;
646 switch (direction) {
647 case '>': FIND_NEXT(c, l, i, l, i+1, w, +); break;
648 case '<': FIND_NEXT(c, l, i, l, i-1, w, -); break;
649 case '^': FIND_NEXT(l, i, c, i-1, c, h, -); break;
650 case 'v': FIND_NEXT(l, i, c, i+1, c, h, +); break;
651 }
652
653 move_hi (new_l, new_c);
654 }
655 #undef FIND_NEXT
656
657 char* cell2schema (int l, int c, int mode) {
658 struct minecell cell = f.c[l][c];
659 /* move past invert-ctrlsequence when highlighting the cursor: */
660 int offset = ((mode==HIGHLIGHT)*op.scheme->flag_offset);
661
662 if (mode == SHOWMINES) return (
663 cell.f == FLAG && cell.m ? op.scheme->field_flagged+offset:
664 cell.f == FLAG && !cell.m ? op.scheme->mine_wrongf:
665 cell.m == STD_MINE ? op.scheme->mine_normal:
666 cell.m == DEATH_MINE ? op.scheme->mine_death:
667 cell.o == CLOSED ? op.scheme->field_closed:
668 /*.......................*/ op.scheme->number[f.c[l][c].n]);
669 else return (
670 cell.f == FLAG ? op.scheme->field_flagged+offset:
671 cell.f == QUESM ? op.scheme->field_question+offset:
672 cell.o == CLOSED ? op.scheme->field_closed:
673 cell.m == STD_MINE ? op.scheme->mine_normal:
674 cell.m == DEATH_MINE ? op.scheme->mine_death:
675 /*.......................*/ op.scheme->number[f.c[l][c].n]);
676 }
677
678 void partial_show_minefield (int l, int c, int mode) {
679 move_ph (l+LINE_OFFSET, field2screen_c(c));
680
681 print (cell2schema(l, c, mode));
682 }
683
684 char* get_emoticon(void) {
685 return f.o==GAME_WON ? EMOT(WON):
686 f.o==GAME_LOST? EMOT(DEAD):
687 EMOT(SMILE);
688 }
689
690 /* https://zserge.com/blog/c-for-loop-tricks.html */
691 #define print_line(which) \
692 for (int _break = (printf(BORDER(which,LEFT)), 1); _break; \
693 _break = 0, printf("%s\r\n", BORDER(which,RIGHT)))
694 #define print_border(which, width) \
695 print_line(which) printm (width, BORDER(which,MIDDLE))
696 void show_minefield (int mode) {
697 int dtime = difftime (time(NULL), f.t)*!!f.t;
698 int half_spaces = f.w*op.scheme->cell_width/2;
699 int left_spaces = MAX(0,half_spaces-7-(f.m-f.f>999));
700 int right_spaces = MAX(0,half_spaces-6-(dtime>999));
701 static char modechar[] = {'*', '!', '?'};
702
703 move_ph (0,0);
704
705 print_border(TOP, f.w);
706 print_line(STATUS) {
707 printf("[%03d%c]%*s%s%*s[%03d]",
708 /* [ */ f.m - f.f, modechar[f.s], /* ] */
709 left_spaces,"", get_emoticon(), right_spaces,"",
710 /* [ */ dtime /* ] */);
711 }
712 print_border(DIVIDER, f.w);
713 /* main body */
714 for (int l = 0; l < f.h; l++) print_line(FIELD)
715 printm (f.w, cell2schema(l, _loop, mode));
716 print_border(BOTTOM, f.w);
717 }
718
719 int get_neighbours (int line, int col, int reduced_mode) {
720 /* counts mines surrounding a square
721 modes: 0=normal; 1=reduced */
722
723 int count = 0;
724
725 for (int l = MAX(line-1, 0); l <= MIN(line+1, f.h-1); l++) {
726 for (int c = MAX(col-1, 0); c <= MIN(col+1, f.w-1); c++) {
727 if (!l && !c) continue;
728
729 count += !!f.c[l][c].m;
730 count -= reduced_mode * f.c[l][c].f==FLAG;
731 }
732 }
733 return count;
734 }
735
736 struct minecell** alloc_array (int lines, int cols) {
737 struct minecell** a = malloc (lines * sizeof(struct minecell*));
738 if (a == NULL) return NULL;
739 for (int l = 0; l < lines; l++) {
740 a[l] = calloc (cols, sizeof(struct minecell));
741 if (a[l] == NULL) goto unalloc;
742 }
743
744 return a;
745 unalloc:
746 for (int l = 0; l < lines; l++)
747 free (a[l]);
748 return NULL;
749 }
750
751 void free_field (void) {
752 if (f.c == NULL) return;
753 for (int l = 0; l < f.h; l++) {
754 free (f.c[l]);
755 }
756
757 free (f.c);
758 f.c = NULL;
759 }
760
761 #define CW op.scheme->cell_width
762 int screen2field_l (int l) {
763 return (l-LINE_OFFSET) - 1;
764 }
765 int screen2field_c (int c) {
766 /* this depends on the cell width and only works when it is 1 or 2. */
767 return (c-COL_OFFSET+1 - 2*(CW%2))/2 - CW/2;
768 }
769 int field2screen_c (int c) {
770 return (CW*c+COL_OFFSET - (CW%2));
771 }
772 int clicked_emoticon (unsigned char* mouse) {
773 return (mouse[2] == LINE_OFFSET-1 && (
774 mouse[1] == f.w+COL_OFFSET ||
775 mouse[1] == f.w+COL_OFFSET+1));
776 }
777 #undef CW
778
779 enum esc_states {
780 START,
781 ESC_SENT,
782 CSI_SENT,
783 MOUSE_EVENT,
784 };
785 int getctrlseq (unsigned char* buf) {
786 int c;
787 int state = START;
788 int offset = 0x20; /* never sends control chars as data */
789 while ((c = getchar()) != EOF) {
790 switch (state) {
791 case START:
792 switch (c) {
793 case '\033': state=ESC_SENT; break;
794 default: return c;
795 }
796 break;
797 case ESC_SENT:
798 switch (c) {
799 case '[': state=CSI_SENT; break;
800 default: return CTRSEQ_INVALID;
801 }
802 break;
803 case CSI_SENT:
804 switch (c) {
805 case 'A': return CTRSEQ_CURSOR_UP;
806 case 'B': return CTRSEQ_CURSOR_DOWN;
807 case 'C': return CTRSEQ_CURSOR_RIGHT;
808 case 'D': return CTRSEQ_CURSOR_LEFT;
809 case 'M': state=MOUSE_EVENT; break;
810 default: return CTRSEQ_INVALID;
811 }
812 break;
813 case MOUSE_EVENT:
814 buf[0] = c - offset;
815 buf[1] = getchar() - offset;
816 buf[2] = getchar() - offset;
817 return CTRSEQ_MOUSE;
818 default:
819 return CTRSEQ_INVALID;
820 }
821 }
822 return 2;
823 }
824
825 int getch(unsigned char* buf) {
826 /* returns a character, EOF, or constant for an escape/control sequence - NOT
827 compatible with the ncurses implementation of same name */
828 int action = getctrlseq(buf);
829 int l, c;
830 switch (action) {
831 case CTRSEQ_MOUSE:
832 l = screen2field_l (buf[2]);
833 c = screen2field_c (buf[1]);
834
835 if (buf[0] > 3) break; /* ignore all but left/middle/right/up */
836 int success = wait_mouse_up(l, c);
837
838 /* mouse moved while pressed: */
839 if (!success) return CTRSEQ_INVALID;
840
841 switch (buf[0]) {
842 case 0: return CTRSEQ_MOUSE_LEFT;
843 case 1: return CTRSEQ_MOUSE_MIDDLE;
844 case 2: return CTRSEQ_MOUSE_RIGHT;
845 }
846 }
847
848 return action;
849 }
850
851 void timer_setup (int enable) {
852 static struct itimerval tbuf;
853 tbuf.it_interval.tv_sec = 1;
854 tbuf.it_interval.tv_usec = 0;
855
856 if (enable) {
857 f.t = time(NULL);
858 tbuf.it_value.tv_sec = 1;
859 tbuf.it_value.tv_usec = 0;
860 if (setitimer(ITIMER_REAL, &tbuf, NULL) == -1) {
861 perror("setitimer");
862 exit(1);
863 }
864 } else {
865 tbuf.it_value.tv_sec = 0;
866 tbuf.it_value.tv_usec = 0;
867 if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
868 perror("setitimer");
869 exit(1);
870 }
871 }
872
873 }
Imprint / Impressum