-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecweeper.c
133 lines (117 loc) · 3.03 KB
/
minecweeper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <curses.h>
#include <stdlib.h>
#include "input.h"
#include "game.h"
#include "graphics.h"
#define STR_BUFFER_SIZE 20
int ask_difficulty(int *rows, int *cols, int *mines) {
/* Start game and ask for difficulty */
clear();
int selection;
printw("Please select a difficulty\n1 Easy\n2 Intermediate\n3 Expect\n4 Custom\n");
do {
selection = getch();
} while (selection > '4' || selection < '1');
/* Parse selection */
selection -= '0';
if (selection == 4) {
clear();
echo();
nocbreak();
// Get rows
printw("Rows: ");
char rows_str[STR_BUFFER_SIZE];
getstr(rows_str);
*rows = atoi(rows_str);
// Get cols
printw("Cols: ");
char cols_str[STR_BUFFER_SIZE];
getstr(cols_str);
*cols = atoi(cols_str);
// Get mines
printw("Mines: ");
char mines_str[STR_BUFFER_SIZE];
getstr(mines_str);
*mines = atoi(mines_str);
noecho();
cbreak();
}
return selection;
}
int main() {
/* Initialize curses */
init_input();
/* Initialize game */
struct ms_game game;
init_game(&game);
/* Initialize graphics */
gfx_init();
int rows;
int cols;
int mines;
int selection = ask_difficulty(&rows, &cols, &mines);
if (selection == -1)
return -1;
/* Setup the game and then draw it on the screen */
setup_game(&game, selection, rows, cols, mines);
clear();
gfx_draw_game(&game);
refresh();
/* Main game loop */
while (true) {
int key = getch();
switch (key) {
// For each direction key, check if cursor is at an edge, if it is don't allow it to
// overflow / wrap
case KEY_UP:
game.cursor_row = game.cursor_row == 0 ? 0 : game.cursor_row - 1;
break;
case KEY_DOWN:
game.cursor_row = game.cursor_row == game.rows - 1 ? game.rows - 1 : game.cursor_row + 1;
break;
case KEY_LEFT:
game.cursor_col = game.cursor_col == 0 ? 0 : game.cursor_col - 1;
break;
case KEY_RIGHT:
game.cursor_col = game.cursor_col == game.cols - 1 ? game.cols - 1 : game.cursor_col + 1;
break;
// Discover a cell
case ' ':
// If the map has not been generated, generate it
if (!game.map_generated)
generate_map(&game);
check_cell(&game);
break;
case 'f':
case 'F':
// Flag or unflag cell at current cursor pos
flag_cell(&game);
break;
case 'd':
case 'D':
// Change difficulty
selection = ask_difficulty(&rows, &cols, &mines);
// Don't break so we restart the game
case 'r':
case 'R':
// Restart game
free_map(&game);
setup_game(&game, selection, rows, cols, mines);
generate_map(&game);
break;
}
clear();
// If game is over, display win/lose message
if (game.game_finished) {
if (game.cells_left == 0)
printw("You won!\n\n");
else
printw("You lost!\n\n");
}
gfx_draw_game(&game);
refresh();
}
deinit_input();
free_map(&game);
return 0;
}