]>
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 /* switch to alternate screen */
236 printf ("\033[?47h");
237 /* reset cursor, clear screen */
238 printf ("\033[H\033[J");
240 /* swich charset, if necessary */
241 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
243 show_minefield (NORMAL
);
245 /* enable mouse, hide cursor */
246 printf ("\033[?1000h\033[?25l");
251 unsigned char mouse
[3];
253 action
= getch(mouse
);
255 case CTRSEQ_MOUSE_LEFT
:
256 f
.p
[0] = screen2field_l (mouse
[2]);
257 f
.p
[1] = screen2field_c (mouse
[1]);
258 /* :D clicked: TODO: won't work in single-width mode! */
259 if (mouse
[2] == LINE_OFFSET
-1 &&
260 (mouse
[1] == f
.w
+COL_OFFSET
||
261 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
265 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
266 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
271 fill_minefield (f
.p
[0], f
.p
[1]);
273 tbuf
.it_value
.tv_sec
= 1;
274 tbuf
.it_value
.tv_usec
= 0;
275 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
281 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) break;
282 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
283 if (uncover_square (f
.p
[0], f
.p
[1])) goto lose
;
284 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
285 if (choord_square (f
.p
[0], f
.p
[1])) goto lose
;
287 if (everything_opened()) goto win
;
289 case CTRSEQ_MOUSE_RIGHT
:
290 f
.p
[0] = screen2field_l (mouse
[2]);
291 f
.p
[1] = screen2field_c (mouse
[1]);
292 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
293 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
295 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
296 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
298 case 'h': set_cursor_pos (f
.p
[0], f
.p
[1]-1 ); break;
299 case 'j': set_cursor_pos (f
.p
[0]+1, f
.p
[1] ); break;
300 case 'k': set_cursor_pos (f
.p
[0]-1, f
.p
[1] ); break;
301 case 'l': set_cursor_pos (f
.p
[0], f
.p
[1]+1 ); break;
302 case 'w': set_cursor_pos (f
.p
[0], f
.p
[1]+BM
); break;
303 case 'b': set_cursor_pos (f
.p
[0], f
.p
[1]-BM
); break;
304 case 'u': set_cursor_pos (f
.p
[0]-BM
, f
.p
[1] ); break;
305 case 'd': set_cursor_pos (f
.p
[0]+BM
, f
.p
[1] ); break;
306 case '0': /* fallthrough */
307 case '^': set_cursor_pos (f
.p
[0], 0 ); break;
308 case '$': set_cursor_pos (f
.p
[0], f
.w
-1 ); break;
309 case 'g': set_cursor_pos (0, f
.p
[1] ); break;
310 case 'G': set_cursor_pos (f
.h
-1, f
.p
[1] ); break;
312 case 'r': /* start a new game */
317 case '\014': /* Ctrl-L -- redraw */
318 show_minefield (NORMAL
);
323 fill_minefield (-1, -1);
325 setitimer (ITIMER_REAL
, &tbuf
, NULL
);
327 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
328 cheatmode
= !cheatmode
;
336 tbuf
.it_value
.tv_sec
= 0;
337 tbuf
.it_value
.tv_usec
= 0;
338 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
342 show_minefield (SHOWMINES
);
345 unsigned char mouse
[3];
346 gotaction
= getch(mouse
);
347 /* :D clicked: TODO: won't work in single-width mode! */
348 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
349 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
352 } else if (gotaction
== 'r') {
355 } else if (gotaction
== 'q') {
365 move(f
.h
+LINE_OFFSET
+2, 0);
366 /* disable mouse, show cursor */
367 printf ("\033[?9l\033[?25h");
368 /* reset charset, if necessary */
369 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
370 /* revert to primary screen */
371 printf ("\033[?47l");
373 restore_term_mode(saved_term_mode
);
376 /* I haven't won as long as a cell exists, that
377 - I haven't opened, and
379 int everything_opened () {
380 for (int row
= 0; row
< f
.h
; row
++)
381 for (int col
= 0; col
< f
.w
; col
++)
382 if (f
.c
[row
][col
].o
== CLOSED
&&
383 f
.c
[row
][col
].m
== NO_MINE
) return 0;
387 int wait_mouse_up (int l
, int c
) {
388 unsigned char mouse2
[3];
393 move (1, field2screen_c (f
.w
/2)-1); print (":o");
395 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
396 /* show a pushed-in button if cursor is on minefield */
397 move (l
+LINE_OFFSET
, field2screen_c(c
));
398 fputs (op
.scheme
->mouse_highlight
, stdout
);
402 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
403 /* ignore mouse wheel events: */
404 if (mouse2
[0] & 0x40) continue;
406 else if (mouse2
[0]&3 == 3) level
--; /* release event */
407 else level
++; /* another button pressed */
411 move (1, field2screen_c (f
.w
/2)-1); print (":D");
412 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
413 partial_show_minefield (l
, c
, NORMAL
);
415 c2
= screen2field_c(mouse2
[1]);
416 l2
= screen2field_l(mouse2
[2]);
417 return ((l2
== l
) && (c2
== c
));
420 int choord_square (int line
, int col
) {
421 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
422 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
423 if (f
.c
[l
][c
].f
!= FLAG
) {
424 if (uncover_square (l
, c
))
433 int uncover_square (int l
, int c
) {
434 f
.c
[l
][c
].o
= OPENED
;
435 f
.c
[l
][c
].f
= NOFLAG
; /* must not be QUESM, otherwise rendering issues */
436 partial_show_minefield (l
, c
, NORMAL
);
439 f
.c
[l
][c
].m
= DEATH_MINE
;
443 /* check for chording */
444 if (f
.c
[l
][c
].n
== 0) {
445 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
446 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
447 int newl
= l
+ choord_l
;
448 int newc
= c
+ choord_c
;
449 if (newl
>= 0 && newl
< f
.h
&&
450 newc
>= 0 && newc
< f
.w
&&
451 f
.c
[newl
][newc
].o
== CLOSED
&&
452 uncover_square (newl
, newc
)) {
462 void flag_square (int l
, int c
) {
463 if (f
.c
[l
][c
].o
!= CLOSED
) return;
464 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
466 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
467 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
469 partial_show_minefield (l
, c
, NORMAL
);
470 move (1, op
.scheme
->cell_width
);
471 printf ("[%03d]", f
.f
);
474 void quesm_square (int l
, int c
) {
475 /* toggle question mark / none. won't turn flags into question marks.
476 unlike flag_square, this function doesn't respect `-q'. */
477 if (f
.c
[l
][c
].o
!= CLOSED
) return;
478 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
479 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
480 partial_show_minefield (l
, c
, NORMAL
);
483 void fill_minefield (int l
, int c
) {
487 int line
= rand() % f
.h
;
488 int col
= rand() % f
.w
;
490 if (f
.c
[line
][col
].m
) {
491 /* skip if field already has a mine */
493 } else if ((line
== l
) && (col
== c
)) {
494 /* don't put a mine on the already opened (first click) field */
498 f
.c
[line
][col
].m
= STD_MINE
;
502 /* precalculate neighbours */
503 for (int l
=0; l
< f
.h
; l
++)
504 for (int c
=0; c
< f
.w
; c
++)
505 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
508 void move (int line
, int col
) {
509 printf ("\033[%d;%dH", line
+1, col
+1);
512 /* absolute coordinates! */
513 void set_cursor_pos (int l
, int c
) {
514 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
516 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
517 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
518 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
519 fputs (op
.scheme
->mouse_highlight
, stdout
);
522 void partial_show_minefield (int l
, int c
, int mode
) {
523 move (l
+LINE_OFFSET
, field2screen_c(c
));
525 if (f
.c
[l
][c
].f
== FLAG
) print (op
.scheme
->field_flagged
);
526 else if (f
.c
[l
][c
].f
== QUESM
) print (op
.scheme
->field_question
);
527 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
528 else if (f
.c
[l
][c
].m
== STD_MINE
||
529 f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_normal
);
530 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
533 void show_minefield (int mode
) {
541 dtime
= difftime (time(NULL
), f
.t
);
545 print (op
.scheme
->border_top_l
);
546 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
547 printf ("%s\r\n", op
.scheme
->border_top_r
);
549 print (op
.scheme
->border_status_l
);
550 printf("[%03d]", f
.f
);
551 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
552 printf ("%s", mode
==SHOWMINES
?":C":":D");
553 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
554 printf ("[%03d]", dtime
);
555 print (op
.scheme
->border_status_r
);
558 print (op
.scheme
->border_spacer_l
);
559 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
560 print (op
.scheme
->border_spacer_r
);
563 for (int l
= 0; l
< f
.h
; l
++) {
564 print (op
.scheme
->border_field_l
);
565 for (int c
= 0; c
< f
.w
; c
++) {
566 if (mode
== SHOWMINES
) {
567 if (f
.c
[l
][c
].f
== FLAG
&&
568 f
.c
[l
][c
].m
) print (op
.scheme
->field_flagged
);
569 else if (f
.c
[l
][c
].f
== FLAG
&&
570 f
.c
[l
][c
].m
== NO_MINE
) print (op
.scheme
->mine_wrongf
);
571 else if (f
.c
[l
][c
].m
== STD_MINE
) print (op
.scheme
->mine_normal
);
572 else if (f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_death
);
573 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
574 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
576 if (f
.c
[l
][c
].f
== FLAG
) print (op
.scheme
->field_flagged
);
577 else if (f
.c
[l
][c
].f
== QUESM
) print (op
.scheme
->field_question
);
578 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
579 else if (f
.c
[l
][c
].m
== STD_MINE
) print (op
.scheme
->mine_normal
);
580 else if (f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_death
);
581 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
584 print (op
.scheme
->border_field_r
); print ("\r\n");
587 print (op
.scheme
->border_bottom_l
);
588 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
589 print (op
.scheme
->border_bottom_r
);
593 int get_neighbours (int line
, int col
, int reduced_mode
) {
594 /* counts mines surrounding a square
595 modes: 0=normal; 1=reduced */
599 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
600 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
601 if (!l
&& !c
) continue;
603 count
+= !!f
.c
[l
][c
].m
;
604 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
610 struct minecell
** alloc_array (int lines
, int cols
) {
611 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
612 if (a
== NULL
) return NULL
;
613 for (int l
= 0; l
< lines
; l
++) {
614 a
[l
] = calloc (cols
, sizeof(struct minecell
));
615 if (a
[l
] == NULL
) goto unalloc
;
620 for (int l
= 0; l
< lines
; l
++)
626 if (f
.c
== NULL
) return;
627 for (int l
= 0; l
< f
.h
; l
++) {
633 int screen2field_l (int l
) {
634 return (l
-LINE_OFFSET
) - 1;
636 /* some trickery is required to extract the mouse position from the cell width,
637 depending on wheather we are using full width characters or double line width.
638 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
639 int screen2field_c (int c
) {
640 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
642 int field2screen_l (int l
) {
643 return 0; //TODO: is never used, therefore not implemented
645 int field2screen_c (int c
) {
646 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
655 int getctrlseq (unsigned char* buf
) {
658 int offset
= 0x20; /* never sends control chars as data */
659 while ((c
= getchar()) != EOF
) {
663 case '\033': state
=ESC_SENT
; break;
669 case '[': state
=CSI_SENT
; break;
670 default: return CTRSEQ_INVALID
;
675 case 'M': state
=MOUSE_EVENT
; break;
676 default: return CTRSEQ_INVALID
;
681 buf
[1] = getchar() - offset
;
682 buf
[2] = getchar() - offset
;
685 return CTRSEQ_INVALID
;
691 int getch(unsigned char* buf
) {
692 /* returns a character, EOF, or constant for an escape/control sequence - NOT
693 compatible with the ncurses implementation of same name */
694 int action
= getctrlseq(buf
);
698 l
= screen2field_l (buf
[2]);
699 c
= screen2field_c (buf
[1]);
701 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
702 int success
= wait_mouse_up(l
, c
);
704 /* mouse moved while pressed: */
705 if (!success
) return CTRSEQ_INVALID
;
708 case 0: return CTRSEQ_MOUSE_LEFT
;
709 case 1: return CTRSEQ_MOUSE_MIDDLE
;
710 case 2: return CTRSEQ_MOUSE_RIGHT
;