]>
git.gir.st - minesVIiper.git/blob - mines_2017.c
1 /*******************************************************************************
3 By Tobias Girstmair, 2015 - 2018
6 (see ./minesviiper -h for full list of options)
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 - bduw to jump left/down/up/right by 5 cells
12 - o to open and choord
15 GNU GPL v3, see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt
16 *******************************************************************************/
19 #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/
35 #define MIN(a,b) (a>b?b:a)
36 #define MAX(a,b) (a>b?a:b)
37 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
38 #define printm(num, str) for (int i = 0; i < num; i++) fputs (str, stdout)
39 #define print(str) fputs (str, stdout)
42 unsigned m
:2; /* mine?1:killmine?2:0 */
43 unsigned o
:1; /* open?1:0 */
44 unsigned f
:2; /* flagged?1:questioned?2:0 */
45 unsigned n
:4; /* 0<= neighbours <=8 */
49 //TODO: rename w, h to L, C
52 int m
; /* number of mines */
54 int f
; /* flags counter */
55 int t
; /* time of game start */
56 int p
[2]; /* cursor position {line, col} */
60 struct minescheme
* scheme
;
61 int mode
; /* allow flags? quesm? */
71 void fill_minefield (int, int);
73 void cursor_move (int, int);
74 int getch (unsigned char*);
75 int getctrlseq (unsigned char*);
76 int everything_opened ();
77 int wait_mouse_up (int, int);
78 void partial_show_minefield (int, int, int);
79 void show_minefield (int);
80 int get_neighbours (int, int, int);
81 int uncover_square (int, int);
82 void flag_square (int, int);
83 void quesm_square (int, int);
84 int choord_square (int, int);
85 int do_uncover (int*);
86 struct minecell
** alloc_array (int, int);
88 int screen2field_l (int);
89 int screen2field_c (int);
90 int field2screen_l (int);
91 int field2screen_c (int);
93 void signal_handler (int signum
);
94 void timer_setup (int);
107 enum fieldopenstates
{
123 /* for getctrlseq() */
129 CTRSEQ_MOUSE_LEFT
= -4,
130 CTRSEQ_MOUSE_MIDDLE
= -5,
131 CTRSEQ_MOUSE_RIGHT
= -6,
139 void signal_handler (int signum
) {
142 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3);
143 printf ("[%03d]", f
.t
?(int)difftime (time(NULL
), f
.t
):0);
150 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
151 struct termios saved_term_mode
;
152 struct termios
set_raw_term_mode() {
153 struct termios cur_term_mode
, raw_term_mode
;
155 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
156 raw_term_mode
= cur_term_mode
;
157 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
158 raw_term_mode
.c_cc
[VMIN
] = 1 ;
159 raw_term_mode
.c_cc
[VTIME
] = 0;
160 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
162 return cur_term_mode
;
164 void restore_term_mode(struct termios saved_term_mode
) {
165 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
169 int main (int argc
, char** argv
) {
170 struct sigaction saction
;
171 saved_term_mode
= set_raw_term_mode();
175 saction
.sa_handler
= signal_handler
;
176 sigemptyset(&saction
.sa_mask
);
177 saction
.sa_flags
= 0;
178 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
183 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
187 /* end screen setup */
193 f
.c
= NULL
; /*to not free() array before it is allocated*/
195 op
.scheme
= &symbols_mono
;
200 opterr
= 0; /* don't print message on unrecognized option */
201 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
203 case 'n': op
.mode
= NOFLAG
; break;
204 case 'f': op
.mode
= FLAG
; break; /*default*/
205 case 'q': op
.mode
= QUESM
; break;
206 case 'c': op
.scheme
= &symbols_col1
; break;
207 case 'd': op
.scheme
= &symbols_doublewidth
; break;
210 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
214 " -q(uestion marks)\n"
215 " -c(olored symbols)\n"
216 " -d(ec charset symbols)\n"
218 " WxHxM (width 'x' height 'x' mines)\n"
219 " defaults to 30x16x99\n"
221 "hjkl: move 1 left/down/up/right\n"
222 "bduw: move 5 left/down/up/right\n"
223 "^Gg$: move to the left/bottom/top/right\n"
224 "left mouse/o: open/choord\n"
225 "right mouse/i: flag/unflag\n"
226 "space: modeful cursor (either open or flag)\n"
227 "a: toggle mode for space (open/flag)\n"
228 ":D / r: start a new game\n"
229 "q: quit\n", argv
[0]);
233 /* end parse options*/
235 sscanf (argv
[optind
], "%dx%dx%d", &(f
.w
), &(f
.h
), &(f
.m
));
237 /* check boundaries */
238 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
239 f
.m
= (f
.w
-1) * (f
.h
-1);
240 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
245 f
.c
= alloc_array (f
.h
, f
.w
);
254 int space_mode
= MODE_OPEN
;
255 struct line_col markers
[26];
256 for (int i
=26; i
; markers
[--i
].l
= -1);
258 /* switch to alternate screen */
259 printf ("\033[?47h");
261 /* reset cursor, clear screen */
262 printf ("\033[H\033[J");
264 /* swich charset, if necessary */
265 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
267 show_minefield (NORMAL
);
269 /* enable mouse, hide cursor */
270 printf ("\033[?1000h\033[?25l");
275 unsigned char mouse
[3];
277 action
= getch(mouse
);
280 if (space_mode
== MODE_OPEN
||
281 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
282 switch (do_uncover(&is_newgame
)) {
283 case GAME_LOST
: goto lose
;
284 case GAME_WON
: goto win
;
286 } else if (space_mode
== MODE_FLAG
) {
287 flag_square (f
.p
[0], f
.p
[1]);
288 } else if (space_mode
== MODE_QUESM
) {
289 quesm_square (f
.p
[0], f
.p
[1]);
292 case 'a': space_mode
= (space_mode
+1)%(op
.mode
+1); break;
293 case CTRSEQ_MOUSE_LEFT
:
294 f
.p
[0] = screen2field_l (mouse
[2]);
295 f
.p
[1] = screen2field_c (mouse
[1]);
296 /* :D clicked: TODO: won't work in single-width mode! */
297 if (mouse
[2] == LINE_OFFSET
-1 &&
298 (mouse
[1] == f
.w
+COL_OFFSET
||
299 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
303 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
304 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
307 switch (do_uncover(&is_newgame
)) {
308 case GAME_LOST
: goto lose
;
309 case GAME_WON
: goto win
;
312 case CTRSEQ_MOUSE_RIGHT
:
313 f
.p
[0] = screen2field_l (mouse
[2]);
314 f
.p
[1] = screen2field_c (mouse
[1]);
315 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
316 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
318 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
319 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
321 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
322 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
323 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
324 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
325 case 'w': cursor_move (f
.p
[0], f
.p
[1]+BM
); break;
326 case 'b': cursor_move (f
.p
[0], f
.p
[1]-BM
); break;
327 case 'u': cursor_move (f
.p
[0]-BM
, f
.p
[1] ); break;
328 case 'd': cursor_move (f
.p
[0]+BM
, f
.p
[1] ); break;
329 case '0': /* fallthrough */
330 case '^': cursor_move (f
.p
[0], 0 ); break;
331 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
332 case 'g': cursor_move (0, f
.p
[1] ); break;
333 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
336 action
= tolower(getch(mouse
));
337 if (action
< 'a' || action
> 'z') break;/*out of bound*/
338 markers
[action
-'a'].l
= f
.p
[0];
339 markers
[action
-'a'].c
= f
.p
[1];
341 case'\'': /* fallthrough */
343 action
= tolower(getch(mouse
));
344 if (action
< 'a' || action
> 'z' /* out of bound or */
345 || markers
[action
-'a'].l
== -1) break; /* unset */
346 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
348 case 'r': /* start a new game */
353 case '\014': /* Ctrl-L -- redraw */
354 show_minefield (NORMAL
);
359 fill_minefield (-1, -1);
362 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
363 cheatmode
= !cheatmode
;
370 timer_setup(0); /* stop timer */
371 show_minefield (SHOWMINES
);
374 unsigned char mouse
[3];
375 gotaction
= getch(mouse
);
376 /* :D clicked: TODO: won't work in single-width mode! */
377 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
378 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
381 } else if (gotaction
== 'r') {
384 } else if (gotaction
== 'q') {
394 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
395 /* disable mouse, show cursor */
396 printf ("\033[?9l\033[?25h");
397 /* reset charset, if necessary */
398 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
399 /* revert to primary screen */
400 if (alt_screen
) printf ("\033[?47l");
402 restore_term_mode(saved_term_mode
);
405 /* I haven't won as long as a cell exists, that
406 - I haven't opened, and
408 int everything_opened () {
409 for (int row
= 0; row
< f
.h
; row
++)
410 for (int col
= 0; col
< f
.w
; col
++)
411 if (f
.c
[row
][col
].o
== CLOSED
&&
412 f
.c
[row
][col
].m
== NO_MINE
) return 0;
416 int wait_mouse_up (int l
, int c
) {
417 unsigned char mouse2
[3];
422 move (1, field2screen_c (f
.w
/2)-1); print (":o");
424 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
425 /* show a pushed-in button if cursor is on minefield */
426 move (l
+LINE_OFFSET
, field2screen_c(c
));
427 fputs (op
.scheme
->mouse_highlight
, stdout
);
431 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
432 /* ignore mouse wheel events: */
433 if (mouse2
[0] & 0x40) continue;
435 else if (mouse2
[0]&3 == 3) level
--; /* release event */
436 else level
++; /* another button pressed */
440 move (1, field2screen_c (f
.w
/2)-1); print (":D");
441 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
442 partial_show_minefield (l
, c
, NORMAL
);
444 c2
= screen2field_c(mouse2
[1]);
445 l2
= screen2field_l(mouse2
[2]);
446 return ((l2
== l
) && (c2
== c
));
449 int choord_square (int line
, int col
) {
450 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
451 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
452 if (f
.c
[l
][c
].f
!= FLAG
) {
453 if (uncover_square (l
, c
))
462 int uncover_square (int l
, int c
) {
463 f
.c
[l
][c
].o
= OPENED
;
464 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
465 partial_show_minefield (l
, c
, NORMAL
);
468 f
.c
[l
][c
].m
= DEATH_MINE
;
472 /* check for chording */
473 if (f
.c
[l
][c
].n
== 0) {
474 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
475 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
476 int newl
= l
+ choord_l
;
477 int newc
= c
+ choord_c
;
478 if (newl
>= 0 && newl
< f
.h
&&
479 newc
>= 0 && newc
< f
.w
&&
480 f
.c
[newl
][newc
].o
== CLOSED
&&
481 uncover_square (newl
, newc
)) {
491 void flag_square (int l
, int c
) {
492 if (f
.c
[l
][c
].o
!= CLOSED
) return;
493 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
495 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
496 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
497 else f
.f
--; //WARN: breaks on `-q'!
498 partial_show_minefield (l
, c
, NORMAL
);
499 move (1, op
.scheme
->cell_width
);
500 printf ("[%03d]", f
.f
);
503 void quesm_square (int l
, int c
) {
504 /* toggle question mark / none. won't turn flags into question marks.
505 unlike flag_square, this function doesn't respect `-q'. */
506 if (f
.c
[l
][c
].o
!= CLOSED
) return;
507 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
508 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
509 partial_show_minefield (l
, c
, NORMAL
);
512 int do_uncover (int* is_newgame
) {
515 fill_minefield (f
.p
[0], f
.p
[1]);
519 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
520 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
521 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
522 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
523 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
525 if (everything_opened()) return GAME_WON
;
527 return GAME_INPROGRESS
;
530 void fill_minefield (int l
, int c
) {
534 int line
= rand() % f
.h
;
535 int col
= rand() % f
.w
;
537 if (f
.c
[line
][col
].m
) {
538 /* skip if field already has a mine */
540 } else if ((line
== l
) && (col
== c
)) {
541 /* don't put a mine on the already opened (first click) field */
545 f
.c
[line
][col
].m
= STD_MINE
;
549 /* precalculate neighbours */
550 for (int l
=0; l
< f
.h
; l
++)
551 for (int c
=0; c
< f
.w
; c
++)
552 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
555 void move (int line
, int col
) {
556 printf ("\033[%d;%dH", line
+1, col
+1);
559 /* absolute coordinates! */
560 void cursor_move (int l
, int c
) {
561 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
563 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
564 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
565 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
566 //fputs (op.scheme->mouse_highlight, stdout);
568 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
) print("\033[7m");//invert unless ! or ?
569 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
570 print("\033[0m");//un-invert
573 char* cell2schema (int l
, int c
, int mode
) {
574 struct minecell cell
= f
.c
[l
][c
];
575 /* move past invert-ctrlsequence when highlighting the cursor: */
576 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
578 if (mode
== SHOWMINES
) return (
579 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
580 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
+offset
:
581 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
582 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
583 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
584 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
586 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
587 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
588 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
589 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
590 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
591 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
594 void partial_show_minefield (int l
, int c
, int mode
) {
595 move (l
+LINE_OFFSET
, field2screen_c(c
));
597 print (cell2schema(l
, c
, mode
));
600 void show_minefield (int mode
) {
608 dtime
= difftime (time(NULL
), f
.t
);
612 print (op
.scheme
->border_top_l
);
613 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
614 printf ("%s\r\n", op
.scheme
->border_top_r
);
616 print (op
.scheme
->border_status_l
);
617 printf("[%03d]", f
.f
);
618 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
619 printf ("%s", mode
==SHOWMINES
?":C":":D");
620 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
621 printf ("[%03d]", dtime
);
622 print (op
.scheme
->border_status_r
);
625 print (op
.scheme
->border_spacer_l
);
626 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
627 print (op
.scheme
->border_spacer_r
);
630 for (int l
= 0; l
< f
.h
; l
++) {
631 print (op
.scheme
->border_field_l
);
632 for (int c
= 0; c
< f
.w
; c
++) {
633 print (cell2schema(l
, c
, mode
));
635 print (op
.scheme
->border_field_r
); print ("\r\n");
638 print (op
.scheme
->border_bottom_l
);
639 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
640 print (op
.scheme
->border_bottom_r
);
644 int get_neighbours (int line
, int col
, int reduced_mode
) {
645 /* counts mines surrounding a square
646 modes: 0=normal; 1=reduced */
650 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
651 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
652 if (!l
&& !c
) continue;
654 count
+= !!f
.c
[l
][c
].m
;
655 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
661 struct minecell
** alloc_array (int lines
, int cols
) {
662 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
663 if (a
== NULL
) return NULL
;
664 for (int l
= 0; l
< lines
; l
++) {
665 a
[l
] = calloc (cols
, sizeof(struct minecell
));
666 if (a
[l
] == NULL
) goto unalloc
;
671 for (int l
= 0; l
< lines
; l
++)
677 if (f
.c
== NULL
) return;
678 for (int l
= 0; l
< f
.h
; l
++) {
684 int screen2field_l (int l
) {
685 return (l
-LINE_OFFSET
) - 1;
687 /* some trickery is required to extract the mouse position from the cell width,
688 depending on wheather we are using full width characters or double line width.
689 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
690 int screen2field_c (int c
) {
691 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
693 int field2screen_l (int l
) {
694 return 0; //TODO: is never used, therefore not implemented
696 int field2screen_c (int c
) {
697 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
706 int getctrlseq (unsigned char* buf
) {
709 int offset
= 0x20; /* never sends control chars as data */
710 while ((c
= getchar()) != EOF
) {
714 case '\033': state
=ESC_SENT
; break;
720 case '[': state
=CSI_SENT
; break;
721 default: return CTRSEQ_INVALID
;
726 case 'M': state
=MOUSE_EVENT
; break;
727 default: return CTRSEQ_INVALID
;
732 buf
[1] = getchar() - offset
;
733 buf
[2] = getchar() - offset
;
736 return CTRSEQ_INVALID
;
742 int getch(unsigned char* buf
) {
743 /* returns a character, EOF, or constant for an escape/control sequence - NOT
744 compatible with the ncurses implementation of same name */
745 int action
= getctrlseq(buf
);
749 l
= screen2field_l (buf
[2]);
750 c
= screen2field_c (buf
[1]);
752 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
753 int success
= wait_mouse_up(l
, c
);
755 /* mouse moved while pressed: */
756 if (!success
) return CTRSEQ_INVALID
;
759 case 0: return CTRSEQ_MOUSE_LEFT
;
760 case 1: return CTRSEQ_MOUSE_MIDDLE
;
761 case 2: return CTRSEQ_MOUSE_RIGHT
;
768 void timer_setup (int enable
) {
769 static struct itimerval tbuf
;
770 tbuf
.it_interval
.tv_sec
= 1;
771 tbuf
.it_interval
.tv_usec
= 0;
775 tbuf
.it_value
.tv_sec
= 1;
776 tbuf
.it_value
.tv_usec
= 0;
777 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
782 tbuf
.it_value
.tv_sec
= 0;
783 tbuf
.it_value
.tv_usec
= 0;
784 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {