uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

gameplay.c (2369B)


      1 #include "gameplay.h"
      2 #include "main.h"
      3 
      4 #define X b->x
      5 #define Y b->y
      6 
      7 void
      8 play(Board *b)
      9 {
     10 	b->x = b->y = 0;
     11 	b->gameover = FALSE;
     12 	b->ndefused = 0;
     13 	int move;
     14 
     15 	do
     16 	{
     17 		erase();
     18 		delwin(b->gw);
     19 		refresh();
     20 		b->gw = game_win(b->rows, b->cols);
     21 		print_board(b);
     22 		session_info(b);
     23 		navigate(b, &move);
     24 		switch (move)
     25 		{
     26 			case ENTER:			/* FALLTHROUGH */
     27 			case OPEN_LOWER:
     28 			case OPEN_UPPER:
     29 				b->gameover = open_cell(b);
     30 				break;
     31 			case FLAG_LOWER:	/* FALLTHROUGH */
     32 			case FLAG_UPPER:
     33 				handle_flags(b);
     34 				break;
     35 			case DEFUSE_LOWER:	/* FALLTHROUGH */
     36 			case DEFUSE_UPPER:
     37 				if (b->db[Y][X] == FLAG && b->mb[Y][X] == MINE)
     38 				{
     39 					b->ndefused++;
     40 					defuse_mine(b);
     41 				}
     42 				else if (b->db[Y][X] == FLAG && b->mb[Y][X] != MINE)
     43 					b->gameover = TRUE;              
     44 				break;
     45 			case PAUSE_AUDIO:
     46 				pause_audio();
     47 				break;
     48 			case VOLUME_UP:		/* FALLTHROUGH */
     49 			case VOLUME_DOWN:
     50 				volume(move);
     51 				break;
     52 			case 'm':
     53 				handle_menu(b);
     54 				break;
     55 			case 'r':
     56 				clear_board(b);
     57 				reset(b);
     58 				init_game(b);
     59 				break;
     60 			default: break;
     61 		}
     62 	} while (((Y >= 0 && Y < b->rows) &&
     63 			(X >= 0 && X < b->cols))  &&
     64 			b->ndefused < b->nmines && !b->gameover &&
     65 			move != QUIT);	
     66 
     67 	if (b->gameover)
     68 		handle_gameover(b);
     69 	if (b->ndefused == b->nmines)
     70 		handle_win(b);
     71 }
     72 
     73 int
     74 open_cell(Board *b)
     75 {
     76 	transfer(b);
     77 	reveal(b);
     78 	return (b->db[Y][X] == MINE) ? TRUE : FALSE;
     79 }
     80 
     81 void
     82 handle_flags(Board *b)
     83 {
     84 	if (b->db[Y][X] == FLAG)
     85 		b->db[Y][X] = BLANK;
     86 	else if (b->db[Y][X] != FLAG &&
     87 			 b->db[Y][X] != BLANK)
     88 		return;
     89 	else b->db[Y][X] = FLAG;
     90 	reveal(b);
     91 }
     92 
     93 void
     94 defuse_mine(Board *b)
     95 {
     96 	refresh();
     97 	b->db[Y][X] = b->mb[Y][X] = DEFUSED;
     98 	reveal(b);
     99 }
    100 
    101 void
    102 transfer(Board *b)
    103 {
    104 	b->db[Y][X] = b->mb[Y][X];
    105 }
    106 
    107 void
    108 reveal(const Board *b)
    109 {
    110 	int y = b->y + 1;
    111 	int x = 3 * b->x + 2;
    112 	mvwaddch(b->gw, y, x, b->db[Y][X]);
    113 	wrefresh(b->gw);
    114 }
    115 
    116 int
    117 is_defused(const Board *b)
    118 {
    119 	return (b->db[Y][X] == DEFUSED) ? TRUE : FALSE;
    120 }
    121 
    122 void
    123 handle_menu(const Board *b)
    124 {
    125 	options_menu();
    126 	box(b->gw, 0, 0);
    127 	print_board(b);
    128 }
    129 
    130 void
    131 handle_gameover(const Board *b)
    132 {
    133 	game_over(b);
    134 	getchar();
    135 	erase();
    136 	refresh();
    137 	box(b->gw, 0, 0);
    138 	print_board(b);
    139 	wrefresh(b->gw);
    140 	session_write(b, GAME_LOST);
    141 }
    142 
    143 void
    144 handle_win(const Board *b)
    145 {
    146 	game_won(b);
    147 	getchar();
    148 	session_write(b, GAME_WON);
    149 	score_write(b);
    150 }
    151 
    152 #undef Y
    153 #undef X