]>
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*/
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? */
69 void fill_minefield (int, int);
71 void set_cursor_pos (int, int);
72 int getch (unsigned char*);
73 int getctrlseq (unsigned char*);
74 int everything_opened ();
75 int wait_mouse_up (int, int);
76 void partial_show_minefield (int, int, int);
77 void show_minefield (int);
78 int get_neighbours (int, int, int);
79 int uncover_square (int, int);
80 void flag_square (int, int);
81 void quesm_square (int, int);
82 int choord_square (int, int);
83 struct minecell
** alloc_array (int, int);
85 int screen2field_l (int);
86 int screen2field_c (int);
87 int field2screen_l (int);
88 int field2screen_c (int);
90 void signal_handler (int signum
);
102 enum fieldopenstates
{
107 /* for getctrlseq() */
113 CTRSEQ_MOUSE_LEFT
= -4,
114 CTRSEQ_MOUSE_MIDDLE
= -5,
115 CTRSEQ_MOUSE_RIGHT
= -6,
123 void signal_handler (int signum
) {
126 move (1, f
.w
*op
.scheme
->cell_width
-(op
.scheme
->cell_width
%2)-3);
127 printf ("[%03d]", f
.t
?(int)difftime (time(NULL
), f
.t
):0);
134 /* http://users.csc.calpoly.edu/~phatalsk/357/lectures/code/sigalrm.c */
135 struct termios saved_term_mode
;
136 struct termios
set_raw_term_mode() {
137 struct termios cur_term_mode
, raw_term_mode
;
139 tcgetattr(STDIN_FILENO
, &cur_term_mode
);
140 raw_term_mode
= cur_term_mode
;
141 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
142 raw_term_mode
.c_cc
[VMIN
] = 1 ;
143 raw_term_mode
.c_cc
[VTIME
] = 0;
144 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
146 return cur_term_mode
;
148 void restore_term_mode(struct termios saved_term_mode
) {
149 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
153 int main (int argc
, char** argv
) {
154 struct itimerval tbuf
;
155 struct sigaction saction
;
156 saved_term_mode
= set_raw_term_mode();
160 saction
.sa_handler
= signal_handler
;
161 sigemptyset(&saction
.sa_mask
);
162 saction
.sa_flags
= 0;
163 if (sigaction(SIGALRM
, &saction
, NULL
) < 0 ) {
167 tbuf
.it_interval
.tv_sec
= 1;
168 tbuf
.it_interval
.tv_usec
= 0;
169 tbuf
.it_value
.tv_sec
= 1;
170 tbuf
.it_value
.tv_usec
= 0;
172 if (sigaction(SIGINT
, &saction
, NULL
) < 0 ) {
176 /* end screen setup */
182 f
.c
= NULL
; /*to not free() array before it is allocated*/
184 op
.scheme
= &symbols_mono
;
189 opterr
= 0; /* don't print message on unrecognized option */
190 while ((optget
= getopt (argc
, argv
, "+w:h:m:nfqcdx")) != -1) {
192 case 'w': f
.w
= atoi (optarg
); break;
193 case 'h': f
.h
= atoi (optarg
); break;
194 case 'm': f
.m
= atoi (optarg
); break;
195 case 'n': op
.mode
= NOFLAG
; break;
196 case 'f': op
.mode
= FLAG
; break; /*default*/
197 case 'q': op
.mode
= QUESM
; break;
198 case 'c': op
.scheme
= &symbols_col1
; break;
199 case 'd': op
.scheme
= &symbols_doublewidth
; break;
201 fprintf (stderr
, "%s [OPTIONS]\n"
208 " q(uestion marks)\n"
209 " c(olored symbols)\n"
210 " d(oublewidth symbols)\n"
212 "hjkl: move 1 left/down/up/right\n"
213 "bduw: move 5 left/down/up/right\n"
214 "^Gg$: move to the left/bottom/top/right\n"
215 "left mouse/space: open/choord\n"
216 "right mouse/i: flag/unflag\n"
217 ":D / r: start a new game\n"
218 "q: quit\n", argv
[0]);
222 /* end parse options*/
223 /* check boundaries */
224 if (f
.m
> (f
.w
-1) * (f
.h
-1)) {
225 f
.m
= (f
.w
-1) * (f
.h
-1);
226 fprintf (stderr
, "too many mines. reduced to %d.\r\n", f
.m
);
231 f
.c
= alloc_array (f
.h
, f
.w
);
240 struct line_col markers
[26];
241 for (int i
=26; i
; markers
[--i
].l
= -1);
243 /* switch to alternate screen */
244 printf ("\033[?47h");
245 /* reset cursor, clear screen */
246 printf ("\033[H\033[J");
248 /* swich charset, if necessary */
249 if (op
.scheme
->init_seq
!= NULL
) print (op
.scheme
->init_seq
);
251 show_minefield (NORMAL
);
253 /* enable mouse, hide cursor */
254 printf ("\033[?1000h\033[?25l");
259 unsigned char mouse
[3];
261 action
= getch(mouse
);
263 case CTRSEQ_MOUSE_LEFT
:
264 f
.p
[0] = screen2field_l (mouse
[2]);
265 f
.p
[1] = screen2field_c (mouse
[1]);
266 /* :D clicked: TODO: won't work in single-width mode! */
267 if (mouse
[2] == LINE_OFFSET
-1 &&
268 (mouse
[1] == f
.w
+COL_OFFSET
||
269 mouse
[1] == f
.w
+COL_OFFSET
+1)) {
273 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
274 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
279 fill_minefield (f
.p
[0], f
.p
[1]);
281 tbuf
.it_value
.tv_sec
= 1;
282 tbuf
.it_value
.tv_usec
= 0;
283 if (setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1) {
289 if (f
.c
[f
.p
[0]][f
.p
[1]].f
== FLAG
) break;
290 if (f
.c
[f
.p
[0]][f
.p
[1]].o
== CLOSED
) {
291 if (uncover_square (f
.p
[0], f
.p
[1])) goto lose
;
292 } else if (get_neighbours (f
.p
[0], f
.p
[1], 1) == 0) {
293 if (choord_square (f
.p
[0], f
.p
[1])) goto lose
;
295 if (everything_opened()) goto win
;
297 case CTRSEQ_MOUSE_RIGHT
:
298 f
.p
[0] = screen2field_l (mouse
[2]);
299 f
.p
[1] = screen2field_c (mouse
[1]);
300 if (f
.p
[1] < 0 || f
.p
[1] >= f
.w
||
301 f
.p
[0] < 0 || f
.p
[0] >= f
.h
) break; /*out of bound*/
303 case 'i': flag_square (f
.p
[0], f
.p
[1]); break;
304 case '?':quesm_square (f
.p
[0], f
.p
[1]); break;
306 case 'h': set_cursor_pos (f
.p
[0], f
.p
[1]-1 ); break;
307 case 'j': set_cursor_pos (f
.p
[0]+1, f
.p
[1] ); break;
308 case 'k': set_cursor_pos (f
.p
[0]-1, f
.p
[1] ); break;
309 case 'l': set_cursor_pos (f
.p
[0], f
.p
[1]+1 ); break;
310 case 'w': set_cursor_pos (f
.p
[0], f
.p
[1]+BM
); break;
311 case 'b': set_cursor_pos (f
.p
[0], f
.p
[1]-BM
); break;
312 case 'u': set_cursor_pos (f
.p
[0]-BM
, f
.p
[1] ); break;
313 case 'd': set_cursor_pos (f
.p
[0]+BM
, f
.p
[1] ); break;
314 case '0': /* fallthrough */
315 case '^': set_cursor_pos (f
.p
[0], 0 ); break;
316 case '$': set_cursor_pos (f
.p
[0], f
.w
-1 ); break;
317 case 'g': set_cursor_pos (0, f
.p
[1] ); break;
318 case 'G': set_cursor_pos (f
.h
-1, f
.p
[1] ); break;
321 action
= tolower(getch(mouse
));
322 if (action
< 'a' || action
> 'z') break;/*out of bound*/
323 markers
[action
-'a'].l
= f
.p
[0];
324 markers
[action
-'a'].c
= f
.p
[1];
326 case'\'': /* fallthrough */
328 action
= tolower(getch(mouse
));
329 if (action
< 'a' || action
> 'z' /* out of bound or */
330 || markers
[action
-'a'].l
== -1) break; /* unset */
331 set_cursor_pos (markers
[action
-'a'].l
, markers
[action
-'a'].c
);
333 case 'r': /* start a new game */
338 case '\014': /* Ctrl-L -- redraw */
339 show_minefield (NORMAL
);
344 fill_minefield (-1, -1);
346 setitimer (ITIMER_REAL
, &tbuf
, NULL
);
348 show_minefield (cheatmode
?NORMAL
:SHOWMINES
);
349 cheatmode
= !cheatmode
;
357 tbuf
.it_value
.tv_sec
= 0;
358 tbuf
.it_value
.tv_usec
= 0;
359 if ( setitimer(ITIMER_REAL
, &tbuf
, NULL
) == -1 ) {
363 show_minefield (SHOWMINES
);
366 unsigned char mouse
[3];
367 gotaction
= getch(mouse
);
368 /* :D clicked: TODO: won't work in single-width mode! */
369 if (gotaction
==CTRSEQ_MOUSE_LEFT
&& mouse
[2]==LINE_OFFSET
-1 &&
370 (mouse
[1]==f
.w
+COL_OFFSET
|| mouse
[1]==f
.w
+COL_OFFSET
+1)) {
373 } else if (gotaction
== 'r') {
376 } else if (gotaction
== 'q') {
386 move(f
.h
+LINE_OFFSET
+2, 0);
387 /* disable mouse, show cursor */
388 printf ("\033[?9l\033[?25h");
389 /* reset charset, if necessary */
390 if (op
.scheme
&& op
.scheme
->reset_seq
) print (op
.scheme
->reset_seq
);
391 /* revert to primary screen */
392 printf ("\033[?47l");
394 restore_term_mode(saved_term_mode
);
397 /* I haven't won as long as a cell exists, that
398 - I haven't opened, and
400 int everything_opened () {
401 for (int row
= 0; row
< f
.h
; row
++)
402 for (int col
= 0; col
< f
.w
; col
++)
403 if (f
.c
[row
][col
].o
== CLOSED
&&
404 f
.c
[row
][col
].m
== NO_MINE
) return 0;
408 int wait_mouse_up (int l
, int c
) {
409 unsigned char mouse2
[3];
414 move (1, field2screen_c (f
.w
/2)-1); print (":o");
416 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
417 /* show a pushed-in button if cursor is on minefield */
418 move (l
+LINE_OFFSET
, field2screen_c(c
));
419 fputs (op
.scheme
->mouse_highlight
, stdout
);
423 if (getctrlseq (mouse2
) == CTRSEQ_MOUSE
) {
424 /* ignore mouse wheel events: */
425 if (mouse2
[0] & 0x40) continue;
427 else if (mouse2
[0]&3 == 3) level
--; /* release event */
428 else level
++; /* another button pressed */
432 move (1, field2screen_c (f
.w
/2)-1); print (":D");
433 if (!(l
< 0 || l
>= f
.h
|| c
< 0 || c
>= f
.w
)) {
434 partial_show_minefield (l
, c
, NORMAL
);
436 c2
= screen2field_c(mouse2
[1]);
437 l2
= screen2field_l(mouse2
[2]);
438 return ((l2
== l
) && (c2
== c
));
441 int choord_square (int line
, int col
) {
442 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
443 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
444 if (f
.c
[l
][c
].f
!= FLAG
) {
445 if (uncover_square (l
, c
))
454 int uncover_square (int l
, int c
) {
455 f
.c
[l
][c
].o
= OPENED
;
456 f
.c
[l
][c
].f
= NOFLAG
; /* must not be QUESM, otherwise rendering issues */
457 partial_show_minefield (l
, c
, NORMAL
);
460 f
.c
[l
][c
].m
= DEATH_MINE
;
464 /* check for chording */
465 if (f
.c
[l
][c
].n
== 0) {
466 for (int choord_l
= -1; choord_l
<= 1; choord_l
++) {
467 for (int choord_c
= -1; choord_c
<= 1; choord_c
++) {
468 int newl
= l
+ choord_l
;
469 int newc
= c
+ choord_c
;
470 if (newl
>= 0 && newl
< f
.h
&&
471 newc
>= 0 && newc
< f
.w
&&
472 f
.c
[newl
][newc
].o
== CLOSED
&&
473 uncover_square (newl
, newc
)) {
483 void flag_square (int l
, int c
) {
484 if (f
.c
[l
][c
].o
!= CLOSED
) return;
485 /* cycles through flag/quesm/noflag (uses op.mode to detect which ones
487 f
.c
[l
][c
].f
= (f
.c
[l
][c
].f
+ 1) % (op
.mode
+ 1);
488 if (f
.c
[l
][c
].f
==FLAG
) f
.f
++;
490 partial_show_minefield (l
, c
, NORMAL
);
491 move (1, op
.scheme
->cell_width
);
492 printf ("[%03d]", f
.f
);
495 void quesm_square (int l
, int c
) {
496 /* toggle question mark / none. won't turn flags into question marks.
497 unlike flag_square, this function doesn't respect `-q'. */
498 if (f
.c
[l
][c
].o
!= CLOSED
) return;
499 else if (f
.c
[l
][c
].f
== NOFLAG
) f
.c
[l
][c
].f
= QUESM
;
500 else if (f
.c
[l
][c
].f
== QUESM
) f
.c
[l
][c
].f
= NOFLAG
;
501 partial_show_minefield (l
, c
, NORMAL
);
504 void fill_minefield (int l
, int c
) {
508 int line
= rand() % f
.h
;
509 int col
= rand() % f
.w
;
511 if (f
.c
[line
][col
].m
) {
512 /* skip if field already has a mine */
514 } else if ((line
== l
) && (col
== c
)) {
515 /* don't put a mine on the already opened (first click) field */
519 f
.c
[line
][col
].m
= STD_MINE
;
523 /* precalculate neighbours */
524 for (int l
=0; l
< f
.h
; l
++)
525 for (int c
=0; c
< f
.w
; c
++)
526 f
.c
[l
][c
].n
= get_neighbours (l
, c
, NORMAL
);
529 void move (int line
, int col
) {
530 printf ("\033[%d;%dH", line
+1, col
+1);
533 /* absolute coordinates! */
534 void set_cursor_pos (int l
, int c
) {
535 partial_show_minefield (f
.p
[0], f
.p
[1], NORMAL
);
537 f
.p
[0] = CLAMP(l
, 0, f
.h
-1);
538 f
.p
[1] = CLAMP(c
, 0, f
.w
-1);
539 move (f
.p
[0]+LINE_OFFSET
, field2screen_c(f
.p
[1]));
540 fputs (op
.scheme
->mouse_highlight
, stdout
);
543 void partial_show_minefield (int l
, int c
, int mode
) {
544 move (l
+LINE_OFFSET
, field2screen_c(c
));
546 if (f
.c
[l
][c
].f
== FLAG
) print (op
.scheme
->field_flagged
);
547 else if (f
.c
[l
][c
].f
== QUESM
) print (op
.scheme
->field_question
);
548 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
549 else if (f
.c
[l
][c
].m
== STD_MINE
||
550 f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_normal
);
551 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
554 void show_minefield (int mode
) {
562 dtime
= difftime (time(NULL
), f
.t
);
566 print (op
.scheme
->border_top_l
);
567 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_top_m
);
568 printf ("%s\r\n", op
.scheme
->border_top_r
);
570 print (op
.scheme
->border_status_l
);
571 printf("[%03d]", f
.f
);
572 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
573 printf ("%s", mode
==SHOWMINES
?":C":":D");
574 printm (f
.w
*op
.scheme
->cell_width
/2-6, " ");
575 printf ("[%03d]", dtime
);
576 print (op
.scheme
->border_status_r
);
579 print (op
.scheme
->border_spacer_l
);
580 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_spacer_m
);
581 print (op
.scheme
->border_spacer_r
);
584 for (int l
= 0; l
< f
.h
; l
++) {
585 print (op
.scheme
->border_field_l
);
586 for (int c
= 0; c
< f
.w
; c
++) {
587 if (mode
== SHOWMINES
) {
588 if (f
.c
[l
][c
].f
== FLAG
&&
589 f
.c
[l
][c
].m
) print (op
.scheme
->field_flagged
);
590 else if (f
.c
[l
][c
].f
== FLAG
&&
591 f
.c
[l
][c
].m
== NO_MINE
) print (op
.scheme
->mine_wrongf
);
592 else if (f
.c
[l
][c
].m
== STD_MINE
) print (op
.scheme
->mine_normal
);
593 else if (f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_death
);
594 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
595 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
597 if (f
.c
[l
][c
].f
== FLAG
) print (op
.scheme
->field_flagged
);
598 else if (f
.c
[l
][c
].f
== QUESM
) print (op
.scheme
->field_question
);
599 else if (f
.c
[l
][c
].o
== CLOSED
) print (op
.scheme
->field_closed
);
600 else if (f
.c
[l
][c
].m
== STD_MINE
) print (op
.scheme
->mine_normal
);
601 else if (f
.c
[l
][c
].m
== DEATH_MINE
) print (op
.scheme
->mine_death
);
602 else /*.......................*/ print (op
.scheme
->number
[f
.c
[l
][c
].n
]);
605 print (op
.scheme
->border_field_r
); print ("\r\n");
608 print (op
.scheme
->border_bottom_l
);
609 printm (f
.w
*op
.scheme
->cell_width
,op
.scheme
->border_bottom_m
);
610 print (op
.scheme
->border_bottom_r
);
614 int get_neighbours (int line
, int col
, int reduced_mode
) {
615 /* counts mines surrounding a square
616 modes: 0=normal; 1=reduced */
620 for (int l
= MAX(line
-1, 0); l
<= MIN(line
+1, f
.h
-1); l
++) {
621 for (int c
= MAX(col
-1, 0); c
<= MIN(col
+1, f
.w
-1); c
++) {
622 if (!l
&& !c
) continue;
624 count
+= !!f
.c
[l
][c
].m
;
625 count
-= reduced_mode
* f
.c
[l
][c
].f
==FLAG
;
631 struct minecell
** alloc_array (int lines
, int cols
) {
632 struct minecell
** a
= malloc (lines
* sizeof(struct minecell
*));
633 if (a
== NULL
) return NULL
;
634 for (int l
= 0; l
< lines
; l
++) {
635 a
[l
] = calloc (cols
, sizeof(struct minecell
));
636 if (a
[l
] == NULL
) goto unalloc
;
641 for (int l
= 0; l
< lines
; l
++)
647 if (f
.c
== NULL
) return;
648 for (int l
= 0; l
< f
.h
; l
++) {
654 int screen2field_l (int l
) {
655 return (l
-LINE_OFFSET
) - 1;
657 /* some trickery is required to extract the mouse position from the cell width,
658 depending on wheather we are using full width characters or double line width.
659 WARN: tested only with scheme.cell_width = 1 and scheme.cell_width = 2. */
660 int screen2field_c (int c
) {
661 return (c
-COL_OFFSET
+1 - 2*(op
.scheme
->cell_width
%2))/2 - op
.scheme
->cell_width
/2;
663 int field2screen_l (int l
) {
664 return 0; //TODO: is never used, therefore not implemented
666 int field2screen_c (int c
) {
667 return (op
.scheme
->cell_width
*c
+COL_OFFSET
- (op
.scheme
->cell_width
%2));
676 int getctrlseq (unsigned char* buf
) {
679 int offset
= 0x20; /* never sends control chars as data */
680 while ((c
= getchar()) != EOF
) {
684 case '\033': state
=ESC_SENT
; break;
690 case '[': state
=CSI_SENT
; break;
691 default: return CTRSEQ_INVALID
;
696 case 'M': state
=MOUSE_EVENT
; break;
697 default: return CTRSEQ_INVALID
;
702 buf
[1] = getchar() - offset
;
703 buf
[2] = getchar() - offset
;
706 return CTRSEQ_INVALID
;
712 int getch(unsigned char* buf
) {
713 /* returns a character, EOF, or constant for an escape/control sequence - NOT
714 compatible with the ncurses implementation of same name */
715 int action
= getctrlseq(buf
);
719 l
= screen2field_l (buf
[2]);
720 c
= screen2field_c (buf
[1]);
722 if (buf
[0] > 3) break; /* ignore all but left/middle/right/up */
723 int success
= wait_mouse_up(l
, c
);
725 /* mouse moved while pressed: */
726 if (!success
) return CTRSEQ_INVALID
;
729 case 0: return CTRSEQ_MOUSE_LEFT
;
730 case 1: return CTRSEQ_MOUSE_MIDDLE
;
731 case 2: return CTRSEQ_MOUSE_RIGHT
;