e3478a4b |
1 | /* |
2 | * lightup.c: Implementation of the Nikoli game 'Light Up'. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <assert.h> |
9 | #include <ctype.h> |
10 | #include <math.h> |
11 | |
12 | #include "puzzles.h" |
13 | |
9b265feb |
14 | /* |
15 | * In standalone solver mode, `verbose' is a variable which can be |
16 | * set by command-line option; in debugging mode it's simply always |
17 | * true. |
18 | */ |
19 | #if defined STANDALONE_SOLVER |
20 | #define SOLVER_DIAGNOSTICS |
21 | int verbose = 0; |
22 | #undef debug |
23 | #define debug(x) printf x |
24 | #elif defined SOLVER_DIAGNOSTICS |
25 | #define verbose 2 |
26 | #endif |
27 | |
e3478a4b |
28 | /* --- Constants, structure definitions, etc. --- */ |
29 | |
30 | #define PREFERRED_TILE_SIZE 32 |
31 | #define TILE_SIZE (ds->tilesize) |
32 | #define BORDER (TILE_SIZE / 2) |
33 | #define TILE_RADIUS (ds->crad) |
34 | |
35 | #define COORD(x) ( (x) * TILE_SIZE + BORDER ) |
36 | #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 ) |
37 | |
38 | #define FLASH_TIME 0.30F |
39 | |
40 | enum { |
41 | COL_BACKGROUND, |
42 | COL_GRID, |
43 | COL_BLACK, /* black */ |
44 | COL_LIGHT, /* white */ |
45 | COL_LIT, /* yellow */ |
46 | COL_ERROR, /* red */ |
47 | COL_CURSOR, |
48 | NCOLOURS |
49 | }; |
50 | |
51 | enum { SYMM_NONE, SYMM_REF2, SYMM_ROT2, SYMM_REF4, SYMM_ROT4, SYMM_MAX }; |
52 | |
9b265feb |
53 | #define DIFFCOUNT 2 |
54 | |
e3478a4b |
55 | struct game_params { |
56 | int w, h; |
57 | int blackpc; /* %age of black squares */ |
58 | int symm; |
9b265feb |
59 | int difficulty; /* 0 to DIFFCOUNT */ |
e3478a4b |
60 | }; |
61 | |
62 | #define F_BLACK 1 |
63 | |
64 | /* flags for black squares */ |
65 | #define F_NUMBERED 2 /* it has a number attached */ |
66 | #define F_NUMBERUSED 4 /* this number was useful for solving */ |
67 | |
68 | /* flags for non-black squares */ |
69 | #define F_IMPOSSIBLE 8 /* can't put a light here */ |
70 | #define F_LIGHT 16 |
71 | |
72 | #define F_MARK 32 |
73 | |
74 | struct game_state { |
75 | int w, h, nlights; |
76 | int *lights; /* For black squares, (optionally) the number |
77 | of surrounding lights. For non-black squares, |
78 | the number of times it's lit. size h*w*/ |
79 | unsigned int *flags; /* size h*w */ |
80 | int completed, used_solve; |
81 | }; |
82 | |
83 | #define GRID(gs,grid,x,y) (gs->grid[(y)*((gs)->w) + (x)]) |
84 | |
85 | /* A ll_data holds information about which lights would be lit by |
86 | * a particular grid location's light (or conversely, which locations |
87 | * could light a specific other location). */ |
88 | /* most things should consider this struct opaque. */ |
89 | typedef struct { |
90 | int ox,oy; |
91 | int minx, maxx, miny, maxy; |
92 | int include_origin; |
93 | } ll_data; |
94 | |
95 | /* Macro that executes 'block' once per light in lld, including |
96 | * the origin if include_origin is specified. 'block' can use |
97 | * lx and ly as the coords. */ |
98 | #define FOREACHLIT(lld,block) do { \ |
99 | int lx,ly; \ |
100 | ly = (lld)->oy; \ |
101 | for (lx = (lld)->minx; lx <= (lld)->maxx; lx++) { \ |
102 | if (lx == (lld)->ox) continue; \ |
103 | block \ |
104 | } \ |
105 | lx = (lld)->ox; \ |
106 | for (ly = (lld)->miny; ly <= (lld)->maxy; ly++) { \ |
107 | if (!(lld)->include_origin && ly == (lld)->oy) continue; \ |
108 | block \ |
109 | } \ |
110 | } while(0) |
111 | |
112 | |
113 | typedef struct { |
114 | struct { int x, y; unsigned int f; } points[4]; |
115 | int npoints; |
116 | } surrounds; |
117 | |
118 | /* Fills in (doesn't allocate) a surrounds structure with the grid locations |
119 | * around a given square, taking account of the edges. */ |
120 | static void get_surrounds(game_state *state, int ox, int oy, surrounds *s) |
121 | { |
122 | assert(ox >= 0 && ox < state->w && oy >= 0 && oy < state->h); |
123 | s->npoints = 0; |
124 | #define ADDPOINT(cond,nx,ny) do {\ |
125 | if (cond) { \ |
126 | s->points[s->npoints].x = (nx); \ |
127 | s->points[s->npoints].y = (ny); \ |
128 | s->points[s->npoints].f = 0; \ |
129 | s->npoints++; \ |
130 | } } while(0) |
131 | ADDPOINT(ox > 0, ox-1, oy); |
132 | ADDPOINT(ox < (state->w-1), ox+1, oy); |
133 | ADDPOINT(oy > 0, ox, oy-1); |
134 | ADDPOINT(oy < (state->h-1), ox, oy+1); |
135 | } |
136 | |
137 | /* --- Game parameter functions --- */ |
138 | |
139 | #define DEFAULT_PRESET 0 |
140 | |
141 | const struct game_params lightup_presets[] = { |
142 | { 7, 7, 20, SYMM_ROT4, 0 }, |
143 | { 7, 7, 20, SYMM_ROT4, 1 }, |
9b265feb |
144 | { 7, 7, 20, SYMM_ROT4, 2 }, |
e3478a4b |
145 | { 10, 10, 20, SYMM_ROT2, 0 }, |
146 | { 10, 10, 20, SYMM_ROT2, 1 }, |
147 | #ifdef SLOW_SYSTEM |
148 | { 12, 12, 20, SYMM_ROT2, 0 }, |
9b265feb |
149 | { 12, 12, 20, SYMM_ROT2, 1 }, |
e3478a4b |
150 | #else |
9b265feb |
151 | { 10, 10, 20, SYMM_ROT2, 2 }, |
e3478a4b |
152 | { 14, 14, 20, SYMM_ROT2, 0 }, |
9b265feb |
153 | { 14, 14, 20, SYMM_ROT2, 1 }, |
154 | { 14, 14, 20, SYMM_ROT2, 2 } |
e3478a4b |
155 | #endif |
156 | }; |
157 | |
158 | static game_params *default_params(void) |
159 | { |
160 | game_params *ret = snew(game_params); |
161 | *ret = lightup_presets[DEFAULT_PRESET]; |
162 | |
163 | return ret; |
164 | } |
165 | |
166 | static int game_fetch_preset(int i, char **name, game_params **params) |
167 | { |
168 | game_params *ret; |
169 | char buf[80]; |
170 | |
171 | if (i < 0 || i >= lenof(lightup_presets)) |
172 | return FALSE; |
173 | |
174 | ret = default_params(); |
175 | *ret = lightup_presets[i]; |
176 | *params = ret; |
177 | |
178 | sprintf(buf, "%dx%d %s", |
9b265feb |
179 | ret->w, ret->h, |
180 | ret->difficulty == 2 ? "hard" : |
181 | ret->difficulty == 1 ? "tricky" : "easy"); |
e3478a4b |
182 | *name = dupstr(buf); |
183 | |
184 | return TRUE; |
185 | } |
186 | |
187 | static void free_params(game_params *params) |
188 | { |
189 | sfree(params); |
190 | } |
191 | |
192 | static game_params *dup_params(game_params *params) |
193 | { |
194 | game_params *ret = snew(game_params); |
195 | *ret = *params; /* structure copy */ |
196 | return ret; |
197 | } |
198 | |
199 | #define EATNUM(x) do { \ |
200 | (x) = atoi(string); \ |
201 | while (*string && isdigit((unsigned char)*string)) string++; \ |
202 | } while(0) |
203 | |
204 | static void decode_params(game_params *params, char const *string) |
205 | { |
206 | EATNUM(params->w); |
207 | if (*string == 'x') { |
208 | string++; |
209 | EATNUM(params->h); |
210 | } |
211 | if (*string == 'b') { |
212 | string++; |
213 | EATNUM(params->blackpc); |
214 | } |
215 | if (*string == 's') { |
216 | string++; |
217 | EATNUM(params->symm); |
f50327a3 |
218 | } else { |
219 | /* cope with user input such as '18x10' by ensuring symmetry |
220 | * is not selected by default to be incompatible with dimensions */ |
221 | if (params->symm == SYMM_ROT4 && params->w != params->h) |
222 | params->symm = SYMM_ROT2; |
e3478a4b |
223 | } |
9b265feb |
224 | params->difficulty = 0; |
225 | /* cope with old params */ |
e3478a4b |
226 | if (*string == 'r') { |
9b265feb |
227 | params->difficulty = 2; |
228 | string++; |
229 | } |
230 | if (*string == 'd') { |
e3478a4b |
231 | string++; |
9b265feb |
232 | EATNUM(params->difficulty); |
e3478a4b |
233 | } |
234 | } |
235 | |
236 | static char *encode_params(game_params *params, int full) |
237 | { |
238 | char buf[80]; |
239 | |
240 | if (full) { |
9b265feb |
241 | sprintf(buf, "%dx%db%ds%dd%d", |
e3478a4b |
242 | params->w, params->h, params->blackpc, |
243 | params->symm, |
9b265feb |
244 | params->difficulty); |
e3478a4b |
245 | } else { |
246 | sprintf(buf, "%dx%d", params->w, params->h); |
247 | } |
248 | return dupstr(buf); |
249 | } |
250 | |
251 | static config_item *game_configure(game_params *params) |
252 | { |
253 | config_item *ret; |
254 | char buf[80]; |
255 | |
256 | ret = snewn(6, config_item); |
257 | |
258 | ret[0].name = "Width"; |
259 | ret[0].type = C_STRING; |
260 | sprintf(buf, "%d", params->w); |
261 | ret[0].sval = dupstr(buf); |
262 | ret[0].ival = 0; |
263 | |
264 | ret[1].name = "Height"; |
265 | ret[1].type = C_STRING; |
266 | sprintf(buf, "%d", params->h); |
267 | ret[1].sval = dupstr(buf); |
268 | ret[1].ival = 0; |
269 | |
270 | ret[2].name = "%age of black squares"; |
271 | ret[2].type = C_STRING; |
272 | sprintf(buf, "%d", params->blackpc); |
273 | ret[2].sval = dupstr(buf); |
274 | ret[2].ival = 0; |
275 | |
276 | ret[3].name = "Symmetry"; |
277 | ret[3].type = C_CHOICES; |
278 | ret[3].sval = ":None" |
279 | ":2-way mirror:2-way rotational" |
280 | ":4-way mirror:4-way rotational"; |
281 | ret[3].ival = params->symm; |
282 | |
283 | ret[4].name = "Difficulty"; |
284 | ret[4].type = C_CHOICES; |
9b265feb |
285 | ret[4].sval = ":Easy:Tricky:Hard"; |
286 | ret[4].ival = params->difficulty; |
e3478a4b |
287 | |
288 | ret[5].name = NULL; |
289 | ret[5].type = C_END; |
290 | ret[5].sval = NULL; |
291 | ret[5].ival = 0; |
292 | |
293 | return ret; |
294 | } |
295 | |
296 | static game_params *custom_params(config_item *cfg) |
297 | { |
298 | game_params *ret = snew(game_params); |
299 | |
300 | ret->w = atoi(cfg[0].sval); |
301 | ret->h = atoi(cfg[1].sval); |
302 | ret->blackpc = atoi(cfg[2].sval); |
303 | ret->symm = cfg[3].ival; |
9b265feb |
304 | ret->difficulty = cfg[4].ival; |
e3478a4b |
305 | |
306 | return ret; |
307 | } |
308 | |
309 | static char *validate_params(game_params *params, int full) |
310 | { |
311 | if (params->w < 2 || params->h < 2) |
312 | return "Width and height must be at least 2"; |
313 | if (full) { |
314 | if (params->blackpc < 5 || params->blackpc > 100) |
315 | return "Percentage of black squares must be between 5% and 100%"; |
316 | if (params->w != params->h) { |
317 | if (params->symm == SYMM_ROT4) |
318 | return "4-fold symmetry is only available with square grids"; |
319 | } |
320 | if (params->symm < 0 || params->symm >= SYMM_MAX) |
9b265feb |
321 | return "Unknown symmetry type"; |
322 | if (params->difficulty < 0 || params->difficulty > DIFFCOUNT) |
323 | return "Unknown difficulty level"; |
e3478a4b |
324 | } |
325 | return NULL; |
326 | } |
327 | |
328 | /* --- Game state construction/freeing helper functions --- */ |
329 | |
330 | static game_state *new_state(game_params *params) |
331 | { |
332 | game_state *ret = snew(game_state); |
333 | |
334 | ret->w = params->w; |
335 | ret->h = params->h; |
336 | ret->lights = snewn(ret->w * ret->h, int); |
337 | ret->nlights = 0; |
338 | memset(ret->lights, 0, ret->w * ret->h * sizeof(int)); |
339 | ret->flags = snewn(ret->w * ret->h, unsigned int); |
340 | memset(ret->flags, 0, ret->w * ret->h * sizeof(unsigned int)); |
341 | ret->completed = ret->used_solve = 0; |
342 | return ret; |
343 | } |
344 | |
345 | static game_state *dup_game(game_state *state) |
346 | { |
347 | game_state *ret = snew(game_state); |
348 | |
349 | ret->w = state->w; |
350 | ret->h = state->h; |
351 | |
352 | ret->lights = snewn(ret->w * ret->h, int); |
353 | memcpy(ret->lights, state->lights, ret->w * ret->h * sizeof(int)); |
354 | ret->nlights = state->nlights; |
355 | |
356 | ret->flags = snewn(ret->w * ret->h, unsigned int); |
357 | memcpy(ret->flags, state->flags, ret->w * ret->h * sizeof(unsigned int)); |
358 | |
359 | ret->completed = state->completed; |
360 | ret->used_solve = state->used_solve; |
361 | |
362 | return ret; |
363 | } |
364 | |
365 | static void free_game(game_state *state) |
366 | { |
367 | sfree(state->lights); |
368 | sfree(state->flags); |
369 | sfree(state); |
370 | } |
371 | |
e3478a4b |
372 | static void debug_state(game_state *state) |
373 | { |
374 | int x, y; |
375 | char c = '?'; |
376 | |
377 | for (y = 0; y < state->h; y++) { |
378 | for (x = 0; x < state->w; x++) { |
379 | c = '.'; |
380 | if (GRID(state, flags, x, y) & F_BLACK) { |
381 | if (GRID(state, flags, x, y) & F_NUMBERED) |
382 | c = GRID(state, lights, x, y) + '0'; |
383 | else |
384 | c = '#'; |
385 | } else { |
386 | if (GRID(state, flags, x, y) & F_LIGHT) |
387 | c = 'O'; |
388 | else if (GRID(state, flags, x, y) & F_IMPOSSIBLE) |
389 | c = 'X'; |
390 | } |
9b265feb |
391 | debug(("%c", (int)c)); |
e3478a4b |
392 | } |
9b265feb |
393 | debug((" ")); |
e3478a4b |
394 | for (x = 0; x < state->w; x++) { |
395 | if (GRID(state, flags, x, y) & F_BLACK) |
396 | c = '#'; |
397 | else { |
398 | c = (GRID(state, flags, x, y) & F_LIGHT) ? 'A' : 'a'; |
399 | c += GRID(state, lights, x, y); |
400 | } |
9b265feb |
401 | debug(("%c", (int)c)); |
e3478a4b |
402 | } |
9b265feb |
403 | debug(("\n")); |
e3478a4b |
404 | } |
e3478a4b |
405 | } |
e3478a4b |
406 | |
407 | /* --- Game completion test routines. --- */ |
408 | |
409 | /* These are split up because occasionally functions are only |
410 | * interested in one particular aspect. */ |
411 | |
412 | /* Returns non-zero if all grid spaces are lit. */ |
413 | static int grid_lit(game_state *state) |
414 | { |
415 | int x, y; |
416 | |
417 | for (x = 0; x < state->w; x++) { |
418 | for (y = 0; y < state->h; y++) { |
419 | if (GRID(state,flags,x,y) & F_BLACK) continue; |
420 | if (GRID(state,lights,x,y) == 0) |
421 | return 0; |
422 | } |
423 | } |
424 | return 1; |
425 | } |
426 | |
427 | /* Returns non-zero if any lights are lit by other lights. */ |
428 | static int grid_overlap(game_state *state) |
429 | { |
430 | int x, y; |
431 | |
432 | for (x = 0; x < state->w; x++) { |
433 | for (y = 0; y < state->h; y++) { |
434 | if (!(GRID(state, flags, x, y) & F_LIGHT)) continue; |
435 | if (GRID(state, lights, x, y) > 1) |
436 | return 1; |
437 | } |
438 | } |
439 | return 0; |
440 | } |
441 | |
442 | static int number_wrong(game_state *state, int x, int y) |
443 | { |
444 | surrounds s; |
445 | int i, n, empty, lights = GRID(state, lights, x, y); |
446 | |
447 | /* |
448 | * This function computes the display hint for a number: we |
449 | * turn the number red if it is definitely wrong. This means |
450 | * that either |
451 | * |
452 | * (a) it has too many lights around it, or |
453 | * (b) it would have too few lights around it even if all the |
454 | * plausible squares (not black, lit or F_IMPOSSIBLE) were |
455 | * filled with lights. |
456 | */ |
457 | |
458 | assert(GRID(state, flags, x, y) & F_NUMBERED); |
459 | get_surrounds(state, x, y, &s); |
460 | |
461 | empty = n = 0; |
462 | for (i = 0; i < s.npoints; i++) { |
463 | if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_LIGHT) { |
464 | n++; |
465 | continue; |
466 | } |
467 | if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_BLACK) |
468 | continue; |
469 | if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_IMPOSSIBLE) |
470 | continue; |
471 | if (GRID(state,lights,s.points[i].x,s.points[i].y)) |
472 | continue; |
473 | empty++; |
474 | } |
475 | return (n > lights || (n + empty < lights)); |
476 | } |
477 | |
478 | static int number_correct(game_state *state, int x, int y) |
479 | { |
480 | surrounds s; |
481 | int n = 0, i, lights = GRID(state, lights, x, y); |
482 | |
483 | assert(GRID(state, flags, x, y) & F_NUMBERED); |
484 | get_surrounds(state, x, y, &s); |
485 | for (i = 0; i < s.npoints; i++) { |
486 | if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_LIGHT) |
487 | n++; |
488 | } |
489 | return (n == lights) ? 1 : 0; |
490 | } |
491 | |
492 | /* Returns non-zero if any numbers add up incorrectly. */ |
493 | static int grid_addsup(game_state *state) |
494 | { |
495 | int x, y; |
496 | |
497 | for (x = 0; x < state->w; x++) { |
498 | for (y = 0; y < state->h; y++) { |
499 | if (!(GRID(state, flags, x, y) & F_NUMBERED)) continue; |
500 | if (!number_correct(state, x, y)) return 0; |
501 | } |
502 | } |
503 | return 1; |
504 | } |
505 | |
506 | static int grid_correct(game_state *state) |
507 | { |
508 | if (grid_lit(state) && |
509 | !grid_overlap(state) && |
510 | grid_addsup(state)) return 1; |
511 | return 0; |
512 | } |
513 | |
514 | /* --- Board initial setup (blacks, lights, numbers) --- */ |
515 | |
516 | static void clean_board(game_state *state, int leave_blacks) |
517 | { |
518 | int x,y; |
519 | for (x = 0; x < state->w; x++) { |
520 | for (y = 0; y < state->h; y++) { |
521 | if (leave_blacks) |
522 | GRID(state, flags, x, y) &= F_BLACK; |
523 | else |
524 | GRID(state, flags, x, y) = 0; |
525 | GRID(state, lights, x, y) = 0; |
526 | } |
527 | } |
528 | state->nlights = 0; |
529 | } |
530 | |
531 | static void set_blacks(game_state *state, game_params *params, random_state *rs) |
532 | { |
533 | int x, y, degree = 0, rotate = 0, nblack; |
534 | int rh, rw, i; |
535 | int wodd = (state->w % 2) ? 1 : 0; |
536 | int hodd = (state->h % 2) ? 1 : 0; |
537 | int xs[4], ys[4]; |
538 | |
539 | switch (params->symm) { |
540 | case SYMM_NONE: degree = 1; rotate = 0; break; |
541 | case SYMM_ROT2: degree = 2; rotate = 1; break; |
542 | case SYMM_REF2: degree = 2; rotate = 0; break; |
543 | case SYMM_ROT4: degree = 4; rotate = 1; break; |
544 | case SYMM_REF4: degree = 4; rotate = 0; break; |
545 | default: assert(!"Unknown symmetry type"); |
546 | } |
547 | if (params->symm == SYMM_ROT4 && (state->h != state->w)) |
548 | assert(!"4-fold symmetry unavailable without square grid"); |
549 | |
550 | if (degree == 4) { |
551 | rw = state->w/2; |
552 | rh = state->h/2; |
553 | if (!rotate) rw += wodd; /* ... but see below. */ |
554 | rh += hodd; |
555 | } else if (degree == 2) { |
556 | rw = state->w; |
557 | rh = state->h/2; |
558 | rh += hodd; |
559 | } else { |
560 | rw = state->w; |
561 | rh = state->h; |
562 | } |
563 | |
564 | /* clear, then randomise, required region. */ |
565 | clean_board(state, 0); |
566 | nblack = (rw * rh * params->blackpc) / 100; |
567 | for (i = 0; i < nblack; i++) { |
568 | do { |
569 | x = random_upto(rs,rw); |
570 | y = random_upto(rs,rh); |
571 | } while (GRID(state,flags,x,y) & F_BLACK); |
572 | GRID(state, flags, x, y) |= F_BLACK; |
573 | } |
574 | |
575 | /* Copy required region. */ |
576 | if (params->symm == SYMM_NONE) return; |
577 | |
578 | for (x = 0; x < rw; x++) { |
579 | for (y = 0; y < rh; y++) { |
580 | if (degree == 4) { |
581 | xs[0] = x; |
582 | ys[0] = y; |
583 | xs[1] = state->w - 1 - (rotate ? y : x); |
584 | ys[1] = rotate ? x : y; |
585 | xs[2] = rotate ? (state->w - 1 - x) : x; |
586 | ys[2] = state->h - 1 - y; |
587 | xs[3] = rotate ? y : (state->w - 1 - x); |
588 | ys[3] = state->h - 1 - (rotate ? x : y); |
589 | } else { |
590 | xs[0] = x; |
591 | ys[0] = y; |
592 | xs[1] = rotate ? (state->w - 1 - x) : x; |
593 | ys[1] = state->h - 1 - y; |
594 | } |
595 | for (i = 1; i < degree; i++) { |
596 | GRID(state, flags, xs[i], ys[i]) = |
597 | GRID(state, flags, xs[0], ys[0]); |
598 | } |
599 | } |
600 | } |
601 | /* SYMM_ROT4 misses the middle square above; fix that here. */ |
602 | if (degree == 4 && rotate && wodd && |
603 | (random_upto(rs,100) <= (unsigned int)params->blackpc)) |
604 | GRID(state,flags, |
605 | state->w/2 + wodd - 1, state->h/2 + hodd - 1) |= F_BLACK; |
606 | |
9b265feb |
607 | #ifdef SOLVER_DIAGNOSTICS |
608 | if (verbose) debug_state(state); |
e3478a4b |
609 | #endif |
610 | } |
611 | |
612 | /* Fills in (does not allocate) a ll_data with all the tiles that would |
613 | * be illuminated by a light at point (ox,oy). If origin=1 then the |
614 | * origin is included in this list. */ |
615 | static void list_lights(game_state *state, int ox, int oy, int origin, |
616 | ll_data *lld) |
617 | { |
618 | int x,y; |
619 | |
620 | memset(lld, 0, sizeof(lld)); |
621 | lld->ox = lld->minx = lld->maxx = ox; |
622 | lld->oy = lld->miny = lld->maxy = oy; |
623 | lld->include_origin = origin; |
624 | |
625 | y = oy; |
626 | for (x = ox-1; x >= 0; x--) { |
627 | if (GRID(state, flags, x, y) & F_BLACK) break; |
628 | if (x < lld->minx) lld->minx = x; |
629 | } |
630 | for (x = ox+1; x < state->w; x++) { |
631 | if (GRID(state, flags, x, y) & F_BLACK) break; |
632 | if (x > lld->maxx) lld->maxx = x; |
633 | } |
634 | |
635 | x = ox; |
636 | for (y = oy-1; y >= 0; y--) { |
637 | if (GRID(state, flags, x, y) & F_BLACK) break; |
638 | if (y < lld->miny) lld->miny = y; |
639 | } |
640 | for (y = oy+1; y < state->h; y++) { |
641 | if (GRID(state, flags, x, y) & F_BLACK) break; |
642 | if (y > lld->maxy) lld->maxy = y; |
643 | } |
644 | } |
645 | |
646 | /* Makes sure a light is the given state, editing the lights table to suit the |
647 | * new state if necessary. */ |
648 | static void set_light(game_state *state, int ox, int oy, int on) |
649 | { |
650 | ll_data lld; |
651 | int diff = 0; |
652 | |
653 | assert(!(GRID(state,flags,ox,oy) & F_BLACK)); |
654 | |
655 | if (!on && GRID(state,flags,ox,oy) & F_LIGHT) { |
656 | diff = -1; |
657 | GRID(state,flags,ox,oy) &= ~F_LIGHT; |
658 | state->nlights--; |
659 | } else if (on && !(GRID(state,flags,ox,oy) & F_LIGHT)) { |
660 | diff = 1; |
661 | GRID(state,flags,ox,oy) |= F_LIGHT; |
662 | state->nlights++; |
663 | } |
664 | |
665 | if (diff != 0) { |
666 | list_lights(state,ox,oy,1,&lld); |
667 | FOREACHLIT(&lld, GRID(state,lights,lx,ly) += diff; ); |
668 | } |
669 | } |
670 | |
671 | /* Returns 1 if removing a light at (x,y) would cause a square to go dark. */ |
672 | static int check_dark(game_state *state, int x, int y) |
673 | { |
674 | ll_data lld; |
675 | |
676 | list_lights(state, x, y, 1, &lld); |
677 | FOREACHLIT(&lld, if (GRID(state,lights,lx,ly) == 1) { return 1; } ); |
678 | return 0; |
679 | } |
680 | |
681 | /* Sets up an initial random correct position (i.e. every |
682 | * space lit, and no lights lit by other lights) by filling the |
683 | * grid with lights and then removing lights one by one at random. */ |
684 | static void place_lights(game_state *state, random_state *rs) |
685 | { |
686 | int i, x, y, n, *numindices, wh = state->w*state->h; |
687 | ll_data lld; |
688 | |
689 | numindices = snewn(wh, int); |
690 | for (i = 0; i < wh; i++) numindices[i] = i; |
691 | shuffle(numindices, wh, sizeof(*numindices), rs); |
692 | |
693 | /* Place a light on all grid squares without lights. */ |
694 | for (x = 0; x < state->w; x++) { |
695 | for (y = 0; y < state->h; y++) { |
696 | GRID(state, flags, x, y) &= ~F_MARK; /* we use this later. */ |
697 | if (GRID(state, flags, x, y) & F_BLACK) continue; |
698 | set_light(state, x, y, 1); |
699 | } |
700 | } |
701 | |
702 | for (i = 0; i < wh; i++) { |
703 | y = numindices[i] / state->w; |
704 | x = numindices[i] % state->w; |
705 | if (!(GRID(state, flags, x, y) & F_LIGHT)) continue; |
706 | if (GRID(state, flags, x, y) & F_MARK) continue; |
707 | list_lights(state, x, y, 0, &lld); |
708 | |
709 | /* If we're not lighting any lights ourself, don't remove anything. */ |
710 | n = 0; |
711 | FOREACHLIT(&lld, if (GRID(state,flags,lx,ly) & F_LIGHT) { n += 1; } ); |
9b265feb |
712 | if (n == 0) continue; /* [1] */ |
e3478a4b |
713 | |
714 | /* Check whether removing lights we're lighting would cause anything |
715 | * to go dark. */ |
716 | n = 0; |
717 | FOREACHLIT(&lld, if (GRID(state,flags,lx,ly) & F_LIGHT) { n += check_dark(state,lx,ly); } ); |
718 | if (n == 0) { |
719 | /* No, it wouldn't, so we can remove them all. */ |
720 | FOREACHLIT(&lld, set_light(state,lx,ly, 0); ); |
721 | GRID(state,flags,x,y) |= F_MARK; |
722 | } |
723 | |
724 | if (!grid_overlap(state)) { |
725 | sfree(numindices); |
726 | return; /* we're done. */ |
727 | } |
728 | assert(grid_lit(state)); |
729 | } |
9b265feb |
730 | /* could get here if the line at [1] continue'd out of the loop. */ |
731 | if (grid_overlap(state)) { |
732 | debug_state(state); |
733 | assert(!"place_lights failed to resolve overlapping lights!"); |
734 | } |
9a6d429a |
735 | sfree(numindices); |
e3478a4b |
736 | } |
737 | |
738 | /* Fills in all black squares with numbers of adjacent lights. */ |
739 | static void place_numbers(game_state *state) |
740 | { |
741 | int x, y, i, n; |
742 | surrounds s; |
743 | |
744 | for (x = 0; x < state->w; x++) { |
745 | for (y = 0; y < state->h; y++) { |
746 | if (!(GRID(state,flags,x,y) & F_BLACK)) continue; |
747 | get_surrounds(state, x, y, &s); |
748 | n = 0; |
749 | for (i = 0; i < s.npoints; i++) { |
750 | if (GRID(state,flags,s.points[i].x, s.points[i].y) & F_LIGHT) |
751 | n++; |
752 | } |
753 | GRID(state,flags,x,y) |= F_NUMBERED; |
754 | GRID(state,lights,x,y) = n; |
755 | } |
756 | } |
757 | } |
758 | |
759 | /* --- Actual solver, with helper subroutines. --- */ |
760 | |
761 | static void tsl_callback(game_state *state, |
762 | int lx, int ly, int *x, int *y, int *n) |
763 | { |
764 | if (GRID(state,flags,lx,ly) & F_IMPOSSIBLE) return; |
765 | if (GRID(state,lights,lx,ly) > 0) return; |
766 | *x = lx; *y = ly; (*n)++; |
767 | } |
768 | |
769 | static int try_solve_light(game_state *state, int ox, int oy, |
770 | unsigned int flags, int lights) |
771 | { |
772 | ll_data lld; |
f02e50a0 |
773 | int sx = 0, sy = 0, n = 0; |
e3478a4b |
774 | |
775 | if (lights > 0) return 0; |
776 | if (flags & F_BLACK) return 0; |
777 | |
778 | /* We have an unlit square; count how many ways there are left to |
779 | * place a light that lights us (including this square); if only |
780 | * one, we must put a light there. Squares that could light us |
781 | * are, of course, the same as the squares we would light... */ |
782 | list_lights(state, ox, oy, 1, &lld); |
783 | FOREACHLIT(&lld, { tsl_callback(state, lx, ly, &sx, &sy, &n); }); |
784 | if (n == 1) { |
785 | set_light(state, sx, sy, 1); |
9b265feb |
786 | #ifdef SOLVER_DIAGNOSTICS |
787 | debug(("(%d,%d) can only be lit from (%d,%d); setting to LIGHT\n", |
788 | ox,oy,sx,sy)); |
789 | if (verbose) debug_state(state); |
e3478a4b |
790 | #endif |
791 | return 1; |
792 | } |
793 | |
794 | return 0; |
795 | } |
796 | |
797 | static int could_place_light(unsigned int flags, int lights) |
798 | { |
799 | if (flags & (F_BLACK | F_IMPOSSIBLE)) return 0; |
800 | return (lights > 0) ? 0 : 1; |
801 | } |
802 | |
9b265feb |
803 | static int could_place_light_xy(game_state *state, int x, int y) |
804 | { |
805 | int lights = GRID(state,lights,x,y); |
806 | unsigned int flags = GRID(state,flags,x,y); |
807 | return (could_place_light(flags, lights)) ? 1 : 0; |
808 | } |
809 | |
e3478a4b |
810 | /* For a given number square, determine whether we have enough info |
811 | * to unambiguously place its lights. */ |
812 | static int try_solve_number(game_state *state, int nx, int ny, |
813 | unsigned int nflags, int nlights) |
814 | { |
815 | surrounds s; |
816 | int x, y, nl, ns, i, ret = 0, lights; |
817 | unsigned int flags; |
818 | |
819 | if (!(nflags & F_NUMBERED)) return 0; |
820 | nl = nlights; |
821 | get_surrounds(state,nx,ny,&s); |
822 | ns = s.npoints; |
823 | |
824 | /* nl is no. of lights we need to place, ns is no. of spaces we |
825 | * have to place them in. Try and narrow these down, and mark |
826 | * points we can ignore later. */ |
827 | for (i = 0; i < s.npoints; i++) { |
828 | x = s.points[i].x; y = s.points[i].y; |
829 | flags = GRID(state,flags,x,y); |
830 | lights = GRID(state,lights,x,y); |
831 | if (flags & F_LIGHT) { |
832 | /* light here already; one less light for one less place. */ |
833 | nl--; ns--; |
834 | s.points[i].f |= F_MARK; |
835 | } else if (!could_place_light(flags, lights)) { |
836 | ns--; |
837 | s.points[i].f |= F_MARK; |
838 | } |
839 | } |
840 | if (ns == 0) return 0; /* nowhere to put anything. */ |
841 | if (nl == 0) { |
842 | /* we have placed all lights we need to around here; all remaining |
843 | * surrounds are therefore IMPOSSIBLE. */ |
e3478a4b |
844 | GRID(state,flags,nx,ny) |= F_NUMBERUSED; |
845 | for (i = 0; i < s.npoints; i++) { |
846 | if (!(s.points[i].f & F_MARK)) { |
847 | GRID(state,flags,s.points[i].x,s.points[i].y) |= F_IMPOSSIBLE; |
848 | ret = 1; |
849 | } |
850 | } |
9b265feb |
851 | #ifdef SOLVER_DIAGNOSTICS |
852 | printf("Clue at (%d,%d) full; setting unlit to IMPOSSIBLE.\n", |
e3478a4b |
853 | nx,ny); |
9b265feb |
854 | if (verbose) debug_state(state); |
e3478a4b |
855 | #endif |
9b265feb |
856 | } else if (nl == ns) { |
857 | /* we have as many lights to place as spaces; fill them all. */ |
e3478a4b |
858 | GRID(state,flags,nx,ny) |= F_NUMBERUSED; |
859 | for (i = 0; i < s.npoints; i++) { |
860 | if (!(s.points[i].f & F_MARK)) { |
861 | set_light(state, s.points[i].x,s.points[i].y, 1); |
862 | ret = 1; |
863 | } |
864 | } |
9b265feb |
865 | #ifdef SOLVER_DIAGNOSTICS |
866 | printf("Clue at (%d,%d) trivial; setting unlit to LIGHT.\n", |
867 | nx,ny); |
868 | if (verbose) debug_state(state); |
869 | #endif |
e3478a4b |
870 | } |
871 | return ret; |
872 | } |
873 | |
9b265feb |
874 | struct setscratch { |
875 | int x, y; |
876 | int n; |
877 | }; |
878 | |
879 | #define SCRATCHSZ (state->w+state->h) |
880 | |
881 | /* New solver algorithm: overlapping sets can add IMPOSSIBLE flags. |
882 | * Algorithm thanks to Simon: |
883 | * |
884 | * (a) Any square where you can place a light has a set of squares |
885 | * which would become non-lights as a result. (This includes |
886 | * squares lit by the first square, and can also include squares |
887 | * adjacent to the same clue square if the new light is the last |
888 | * one around that clue.) Call this MAKESDARK(x,y) with (x,y) being |
889 | * the square you place a light. |
890 | |
891 | * (b) Any unlit square has a set of squares on which you could place |
892 | * a light to illuminate it. (Possibly including itself, of |
893 | * course.) This set of squares has the property that _at least |
894 | * one_ of them must contain a light. Sets of this type also arise |
895 | * from clue squares. Call this MAKESLIGHT(x,y), again with (x,y) |
896 | * the square you would place a light. |
897 | |
898 | * (c) If there exists (dx,dy) and (lx,ly) such that MAKESDARK(dx,dy) is |
899 | * a superset of MAKESLIGHT(lx,ly), this implies that placing a light at |
900 | * (dx,dy) would either leave no remaining way to illuminate a certain |
901 | * square, or would leave no remaining way to fulfill a certain clue |
902 | * (at lx,ly). In either case, a light can be ruled out at that position. |
903 | * |
904 | * So, we construct all possible MAKESLIGHT sets, both from unlit squares |
905 | * and clue squares, and then we look for plausible MAKESDARK sets that include |
906 | * our (lx,ly) to see if we can find a (dx,dy) to rule out. By the time we have |
907 | * constructed the MAKESLIGHT set we don't care about (lx,ly), just the set |
908 | * members. |
909 | * |
910 | * Once we have such a set, Simon came up with a Cunning Plan to find |
911 | * the most sensible MAKESDARK candidate: |
912 | * |
913 | * (a) for each square S in your set X, find all the squares which _would_ |
914 | * rule it out. That means any square which would light S, plus |
915 | * any square adjacent to the same clue square as S (provided |
916 | * that clue square has only one remaining light to be placed). |
917 | * It's not hard to make this list. Don't do anything with this |
918 | * data at the moment except _count_ the squares. |
919 | |
920 | * (b) Find the square S_min in the original set which has the |
921 | * _smallest_ number of other squares which would rule it out. |
922 | |
923 | * (c) Find all the squares that rule out S_min (it's probably |
924 | * better to recompute this than to have stored it during step |
925 | * (a), since the CPU requirement is modest but the storage |
926 | * cost would get ugly.) For each of these squares, see if it |
927 | * rules out everything else in the set X. Any which does can |
928 | * be marked as not-a-light. |
929 | * |
930 | */ |
931 | |
932 | typedef void (*trl_cb)(game_state *state, int dx, int dy, |
933 | struct setscratch *scratch, int n, void *ctx); |
934 | |
935 | static void try_rule_out(game_state *state, int x, int y, |
936 | struct setscratch *scratch, int n, |
937 | trl_cb cb, void *ctx); |
938 | |
939 | static void trl_callback_search(game_state *state, int dx, int dy, |
940 | struct setscratch *scratch, int n, void *ignored) |
941 | { |
942 | int i; |
943 | |
944 | #ifdef SOLVER_DIAGNOSTICS |
945 | if (verbose) debug(("discount cb: light at (%d,%d)\n", dx, dy)); |
946 | #endif |
947 | |
948 | for (i = 0; i < n; i++) { |
949 | if (dx == scratch[i].x && dy == scratch[i].y) { |
950 | scratch[i].n = 1; |
951 | return; |
952 | } |
953 | } |
954 | } |
955 | |
956 | static void trl_callback_discount(game_state *state, int dx, int dy, |
957 | struct setscratch *scratch, int n, void *ctx) |
958 | { |
959 | int *didsth = (int *)ctx; |
960 | int i; |
961 | |
962 | if (GRID(state,flags,dx,dy) & F_IMPOSSIBLE) { |
963 | #ifdef SOLVER_DIAGNOSTICS |
964 | debug(("Square at (%d,%d) already impossible.\n", dx,dy)); |
965 | #endif |
966 | return; |
967 | } |
968 | |
969 | /* Check whether a light at (dx,dy) rules out everything |
970 | * in scratch, and mark (dx,dy) as IMPOSSIBLE if it does. |
971 | * We can use try_rule_out for this as well, as the set of |
972 | * squares which would rule out (x,y) is the same as the |
973 | * set of squares which (x,y) would rule out. */ |
974 | |
975 | #ifdef SOLVER_DIAGNOSTICS |
976 | if (verbose) debug(("Checking whether light at (%d,%d) rules out everything in scratch.\n", dx, dy)); |
977 | #endif |
978 | |
979 | for (i = 0; i < n; i++) |
980 | scratch[i].n = 0; |
981 | try_rule_out(state, dx, dy, scratch, n, trl_callback_search, NULL); |
982 | for (i = 0; i < n; i++) { |
983 | if (scratch[i].n == 0) return; |
984 | } |
985 | /* The light ruled out everything in scratch. Yay. */ |
986 | GRID(state,flags,dx,dy) |= F_IMPOSSIBLE; |
987 | #ifdef SOLVER_DIAGNOSTICS |
988 | debug(("Set reduction discounted square at (%d,%d):\n", dx,dy)); |
989 | if (verbose) debug_state(state); |
990 | #endif |
991 | |
992 | *didsth = 1; |
993 | } |
994 | |
995 | static void trl_callback_incn(game_state *state, int dx, int dy, |
996 | struct setscratch *scratch, int n, void *ctx) |
997 | { |
998 | struct setscratch *s = (struct setscratch *)ctx; |
999 | s->n++; |
1000 | } |
1001 | |
1002 | static void try_rule_out(game_state *state, int x, int y, |
1003 | struct setscratch *scratch, int n, |
1004 | trl_cb cb, void *ctx) |
1005 | { |
1006 | /* XXX Find all the squares which would rule out (x,y); anything |
1007 | * that would light it as well as squares adjacent to same clues |
1008 | * as X assuming that clue only has one remaining light. |
1009 | * Call the callback with each square. */ |
1010 | ll_data lld; |
1011 | surrounds s, ss; |
1012 | int i, j, curr_lights, tot_lights; |
1013 | |
1014 | /* Find all squares that would rule out a light at (x,y) and call trl_cb |
1015 | * with them: anything that would light (x,y)... */ |
1016 | |
1017 | list_lights(state, x, y, 0, &lld); |
1018 | FOREACHLIT(&lld, { if (could_place_light_xy(state, lx, ly)) { cb(state, lx, ly, scratch, n, ctx); } }); |
1019 | |
1020 | /* ... as well as any empty space (that isn't x,y) next to any clue square |
1021 | * next to (x,y) that only has one light left to place. */ |
1022 | |
1023 | get_surrounds(state, x, y, &s); |
1024 | for (i = 0; i < s.npoints; i++) { |
6f745fb1 |
1025 | if (!(GRID(state,flags,s.points[i].x,s.points[i].y) & F_NUMBERED)) |
9b265feb |
1026 | continue; |
6f745fb1 |
1027 | /* we have an adjacent clue square; find /its/ surrounds |
9b265feb |
1028 | * and count the remaining lights it needs. */ |
1029 | get_surrounds(state,s.points[i].x,s.points[i].y,&ss); |
1030 | curr_lights = 0; |
1031 | for (j = 0; j < ss.npoints; j++) { |
1032 | if (GRID(state,flags,ss.points[j].x,ss.points[j].y) & F_LIGHT) |
1033 | curr_lights++; |
1034 | } |
1035 | tot_lights = GRID(state, lights, s.points[i].x, s.points[i].y); |
1036 | /* We have a clue with tot_lights to fill, and curr_lights currently |
1037 | * around it. If adding a light at (x,y) fills up the clue (i.e. |
1038 | * curr_lights + 1 = tot_lights) then we need to discount all other |
1039 | * unlit squares around the clue. */ |
1040 | if ((curr_lights + 1) == tot_lights) { |
1041 | for (j = 0; j < ss.npoints; j++) { |
1042 | int lx = ss.points[j].x, ly = ss.points[j].y; |
1043 | if (lx == x && ly == y) continue; |
1044 | if (could_place_light_xy(state, lx, ly)) |
1045 | cb(state, lx, ly, scratch, n, ctx); |
1046 | } |
1047 | } |
1048 | } |
1049 | } |
1050 | |
1051 | #ifdef SOLVER_DIAGNOSTICS |
1052 | static void debug_scratch(const char *msg, struct setscratch *scratch, int n) |
1053 | { |
1054 | int i; |
1055 | debug(("%s scratch (%d elements):\n", msg, n)); |
1056 | for (i = 0; i < n; i++) { |
1057 | debug((" (%d,%d) n%d\n", scratch[i].x, scratch[i].y, scratch[i].n)); |
1058 | } |
1059 | } |
1060 | #endif |
1061 | |
1062 | static int discount_set(game_state *state, |
1063 | struct setscratch *scratch, int n) |
1064 | { |
1065 | int i, besti, bestn, didsth = 0; |
1066 | |
1067 | #ifdef SOLVER_DIAGNOSTICS |
1068 | if (verbose > 1) debug_scratch("discount_set", scratch, n); |
1069 | #endif |
1070 | if (n == 0) return 0; |
1071 | |
1072 | for (i = 0; i < n; i++) { |
1073 | try_rule_out(state, scratch[i].x, scratch[i].y, scratch, n, |
1074 | trl_callback_incn, (void*)&(scratch[i])); |
1075 | } |
1076 | #ifdef SOLVER_DIAGNOSTICS |
1077 | if (verbose > 1) debug_scratch("discount_set after count", scratch, n); |
1078 | #endif |
1079 | |
1080 | besti = -1; bestn = SCRATCHSZ; |
1081 | for (i = 0; i < n; i++) { |
1082 | if (scratch[i].n < bestn) { |
1083 | bestn = scratch[i].n; |
1084 | besti = i; |
1085 | } |
1086 | } |
1087 | #ifdef SOLVER_DIAGNOSTICS |
1088 | if (verbose > 1) debug(("best square (%d,%d) with n%d.\n", |
1089 | scratch[besti].x, scratch[besti].y, scratch[besti].n)); |
1090 | #endif |
1091 | try_rule_out(state, scratch[besti].x, scratch[besti].y, scratch, n, |
1092 | trl_callback_discount, (void*)&didsth); |
1093 | #ifdef SOLVER_DIAGNOSTICS |
1094 | if (didsth) debug((" [from square (%d,%d)]\n", |
1095 | scratch[besti].x, scratch[besti].y)); |
1096 | #endif |
1097 | |
1098 | return didsth; |
1099 | } |
1100 | |
1101 | static void discount_clear(game_state *state, struct setscratch *scratch, int *n) |
1102 | { |
1103 | *n = 0; |
1104 | memset(scratch, 0, SCRATCHSZ * sizeof(struct setscratch)); |
1105 | } |
1106 | |
1107 | static void unlit_cb(game_state *state, int lx, int ly, |
1108 | struct setscratch *scratch, int *n) |
1109 | { |
1110 | if (could_place_light_xy(state, lx, ly)) { |
1111 | scratch[*n].x = lx; scratch[*n].y = ly; (*n)++; |
1112 | } |
1113 | } |
1114 | |
1115 | /* Construct a MAKESLIGHT set from an unlit square. */ |
1116 | static int discount_unlit(game_state *state, int x, int y, |
1117 | struct setscratch *scratch) |
1118 | { |
1119 | ll_data lld; |
1120 | int n, didsth; |
1121 | |
1122 | #ifdef SOLVER_DIAGNOSTICS |
1123 | if (verbose) debug(("Trying to discount for unlit square at (%d,%d).\n", x, y)); |
1124 | if (verbose > 1) debug_state(state); |
1125 | #endif |
1126 | |
1127 | discount_clear(state, scratch, &n); |
1128 | |
1129 | list_lights(state, x, y, 1, &lld); |
1130 | FOREACHLIT(&lld, { unlit_cb(state, lx, ly, scratch, &n); }); |
1131 | didsth = discount_set(state, scratch, n); |
1132 | #ifdef SOLVER_DIAGNOSTICS |
1133 | if (didsth) debug((" [from unlit square at (%d,%d)].\n", x, y)); |
1134 | #endif |
1135 | return didsth; |
1136 | |
1137 | } |
1138 | |
1139 | /* Construct a series of MAKESLIGHT sets from a clue square. |
1140 | * for a clue square with N remaining spaces that must contain M lights, every |
1141 | * subset of size N-M+1 of those N spaces forms such a set. |
1142 | */ |
1143 | |
1144 | static int discount_clue(game_state *state, int x, int y, |
1145 | struct setscratch *scratch) |
1146 | { |
1147 | int slen, m = GRID(state, lights, x, y), n, i, didsth = 0, lights; |
1148 | unsigned int flags; |
1149 | surrounds s, sempty; |
1150 | combi_ctx *combi; |
1151 | |
1152 | if (m == 0) return 0; |
1153 | |
1154 | #ifdef SOLVER_DIAGNOSTICS |
1155 | if (verbose) debug(("Trying to discount for sets at clue (%d,%d).\n", x, y)); |
1156 | if (verbose > 1) debug_state(state); |
1157 | #endif |
1158 | |
1159 | /* m is no. of lights still to place; starts off at the clue value |
1160 | * and decreases when we find a light already down. |
1161 | * n is no. of spaces left; starts off at 0 and goes up when we find |
1162 | * a plausible space. */ |
1163 | |
1164 | get_surrounds(state, x, y, &s); |
1165 | memset(&sempty, 0, sizeof(surrounds)); |
1166 | for (i = 0; i < s.npoints; i++) { |
1167 | int lx = s.points[i].x, ly = s.points[i].y; |
1168 | flags = GRID(state,flags,lx,ly); |
1169 | lights = GRID(state,lights,lx,ly); |
1170 | |
1171 | if (flags & F_LIGHT) m--; |
1172 | |
1173 | if (could_place_light(flags, lights)) { |
1174 | sempty.points[sempty.npoints].x = lx; |
1175 | sempty.points[sempty.npoints].y = ly; |
1176 | sempty.npoints++; |
1177 | } |
1178 | } |
1179 | n = sempty.npoints; /* sempty is now a surrounds of only blank squares. */ |
1180 | if (n == 0) return 0; /* clue is full already. */ |
1181 | |
1182 | if (m < 0 || m > n) return 0; /* become impossible. */ |
1183 | |
1184 | combi = new_combi(n - m + 1, n); |
1185 | while (next_combi(combi)) { |
1186 | discount_clear(state, scratch, &slen); |
1187 | for (i = 0; i < combi->r; i++) { |
1188 | scratch[slen].x = sempty.points[combi->a[i]].x; |
1189 | scratch[slen].y = sempty.points[combi->a[i]].y; |
1190 | slen++; |
1191 | } |
1192 | if (discount_set(state, scratch, slen)) didsth = 1; |
1193 | } |
1194 | free_combi(combi); |
1195 | #ifdef SOLVER_DIAGNOSTICS |
1196 | if (didsth) debug((" [from clue at (%d,%d)].\n", x, y)); |
1197 | #endif |
1198 | return didsth; |
1199 | } |
1200 | |
1201 | #define F_SOLVE_FORCEUNIQUE 1 |
1202 | #define F_SOLVE_DISCOUNTSETS 2 |
1203 | #define F_SOLVE_ALLOWRECURSE 4 |
1204 | |
1205 | static unsigned int flags_from_difficulty(int difficulty) |
1206 | { |
1207 | unsigned int sflags = F_SOLVE_FORCEUNIQUE; |
1208 | assert(difficulty <= DIFFCOUNT); |
1209 | if (difficulty >= 1) sflags |= F_SOLVE_DISCOUNTSETS; |
1210 | if (difficulty >= 2) sflags |= F_SOLVE_ALLOWRECURSE; |
1211 | return sflags; |
1212 | } |
1213 | |
1214 | #define MAXRECURSE 5 |
1215 | |
e3478a4b |
1216 | static int solve_sub(game_state *state, |
9b265feb |
1217 | unsigned int solve_flags, int depth, |
e3478a4b |
1218 | int *maxdepth) |
1219 | { |
1220 | unsigned int flags; |
1221 | int x, y, didstuff, ncanplace, lights; |
9b265feb |
1222 | int bestx, besty, n, bestn, copy_soluble, self_soluble, ret, maxrecurse = 0; |
e3478a4b |
1223 | game_state *scopy; |
1224 | ll_data lld; |
9b265feb |
1225 | struct setscratch *sscratch = NULL; |
e3478a4b |
1226 | |
9b265feb |
1227 | #ifdef SOLVER_DIAGNOSTICS |
e3478a4b |
1228 | printf("solve_sub: depth = %d\n", depth); |
1229 | #endif |
1230 | if (maxdepth && *maxdepth < depth) *maxdepth = depth; |
9b265feb |
1231 | if (solve_flags & F_SOLVE_ALLOWRECURSE) maxrecurse = MAXRECURSE; |
e3478a4b |
1232 | |
1233 | while (1) { |
1234 | if (grid_overlap(state)) { |
1235 | /* Our own solver, from scratch, should never cause this to happen |
1236 | * (assuming a soluble grid). However, if we're trying to solve |
1237 | * from a half-completed *incorrect* grid this might occur; we |
1238 | * just return the 'no solutions' code in this case. */ |
9b265feb |
1239 | ret = 0; goto done; |
e3478a4b |
1240 | } |
1241 | |
9b265feb |
1242 | if (grid_correct(state)) { ret = 1; goto done; } |
e3478a4b |
1243 | |
1244 | ncanplace = 0; |
1245 | didstuff = 0; |
1246 | /* These 2 loops, and the functions they call, are the critical loops |
1247 | * for timing; any optimisations should look here first. */ |
1248 | for (x = 0; x < state->w; x++) { |
1249 | for (y = 0; y < state->h; y++) { |
1250 | flags = GRID(state,flags,x,y); |
1251 | lights = GRID(state,lights,x,y); |
1252 | ncanplace += could_place_light(flags, lights); |
1253 | |
1254 | if (try_solve_light(state, x, y, flags, lights)) didstuff = 1; |
1255 | if (try_solve_number(state, x, y, flags, lights)) didstuff = 1; |
1256 | } |
1257 | } |
1258 | if (didstuff) continue; |
9b265feb |
1259 | if (!ncanplace) { |
1260 | /* nowhere to put a light, puzzle is unsoluble. */ |
1261 | ret = 0; goto done; |
1262 | } |
1263 | |
1264 | if (solve_flags & F_SOLVE_DISCOUNTSETS) { |
1265 | if (!sscratch) sscratch = snewn(SCRATCHSZ, struct setscratch); |
1266 | /* Try a more cunning (and more involved) way... more details above. */ |
1267 | for (x = 0; x < state->w; x++) { |
1268 | for (y = 0; y < state->h; y++) { |
1269 | flags = GRID(state,flags,x,y); |
1270 | lights = GRID(state,lights,x,y); |
1271 | |
1272 | if (!(flags & F_BLACK) && lights == 0) { |
1273 | if (discount_unlit(state, x, y, sscratch)) { |
1274 | didstuff = 1; |
1275 | goto reduction_success; |
1276 | } |
1277 | } else if (flags & F_NUMBERED) { |
1278 | if (discount_clue(state, x, y, sscratch)) { |
1279 | didstuff = 1; |
1280 | goto reduction_success; |
1281 | } |
1282 | } |
1283 | } |
1284 | } |
1285 | } |
1286 | reduction_success: |
1287 | if (didstuff) continue; |
e3478a4b |
1288 | |
1289 | /* We now have to make a guess; we have places to put lights but |
1290 | * no definite idea about where they can go. */ |
9b265feb |
1291 | if (depth >= maxrecurse) { |
1292 | /* mustn't delve any deeper. */ |
1293 | ret = -1; goto done; |
1294 | } |
e3478a4b |
1295 | /* Of all the squares that we could place a light, pick the one |
1296 | * that would light the most currently unlit squares. */ |
1297 | /* This heuristic was just plucked from the air; there may well be |
1298 | * a more efficient way of choosing a square to flip to minimise |
1299 | * recursion. */ |
1300 | bestn = 0; |
1301 | bestx = besty = -1; /* suyb */ |
1302 | for (x = 0; x < state->w; x++) { |
1303 | for (y = 0; y < state->h; y++) { |
1304 | flags = GRID(state,flags,x,y); |
1305 | lights = GRID(state,lights,x,y); |
1306 | if (!could_place_light(flags, lights)) continue; |
1307 | |
1308 | n = 0; |
1309 | list_lights(state, x, y, 1, &lld); |
1310 | FOREACHLIT(&lld, { if (GRID(state,lights,lx,ly) == 0) n++; }); |
1311 | if (n > bestn) { |
1312 | bestn = n; bestx = x; besty = y; |
1313 | } |
1314 | } |
1315 | } |
1316 | assert(bestn > 0); |
1317 | assert(bestx >= 0 && besty >= 0); |
1318 | |
1319 | /* Now we've chosen a plausible (x,y), try to solve it once as 'lit' |
1320 | * and once as 'impossible'; we need to make one copy to do this. */ |
1321 | |
1322 | scopy = dup_game(state); |
9b265feb |
1323 | #ifdef SOLVER_DIAGNOSTICS |
1324 | debug(("Recursing #1: trying (%d,%d) as IMPOSSIBLE\n", bestx, besty)); |
1325 | #endif |
e3478a4b |
1326 | GRID(state,flags,bestx,besty) |= F_IMPOSSIBLE; |
9b265feb |
1327 | self_soluble = solve_sub(state, solve_flags, depth+1, maxdepth); |
e3478a4b |
1328 | |
9b265feb |
1329 | if (!(solve_flags & F_SOLVE_FORCEUNIQUE) && self_soluble > 0) { |
e3478a4b |
1330 | /* we didn't care about finding all solutions, and we just |
1331 | * found one; return with it immediately. */ |
1332 | free_game(scopy); |
9b265feb |
1333 | ret = self_soluble; |
1334 | goto done; |
e3478a4b |
1335 | } |
1336 | |
9b265feb |
1337 | #ifdef SOLVER_DIAGNOSTICS |
1338 | debug(("Recursing #2: trying (%d,%d) as LIGHT\n", bestx, besty)); |
1339 | #endif |
e3478a4b |
1340 | set_light(scopy, bestx, besty, 1); |
9b265feb |
1341 | copy_soluble = solve_sub(scopy, solve_flags, depth+1, maxdepth); |
e3478a4b |
1342 | |
1343 | /* If we wanted a unique solution but we hit our recursion limit |
1344 | * (on either branch) then we have to assume we didn't find possible |
1345 | * extra solutions, and return 'not soluble'. */ |
9b265feb |
1346 | if ((solve_flags & F_SOLVE_FORCEUNIQUE) && |
e3478a4b |
1347 | ((copy_soluble < 0) || (self_soluble < 0))) { |
1348 | ret = -1; |
1349 | /* Make sure that whether or not it was self or copy (or both) that |
1350 | * were soluble, that we return a solved state in self. */ |
1351 | } else if (copy_soluble <= 0) { |
1352 | /* copy wasn't soluble; keep self state and return that result. */ |
1353 | ret = self_soluble; |
1354 | } else if (self_soluble <= 0) { |
1355 | /* copy solved and we didn't, so copy in copy's (now solved) |
1356 | * flags and light state. */ |
1357 | memcpy(state->lights, scopy->lights, |
1358 | scopy->w * scopy->h * sizeof(int)); |
1359 | memcpy(state->flags, scopy->flags, |
1360 | scopy->w * scopy->h * sizeof(unsigned int)); |
1361 | ret = copy_soluble; |
1362 | } else { |
1363 | ret = copy_soluble + self_soluble; |
1364 | } |
1365 | free_game(scopy); |
9b265feb |
1366 | goto done; |
e3478a4b |
1367 | } |
9b265feb |
1368 | done: |
1369 | if (sscratch) sfree(sscratch); |
1370 | #ifdef SOLVER_DIAGNOSTICS |
1371 | if (ret < 0) |
1372 | debug(("solve_sub: depth = %d returning, ran out of recursion.\n", |
1373 | depth)); |
1374 | else |
1375 | debug(("solve_sub: depth = %d returning, %d solutions.\n", |
1376 | depth, ret)); |
1377 | #endif |
1378 | return ret; |
e3478a4b |
1379 | } |
1380 | |
e3478a4b |
1381 | /* Fills in the (possibly partially-complete) game_state as far as it can, |
1382 | * returning the number of possible solutions. If it returns >0 then the |
1383 | * game_state will be in a solved state, but you won't know which one. */ |
9b265feb |
1384 | static int dosolve(game_state *state, int solve_flags, int *maxdepth) |
e3478a4b |
1385 | { |
1386 | int x, y, nsol; |
1387 | |
1388 | for (x = 0; x < state->w; x++) { |
1389 | for (y = 0; y < state->h; y++) { |
1390 | GRID(state,flags,x,y) &= ~F_NUMBERUSED; |
1391 | } |
1392 | } |
9b265feb |
1393 | nsol = solve_sub(state, solve_flags, 0, maxdepth); |
e3478a4b |
1394 | return nsol; |
1395 | } |
1396 | |
1397 | static int strip_unused_nums(game_state *state) |
1398 | { |
1399 | int x,y,n=0; |
1400 | for (x = 0; x < state->w; x++) { |
1401 | for (y = 0; y < state->h; y++) { |
1402 | if ((GRID(state,flags,x,y) & F_NUMBERED) && |
1403 | !(GRID(state,flags,x,y) & F_NUMBERUSED)) { |
1404 | GRID(state,flags,x,y) &= ~F_NUMBERED; |
1405 | GRID(state,lights,x,y) = 0; |
1406 | n++; |
1407 | } |
1408 | } |
1409 | } |
72c15821 |
1410 | debug(("Stripped %d unused numbers.\n", n)); |
e3478a4b |
1411 | return n; |
1412 | } |
1413 | |
1414 | static void unplace_lights(game_state *state) |
1415 | { |
1416 | int x,y; |
1417 | for (x = 0; x < state->w; x++) { |
1418 | for (y = 0; y < state->h; y++) { |
1419 | if (GRID(state,flags,x,y) & F_LIGHT) |
1420 | set_light(state,x,y,0); |
1421 | GRID(state,flags,x,y) &= ~F_IMPOSSIBLE; |
1422 | GRID(state,flags,x,y) &= ~F_NUMBERUSED; |
1423 | } |
1424 | } |
1425 | } |
1426 | |
9b265feb |
1427 | static int puzzle_is_good(game_state *state, int difficulty) |
e3478a4b |
1428 | { |
9b265feb |
1429 | int nsol, mdepth = 0; |
1430 | unsigned int sflags = flags_from_difficulty(difficulty); |
e3478a4b |
1431 | |
e3478a4b |
1432 | unplace_lights(state); |
1433 | |
9b265feb |
1434 | #ifdef SOLVER_DIAGNOSTICS |
1435 | debug(("Trying to solve with difficulty %d (0x%x):\n", |
1436 | difficulty, sflags)); |
1437 | if (verbose) debug_state(state); |
e3478a4b |
1438 | #endif |
1439 | |
9b265feb |
1440 | nsol = dosolve(state, sflags, &mdepth); |
e3478a4b |
1441 | /* if we wanted an easy puzzle, make sure we didn't need recursion. */ |
9b265feb |
1442 | if (!(sflags & F_SOLVE_ALLOWRECURSE) && mdepth > 0) { |
1443 | debug(("Ignoring recursive puzzle.\n")); |
e3478a4b |
1444 | return 0; |
1445 | } |
1446 | |
9b265feb |
1447 | debug(("%d solutions found.\n", nsol)); |
e3478a4b |
1448 | if (nsol <= 0) return 0; |
1449 | if (nsol > 1) return 0; |
1450 | return 1; |
1451 | } |
1452 | |
1453 | /* --- New game creation and user input code. --- */ |
1454 | |
1455 | /* The basic algorithm here is to generate the most complex grid possible |
1456 | * while honouring two restrictions: |
1457 | * |
1458 | * * we require a unique solution, and |
1459 | * * either we require solubility with no recursion (!params->recurse) |
1460 | * * or we require some recursion. (params->recurse). |
1461 | * |
1462 | * The solver helpfully keeps track of the numbers it needed to use to |
1463 | * get its solution, so we use that to remove an initial set of numbers |
1464 | * and check we still satsify our requirements (on uniqueness and |
1465 | * non-recursiveness, if applicable; we don't check explicit recursiveness |
1466 | * until the end). |
1467 | * |
1468 | * Then we try to remove all numbers in a random order, and see if we |
1469 | * still satisfy requirements (putting them back if we didn't). |
1470 | * |
1471 | * Removing numbers will always, in general terms, make a puzzle require |
1472 | * more recursion but it may also mean a puzzle becomes non-unique. |
1473 | * |
1474 | * Once we're done, if we wanted a recursive puzzle but the most difficult |
1475 | * puzzle we could come up with was non-recursive, we give up and try a new |
1476 | * grid. */ |
1477 | |
e3478a4b |
1478 | #define MAX_GRIDGEN_TRIES 20 |
e3478a4b |
1479 | |
1480 | static char *new_game_desc(game_params *params, random_state *rs, |
1481 | char **aux, int interactive) |
1482 | { |
1483 | game_state *news = new_state(params), *copys; |
72c15821 |
1484 | int i, j, run, x, y, wh = params->w*params->h, num; |
e3478a4b |
1485 | char *ret, *p; |
1486 | int *numindices; |
1487 | |
1488 | /* Construct a shuffled list of grid positions; we only |
1489 | * do this once, because if it gets used more than once it'll |
1490 | * be on a different grid layout. */ |
1491 | numindices = snewn(wh, int); |
9b265feb |
1492 | for (j = 0; j < wh; j++) numindices[j] = j; |
e3478a4b |
1493 | shuffle(numindices, wh, sizeof(*numindices), rs); |
1494 | |
1495 | while (1) { |
1496 | for (i = 0; i < MAX_GRIDGEN_TRIES; i++) { |
1497 | set_blacks(news, params, rs); /* also cleans board. */ |
1498 | |
1499 | /* set up lights and then the numbers, and remove the lights */ |
1500 | place_lights(news, rs); |
1501 | debug(("Generating initial grid.\n")); |
1502 | place_numbers(news); |
9b265feb |
1503 | if (!puzzle_is_good(news, params->difficulty)) continue; |
e3478a4b |
1504 | |
1505 | /* Take a copy, remove numbers we didn't use and check there's |
1506 | * still a unique solution; if so, use the copy subsequently. */ |
1507 | copys = dup_game(news); |
72c15821 |
1508 | strip_unused_nums(copys); |
9b265feb |
1509 | if (!puzzle_is_good(copys, params->difficulty)) { |
e3478a4b |
1510 | debug(("Stripped grid is not good, reverting.\n")); |
1511 | free_game(copys); |
1512 | } else { |
1513 | free_game(news); |
1514 | news = copys; |
1515 | } |
1516 | |
1517 | /* Go through grid removing numbers at random one-by-one and |
1518 | * trying to solve again; if it ceases to be good put the number back. */ |
9b265feb |
1519 | for (j = 0; j < wh; j++) { |
1520 | y = numindices[j] / params->w; |
1521 | x = numindices[j] % params->w; |
e3478a4b |
1522 | if (!(GRID(news, flags, x, y) & F_NUMBERED)) continue; |
1523 | num = GRID(news, lights, x, y); |
1524 | GRID(news, lights, x, y) = 0; |
1525 | GRID(news, flags, x, y) &= ~F_NUMBERED; |
9b265feb |
1526 | if (!puzzle_is_good(news, params->difficulty)) { |
e3478a4b |
1527 | GRID(news, lights, x, y) = num; |
1528 | GRID(news, flags, x, y) |= F_NUMBERED; |
1529 | } else |
1530 | debug(("Removed (%d,%d) still soluble.\n", x, y)); |
1531 | } |
9b265feb |
1532 | if (params->difficulty > 0) { |
1533 | /* Was the maximally-difficult puzzle difficult enough? |
1534 | * Check we can't solve it with a more simplistic solver. */ |
1535 | if (puzzle_is_good(news, params->difficulty-1)) { |
1536 | debug(("Maximally-hard puzzle still not hard enough, skipping.\n")); |
1537 | continue; |
1538 | } |
e3478a4b |
1539 | } |
1540 | |
1541 | goto goodpuzzle; |
1542 | } |
1543 | /* Couldn't generate a good puzzle in however many goes. Ramp up the |
1544 | * %age of black squares (if we didn't already have lots; in which case |
1545 | * why couldn't we generate a puzzle?) and try again. */ |
1546 | if (params->blackpc < 90) params->blackpc += 5; |
9b265feb |
1547 | debug(("New black layout %d%%.\n", params->blackpc)); |
e3478a4b |
1548 | } |
1549 | goodpuzzle: |
1550 | /* Game is encoded as a long string one character per square; |
1551 | * 'S' is a space |
1552 | * 'B' is a black square with no number |
1553 | * '0', '1', '2', '3', '4' is a black square with a number. */ |
1554 | ret = snewn((params->w * params->h) + 1, char); |
1555 | p = ret; |
1556 | run = 0; |
1557 | for (y = 0; y < params->h; y++) { |
1558 | for (x = 0; x < params->w; x++) { |
1559 | if (GRID(news,flags,x,y) & F_BLACK) { |
1560 | if (run) { |
1561 | *p++ = ('a'-1) + run; |
1562 | run = 0; |
1563 | } |
1564 | if (GRID(news,flags,x,y) & F_NUMBERED) |
1565 | *p++ = '0' + GRID(news,lights,x,y); |
1566 | else |
1567 | *p++ = 'B'; |
1568 | } else { |
1569 | if (run == 26) { |
1570 | *p++ = ('a'-1) + run; |
1571 | run = 0; |
1572 | } |
1573 | run++; |
1574 | } |
1575 | } |
1576 | } |
1577 | if (run) { |
1578 | *p++ = ('a'-1) + run; |
1579 | run = 0; |
1580 | } |
1581 | *p = '\0'; |
1582 | assert(p - ret <= params->w * params->h); |
1583 | free_game(news); |
1584 | sfree(numindices); |
1585 | |
1586 | return ret; |
1587 | } |
1588 | |
1589 | static char *validate_desc(game_params *params, char *desc) |
1590 | { |
1591 | int i; |
1592 | for (i = 0; i < params->w*params->h; i++) { |
1593 | if (*desc >= '0' && *desc <= '4') |
1594 | /* OK */; |
1595 | else if (*desc == 'B') |
1596 | /* OK */; |
1597 | else if (*desc >= 'a' && *desc <= 'z') |
1598 | i += *desc - 'a'; /* and the i++ will add another one */ |
1599 | else if (!*desc) |
1600 | return "Game description shorter than expected"; |
1601 | else |
1602 | return "Game description contained unexpected character"; |
1603 | desc++; |
1604 | } |
1605 | if (*desc || i > params->w*params->h) |
1606 | return "Game description longer than expected"; |
1607 | |
1608 | return NULL; |
1609 | } |
1610 | |
dafd6cf6 |
1611 | static game_state *new_game(midend *me, game_params *params, char *desc) |
e3478a4b |
1612 | { |
1613 | game_state *ret = new_state(params); |
1614 | int x,y; |
1615 | int run = 0; |
1616 | |
1617 | for (y = 0; y < params->h; y++) { |
1618 | for (x = 0; x < params->w; x++) { |
1619 | char c = '\0'; |
1620 | |
1621 | if (run == 0) { |
1622 | c = *desc++; |
1623 | assert(c != 'S'); |
1624 | if (c >= 'a' && c <= 'z') |
1625 | run = c - 'a' + 1; |
1626 | } |
1627 | |
1628 | if (run > 0) { |
1629 | c = 'S'; |
1630 | run--; |
1631 | } |
1632 | |
1633 | switch (c) { |
1634 | case '0': case '1': case '2': case '3': case '4': |
1635 | GRID(ret,flags,x,y) |= F_NUMBERED; |
1636 | GRID(ret,lights,x,y) = (c - '0'); |
1637 | /* run-on... */ |
1638 | |
1639 | case 'B': |
1640 | GRID(ret,flags,x,y) |= F_BLACK; |
1641 | break; |
1642 | |
1643 | case 'S': |
1644 | /* empty square */ |
1645 | break; |
1646 | |
1647 | default: |
1648 | assert(!"Malformed desc."); |
1649 | break; |
1650 | } |
1651 | } |
1652 | } |
1653 | if (*desc) assert(!"Over-long desc."); |
1654 | |
1655 | return ret; |
1656 | } |
1657 | |
1658 | static char *solve_game(game_state *state, game_state *currstate, |
1659 | char *aux, char **error) |
1660 | { |
1661 | game_state *solved; |
1662 | char *move = NULL, buf[80]; |
1663 | int movelen, movesize, x, y, len; |
9b265feb |
1664 | unsigned int oldflags, solvedflags, sflags; |
e3478a4b |
1665 | |
1666 | /* We don't care here about non-unique puzzles; if the |
1667 | * user entered one themself then I doubt they care. */ |
1668 | |
9b265feb |
1669 | sflags = F_SOLVE_ALLOWRECURSE | F_SOLVE_DISCOUNTSETS; |
1670 | |
e3478a4b |
1671 | /* Try and solve from where we are now (for non-unique |
1672 | * puzzles this may produce a different answer). */ |
1673 | solved = dup_game(currstate); |
9b265feb |
1674 | if (dosolve(solved, sflags, NULL) > 0) goto solved; |
e3478a4b |
1675 | free_game(solved); |
1676 | |
1677 | /* That didn't work; try solving from the clean puzzle. */ |
1678 | solved = dup_game(state); |
9b265feb |
1679 | if (dosolve(solved, sflags, NULL) > 0) goto solved; |
522cc971 |
1680 | *error = "Unable to find a solution to this puzzle."; |
e3478a4b |
1681 | goto done; |
1682 | |
1683 | solved: |
1684 | movesize = 256; |
1685 | move = snewn(movesize, char); |
1686 | movelen = 0; |
1687 | move[movelen++] = 'S'; |
1688 | move[movelen] = '\0'; |
1689 | for (x = 0; x < currstate->w; x++) { |
1690 | for (y = 0; y < currstate->h; y++) { |
1691 | len = 0; |
1692 | oldflags = GRID(currstate, flags, x, y); |
1693 | solvedflags = GRID(solved, flags, x, y); |
1694 | if ((oldflags & F_LIGHT) != (solvedflags & F_LIGHT)) |
1695 | len = sprintf(buf, ";L%d,%d", x, y); |
1696 | else if ((oldflags & F_IMPOSSIBLE) != (solvedflags & F_IMPOSSIBLE)) |
1697 | len = sprintf(buf, ";I%d,%d", x, y); |
1698 | if (len) { |
1699 | if (movelen + len >= movesize) { |
1700 | movesize = movelen + len + 256; |
1701 | move = sresize(move, movesize, char); |
1702 | } |
1703 | strcpy(move + movelen, buf); |
1704 | movelen += len; |
1705 | } |
1706 | } |
1707 | } |
1708 | |
1709 | done: |
1710 | free_game(solved); |
1711 | return move; |
1712 | } |
1713 | |
fa3abef5 |
1714 | static int game_can_format_as_text_now(game_params *params) |
1715 | { |
1716 | return TRUE; |
1717 | } |
1718 | |
e3478a4b |
1719 | /* 'borrowed' from slant.c, mainly. I could have printed it one |
1720 | * character per cell (like debug_state) but that comes out tiny. |
1721 | * 'L' is used for 'light here' because 'O' looks too much like '0' |
1722 | * (black square with no surrounding lights). */ |
1723 | static char *game_text_format(game_state *state) |
1724 | { |
1725 | int w = state->w, h = state->h, W = w+1, H = h+1; |
1726 | int x, y, len, lights; |
1727 | unsigned int flags; |
1728 | char *ret, *p; |
1729 | |
1730 | len = (h+H) * (w+W+1) + 1; |
1731 | ret = snewn(len, char); |
1732 | p = ret; |
1733 | |
1734 | for (y = 0; y < H; y++) { |
1735 | for (x = 0; x < W; x++) { |
1736 | *p++ = '+'; |
1737 | if (x < w) |
1738 | *p++ = '-'; |
1739 | } |
1740 | *p++ = '\n'; |
1741 | if (y < h) { |
1742 | for (x = 0; x < W; x++) { |
1743 | *p++ = '|'; |
1744 | if (x < w) { |
1745 | /* actual interesting bit. */ |
1746 | flags = GRID(state, flags, x, y); |
1747 | lights = GRID(state, lights, x, y); |
1748 | if (flags & F_BLACK) { |
1749 | if (flags & F_NUMBERED) |
1750 | *p++ = '0' + lights; |
1751 | else |
1752 | *p++ = '#'; |
1753 | } else { |
1754 | if (flags & F_LIGHT) |
1755 | *p++ = 'L'; |
1756 | else if (flags & F_IMPOSSIBLE) |
1757 | *p++ = 'x'; |
1758 | else if (lights > 0) |
1759 | *p++ = '.'; |
1760 | else |
1761 | *p++ = ' '; |
1762 | } |
1763 | } |
1764 | } |
1765 | *p++ = '\n'; |
1766 | } |
1767 | } |
1768 | *p++ = '\0'; |
1769 | |
1770 | assert(p - ret == len); |
1771 | return ret; |
1772 | } |
1773 | |
1774 | struct game_ui { |
1775 | int cur_x, cur_y, cur_visible; |
1776 | }; |
1777 | |
1778 | static game_ui *new_ui(game_state *state) |
1779 | { |
1780 | game_ui *ui = snew(game_ui); |
1781 | ui->cur_x = ui->cur_y = ui->cur_visible = 0; |
1782 | return ui; |
1783 | } |
1784 | |
1785 | static void free_ui(game_ui *ui) |
1786 | { |
1787 | sfree(ui); |
1788 | } |
1789 | |
1790 | static char *encode_ui(game_ui *ui) |
1791 | { |
1792 | /* nothing to encode. */ |
1793 | return NULL; |
1794 | } |
1795 | |
1796 | static void decode_ui(game_ui *ui, char *encoding) |
1797 | { |
1798 | /* nothing to decode. */ |
1799 | } |
1800 | |
1801 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
1802 | game_state *newstate) |
1803 | { |
1804 | if (newstate->completed) |
1805 | ui->cur_visible = 0; |
1806 | } |
1807 | |
1808 | #define DF_BLACK 1 /* black square */ |
1809 | #define DF_NUMBERED 2 /* black square with number */ |
1810 | #define DF_LIT 4 /* display (white) square lit up */ |
1811 | #define DF_LIGHT 8 /* display light in square */ |
1812 | #define DF_OVERLAP 16 /* display light as overlapped */ |
1813 | #define DF_CURSOR 32 /* display cursor */ |
1814 | #define DF_NUMBERWRONG 64 /* display black numbered square as error. */ |
1815 | #define DF_FLASH 128 /* background flash is on. */ |
1816 | #define DF_IMPOSSIBLE 256 /* display non-light little square */ |
1817 | |
1818 | struct game_drawstate { |
1819 | int tilesize, crad; |
1820 | int w, h; |
1821 | unsigned int *flags; /* width * height */ |
1822 | int started; |
1823 | }; |
1824 | |
1825 | |
1826 | /* Believe it or not, this empty = "" hack is needed to get around a bug in |
1827 | * the prc-tools gcc when optimisation is turned on; before, it produced: |
1828 | lightup-sect.c: In function `interpret_move': |
1829 | lightup-sect.c:1416: internal error--unrecognizable insn: |
1830 | (insn 582 580 583 (set (reg:SI 134) |
1831 | (pc)) -1 (nil) |
1832 | (nil)) |
1833 | */ |
1834 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
1835 | int x, int y, int button) |
1836 | { |
1837 | enum { NONE, FLIP_LIGHT, FLIP_IMPOSSIBLE } action = NONE; |
d7b7d7cd |
1838 | int cx = -1, cy = -1; |
e3478a4b |
1839 | unsigned int flags; |
d7b7d7cd |
1840 | char buf[80], *nullret = NULL, *empty = "", c; |
e3478a4b |
1841 | |
1842 | if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { |
d7b7d7cd |
1843 | if (ui->cur_visible) |
1844 | nullret = empty; |
e3478a4b |
1845 | ui->cur_visible = 0; |
1846 | cx = FROMCOORD(x); |
1847 | cy = FROMCOORD(y); |
1848 | action = (button == LEFT_BUTTON) ? FLIP_LIGHT : FLIP_IMPOSSIBLE; |
94cfbe9d |
1849 | } else if (IS_CURSOR_SELECT(button) || |
e3478a4b |
1850 | button == 'i' || button == 'I' || |
1851 | button == ' ' || button == '\r' || button == '\n') { |
94cfbe9d |
1852 | if (ui->cur_visible) { |
1853 | /* Only allow cursor-effect operations if the cursor is visible |
1854 | * (otherwise you have no idea which square it might be affecting) */ |
1855 | cx = ui->cur_x; |
1856 | cy = ui->cur_y; |
1857 | action = (button == 'i' || button == 'I' || button == CURSOR_SELECT2) ? |
1858 | FLIP_IMPOSSIBLE : FLIP_LIGHT; |
e3478a4b |
1859 | } |
94cfbe9d |
1860 | ui->cur_visible = 1; |
1861 | } else if (IS_CURSOR_MOVE(button)) { |
1862 | move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0); |
e3478a4b |
1863 | ui->cur_visible = 1; |
d7b7d7cd |
1864 | nullret = empty; |
1865 | } else |
1866 | return NULL; |
e3478a4b |
1867 | |
1868 | switch (action) { |
1869 | case FLIP_LIGHT: |
1870 | case FLIP_IMPOSSIBLE: |
1871 | if (cx < 0 || cy < 0 || cx >= state->w || cy >= state->h) |
1872 | return nullret; |
1873 | flags = GRID(state, flags, cx, cy); |
1874 | if (flags & F_BLACK) |
1875 | return nullret; |
1876 | if (action == FLIP_LIGHT) { |
80e7e37c |
1877 | #ifdef STYLUS_BASED |
1878 | if (flags & F_IMPOSSIBLE || flags & F_LIGHT) c = 'I'; else c = 'L'; |
1879 | #else |
e3478a4b |
1880 | if (flags & F_IMPOSSIBLE) return nullret; |
1881 | c = 'L'; |
80e7e37c |
1882 | #endif |
e3478a4b |
1883 | } else { |
80e7e37c |
1884 | #ifdef STYLUS_BASED |
1885 | if (flags & F_IMPOSSIBLE || flags & F_LIGHT) c = 'L'; else c = 'I'; |
1886 | #else |
e3478a4b |
1887 | if (flags & F_LIGHT) return nullret; |
1888 | c = 'I'; |
80e7e37c |
1889 | #endif |
e3478a4b |
1890 | } |
1891 | sprintf(buf, "%c%d,%d", (int)c, cx, cy); |
1892 | break; |
1893 | |
1894 | case NONE: |
1895 | return nullret; |
1896 | |
1897 | default: |
1898 | assert(!"Shouldn't get here!"); |
1899 | } |
1900 | return dupstr(buf); |
1901 | } |
1902 | |
1903 | static game_state *execute_move(game_state *state, char *move) |
1904 | { |
1905 | game_state *ret = dup_game(state); |
1906 | int x, y, n, flags; |
1907 | char c; |
1908 | |
1909 | if (!*move) goto badmove; |
1910 | |
1911 | while (*move) { |
1912 | c = *move; |
1913 | if (c == 'S') { |
1914 | ret->used_solve = TRUE; |
1915 | move++; |
1916 | } else if (c == 'L' || c == 'I') { |
1917 | move++; |
1918 | if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 || |
1919 | x < 0 || y < 0 || x >= ret->w || y >= ret->h) |
1920 | goto badmove; |
1921 | |
1922 | flags = GRID(ret, flags, x, y); |
1923 | if (flags & F_BLACK) goto badmove; |
1924 | |
1925 | /* LIGHT and IMPOSSIBLE are mutually exclusive. */ |
1926 | if (c == 'L') { |
1927 | GRID(ret, flags, x, y) &= ~F_IMPOSSIBLE; |
1928 | set_light(ret, x, y, (flags & F_LIGHT) ? 0 : 1); |
1929 | } else { |
1930 | set_light(ret, x, y, 0); |
1931 | GRID(ret, flags, x, y) ^= F_IMPOSSIBLE; |
1932 | } |
1933 | move += n; |
1934 | } else goto badmove; |
1935 | |
1936 | if (*move == ';') |
1937 | move++; |
1938 | else if (*move) goto badmove; |
1939 | } |
1940 | if (grid_correct(ret)) ret->completed = 1; |
1941 | return ret; |
1942 | |
1943 | badmove: |
1944 | free_game(ret); |
1945 | return NULL; |
1946 | } |
1947 | |
1948 | /* ---------------------------------------------------------------------- |
1949 | * Drawing routines. |
1950 | */ |
1951 | |
1952 | /* XXX entirely cloned from fifteen.c; separate out? */ |
1953 | static void game_compute_size(game_params *params, int tilesize, |
1954 | int *x, int *y) |
1955 | { |
1956 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1957 | struct { int tilesize; } ads, *ds = &ads; |
1958 | ads.tilesize = tilesize; |
1959 | |
1960 | *x = TILE_SIZE * params->w + 2 * BORDER; |
1961 | *y = TILE_SIZE * params->h + 2 * BORDER; |
1962 | } |
1963 | |
dafd6cf6 |
1964 | static void game_set_size(drawing *dr, game_drawstate *ds, |
1965 | game_params *params, int tilesize) |
e3478a4b |
1966 | { |
1967 | ds->tilesize = tilesize; |
1968 | ds->crad = 3*(tilesize-1)/8; |
1969 | } |
1970 | |
8266f3fc |
1971 | static float *game_colours(frontend *fe, int *ncolours) |
e3478a4b |
1972 | { |
1973 | float *ret = snewn(3 * NCOLOURS, float); |
1974 | int i; |
1975 | |
1976 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1977 | |
1978 | for (i = 0; i < 3; i++) { |
1979 | ret[COL_BLACK * 3 + i] = 0.0F; |
1980 | ret[COL_LIGHT * 3 + i] = 1.0F; |
1981 | ret[COL_CURSOR * 3 + i] = ret[COL_BACKGROUND * 3 + i] / 2.0F; |
1982 | ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] / 1.5F; |
1983 | |
1984 | } |
1985 | |
1986 | ret[COL_ERROR * 3 + 0] = 1.0F; |
1987 | ret[COL_ERROR * 3 + 1] = 0.25F; |
1988 | ret[COL_ERROR * 3 + 2] = 0.25F; |
1989 | |
1990 | ret[COL_LIT * 3 + 0] = 1.0F; |
1991 | ret[COL_LIT * 3 + 1] = 1.0F; |
1992 | ret[COL_LIT * 3 + 2] = 0.0F; |
1993 | |
1994 | *ncolours = NCOLOURS; |
1995 | return ret; |
1996 | } |
1997 | |
dafd6cf6 |
1998 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
e3478a4b |
1999 | { |
2000 | struct game_drawstate *ds = snew(struct game_drawstate); |
2001 | int i; |
2002 | |
2003 | ds->tilesize = ds->crad = 0; |
2004 | ds->w = state->w; ds->h = state->h; |
2005 | |
2006 | ds->flags = snewn(ds->w*ds->h, unsigned int); |
2007 | for (i = 0; i < ds->w*ds->h; i++) |
2008 | ds->flags[i] = -1; |
2009 | |
2010 | ds->started = 0; |
2011 | |
2012 | return ds; |
2013 | } |
2014 | |
dafd6cf6 |
2015 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
e3478a4b |
2016 | { |
2017 | sfree(ds->flags); |
2018 | sfree(ds); |
2019 | } |
2020 | |
2021 | /* At some stage we should put these into a real options struct. |
2022 | * Note that tile_redraw has no #ifdeffery; it relies on tile_flags not |
2023 | * to put those flags in. */ |
2024 | #define HINT_LIGHTS |
2025 | #define HINT_OVERLAPS |
2026 | #define HINT_NUMBERS |
2027 | |
2028 | static unsigned int tile_flags(game_drawstate *ds, game_state *state, game_ui *ui, |
2029 | int x, int y, int flashing) |
2030 | { |
2031 | unsigned int flags = GRID(state, flags, x, y); |
2032 | int lights = GRID(state, lights, x, y); |
2033 | unsigned int ret = 0; |
2034 | |
2035 | if (flashing) ret |= DF_FLASH; |
dafd6cf6 |
2036 | if (ui && ui->cur_visible && x == ui->cur_x && y == ui->cur_y) |
e3478a4b |
2037 | ret |= DF_CURSOR; |
2038 | |
2039 | if (flags & F_BLACK) { |
2040 | ret |= DF_BLACK; |
2041 | if (flags & F_NUMBERED) { |
2042 | #ifdef HINT_NUMBERS |
2043 | if (number_wrong(state, x, y)) |
2044 | ret |= DF_NUMBERWRONG; |
2045 | #endif |
2046 | ret |= DF_NUMBERED; |
2047 | } |
2048 | } else { |
2049 | #ifdef HINT_LIGHTS |
2050 | if (lights > 0) ret |= DF_LIT; |
2051 | #endif |
2052 | if (flags & F_LIGHT) { |
2053 | ret |= DF_LIGHT; |
2054 | #ifdef HINT_OVERLAPS |
2055 | if (lights > 1) ret |= DF_OVERLAP; |
2056 | #endif |
2057 | } |
2058 | if (flags & F_IMPOSSIBLE) ret |= DF_IMPOSSIBLE; |
2059 | } |
2060 | return ret; |
2061 | } |
2062 | |
dafd6cf6 |
2063 | static void tile_redraw(drawing *dr, game_drawstate *ds, game_state *state, |
e3478a4b |
2064 | int x, int y) |
2065 | { |
2066 | unsigned int ds_flags = GRID(ds, flags, x, y); |
2067 | int dx = COORD(x), dy = COORD(y); |
2068 | int lit = (ds_flags & DF_FLASH) ? COL_GRID : COL_LIT; |
2069 | |
2070 | if (ds_flags & DF_BLACK) { |
dafd6cf6 |
2071 | draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BLACK); |
e3478a4b |
2072 | if (ds_flags & DF_NUMBERED) { |
2073 | int ccol = (ds_flags & DF_NUMBERWRONG) ? COL_ERROR : COL_LIGHT; |
092e9395 |
2074 | char str[32]; |
e3478a4b |
2075 | |
2076 | /* We know that this won't change over the course of the game |
2077 | * so it's OK to ignore this when calculating whether or not |
2078 | * to redraw the tile. */ |
2079 | sprintf(str, "%d", GRID(state, lights, x, y)); |
dafd6cf6 |
2080 | draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, |
e3478a4b |
2081 | FONT_VARIABLE, TILE_SIZE*3/5, |
2082 | ALIGN_VCENTRE | ALIGN_HCENTRE, ccol, str); |
2083 | } |
2084 | } else { |
dafd6cf6 |
2085 | draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, |
e3478a4b |
2086 | (ds_flags & DF_LIT) ? lit : COL_BACKGROUND); |
dafd6cf6 |
2087 | draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID); |
e3478a4b |
2088 | if (ds_flags & DF_LIGHT) { |
2089 | int lcol = (ds_flags & DF_OVERLAP) ? COL_ERROR : COL_LIGHT; |
dafd6cf6 |
2090 | draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, TILE_RADIUS, |
e3478a4b |
2091 | lcol, COL_BLACK); |
d7bd2e39 |
2092 | } else if ((ds_flags & DF_IMPOSSIBLE)) { |
2093 | static int draw_blobs_when_lit = -1; |
2094 | if (draw_blobs_when_lit < 0) { |
2095 | char *env = getenv("LIGHTUP_LIT_BLOBS"); |
2096 | draw_blobs_when_lit = (!env || (env[0] == 'y' || |
2097 | env[0] == 'Y')); |
2098 | } |
2099 | if (!(ds_flags & DF_LIT) || draw_blobs_when_lit) { |
2100 | int rlen = TILE_SIZE / 4; |
2101 | draw_rect(dr, dx + TILE_SIZE/2 - rlen/2, |
2102 | dy + TILE_SIZE/2 - rlen/2, |
2103 | rlen, rlen, COL_BLACK); |
2104 | } |
e3478a4b |
2105 | } |
2106 | } |
2107 | |
2108 | if (ds_flags & DF_CURSOR) { |
2109 | int coff = TILE_SIZE/8; |
dafd6cf6 |
2110 | draw_rect_outline(dr, dx + coff, dy + coff, |
e3478a4b |
2111 | TILE_SIZE - coff*2, TILE_SIZE - coff*2, COL_CURSOR); |
2112 | } |
2113 | |
dafd6cf6 |
2114 | draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE); |
e3478a4b |
2115 | } |
2116 | |
dafd6cf6 |
2117 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
e3478a4b |
2118 | game_state *state, int dir, game_ui *ui, |
2119 | float animtime, float flashtime) |
2120 | { |
2121 | int flashing = FALSE; |
2122 | int x,y; |
2123 | |
2124 | if (flashtime) flashing = (int)(flashtime * 3 / FLASH_TIME) != 1; |
2125 | |
2126 | if (!ds->started) { |
dafd6cf6 |
2127 | draw_rect(dr, 0, 0, |
e3478a4b |
2128 | TILE_SIZE * ds->w + 2 * BORDER, |
2129 | TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND); |
2130 | |
dafd6cf6 |
2131 | draw_rect_outline(dr, COORD(0)-1, COORD(0)-1, |
e3478a4b |
2132 | TILE_SIZE * ds->w + 2, |
2133 | TILE_SIZE * ds->h + 2, |
2134 | COL_GRID); |
2135 | |
dafd6cf6 |
2136 | draw_update(dr, 0, 0, |
e3478a4b |
2137 | TILE_SIZE * ds->w + 2 * BORDER, |
2138 | TILE_SIZE * ds->h + 2 * BORDER); |
2139 | ds->started = 1; |
2140 | } |
2141 | |
2142 | for (x = 0; x < ds->w; x++) { |
2143 | for (y = 0; y < ds->h; y++) { |
2144 | unsigned int ds_flags = tile_flags(ds, state, ui, x, y, flashing); |
2145 | if (ds_flags != GRID(ds, flags, x, y)) { |
2146 | GRID(ds, flags, x, y) = ds_flags; |
dafd6cf6 |
2147 | tile_redraw(dr, ds, state, x, y); |
e3478a4b |
2148 | } |
2149 | } |
2150 | } |
2151 | } |
2152 | |
2153 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
2154 | int dir, game_ui *ui) |
2155 | { |
2156 | return 0.0F; |
2157 | } |
2158 | |
2159 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
2160 | int dir, game_ui *ui) |
2161 | { |
2162 | if (!oldstate->completed && newstate->completed && |
2163 | !oldstate->used_solve && !newstate->used_solve) |
2164 | return FLASH_TIME; |
2165 | return 0.0F; |
2166 | } |
2167 | |
1cea529f |
2168 | static int game_status(game_state *state) |
4496362f |
2169 | { |
1cea529f |
2170 | return state->completed ? +1 : 0; |
4496362f |
2171 | } |
2172 | |
e3478a4b |
2173 | static int game_timing_state(game_state *state, game_ui *ui) |
2174 | { |
2175 | return TRUE; |
2176 | } |
2177 | |
dafd6cf6 |
2178 | static void game_print_size(game_params *params, float *x, float *y) |
2179 | { |
2180 | int pw, ph; |
2181 | |
2182 | /* |
2183 | * I'll use 6mm squares by default. |
2184 | */ |
2185 | game_compute_size(params, 600, &pw, &ph); |
5b502ae8 |
2186 | *x = pw / 100.0F; |
2187 | *y = ph / 100.0F; |
dafd6cf6 |
2188 | } |
2189 | |
2190 | static void game_print(drawing *dr, game_state *state, int tilesize) |
2191 | { |
2192 | int w = state->w, h = state->h; |
2193 | int ink = print_mono_colour(dr, 0); |
2194 | int paper = print_mono_colour(dr, 1); |
2195 | int x, y; |
2196 | |
2197 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
2198 | game_drawstate ads, *ds = &ads; |
4413ef0f |
2199 | game_set_size(dr, ds, NULL, tilesize); |
dafd6cf6 |
2200 | |
2201 | /* |
2202 | * Border. |
2203 | */ |
2204 | print_line_width(dr, TILE_SIZE / 16); |
2205 | draw_rect_outline(dr, COORD(0), COORD(0), |
2206 | TILE_SIZE * w, TILE_SIZE * h, ink); |
2207 | |
2208 | /* |
2209 | * Grid. |
2210 | */ |
2211 | print_line_width(dr, TILE_SIZE / 24); |
2212 | for (x = 1; x < w; x++) |
2213 | draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), ink); |
2214 | for (y = 1; y < h; y++) |
2215 | draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), ink); |
2216 | |
2217 | /* |
2218 | * Grid contents. |
2219 | */ |
2220 | for (y = 0; y < h; y++) |
2221 | for (x = 0; x < w; x++) { |
2222 | unsigned int ds_flags = tile_flags(ds, state, NULL, x, y, FALSE); |
2223 | int dx = COORD(x), dy = COORD(y); |
2224 | if (ds_flags & DF_BLACK) { |
2225 | draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, ink); |
2226 | if (ds_flags & DF_NUMBERED) { |
092e9395 |
2227 | char str[32]; |
dafd6cf6 |
2228 | sprintf(str, "%d", GRID(state, lights, x, y)); |
2229 | draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, |
2230 | FONT_VARIABLE, TILE_SIZE*3/5, |
2231 | ALIGN_VCENTRE | ALIGN_HCENTRE, paper, str); |
2232 | } |
2233 | } else if (ds_flags & DF_LIGHT) { |
2234 | draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, |
2235 | TILE_RADIUS, -1, ink); |
2236 | } |
2237 | } |
2238 | } |
2239 | |
e3478a4b |
2240 | #ifdef COMBINED |
2241 | #define thegame lightup |
2242 | #endif |
2243 | |
2244 | const struct game thegame = { |
750037d7 |
2245 | "Light Up", "games.lightup", "lightup", |
e3478a4b |
2246 | default_params, |
2247 | game_fetch_preset, |
2248 | decode_params, |
2249 | encode_params, |
2250 | free_params, |
2251 | dup_params, |
2252 | TRUE, game_configure, custom_params, |
2253 | validate_params, |
2254 | new_game_desc, |
2255 | validate_desc, |
2256 | new_game, |
2257 | dup_game, |
2258 | free_game, |
2259 | TRUE, solve_game, |
fa3abef5 |
2260 | TRUE, game_can_format_as_text_now, game_text_format, |
e3478a4b |
2261 | new_ui, |
2262 | free_ui, |
2263 | encode_ui, |
2264 | decode_ui, |
2265 | game_changed_state, |
2266 | interpret_move, |
2267 | execute_move, |
2268 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
2269 | game_colours, |
2270 | game_new_drawstate, |
2271 | game_free_drawstate, |
2272 | game_redraw, |
2273 | game_anim_length, |
2274 | game_flash_length, |
1cea529f |
2275 | game_status, |
dafd6cf6 |
2276 | TRUE, FALSE, game_print_size, game_print, |
ac9f41c4 |
2277 | FALSE, /* wants_statusbar */ |
e3478a4b |
2278 | FALSE, game_timing_state, |
2705d374 |
2279 | 0, /* flags */ |
e3478a4b |
2280 | }; |
2281 | |
9b265feb |
2282 | #ifdef STANDALONE_SOLVER |
2283 | |
2284 | int main(int argc, char **argv) |
2285 | { |
2286 | game_params *p; |
2287 | game_state *s; |
2288 | char *id = NULL, *desc, *err, *result; |
2289 | int nsol, diff, really_verbose = 0; |
2290 | unsigned int sflags; |
2291 | |
2292 | while (--argc > 0) { |
2293 | char *p = *++argv; |
2294 | if (!strcmp(p, "-v")) { |
2295 | really_verbose++; |
2296 | } else if (*p == '-') { |
2297 | fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); |
2298 | return 1; |
2299 | } else { |
2300 | id = p; |
2301 | } |
2302 | } |
2303 | |
2304 | if (!id) { |
2305 | fprintf(stderr, "usage: %s [-v] <game_id>\n", argv[0]); |
2306 | return 1; |
2307 | } |
2308 | |
2309 | desc = strchr(id, ':'); |
2310 | if (!desc) { |
2311 | fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); |
2312 | return 1; |
2313 | } |
2314 | *desc++ = '\0'; |
2315 | |
2316 | p = default_params(); |
2317 | decode_params(p, id); |
2318 | err = validate_desc(p, desc); |
2319 | if (err) { |
2320 | fprintf(stderr, "%s: %s\n", argv[0], err); |
2321 | return 1; |
2322 | } |
2323 | s = new_game(NULL, p, desc); |
2324 | |
2325 | /* Run the solvers easiest to hardest until we find one that |
2326 | * can solve our puzzle. If it's soluble we know that the |
2327 | * hardest (recursive) solver will always find the solution. */ |
6f3dfb23 |
2328 | nsol = sflags = 0; |
9b265feb |
2329 | for (diff = 0; diff <= DIFFCOUNT; diff++) { |
2330 | printf("\nSolving with difficulty %d.\n", diff); |
2331 | sflags = flags_from_difficulty(diff); |
2332 | unplace_lights(s); |
2333 | nsol = dosolve(s, sflags, NULL); |
2334 | if (nsol == 1) break; |
2335 | } |
2336 | |
2337 | printf("\n"); |
2338 | if (nsol == 0) { |
2339 | printf("Puzzle has no solution.\n"); |
2340 | } else if (nsol < 0) { |
2341 | printf("Unable to find a unique solution.\n"); |
2342 | } else if (nsol > 1) { |
2343 | printf("Puzzle has multiple solutions.\n"); |
2344 | } else { |
2345 | verbose = really_verbose; |
2346 | unplace_lights(s); |
2347 | printf("Puzzle has difficulty %d: solving...\n", diff); |
2348 | dosolve(s, sflags, NULL); /* sflags from last successful solve */ |
2349 | result = game_text_format(s); |
2350 | printf("%s", result); |
2351 | sfree(result); |
2352 | } |
2353 | |
2354 | return 0; |
2355 | } |
2356 | |
2357 | #endif |
2358 | |
e3478a4b |
2359 | /* vim: set shiftwidth=4 tabstop=8: */ |