]>
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} */
57 int s
; /* space mode */
61 struct minescheme
* scheme
;
62 int mode
; /* allow flags? quesm? */
72 void fill_minefield (int, int);
74 void cursor_move (int, int);
75 int getch (unsigned char*);
76 int getctrlseq (unsigned char*);
77 int everything_opened ();
78 int wait_mouse_up (int, int);
79 void partial_show_minefield (int, int, int);
80 void show_minefield (int);
81 int get_neighbours (int, int, int);
82 int uncover_square (int, int);
83 void flag_square (int, int);
84 void quesm_square (int, int);
85 int choord_square (int, int);
86 int do_uncover (int*);
87 struct minecell
** alloc_array (int, int);
89 int screen2field_l (int);
90 int screen2field_c (int);
91 int field2screen_l (int);
92 int field2screen_c (int);
94 void signal_handler (int signum
);
95 void timer_setup (int);
108 enum fieldopenstates
{
124 /* for getctrlseq() */
130 CTRSEQ_MOUSE_LEFT
= -4,
131 CTRSEQ_MOUSE_MIDDLE
= -5,
132 CTRSEQ_MOUSE_RIGHT
= -6,
140 void signal_handler (int signum
) {
143 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3);
144 printf ("[%03d]", f
.t
?(int)difftime (time(NULL
), f
.t
):0);
151 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
152 struct termios saved_term_mode
;
153 struct termios
set_raw_term_mode() {
154 struct termios cur_term_mode
, raw_term_mode
;
156 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
157 raw_term_mode
= cur_term_mode
;
158 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
159 raw_term_mode
.c_cc
[VMIN
] = 1 ;
160 raw_term_mode
.c_cc
[VTIME
] = 0;
161 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
163 return cur_term_mode
;
165 void restore_term_mode(struct termios saved_term_mode
) {
166 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
170 int main (int argc
, char** argv
) {
171 struct sigaction saction
;
172 saved_term_mode
= set_raw_term_mode();
176 saction
.sa_handler
= signal_handler
;
177 sigemptyset(&saction
.sa_mask
);
178 saction
.sa_flags
= 0;
179 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
184 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
188 /* end screen setup */
194 f
.c
= NULL
; /*to not free() array before it is allocated*/
196 op
.scheme
= &symbols_mono
;
201 opterr
= 0; /* don't print message on unrecognized option */
202 while ((optget
= getopt (argc
, argv
, "+hnfqcdx")) != -1) {
204 case 'n': op
.mode
= NOFLAG
; break;
205 case 'f': op
.mode
= FLAG
; break; /*default*/
206 case 'q': op
.mode
= QUESM
; break;
207 case 'c': op
.scheme
= &symbols_col1
; break;
208 case 'd': op
.scheme
= &symbols_doublewidth
; break;
211 fprintf (stderr
, "%s [OPTIONS] [FIELDSPEC]\n"
215 " -q(uestion marks)\n"
216 " -c(olored symbols)\n"
217 " -d(ec charset symbols)\n"
219 " WxHxM (width 'x' height 'x' mines)\n"
220 " defaults to 30x16x99\n"
222 "hjkl: move 1 left/down/up/right\n"
223 "bduw: move 5 left/down/up/right\n"
224 "^Gg$: move to the left/bottom/top/right\n"
225 "left mouse/o: open/choord\n"
226 "right mouse/i: flag/unflag\n"
227 "space: modeful cursor (either open or flag)\n"
228 "a: toggle mode for space (open/flag)\n"
229 ":D / r: start a new game\n"
230 "q: quit\n", argv
[0]);
234 /* end parse options*/
236 sscanf (argv
[optind
], "%dx%dx%d", &(f
.w
), &(f
.h
), &(f
.m
));
238 /* check boundaries */
239 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
240 f
.m
= (f
.w
-1) * (f
.h
-1);
241 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
246 f
.c
= alloc_array (f
.h
, f
.w
);
256 struct line_col markers
[26];
257 for (int i
=26; i
; markers
[--i
].l
= -1);
259 /* switch to alternate screen */
260 printf ("\033[?47h");
262 /* reset cursor, clear screen */
263 printf ("\033[H\033[J");
265 /* swich charset, if necessary */
266 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
268 show_minefield (NORMAL
);
270 /* enable mouse, hide cursor */
271 printf ("\033[?1000h\033[?25l");
276 unsigned char mouse
[3];
278 action
= getch(mouse
);
281 if (f
.s
== MODE_OPEN
||
282 f
.c
[f
.p
[0]][f
.p
[1]].o
== OPENED
) {
283 switch (do_uncover(&is_newgame
)) {
284 case GAME_LOST
: goto lose
;
285 case GAME_WON
: goto win
;
287 } else if (f
.s
== MODE_FLAG
) {
288 flag_square (f
.p
[0], f
.p
[1]);
289 } else if (f
.s
== MODE_QUESM
) {
290 quesm_square (f
.p
[0], f
.p
[1]);
294 f
.s
= (f
.s
+1)%(op
.mode
+1);
295 show_minefield (cheatmode
?SHOWMINES
:NORMAL
);
297 case CTRSEQ_MOUSE_LEFT
:
298 f
.p
[0] = screen2field_l (mouse
[2]);
299 f
.p
[1] = screen2field_c (mouse
[1]);
300 /* :D clicked: TODO: won't work in single-width mode! */
301 if (mouse
[2] == LINE_OFFSET
-1 &&
302 (mouse
[1] == f
.w
+COL_OFFSET
||
303 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
307 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
308 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
311 switch (do_uncover(&is_newgame
)) {
312 case GAME_LOST
: goto lose
;
313 case GAME_WON
: goto win
;
316 case CTRSEQ_MOUSE_RIGHT
:
317 f
.p
[0] = screen2field_l (mouse
[2]);
318 f
.p
[1] = screen2field_c (mouse
[1]);
319 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
320 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
322 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
323 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
325 case 'h': cursor_move (f
.p
[0], f
.p
[1]-1 ); break;
326 case 'j': cursor_move (f
.p
[0]+1, f
.p
[1] ); break;
327 case 'k': cursor_move (f
.p
[0]-1, f
.p
[1] ); break;
328 case 'l': cursor_move (f
.p
[0], f
.p
[1]+1 ); break;
329 case 'w': cursor_move (f
.p
[0], f
.p
[1]+BM
); break;
330 case 'b': cursor_move (f
.p
[0], f
.p
[1]-BM
); break;
331 case 'u': cursor_move (f
.p
[0]-BM
, f
.p
[1] ); break;
332 case 'd': cursor_move (f
.p
[0]+BM
, f
.p
[1] ); break;
333 case '0': /* fallthrough */
334 case '^': cursor_move (f
.p
[0], 0 ); break;
335 case '$': cursor_move (f
.p
[0], f
.w
-1 ); break;
336 case 'g': cursor_move (0, f
.p
[1] ); break;
337 case 'G': cursor_move (f
.h
-1, f
.p
[1] ); break;
340 action
= tolower(getch(mouse
));
341 if (action
< 'a' || action
> 'z') break;/*out of bound*/
342 markers
[action
-'a'].l
= f
.p
[0];
343 markers
[action
-'a'].c
= f
.p
[1];
345 case'\'': /* fallthrough */
347 action
= tolower(getch(mouse
));
348 if (action
< 'a' || action
> 'z' /* out of bound or */
349 || markers
[action
-'a'].l
== -1) break; /* unset */
350 cursor_move (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
352 case 'r': /* start a new game */
357 case '\014': /* Ctrl-L -- redraw */
358 show_minefield (NORMAL
);
363 fill_minefield (-1, -1);
366 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
367 cheatmode
= !cheatmode
;
374 timer_setup(0); /* stop timer */
375 show_minefield (SHOWMINES
);
378 unsigned char mouse
[3];
379 gotaction
= getch(mouse
);
380 /* :D clicked: TODO: won't work in single-width mode! */
381 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
382 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
385 } else if (gotaction
== 'r') {
388 } else if (gotaction
== 'q') {
398 move(0,0); //move(f.h+LINE_OFFSET+2, 0);
399 /* disable mouse, show cursor */
400 printf ("\033[?9l\033[?25h");
401 /* reset charset, if necessary */
402 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
403 /* revert to primary screen */
404 if (alt_screen
) printf ("\033[?47l");
406 restore_term_mode(saved_term_mode
);
409 /* I haven't won as long as a cell exists, that
410 - I haven't opened, and
412 int everything_opened () {
413 for (int row
= 0; row
< f
.h
; row
++)
414 for (int col
= 0; col
< f
.w
; col
++)
415 if (f
.c
[row
][col
].o
== CLOSED
&&
416 f
.c
[row
][col
].m
== NO_MINE
) return 0;
420 int wait_mouse_up (int l
, int c
) {
421 unsigned char mouse2
[3];
426 move (1, field2screen_c (f
.w
/2)-1); print (":o");
428 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
429 /* show a pushed-in button if cursor is on minefield */
430 move (l
+LINE_OFFSET
, field2screen_c(c
));
431 fputs (op
.scheme
->mouse_highlight
, stdout
);
435 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
436 /* ignore mouse wheel events: */
437 if (mouse2
[0] & 0x40) continue;
439 else if (mouse2
[0]&3 == 3) level
--; /* release event */
440 else level
++; /* another button pressed */
444 move (1, field2screen_c (f
.w
/2)-1); print (":D");
445 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
446 partial_show_minefield (l
, c
, NORMAL
);
448 c2
= screen2field_c(mouse2
[1]);
449 l2
= screen2field_l(mouse2
[2]);
450 return ((l2
== l
) && (c2
== c
));
453 int choord_square (int line
, int col
) {
454 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
455 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
456 if (f
.c
[l
][c
].f
!= FLAG
) {
457 if (uncover_square (l
, c
))
466 int uncover_square (int l
, int c
) {
467 f
.c
[l
][c
].o
= OPENED
;
468 f
.c
[l
][c
].f
= NOFLAG
; /*must not be QUESM, otherwise rendering issues*/
469 partial_show_minefield (l
, c
, NORMAL
);
472 f
.c
[l
][c
].m
= DEATH_MINE
;
476 /* check for chording */
477 if (f
.c
[l
][c
].n
== 0) {
478 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
479 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
480 int newl
= l
+ choord_l
;
481 int newc
= c
+ choord_c
;
482 if (newl
>= 0 && newl
< f
.h
&&
483 newc
>= 0 && newc
< f
.w
&&
484 f
.c
[newl
][newc
].o
== CLOSED
&&
485 uncover_square (newl
, newc
)) {
495 void flag_square (int l
, int c
) {
496 if (f
.c
[l
][c
].o
!= CLOSED
) return;
497 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
499 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
500 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
501 else f
.f
--; //WARN: breaks on `-q'!
502 partial_show_minefield (l
, c
, NORMAL
);
503 move (1, op
.scheme
->cell_width
);
504 printf ("[%03d]", f
.f
);
507 void quesm_square (int l
, int c
) {
508 /* toggle question mark / none. won't turn flags into question marks.
509 unlike flag_square, this function doesn't respect `-q'. */
510 if (f
.c
[l
][c
].o
!= CLOSED
) return;
511 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
512 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
513 partial_show_minefield (l
, c
, NORMAL
);
516 int do_uncover (int* is_newgame
) {
519 fill_minefield (f
.p
[0], f
.p
[1]);
523 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) return GAME_INPROGRESS
;
524 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
525 if (uncover_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
526 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
527 if (choord_square (f
.p
[0], f
.p
[1])) return GAME_LOST
;
529 if (everything_opened()) return GAME_WON
;
531 return GAME_INPROGRESS
;
534 void fill_minefield (int l
, int c
) {
538 int line
= rand() % f
.h
;
539 int col
= rand() % f
.w
;
541 if (f
.c
[line
][col
].m
) {
542 /* skip if field already has a mine */
544 } else if ((line
== l
) && (col
== c
)) {
545 /* don't put a mine on the already opened (first click) field */
549 f
.c
[line
][col
].m
= STD_MINE
;
553 /* precalculate neighbours */
554 for (int l
=0; l
< f
.h
; l
++)
555 for (int c
=0; c
< f
.w
; c
++)
556 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
559 void move (int line
, int col
) {
560 printf ("\033[%d;%dH", line
+1, col
+1);
563 /* absolute coordinates! */
564 void cursor_move (int l
, int c
) {
565 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
567 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
568 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
569 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
570 //fputs (op.scheme->mouse_highlight, stdout);
572 if (!f
.c
[f
.p
[0]][f
.p
[1]].f
) print("\033[7m");//invert unless ! or ?
573 partial_show_minefield (f
.p
[0], f
.p
[1], HIGHLIGHT
);
574 print("\033[0m");//un-invert
577 char* cell2schema (int l
, int c
, int mode
) {
578 struct minecell cell
= f
.c
[l
][c
];
579 /* move past invert-ctrlsequence when highlighting the cursor: */
580 int offset
= ((mode
==HIGHLIGHT
)*op
.scheme
->flag_offset
);
582 if (mode
== SHOWMINES
) return (
583 cell
.f
== FLAG
&& cell
.m
? op
.scheme
->field_flagged
+offset
:
584 cell
.f
== FLAG
&& !cell
.m
? op
.scheme
->mine_wrongf
+offset
:
585 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
586 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
587 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
588 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
590 cell
.f
== FLAG
? op
.scheme
->field_flagged
+offset
:
591 cell
.f
== QUESM
? op
.scheme
->field_question
+offset
:
592 cell
.o
== CLOSED
? op
.scheme
->field_closed
:
593 cell
.m
== STD_MINE
? op
.scheme
->mine_normal
:
594 cell
.m
== DEATH_MINE
? op
.scheme
->mine_death
:
595 /*.......................*/ op
.scheme
->number
[f
.c
[l
][c
].n
]);
598 void partial_show_minefield (int l
, int c
, int mode
) {
599 move (l
+LINE_OFFSET
, field2screen_c(c
));
601 print (cell2schema(l
, c
, mode
));
604 void show_minefield (int mode
) {
606 static char modechar
[] = {'*', '!', '?'};
613 dtime
= difftime (time(NULL
), f
.t
);
617 print (op
.scheme
->border_top_l
);
618 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
619 printf ("%s\r\n", op
.scheme
->border_top_r
);
621 print (op
.scheme
->border_status_l
);
622 printf("[%03d]", f
.f
);
623 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
624 printf ("%s", mode
==SHOWMINES
?":C":":D");
625 printm (f
.w
*op
.scheme
->cell_width
/2-6-4, " ");
626 printf ("[%c] [%03d]", modechar
[f
.s
], dtime
);
627 print (op
.scheme
->border_status_r
);
630 print (op
.scheme
->border_spacer_l
);
631 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
632 print (op
.scheme
->border_spacer_r
);
635 for (int l
= 0; l
< f
.h
; l
++) {
636 print (op
.scheme
->border_field_l
);
637 for (int c
= 0; c
< f
.w
; c
++) {
638 print (cell2schema(l
, c
, mode
));
640 print (op
.scheme
->border_field_r
); print ("\r\n");
643 print (op
.scheme
->border_bottom_l
);
644 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
645 print (op
.scheme
->border_bottom_r
);
649 int get_neighbours (int line
, int col
, int reduced_mode
) {
650 /* counts mines surrounding a square
651 modes: 0=normal; 1=reduced */
655 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
656 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
657 if (!l
&& !c
) continue;
659 count
+= !!f
.c
[l
][c
].m
;
660 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
666 struct minecell
** alloc_array (int lines
, int cols
) {
667 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
668 if (a
== NULL
) return NULL
;
669 for (int l
= 0; l
< lines
; l
++) {
670 a
[l
] = calloc (cols
, sizeof(struct minecell
));
671 if (a
[l
] == NULL
) goto unalloc
;
676 for (int l
= 0; l
< lines
; l
++)
682 if (f
.c
== NULL
) return;
683 for (int l
= 0; l
< f
.h
; l
++) {
689 int screen2field_l (int l
) {
690 return (l
-LINE_OFFSET
) - 1;
692 /* some trickery is required to extract the mouse position from the cell width,
693 depending on wheather we are using full width characters or double line width.
694 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
695 int screen2field_c (int c
) {
696 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
698 int field2screen_l (int l
) {
699 return 0; //TODO: is never used, therefore not implemented
701 int field2screen_c (int c
) {
702 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
711 int getctrlseq (unsigned char* buf
) {
714 int offset
= 0x20; /* never sends control chars as data */
715 while ((c
= getchar()) != EOF
) {
719 case '\033': state
=ESC_SENT
; break;
725 case '[': state
=CSI_SENT
; break;
726 default: return CTRSEQ_INVALID
;
731 case 'M': state
=MOUSE_EVENT
; break;
732 default: return CTRSEQ_INVALID
;
737 buf
[1] = getchar() - offset
;
738 buf
[2] = getchar() - offset
;
741 return CTRSEQ_INVALID
;
747 int getch(unsigned char* buf
) {
748 /* returns a character, EOF, or constant for an escape/control sequence - NOT
749 compatible with the ncurses implementation of same name */
750 int action
= getctrlseq(buf
);
754 l
= screen2field_l (buf
[2]);
755 c
= screen2field_c (buf
[1]);
757 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
758 int success
= wait_mouse_up(l
, c
);
760 /* mouse moved while pressed: */
761 if (!success
) return CTRSEQ_INVALID
;
764 case 0: return CTRSEQ_MOUSE_LEFT
;
765 case 1: return CTRSEQ_MOUSE_MIDDLE
;
766 case 2: return CTRSEQ_MOUSE_RIGHT
;
773 void timer_setup (int enable
) {
774 static struct itimerval tbuf
;
775 tbuf
.it_interval
.tv_sec
= 1;
776 tbuf
.it_interval
.tv_usec
= 0;
780 tbuf
.it_value
.tv_sec
= 1;
781 tbuf
.it_value
.tv_usec
= 0;
782 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
787 tbuf
.it_value
.tv_sec
= 0;
788 tbuf
.it_value
.tv_usec
= 0;
789 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {