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