]>
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);
106 enum fieldopenstates
{
122 /* for getctrlseq() */
128 CTRSEQ_MOUSE_LEFT
= -4,
129 CTRSEQ_MOUSE_MIDDLE
= -5,
130 CTRSEQ_MOUSE_RIGHT
= -6,
138 void signal_handler (int signum
) {
141 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3);
142 printf ("[%03d]", f
.t
?(int)difftime (time(NULL
), f
.t
):0);
149 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
150 struct termios saved_term_mode
;
151 struct termios
set_raw_term_mode() {
152 struct termios cur_term_mode
, raw_term_mode
;
154 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
155 raw_term_mode
= cur_term_mode
;
156 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
157 raw_term_mode
.c_cc
[VMIN
] = 1 ;
158 raw_term_mode
.c_cc
[VTIME
] = 0;
159 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
161 return cur_term_mode
;
163 void restore_term_mode(struct termios saved_term_mode
) {
164 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
168 int main (int argc
, char** argv
) {
169 struct sigaction saction
;
170 saved_term_mode
= set_raw_term_mode();
174 saction
.sa_handler
= signal_handler
;
175 sigemptyset(&saction
.sa_mask
);
176 saction
.sa_flags
= 0;
177 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
182 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
186 /* end screen setup */
192 f
.c
= NULL
; /*to not free() array before it is allocated*/
194 op
.scheme
= &symbols_mono
;
199 opterr
= 0; /* don't print message on unrecognized option */
200 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
202 case 'n': op
.mode
= NOFLAG
; break;
203 case 'f': op
.mode
= FLAG
; break; /*default*/
204 case 'q': op
.mode
= QUESM
; break;
205 case 'c': op
.scheme
= &symbols_col1
; break;
206 case 'd': op
.scheme
= &symbols_doublewidth
; break;
209 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
213 " -q(uestion marks)\n"
214 " -c(olored symbols)\n"
215 " -d(ec charset symbols)\n"
217 " WxHxM (width 'x' height 'x' mines)\n"
218 " defaults to 30x16x99\n"
220 "hjkl: move 1 left/down/up/right\n"
221 "bduw: move 5 left/down/up/right\n"
222 "^Gg$: move to the left/bottom/top/right\n"
223 "left mouse/o: open/choord\n"
224 "right mouse/i: flag/unflag\n"
225 "space: modeful cursor (either open or flag)\n"
226 "a: toggle mode for space (open/flag)\n"
227 ":D / r: start a new game\n"
228 "q: quit\n", argv
[0]);
232 /* end parse options*/
234 sscanf (argv
[optind
], "%dx%dx%d", &(f
.w
), &(f
.h
), &(f
.m
));
236 /* check boundaries */
237 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
238 f
.m
= (f
.w
-1) * (f
.h
-1);
239 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
244 f
.c
= alloc_array (f
.h
, f
.w
);
253 int space_mode
= MODE_OPEN
;
254 struct line_col markers
[26];
255 for (int i
=26; i
; markers
[--i
].l
= -1);
257 /* switch to alternate screen */
258 printf ("\033[?47h");
260 /* reset cursor, clear screen */
261 printf ("\033[H\033[J");
263 /* swich charset, if necessary */
264 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
266 show_minefield (NORMAL
);
268 /* enable mouse, hide cursor */
269 printf ("\033[?1000h\033[?25l");
274 unsigned char mouse
[3];
276 action
= getch(mouse
);
279 if (space_mode
== MODE_OPEN
||
280 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
281 switch (do_uncover(&is_newgame
)) {
282 case GAME_LOST
: goto lose
;
283 case GAME_WON
: goto win
;
285 } else if (space_mode
== MODE_FLAG
) {
286 flag_square (f
.p
[0], f
.p
[1]);
287 } else if (space_mode
== MODE_QUESM
) {
288 quesm_square (f
.p
[0], f
.p
[1]);
291 case 'a': space_mode
= (space_mode
+1)%(op
.mode
+1); break;
292 case CTRSEQ_MOUSE_LEFT
:
293 f
.p
[0] = screen2field_l (mouse
[2]);
294 f
.p
[1] = screen2field_c (mouse
[1]);
295 /* :D clicked: TODO: won't work in single-width mode! */
296 if (mouse
[2] == LINE_OFFSET
-1 &&
297 (mouse
[1] == f
.w
+COL_OFFSET
||
298 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
302 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
303 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
306 switch (do_uncover(&is_newgame
)) {
307 case GAME_LOST
: goto lose
;
308 case GAME_WON
: goto win
;
311 case CTRSEQ_MOUSE_RIGHT
:
312 f
.p
[0] = screen2field_l (mouse
[2]);
313 f
.p
[1] = screen2field_c (mouse
[1]);
314 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
315 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
317 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
318 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
320 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
321 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
322 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
323 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
324 case 'w': cursor_move (f
.p
[0], f
.p
[1]+BM
); break;
325 case 'b': cursor_move (f
.p
[0], f
.p
[1]-BM
); break;
326 case 'u': cursor_move (f
.p
[0]-BM
, f
.p
[1] ); break;
327 case 'd': cursor_move (f
.p
[0]+BM
, f
.p
[1] ); break;
328 case '0': /* fallthrough */
329 case '^': cursor_move (f
.p
[0], 0 ); break;
330 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
331 case 'g': cursor_move (0, f
.p
[1] ); break;
332 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
335 action
= tolower(getch(mouse
));
336 if (action
< 'a' || action
> 'z') break;/*out of bound*/
337 markers
[action
-'a'].l
= f
.p
[0];
338 markers
[action
-'a'].c
= f
.p
[1];
340 case'\'': /* fallthrough */
342 action
= tolower(getch(mouse
));
343 if (action
< 'a' || action
> 'z' /* out of bound or */
344 || markers
[action
-'a'].l
== -1) break; /* unset */
345 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
347 case 'r': /* start a new game */
352 case '\014': /* Ctrl-L -- redraw */
353 show_minefield (NORMAL
);
358 fill_minefield (-1, -1);
361 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
362 cheatmode
= !cheatmode
;
369 timer_setup(0); /* stop timer */
370 show_minefield (SHOWMINES
);
373 unsigned char mouse
[3];
374 gotaction
= getch(mouse
);
375 /* :D clicked: TODO: won't work in single-width mode! */
376 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
377 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
380 } else if (gotaction
== 'r') {
383 } else if (gotaction
== 'q') {
393 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
394 /* disable mouse, show cursor */
395 printf ("\033[?9l\033[?25h");
396 /* reset charset, if necessary */
397 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
398 /* revert to primary screen */
399 if (alt_screen
) printf ("\033[?47l");
401 restore_term_mode(saved_term_mode
);
404 /* I haven't won as long as a cell exists, that
405 - I haven't opened, and
407 int everything_opened () {
408 for (int row
= 0; row
< f
.h
; row
++)
409 for (int col
= 0; col
< f
.w
; col
++)
410 if (f
.c
[row
][col
].o
== CLOSED
&&
411 f
.c
[row
][col
].m
== NO_MINE
) return 0;
415 int wait_mouse_up (int l
, int c
) {
416 unsigned char mouse2
[3];
421 move (1, field2screen_c (f
.w
/2)-1); print (":o");
423 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
424 /* show a pushed-in button if cursor is on minefield */
425 move (l
+LINE_OFFSET
, field2screen_c(c
));
426 fputs (op
.scheme
->mouse_highlight
, stdout
);
430 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
431 /* ignore mouse wheel events: */
432 if (mouse2
[0] & 0x40) continue;
434 else if (mouse2
[0]&3 == 3) level
--; /* release event */
435 else level
++; /* another button pressed */
439 move (1, field2screen_c (f
.w
/2)-1); print (":D");
440 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
441 partial_show_minefield (l
, c
, NORMAL
);
443 c2
= screen2field_c(mouse2
[1]);
444 l2
= screen2field_l(mouse2
[2]);
445 return ((l2
== l
) && (c2
== c
));
448 int choord_square (int line
, int col
) {
449 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
450 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
451 if (f
.c
[l
][c
].f
!= FLAG
) {
452 if (uncover_square (l
, c
))
461 int uncover_square (int l
, int c
) {
462 f
.c
[l
][c
].o
= OPENED
;
463 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
464 partial_show_minefield (l
, c
, NORMAL
);
467 f
.c
[l
][c
].m
= DEATH_MINE
;
471 /* check for chording */
472 if (f
.c
[l
][c
].n
== 0) {
473 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
474 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
475 int newl
= l
+ choord_l
;
476 int newc
= c
+ choord_c
;
477 if (newl
>= 0 && newl
< f
.h
&&
478 newc
>= 0 && newc
< f
.w
&&
479 f
.c
[newl
][newc
].o
== CLOSED
&&
480 uncover_square (newl
, newc
)) {
490 void flag_square (int l
, int c
) {
491 if (f
.c
[l
][c
].o
!= CLOSED
) return;
492 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
494 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
495 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
497 partial_show_minefield (l
, c
, NORMAL
);
498 move (1, op
.scheme
->cell_width
);
499 printf ("[%03d]", f
.f
);
502 void quesm_square (int l
, int c
) {
503 /* toggle question mark / none. won't turn flags into question marks.
504 unlike flag_square, this function doesn't respect `-q'. */
505 if (f
.c
[l
][c
].o
!= CLOSED
) return;
506 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
507 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
508 partial_show_minefield (l
, c
, NORMAL
);
511 int do_uncover (int* is_newgame
) {
514 fill_minefield (f
.p
[0], f
.p
[1]);
518 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
519 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
520 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
521 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
522 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
524 if (everything_opened()) return GAME_WON
;
526 return GAME_INPROGRESS
;
529 void fill_minefield (int l
, int c
) {
533 int line
= rand() % f
.h
;
534 int col
= rand() % f
.w
;
536 if (f
.c
[line
][col
].m
) {
537 /* skip if field already has a mine */
539 } else if ((line
== l
) && (col
== c
)) {
540 /* don't put a mine on the already opened (first click) field */
544 f
.c
[line
][col
].m
= STD_MINE
;
548 /* precalculate neighbours */
549 for (int l
=0; l
< f
.h
; l
++)
550 for (int c
=0; c
< f
.w
; c
++)
551 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
554 void move (int line
, int col
) {
555 printf ("\033[%d;%dH", line
+1, col
+1);
558 /* absolute coordinates! */
559 void cursor_move (int l
, int c
) {
560 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
562 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
563 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
564 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
565 fputs (op
.scheme
->mouse_highlight
, stdout
);
568 char* cell2schema (int l
, int c
, int mode
) {
569 if (mode
== SHOWMINES
) return (
570 f
.c
[l
][c
].f
== FLAG
&& f
.c
[l
][c
].m
? op
.scheme
->field_flagged
:
571 f
.c
[l
][c
].f
== FLAG
&& !f
.c
[l
][c
].m
? op
.scheme
->mine_wrongf
:
572 f
.c
[l
][c
].m
== STD_MINE
? op
.scheme
->mine_normal
:
573 f
.c
[l
][c
].m
== DEATH_MINE
? op
.scheme
->mine_death
:
574 f
.c
[l
][c
].o
== CLOSED
? op
.scheme
->field_closed
:
575 /*...........................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
577 f
.c
[l
][c
].f
== FLAG
? op
.scheme
->field_flagged
:
578 f
.c
[l
][c
].f
== QUESM
? op
.scheme
->field_question
:
579 f
.c
[l
][c
].o
== CLOSED
? op
.scheme
->field_closed
:
580 f
.c
[l
][c
].m
== STD_MINE
? op
.scheme
->mine_normal
:
581 f
.c
[l
][c
].m
== DEATH_MINE
? op
.scheme
->mine_death
:
582 /*...........................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
585 void partial_show_minefield (int l
, int c
, int mode
) {
586 move (l
+LINE_OFFSET
, field2screen_c(c
));
588 print (cell2schema(l
, c
, mode
));
591 void show_minefield (int mode
) {
599 dtime
= difftime (time(NULL
), f
.t
);
603 print (op
.scheme
->border_top_l
);
604 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
605 printf ("%s\r\n", op
.scheme
->border_top_r
);
607 print (op
.scheme
->border_status_l
);
608 printf("[%03d]", f
.f
);
609 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
610 printf ("%s", mode
==SHOWMINES
?":C":":D");
611 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
612 printf ("[%03d]", dtime
);
613 print (op
.scheme
->border_status_r
);
616 print (op
.scheme
->border_spacer_l
);
617 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
618 print (op
.scheme
->border_spacer_r
);
621 for (int l
= 0; l
< f
.h
; l
++) {
622 print (op
.scheme
->border_field_l
);
623 for (int c
= 0; c
< f
.w
; c
++) {
624 print (cell2schema(l
, c
, mode
));
626 print (op
.scheme
->border_field_r
); print ("\r\n");
629 print (op
.scheme
->border_bottom_l
);
630 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
631 print (op
.scheme
->border_bottom_r
);
635 int get_neighbours (int line
, int col
, int reduced_mode
) {
636 /* counts mines surrounding a square
637 modes: 0=normal; 1=reduced */
641 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
642 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
643 if (!l
&& !c
) continue;
645 count
+= !!f
.c
[l
][c
].m
;
646 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
652 struct minecell
** alloc_array (int lines
, int cols
) {
653 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
654 if (a
== NULL
) return NULL
;
655 for (int l
= 0; l
< lines
; l
++) {
656 a
[l
] = calloc (cols
, sizeof(struct minecell
));
657 if (a
[l
] == NULL
) goto unalloc
;
662 for (int l
= 0; l
< lines
; l
++)
668 if (f
.c
== NULL
) return;
669 for (int l
= 0; l
< f
.h
; l
++) {
675 int screen2field_l (int l
) {
676 return (l
-LINE_OFFSET
) - 1;
678 /* some trickery is required to extract the mouse position from the cell width,
679 depending on wheather we are using full width characters or double line width.
680 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
681 int screen2field_c (int c
) {
682 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
684 int field2screen_l (int l
) {
685 return 0; //TODO: is never used, therefore not implemented
687 int field2screen_c (int c
) {
688 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
697 int getctrlseq (unsigned char* buf
) {
700 int offset
= 0x20; /* never sends control chars as data */
701 while ((c
= getchar()) != EOF
) {
705 case '\033': state
=ESC_SENT
; break;
711 case '[': state
=CSI_SENT
; break;
712 default: return CTRSEQ_INVALID
;
717 case 'M': state
=MOUSE_EVENT
; break;
718 default: return CTRSEQ_INVALID
;
723 buf
[1] = getchar() - offset
;
724 buf
[2] = getchar() - offset
;
727 return CTRSEQ_INVALID
;
733 int getch(unsigned char* buf
) {
734 /* returns a character, EOF, or constant for an escape/control sequence - NOT
735 compatible with the ncurses implementation of same name */
736 int action
= getctrlseq(buf
);
740 l
= screen2field_l (buf
[2]);
741 c
= screen2field_c (buf
[1]);
743 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
744 int success
= wait_mouse_up(l
, c
);
746 /* mouse moved while pressed: */
747 if (!success
) return CTRSEQ_INVALID
;
750 case 0: return CTRSEQ_MOUSE_LEFT
;
751 case 1: return CTRSEQ_MOUSE_MIDDLE
;
752 case 2: return CTRSEQ_MOUSE_RIGHT
;
759 void timer_setup (int enable
) {
760 static struct itimerval tbuf
;
761 tbuf
.it_interval
.tv_sec
= 1;
762 tbuf
.it_interval
.tv_usec
= 0;
766 tbuf
.it_value
.tv_sec
= 1;
767 tbuf
.it_value
.tv_usec
= 0;
768 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
773 tbuf
.it_value
.tv_sec
= 0;
774 tbuf
.it_value
.tv_usec
= 0;
775 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {