-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave_function.c
249 lines (243 loc) · 7.09 KB
/
wave_function.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "wave_function.h"
void enqueue_neighbors(BOARD* board, POINT point) {
bool left = point.x > 0;
bool right = point.x < board->width - 1;
bool up = point.y > 0;
bool down = point.y < board->height - 1;
bool mask[9] = {
left && up,
up,
right && up,
left,
false,
right,
left && down,
down,
right && down
};
POINT p;
queue_enqueue(&board->update_queue, &point);
for (int y = -1; y < 2; y++) {
for (int x = -1; x < 2; x++) {
if (mask[(y + 1) * 3 + (x + 1)]) {
p.x = point.x + x;
p.y = point.y + y;
queue_enqueue(&board->update_queue, &p);
}
}
}
}
int num_possibilities(uint_fast16_t tile) {
int num = 0;
for (int i = 0; i < 10; i++) {
if (tile & 1) {
num++;
}
tile >>= 1;
}
return num;
}
void num_mine_neighbors(BOARD* board, POINT point, unsigned int mines[2]) {
bool left = point.x > 0;
bool right = point.x < board->width - 1;
bool up = point.y > 0;
bool down = point.y < board->height - 1;
bool mask[9] = {
left && up,
up,
right && up,
left,
false,
right,
left && down,
down,
right && down
};
POINT p;
mines[0] = 0;
mines[1] = 0;
for (int y = -1; y < 2; y++) {
for (int x = -1; x < 2; x++) {
if (mask[(y + 1) * 3 + (x + 1)]) {
p.x = point.x + x;
p.y = point.y + y;
if ((get_tile_point(board, p) & 0b010000001111) == 0b010000001001) {
mines[0]++;
mines[1]++;
} else if ((get_tile_point(board, p) & 0b011000000000) == 0b001000000000) {
mines[1]++;
}
}
}
}
}
void collapse_mine_neighbors(BOARD* board, POINT point) {
bool left = point.x > 0;
bool right = point.x < board->width - 1;
bool up = point.y > 0;
bool down = point.y < board->height - 1;
bool mask[9] = {
left && up,
up,
right && up,
left,
false,
right,
left && down,
down,
right && down
};
POINT p;
for (int y = -1; y < 2; y++) {
for (int x = -1; x < 2; x++) {
if (mask[(y + 1) * 3 + (x + 1)]) {
p.x = point.x + x;
p.y = point.y + y;
#ifdef DEBUG
printf("Checking (%d, %d)\n", p.x, p.y);
#endif
if ((get_tile_point(board, p) & 0b011000000000) == 0b001000000000) {
#ifdef DEBUG
printf("Collapsing\n");
#endif
set_tile_point(board, p, 0b010000001001);
enqueue_neighbors(board, p);
}
}
}
}
}
void collapse_non_mine_neighbors(BOARD* board, POINT point) {
bool left = point.x > 0;
bool right = point.x < board->width - 1;
bool up = point.y > 0;
bool down = point.y < board->height - 1;
bool mask[9] = {
left && up,
up,
right && up,
left,
false,
right,
left && down,
down,
right && down
};
POINT p;
for (int y = -1; y < 2; y++) {
for (int x = -1; x < 2; x++) {
if (mask[(y + 1) * 3 + (x + 1)]) {
p.x = point.x + x;
p.y = point.y + y;
#ifdef DEBUG
printf("Checking (%d, %d)\n", p.x, p.y);
#endif
if ((get_tile_point(board, p) & 0b011000000000) == 0b001000000000) {
#ifdef DEBUG
printf("Collapsing\n");
#endif
set_tile_point(board, p, get_tile_point(board, p) & 0b110111111111);
enqueue_neighbors(board, p);
}
}
}
}
}
void update_wave_function(BOARD* board) {
#ifdef DEBUG
printf("------------------------------------------------------------------------\n");
printf("Updating\n");
#endif
while (!(queue_is_empty(&board->update_queue))) {
POINT point;
queue_dequeue(&board->update_queue, &point);
uint_fast16_t tile = get_tile_point(board, point);
#ifdef DEBUG
printf("Point: (%d, %d)\n", point.x, point.y);
printf("Tile: %" PRIxFAST16 "\n", tile);
#endif
if (tile & 0b010000000000) {
#ifdef DEBUG
printf("Tile is known\n");
#endif
unsigned int mines[2];
num_mine_neighbors(board, point, mines);
if (mines[0] == mines[1]) {
#ifdef DEBUG
printf("Mines already collapsed\n");
#endif
continue;
}
if (mines[0] == (tile & 0b1111)) {
#ifdef DEBUG
printf("Collapsing non mine neighbors\n");
#endif
collapse_non_mine_neighbors(board, point);
continue;
}
if (mines[1] == (tile & 0b1111)) {
#ifdef DEBUG
printf("Collapsing mine neighbors\n");
#endif
collapse_mine_neighbors(board, point);
continue;
}
continue;
}
int possibilities = num_possibilities(tile);
assert(possibilities > 0);
if (possibilities == 1) {
int i;
for (i = 0; i < 10; i++) {
if (tile & 1 << i)
break;
}
#ifdef DEBUG
printf("Only 1 possibility: %d\n", i);
#endif
tile = 0b010000000000 | i;
set_tile_point(board, point, tile);
enqueue_neighbors(board, point);
continue;
}
unsigned int mines[2];
num_mine_neighbors(board, point, mines);
if (mines[0] == mines[1] && !(tile & 0b001000000000)) {
#ifdef DEBUG
printf("Is a number: %d\n", mines[0]);
#endif
tile = 0b010000000000 | mines[0];
set_tile_point(board, point, tile);
enqueue_neighbors(board, point);
continue;
}
#ifdef DEBUG
printf("Min mines: %d\n", mines[0]);
printf("Max mines: %d\n", mines[1]);
#endif
uint_fast16_t old_tile = tile;
for (unsigned int i = 0; i < mines[0]; i++) {
#ifdef DEBUG
printf("Removing %d\n", i);
#endif
tile &= ~(1 << i);
}
for (int i = mines[1] + 1; i < 9; i++) {
#ifdef DEBUG
printf("Removing %d\n", i);
#endif
tile &= ~(1 << i);
}
if (old_tile == tile) {
#ifdef DEBUG
printf("No change\n");
#endif
continue;
}
#ifdef DEBUG
printf("Tile is now %" PRIxFAST16 "\n" , tile);
#endif
set_tile_point(board, point, tile);
enqueue_neighbors(board, point);
}
}