]>
git.gir.st - minesVIiper.git/blob - mines_2017.c
1 /*******************************************************************************
3 By Tobias Girstmair, 2015 - 2017
5 ./minesviiper -w 16 -h 16 -m 40
6 (see ./minesviiper -\? 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 - space 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*/
34 #define MIN(a,b) (a>b?b:a)
35 #define MAX(a,b) (a>b?a:b)
36 #define CLAMP(a,m,M) (a<m?m:(a>M?M:a))
37 #define printm(num, str) for (int i = 0; i < num; i++) fputs (str, stdout)
38 #define print(str) fputs (str, stdout)
41 unsigned m
:2; /* mine?1:killmine?2:0 */
42 unsigned o
:1; /* open?1:0 */
43 unsigned f
:2; /* flagged?1:questioned?2:0 */
44 unsigned n
:4; /* 0<= neighbours <=8 */
48 //TODO: rename w, h to L, C
51 int m
; /* number of mines */
53 int f
; /* flags counter */
54 int t
; /* time of game start */
55 int p
[2]; /* cursor position {line, col} */
59 struct minescheme
* scheme
;
60 int mode
; /* allow flags? quesm? */
63 void fill_minefield (int, int);
65 void set_cursor_pos (int, int);
66 int getch (unsigned char*);
67 int getctrlseq (unsigned char*);
68 int everything_opened ();
69 int wait_mouse_up (int, int);
70 void partial_show_minefield (int, int, int);
71 void show_minefield (int);
72 int get_neighbours (int, int, int);
73 int uncover_square (int, int);
74 void flag_square (int, int);
75 void quesm_square (int, int);
76 int choord_square (int, int);
77 struct minecell
** alloc_array (int, int);
79 int screen2field_l (int);
80 int screen2field_c (int);
81 int field2screen_l (int);
82 int field2screen_c (int);
84 void signal_handler (int signum
);
96 enum fieldopenstates
{
101 /* for getctrlseq() */
107 CTRSEQ_MOUSE_LEFT
= -4,
108 CTRSEQ_MOUSE_MIDDLE
= -5,
109 CTRSEQ_MOUSE_RIGHT
= -6,
117 void signal_handler (int signum
) {
120 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3);
121 printf ("[%03d]", f
.t
?(int)difftime (time(NULL
), f
.t
):0);
128 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
129 struct termios saved_term_mode
;
130 struct termios
set_raw_term_mode() {
131 struct termios cur_term_mode
, raw_term_mode
;
133 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
134 raw_term_mode
= cur_term_mode
;
135 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
136 raw_term_mode
.c_cc
[VMIN
] = 1 ;
137 raw_term_mode
.c_cc
[VTIME
] = 0;
138 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
140 return cur_term_mode
;
142 void restore_term_mode(struct termios saved_term_mode
) {
143 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
147 int main (int argc
, char** argv
) {
148 struct itimerval tbuf
;
149 struct sigaction saction
;
150 saved_term_mode
= set_raw_term_mode();
154 saction
.sa_handler
= signal_handler
;
155 sigemptyset(&saction
.sa_mask
);
156 saction
.sa_flags
= 0;
157 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
161 tbuf
.it_interval
.tv_sec
= 1;
162 tbuf
.it_interval
.tv_usec
= 0;
163 tbuf
.it_value
.tv_sec
= 1;
164 tbuf
.it_value
.tv_usec
= 0;
166 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
170 /* end screen setup */
176 f
.c
= NULL
; /*to not free() array before it is allocated*/
178 op
.scheme
= &symbols_mono
;
183 opterr
= 0; /* don't print message on unrecognized option */
184 while ((optget
= getopt (argc
, argv
, "+w:h:m:nfqcdx")) != -1) {
186 case 'w': f
.w
= atoi (optarg
); break;
187 case 'h': f
.h
= atoi (optarg
); break;
188 case 'm': f
.m
= atoi (optarg
); break;
189 case 'n': op
.mode
= NOFLAG
; break;
190 case 'f': op
.mode
= FLAG
; break; /*default*/
191 case 'q': op
.mode
= QUESM
; break;
192 case 'c': op
.scheme
= &symbols_col1
; break;
193 case 'd': op
.scheme
= &symbols_doublewidth
; break;
195 fprintf (stderr
, "%s [OPTIONS]\n"
202 " q(uestion marks)\n"
203 " c(olored symbols)\n"
204 " d(oublewidth symbols)\n"
206 "hjkl: move 1 left/down/up/right\n"
207 "bduw: move 5 left/down/up/right\n"
208 "^Gg$: move to the left/bottom/top/right\n"
209 "left mouse/space: open/choord\n"
210 "right mouse/i: flag/unflag\n"
211 ":D / r: start a new game\n"
212 "q: quit\n", argv
[0]);
216 /* end parse options*/
217 /* check boundaries */
218 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
219 f
.m
= (f
.w
-1) * (f
.h
-1);
220 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
225 f
.c
= alloc_array (f
.h
, f
.w
);
235 printf ("\033[H\033[J");
237 /* swich charset, if necessary */
238 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
240 show_minefield (NORMAL
);
242 /* enable mouse, hide cursor */
243 printf ("\033[?1000h\033[?25l");
248 unsigned char mouse
[3];
250 action
= getch(mouse
);
252 case CTRSEQ_MOUSE_LEFT
:
253 f
.p
[0] = screen2field_l (mouse
[2]);
254 f
.p
[1] = screen2field_c (mouse
[1]);
255 /* :D clicked: TODO: won't work in single-width mode! */
256 if (mouse
[2] == LINE_OFFSET
-1 &&
257 (mouse
[1] == f
.w
+COL_OFFSET
||
258 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
262 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
263 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
268 fill_minefield (f
.p
[0], f
.p
[1]);
270 tbuf
.it_value
.tv_sec
= 1;
271 tbuf
.it_value
.tv_usec
= 0;
272 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
278 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) break;
279 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
280 if (uncover_square (f
.p
[0], f
.p
[1])) goto lose
;
281 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
282 if (choord_square (f
.p
[0], f
.p
[1])) goto lose
;
284 if (everything_opened()) goto win
;
286 case CTRSEQ_MOUSE_RIGHT
:
287 f
.p
[0] = screen2field_l (mouse
[2]);
288 f
.p
[1] = screen2field_c (mouse
[1]);
289 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
290 f
.p
[0] < 0 || f
.p
[1] >= f
.h
) break; /*out of bound*/
292 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
293 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
295 case 'h': set_cursor_pos (f
.p
[0], f
.p
[1]-1 ); break;
296 case 'j': set_cursor_pos (f
.p
[0]+1, f
.p
[1] ); break;
297 case 'k': set_cursor_pos (f
.p
[0]-1, f
.p
[1] ); break;
298 case 'l': set_cursor_pos (f
.p
[0], f
.p
[1]+1 ); break;
299 case 'w': set_cursor_pos (f
.p
[0], f
.p
[1]+BM
); break;
300 case 'b': set_cursor_pos (f
.p
[0], f
.p
[1]-BM
); break;
301 case 'u': set_cursor_pos (f
.p
[0]-BM
, f
.p
[1] ); break;
302 case 'd': set_cursor_pos (f
.p
[0]+BM
, f
.p
[1] ); break;
303 case '0': /* fallthrough */
304 case '^': set_cursor_pos (f
.p
[0], 0 ); break;
305 case '$': set_cursor_pos (f
.p
[0], f
.w
-1 ); break;
306 case 'g': set_cursor_pos (0, f
.p
[1] ); break;
307 case 'G': set_cursor_pos (f
.h
-1, f
.p
[1] ); break;
309 case 'r': /* start a new game */
314 case '\014': /* Ctrl-L -- redraw */
315 show_minefield (NORMAL
);
320 fill_minefield (-1, -1);
322 setitimer (ITIMER_REAL
, &tbuf
, NULL
);
324 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
325 cheatmode
= !cheatmode
;
333 tbuf
.it_value
.tv_sec
= 0;
334 tbuf
.it_value
.tv_usec
= 0;
335 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
339 show_minefield (SHOWMINES
);
342 unsigned char mouse
[3];
343 gotaction
= getch(mouse
);
344 /* :D clicked: TODO: won't work in single-width mode! */
345 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
346 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
349 } else if (gotaction
== 'r') {
352 } else if (gotaction
== 'q') {
362 move(f
.h
+LINE_OFFSET
+2, 0);
363 /* disable mouse, show cursor */
364 printf ("\033[?9l\033[?25h");
365 /* reset charset, if necessary */
366 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
368 restore_term_mode(saved_term_mode
);
371 /* I haven't won as long as a cell exists, that
372 - I haven't opened, and
374 int everything_opened () {
375 for (int row
= 0; row
< f
.h
; row
++)
376 for (int col
= 0; col
< f
.w
; col
++)
377 if (f
.c
[row
][col
].o
== CLOSED
&&
378 f
.c
[row
][col
].m
== NO_MINE
) return 0;
382 int wait_mouse_up (int l
, int c
) {
383 unsigned char mouse2
[3];
388 move (1, field2screen_c (f
.w
/2)-1); print (":o");
390 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
391 /* show a pushed-in button if cursor is on minefield */
392 move (l
+LINE_OFFSET
, field2screen_c(c
));
393 fputs (op
.scheme
->mouse_highlight
, stdout
);
397 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
398 /* ignore mouse wheel events: */
399 if (mouse2
[0] & 0x40) continue;
401 else if (mouse2
[0]&3 == 3) level
--; /* release event */
402 else level
++; /* another button pressed */
406 move (1, field2screen_c (f
.w
/2)-1); print (":D");
407 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
408 partial_show_minefield (l
, c
, NORMAL
);
410 c2
= screen2field_c(mouse2
[1]);
411 l2
= screen2field_l(mouse2
[2]);
412 return ((l2
== l
) && (c2
== c
));
415 int choord_square (int line
, int col
) {
416 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
417 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
418 if (f
.c
[l
][c
].f
!= FLAG
) {
419 if (uncover_square (l
, c
))
428 int uncover_square (int l
, int c
) {
429 f
.c
[l
][c
].o
= OPENED
;
430 f
.c
[l
][c
].f
= NOFLAG
; /* must not be QUESM, otherwise rendering issues */
431 partial_show_minefield (l
, c
, NORMAL
);
434 f
.c
[l
][c
].m
= DEATH_MINE
;
438 /* check for chording */
439 if (f
.c
[l
][c
].n
== 0) {
440 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
441 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
442 int newl
= l
+ choord_l
;
443 int newc
= c
+ choord_c
;
444 if (newl
>= 0 && newl
< f
.h
&&
445 newc
>= 0 && newc
< f
.w
&&
446 f
.c
[newl
][newc
].o
== CLOSED
&&
447 uncover_square (newl
, newc
)) {
457 void flag_square (int l
, int c
) {
458 if (f
.c
[l
][c
].o
!= CLOSED
) return;
459 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
461 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
462 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
464 partial_show_minefield (l
, c
, NORMAL
);
465 move (1, op
.scheme
->cell_width
);
466 printf ("[%03d]", f
.f
);
469 void quesm_square (int l
, int c
) {
470 /* toggle question mark / none. won't turn flags into question marks.
471 unlike flag_square, this function doesn't respect `-q'. */
472 if (f
.c
[l
][c
].o
!= CLOSED
) return;
473 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
474 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
475 partial_show_minefield (l
, c
, NORMAL
);
478 void fill_minefield (int l
, int c
) {
482 int line
= rand() % f
.h
;
483 int col
= rand() % f
.w
;
485 if (f
.c
[line
][col
].m
) {
486 /* skip if field already has a mine */
488 } else if ((line
== l
) && (col
== c
)) {
489 /* don't put a mine on the already opened (first click) field */
493 f
.c
[line
][col
].m
= STD_MINE
;
497 /* precalculate neighbours */
498 for (int l
=0; l
< f
.h
; l
++)
499 for (int c
=0; c
< f
.w
; c
++)
500 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
503 void move (int line
, int col
) {
504 printf ("\033[%d;%dH", line
+1, col
+1);
507 /* absolute coordinates! */
508 void set_cursor_pos (int l
, int c
) {
509 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
511 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
512 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
513 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
514 fputs (op
.scheme
->mouse_highlight
, stdout
);
517 void partial_show_minefield (int l
, int c
, int mode
) {
518 move (l
+LINE_OFFSET
, field2screen_c(c
));
520 if (f
.c
[l
][c
].f
== FLAG
) print (op
.scheme
->field_flagged
);
521 else if (f
.c
[l
][c
].f
== QUESM
) print (op
.scheme
->field_question
);
522 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
523 else if (f
.c
[l
][c
].m
== STD_MINE
||
524 f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_normal
);
525 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
528 void show_minefield (int mode
) {
536 dtime
= difftime (time(NULL
), f
.t
);
540 print (op
.scheme
->border_top_l
);
541 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
542 printf ("%s\r\n", op
.scheme
->border_top_r
);
544 print (op
.scheme
->border_status_l
);
545 printf("[%03d]", f
.f
);
546 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
547 printf ("%s", mode
==SHOWMINES
?":C":":D");
548 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
549 printf ("[%03d]", dtime
);
550 print (op
.scheme
->border_status_r
);
553 print (op
.scheme
->border_spacer_l
);
554 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
555 print (op
.scheme
->border_spacer_r
);
558 for (int l
= 0; l
< f
.h
; l
++) {
559 print (op
.scheme
->border_field_l
);
560 for (int c
= 0; c
< f
.w
; c
++) {
561 if (mode
== SHOWMINES
) {
562 if (f
.c
[l
][c
].f
== FLAG
&&
563 f
.c
[l
][c
].m
) print (op
.scheme
->field_flagged
);
564 else if (f
.c
[l
][c
].f
== FLAG
&&
565 f
.c
[l
][c
].m
== NO_MINE
) print (op
.scheme
->mine_wrongf
);
566 else if (f
.c
[l
][c
].m
== STD_MINE
) print (op
.scheme
->mine_normal
);
567 else if (f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_death
);
568 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
569 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
571 if (f
.c
[l
][c
].f
== FLAG
) print (op
.scheme
->field_flagged
);
572 else if (f
.c
[l
][c
].f
== QUESM
) print (op
.scheme
->field_question
);
573 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
574 else if (f
.c
[l
][c
].m
== STD_MINE
) print (op
.scheme
->mine_normal
);
575 else if (f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_death
);
576 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
579 print (op
.scheme
->border_field_r
); print ("\r\n");
582 print (op
.scheme
->border_bottom_l
);
583 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
584 print (op
.scheme
->border_bottom_r
);
588 int get_neighbours (int line
, int col
, int reduced_mode
) {
589 /* counts mines surrounding a square
590 modes: 0=normal; 1=reduced */
594 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
595 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
596 if (!l
&& !c
) continue;
598 count
+= !!f
.c
[l
][c
].m
;
599 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
605 struct minecell
** alloc_array (int lines
, int cols
) {
606 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
607 if (a
== NULL
) return NULL
;
608 for (int l
= 0; l
< lines
; l
++) {
609 a
[l
] = calloc (cols
, sizeof(struct minecell
));
610 if (a
[l
] == NULL
) goto unalloc
;
615 for (int l
= 0; l
< lines
; l
++)
621 if (f
.c
== NULL
) return; /* quit() could be called before alloc_array() */
622 for (int l
= 0; l
< f
.h
; l
++) {
628 int screen2field_l (int l
) {
629 return (l
-LINE_OFFSET
) - 1;
631 /* some trickery is required to extract the mouse position from the cell width,
632 depending on wheather we are using full width characters or double line width.
633 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
634 int screen2field_c (int c
) {
635 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
637 int field2screen_l (int l
) {
638 return 0; //TODO: is never used, therefore not implemented
640 int field2screen_c (int c
) {
641 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
650 int getctrlseq (unsigned char* buf
) {
653 int offset
= 0x20; /* never sends control chars as data */
654 while ((c
= getchar()) != EOF
) {
658 case '\033': state
=ESC_SENT
; break;
664 case '[': state
=CSI_SENT
; break;
665 default: return CTRSEQ_INVALID
;
670 case 'M': state
=MOUSE_EVENT
; break;
671 default: return CTRSEQ_INVALID
;
676 buf
[1] = getchar() - offset
;
677 buf
[2] = getchar() - offset
;
680 return CTRSEQ_INVALID
;
686 int getch(unsigned char* buf
) {
687 /* returns a character, EOF, or constant for an escape/control sequence - NOT
688 compatible with the ncurses implementation of same name */
689 int action
= getctrlseq(buf
);
693 l
= screen2field_l (buf
[2]);
694 c
= screen2field_c (buf
[1]);
696 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
697 int success
= wait_mouse_up(l
, c
);
699 /* mouse moved while pressed: */
700 if (!success
) return CTRSEQ_INVALID
;
703 case 0: return CTRSEQ_MOUSE_LEFT
;
704 case 1: return CTRSEQ_MOUSE_MIDDLE
;
705 case 2: return CTRSEQ_MOUSE_RIGHT
;