-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
377 lines (302 loc) · 9.88 KB
/
game.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* @file game.c
* @copyright University of Bordeaux. All rights reserved, 2021.
**/
#include "game.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "game_ext.h"
#include "game_private.h"
#include "queue.h"
/* ************************************************************************** */
/* INTERNAL */
/* ************************************************************************** */
static void _update_lighted_flags(game g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
// we just have put a lightbulb at (i,j) and we want to update lighted flags
square s = STATE(g, i, j);
s = s;
assert(s == S_LIGHTBULB);
/* update square (i,j) */
SQUARE(g, i, j) |= F_LIGHTED;
uint startdir = UP;
uint enddir = RIGHT;
uint dim = MAX(g->nb_rows, g->nb_cols);
// update lighted flags in all directions
for (uint dir = startdir; dir <= enddir; dir++) {
int ii = i;
int jj = j;
for (uint k = 1; k < dim; k++) {
bool cont = _next(g, &ii, &jj, dir); // update next coord (ii,jj)
if (!cont) break;
if (STATE(g, ii, jj) & S_BLACK) break;
SQUARE(g, ii, jj) |= F_LIGHTED;
}
}
}
/* ************************************************************************** */
static bool _check_lightbulb_error(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
// square s = STATE(g, i, j);
// assert(s == S_LIGHTBULB);
// update lighted flags in all directions
direction dirs[] = {UP, DOWN, LEFT, RIGHT};
for (uint d = 0; d < 4; d++) {
direction dir = dirs[d];
int ii = i;
int jj = j;
uint dim = 0;
if (dir == UP || dir == DOWN) dim = g->nb_rows;
if (dir == LEFT || dir == RIGHT) dim = g->nb_cols;
for (uint k = 1; k < dim; k++) {
bool cont = _next(g, &ii, &jj, dir); // update next coord (ii,jj)
if (!cont) break;
if (STATE(g, ii, jj) & S_BLACK) break;
if (STATE(g, ii, jj) == S_LIGHTBULB) return false;
}
}
return true;
}
/* ************************************************************************** */
static bool _check_blackwall_error(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square s = STATE(g, i, j);
assert(s & S_BLACK);
if (s == S_BLACKU) return true; /* no constraint for unumbered wall */
int expected = game_get_black_number(g, i, j);
int nb_lightbulbs = _neigh_count(g, i, j, S_LIGHTBULB, S_MASK, false);
int nb_blanks = _neigh_count(g, i, j, S_BLANK, A_MASK, false); // blank state, without lighted flag
// 1) too many lightbulbs
if (nb_lightbulbs > expected) return false;
// 2) not enough blank squares (without lighted flag) to place the expected number of lightbulbs
if (nb_blanks < (expected - nb_lightbulbs)) return false;
return true;
}
/* ************************************************************************** */
/* GAME BASIC */
/* ************************************************************************** */
game game_new(square* squares) { return game_new_ext(DEFAULT_SIZE, DEFAULT_SIZE, squares, false); }
/* ************************************************************************** */
game game_new_empty(void) { return game_new_empty_ext(DEFAULT_SIZE, DEFAULT_SIZE, false); }
/* ************************************************************************** */
game game_copy(cgame g)
{
game gg = game_new_ext(g->nb_rows, g->nb_cols, g->squares, g->wrapping);
return gg;
}
/* ************************************************************************** */
bool game_equal(cgame g1, cgame g2)
{
assert(g1 && g2);
if (g1->nb_rows != g2->nb_rows) return false;
if (g1->nb_cols != g2->nb_cols) return false;
for (uint i = 0; i < g1->nb_rows; i++)
for (uint j = 0; j < g1->nb_cols; j++) {
if (SQUARE(g1, i, j) != SQUARE(g2, i, j)) return false;
}
if (g1->wrapping != g2->wrapping) return false;
return true;
}
/* ************************************************************************** */
void game_delete(game g)
{
free(g->squares);
queue_free_full(g->undo_stack, free);
queue_free_full(g->redo_stack, free);
free(g);
}
/* ************************************************************************** */
void game_set_square(game g, uint i, uint j, square s)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
SQUARE(g, i, j) = s;
}
/* ************************************************************************** */
square game_get_square(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
return SQUARE(g, i, j);
}
/* ************************************************************************** */
square game_get_state(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
return STATE(g, i, j);
}
/* ************************************************************************** */
square game_get_flags(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
return FLAGS(g, i, j);
}
/* ************************************************************************** */
void game_update_flags(game g)
{
assert(g);
// 0) reset all flags
for (uint i = 0; i < g->nb_rows; i++)
for (uint j = 0; j < g->nb_cols; j++) SQUARE(g, i, j) = STATE(g, i, j);
// 1) update lighted flag
for (uint i = 0; i < g->nb_rows; i++)
for (uint j = 0; j < g->nb_cols; j++)
if (STATE(g, i, j) == S_LIGHTBULB) _update_lighted_flags(g, i, j);
// 2) update error flag
for (uint i = 0; i < g->nb_rows; i++)
for (uint j = 0; j < g->nb_cols; j++) {
if (game_is_lightbulb(g, i, j) && !_check_lightbulb_error(g, i, j)) SQUARE(g, i, j) |= F_ERROR;
if (game_is_black(g, i, j) && !_check_blackwall_error(g, i, j)) SQUARE(g, i, j) |= F_ERROR;
}
}
/* ************************************************************************** */
bool game_check_move(cgame g, uint i, uint j, square s)
{
assert(g);
if (i > g->nb_rows - 1) return false;
if (j > g->nb_cols - 1) return false;
if (s != S_BLANK && s != S_LIGHTBULB && s != S_MARK) return false;
square cs = STATE(g, i, j); // current state at position (i,j)
if (s & S_BLACK) return false;
if (cs & S_BLACK) return false;
return true; // regular move
}
/* ************************************************************************** */
void game_play_move(game g, uint i, uint j, square s)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
assert(s == S_BLANK || s == S_MARK || s == S_LIGHTBULB);
bool black = game_is_black(g, i, j);
black = black;
assert(!black);
square cs = STATE(g, i, j); // save current state
SQUARE(g, i, j) = s; // update with new state
// update flags
game_update_flags(g);
// save history
_stack_clear(g->redo_stack);
move m = {i, j, cs, s};
_stack_push_move(g->undo_stack, m);
}
/* ************************************************************************** */
bool game_is_over(cgame g)
{
assert(g);
for (uint i = 0; i < g->nb_rows; i++)
for (uint j = 0; j < g->nb_cols; j++) {
// 1) check that square is lighted (except if it is a wall)
if (!game_is_black(g, i, j) && !game_is_lighted(g, i, j)) return false;
// 2) check if there are no errors
if (game_has_error(g, i, j)) return false;
}
return true;
}
/* ************************************************************************** */
void game_restart(game g)
{
assert(g);
for (uint i = 0; i < g->nb_rows; i++)
for (uint j = 0; j < g->nb_cols; j++) {
square s = STATE(g, i, j); // ignore flags
if (s & S_BLACK) // keep only wall
SQUARE(g, i, j) = s;
else // blank other
SQUARE(g, i, j) = S_BLANK;
}
// reset history
_stack_clear(g->undo_stack);
_stack_clear(g->redo_stack);
}
/* ************************************************************************** */
bool game_is_blank(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square s = STATE(g, i, j);
if (s == S_BLANK) return true;
return false;
}
/* ************************************************************************** */
bool game_is_black(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square s = STATE(g, i, j);
if (s & S_BLACK) return true;
return false;
}
/* ************************************************************************** */
bool game_is_lightbulb(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square s = STATE(g, i, j);
if (s == S_LIGHTBULB) return true;
return false;
}
/* ************************************************************************** */
int game_get_black_number(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square s = STATE(g, i, j);
assert(s & S_BLACK);
if (s == S_BLACKU)
return -1;
else
return (s - S_BLACK);
}
/* ************************************************************************** */
bool game_is_marked(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square s = STATE(g, i, j);
if (s == S_MARK) return true;
return false;
}
/* ************************************************************************** */
bool game_is_lighted(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square f = FLAGS(g, i, j);
if (f & F_LIGHTED) return true;
return false;
}
/* ************************************************************************** */
bool game_has_error(cgame g, uint i, uint j)
{
assert(g);
assert(i < g->nb_rows);
assert(j < g->nb_cols);
square f = FLAGS(g, i, j);
if (f & F_ERROR) return true;
return false;
}
/* ************************************************************************** */