-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.c
173 lines (144 loc) · 2.99 KB
/
minesweeper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define GRID_WIDTH_SIZE 10
#define GRID_HEIGHT_SIZE 10
#define BOMB_PERCENT 10 // in percentage of entire grid (truncated)
#define BOMB_COUNT (GRID_WIDTH_SIZE * GRID_HEIGHT_SIZE * BOMB_PERCENT / 100)
#define SUCCESS 0
#define FAILURE -1
typedef struct cell {
bool flagged;
bool revealed;
bool bomb;
int bomb_count;
} cell_t;
cell_t sweeper_grid[GRID_WIDTH_SIZE][GRID_HEIGHT_SIZE];
int bombs_flagged;
int flag_count;
int revealed_cells;
bool lost;
typedef enum mouseevent {
MOUSEEVENT_LEFT,
MOUSEEVENT_RIGHT,
} mouseevent_t;
void init_grid(void)
{
srand(time(NULL));
memset(sweeper_grid, 0, sizeof(sweeper_grid));
lost = false;
bombs_flagged = 0;
flag_count = 0;
revealed_cells = 0;
}
void get_mousepos(int* x, int* y)
{
// returns (x,y) grid coordinates
}
mouseevent_t get_mouseevent(void)
{
// returns mouse event somehow
}
int place_bomb(int x, int y)
{
if (sweeper_grid[x][y].bomb)
return FAILURE;
sweeper_grid[x][y].bomb = true;
for (int i=x-1; i<=x+1; i++) {
for (int j=y-1; j<=y+1; j++) {
if (i < 0 || i >= GRID_WIDTH_SIZE)
continue;
if (j < 0 || j >= GRID_HEIGHT_SIZE)
continue;
sweeper_grid[i][j].bomb_count++;
}
}
return SUCCESS;
}
void toggle_flag(int x, int y)
{
sweeper_grid[x][y].flagged = !sweeper_grid[x][y].flagged;
if (sweeper_grid[x][y].flagged)
flag_count++;
else
flag_count--;
if (sweeper_grid[x][y].bomb) {
if (sweeper_grid[x][y].flagged)
bombs_flagged++;
else
bombs_flagged--;
}
}
void generate_grid(void)
{
for (int i=0; i<BOMB_COUNT; i++) {
while (1) {
int x = rand() % GRID_WIDTH_SIZE;
int y = rand() % GRID_HEIGHT_SIZE;
if (place_bomb(x, y) == SUCCESS)
break;
}
}
}
void reveal_square(int x, int y)
{
if (!sweeper_grid[x][y].revealed) {
sweeper_grid[x][y].revealed = true;
revealed_cells++;
}
if (sweeper_grid[x][y].bomb) {
lost = true;
return;
}
if (sweeper_grid[x][y].bomb_count == 0) {
for (int i=x-1; i<=x+1; i++) {
for (int j=y-1; j<=y+1; j++) {
if (i < 0 || i >= GRID_WIDTH_SIZE)
continue;
if (j < 0 || j >= GRID_HEIGHT_SIZE)
continue;
if (!sweeper_grid[i][j].revealed)
reveal_square(i, j);
}
}
}
}
void handle_leftclick(int x, int y)
{
reveal_square(x, y);
}
void handle_rightclick(int x, int y)
{
toggle_flag(x, y);
}
void determine_win(void)
{
if (lost)
graphics_lost();
if (flag_count == BOMB_COUNT && bombs_flagged == BOMB_COUNT)
graphics_win();
if (revealed_cells == (GRID_WIDTH_SIZE * GRID_HEIGHT_SIZE) - BOMB_COUNT)
graphics_win();
}
void main(void)
{
init_grid();
generate_grid();
while (1) {
mouseevent_t event = get_mouseevent();
int x, y;
get_mousepos(&x, &y);
switch (event) {
case MOUSEEVENT_LEFT:
handle_leftclick(x, y);
break;
case MOUSEEVENT_RIGHT:
handle_rightclick(x, y);
break;
default:
printf("Unhandled mouse event.\n");
assert(false);
}
determine_win();
}
}