6193da8d |
1 | /* |
2 | * loopy.c: An implementation of the Nikoli game 'Loop the loop'. |
3 | * (c) Mike Pinna, 2005 |
4 | * |
5 | * vim: set shiftwidth=4 :set textwidth=80: |
6 | */ |
7 | |
8 | /* |
9 | * TODO: |
10 | * |
11 | * - setting very high recursion depth seems to cause memory |
12 | * munching: are we recursing before checking completion, by any |
13 | * chance? |
14 | * |
15 | * - there's an interesting deductive technique which makes use of |
16 | * topology rather than just graph theory. Each _square_ in the |
17 | * grid is either inside or outside the loop; you can tell that |
18 | * two squares are on the same side of the loop if they're |
19 | * separated by an x (or, more generally, by a path crossing no |
20 | * LINE_UNKNOWNs and an even number of LINE_YESes), and on the |
21 | * opposite side of the loop if they're separated by a line (or |
22 | * an odd number of LINE_YESes and no LINE_UNKNOWNs). Oh, and |
23 | * any square separated from the outside of the grid by a |
24 | * LINE_YES or a LINE_NO is on the inside or outside |
25 | * respectively. So if you can track this for all squares, you |
26 | * can occasionally spot that two squares are separated by a |
27 | * LINE_UNKNOWN but their relative insideness is known, and |
28 | * therefore deduce the state of the edge between them. |
29 | * + An efficient way to track this would be by augmenting the |
30 | * disjoint set forest data structure. Each element, along |
31 | * with a pointer to a parent member of its equivalence |
32 | * class, would also carry a one-bit field indicating whether |
33 | * it was equal or opposite to its parent. Then you could |
34 | * keep flipping a bit as you ascended the tree during |
35 | * dsf_canonify(), and hence you'd be able to return the |
36 | * relationship of the input value to its ultimate parent |
37 | * (and also you could then get all those bits right when you |
38 | * went back up the tree rewriting). So you'd be able to |
39 | * query whether any two elements were known-equal, |
40 | * known-opposite, or not-known, and you could add new |
41 | * equalities or oppositenesses to increase your knowledge. |
42 | * (Of course the algorithm would have to fail an assertion |
43 | * if you tried to tell it two things it already knew to be |
44 | * opposite were equal, or vice versa!) |
45 | */ |
46 | |
47 | #include <stdio.h> |
48 | #include <stdlib.h> |
49 | #include <string.h> |
50 | #include <assert.h> |
51 | #include <ctype.h> |
52 | #include <math.h> |
53 | |
54 | #include "puzzles.h" |
55 | #include "tree234.h" |
56 | |
57 | #define PREFERRED_TILE_SIZE 32 |
58 | #define TILE_SIZE (ds->tilesize) |
59 | #define LINEWIDTH TILE_SIZE / 16 |
60 | #define BORDER (TILE_SIZE / 2) |
61 | |
62 | #define FLASH_TIME 0.4F |
63 | |
64 | #define HL_COUNT(state) ((state)->w * ((state)->h + 1)) |
65 | #define VL_COUNT(state) (((state)->w + 1) * (state)->h) |
66 | #define DOT_COUNT(state) (((state)->w + 1) * ((state)->h + 1)) |
67 | #define SQUARE_COUNT(state) ((state)->w * (state)->h) |
68 | |
69 | #define ABOVE_SQUARE(state, i, j) ((state)->hl[(i) + (state)->w * (j)]) |
70 | #define BELOW_SQUARE(state, i, j) ABOVE_SQUARE(state, i, (j)+1) |
71 | |
72 | #define LEFTOF_SQUARE(state, i, j) ((state)->vl[(i) + ((state)->w + 1) * (j)]) |
73 | #define RIGHTOF_SQUARE(state, i, j) LEFTOF_SQUARE(state, (i)+1, j) |
74 | |
75 | #define LEGAL_DOT(state, i, j) ((i) >= 0 && (j) >= 0 && \ |
76 | (i) <= (state)->w && (j) <= (state)->h) |
77 | |
78 | /* |
79 | * These macros return rvalues only, but can cope with being passed |
80 | * out-of-range coordinates. |
81 | */ |
82 | #define ABOVE_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || j <= 0) ? \ |
83 | LINE_NO : LV_ABOVE_DOT(state, i, j)) |
84 | #define BELOW_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || j >= (state)->h) ? \ |
85 | LINE_NO : LV_BELOW_DOT(state, i, j)) |
86 | |
87 | #define LEFTOF_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || i <= 0) ? \ |
88 | LINE_NO : LV_LEFTOF_DOT(state, i, j)) |
89 | #define RIGHTOF_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || i >= (state)->w)?\ |
90 | LINE_NO : LV_RIGHTOF_DOT(state, i, j)) |
91 | |
92 | /* |
93 | * These macros expect to be passed valid coordinates, and return |
94 | * lvalues. |
95 | */ |
96 | #define LV_BELOW_DOT(state, i, j) ((state)->vl[(i) + ((state)->w + 1) * (j)]) |
97 | #define LV_ABOVE_DOT(state, i, j) LV_BELOW_DOT(state, i, (j)-1) |
98 | |
99 | #define LV_RIGHTOF_DOT(state, i, j) ((state)->hl[(i) + (state)->w * (j)]) |
100 | #define LV_LEFTOF_DOT(state, i, j) LV_RIGHTOF_DOT(state, (i)-1, j) |
101 | |
102 | #define CLUE_AT(state, i, j) ((i < 0 || i >= (state)->w || \ |
103 | j < 0 || j >= (state)->h) ? \ |
104 | ' ' : LV_CLUE_AT(state, i, j)) |
105 | |
106 | #define LV_CLUE_AT(state, i, j) ((state)->clues[(i) + (state)->w * (j)]) |
107 | |
108 | #define OPP(dir) (dir == LINE_UNKNOWN ? LINE_UNKNOWN : \ |
109 | dir == LINE_YES ? LINE_NO : LINE_YES) |
110 | |
111 | static char *game_text_format(game_state *state); |
112 | |
113 | enum { |
114 | COL_BACKGROUND, |
115 | COL_FOREGROUND, |
116 | COL_HIGHLIGHT, |
117 | NCOLOURS |
118 | }; |
119 | |
120 | enum line_state { LINE_UNKNOWN, LINE_YES, LINE_NO }; |
121 | |
122 | enum direction { UP, DOWN, LEFT, RIGHT }; |
123 | |
124 | struct game_params { |
125 | int w, h, rec; |
126 | }; |
127 | |
128 | struct game_state { |
129 | int w, h; |
130 | |
131 | /* Put ' ' in a square that doesn't get a clue */ |
132 | char *clues; |
133 | |
134 | /* Arrays of line states, stored left-to-right, top-to-bottom */ |
135 | char *hl, *vl; |
136 | |
137 | int solved; |
138 | int cheated; |
139 | |
140 | int recursion_depth; |
141 | }; |
142 | |
143 | static game_state *dup_game(game_state *state) |
144 | { |
145 | game_state *ret = snew(game_state); |
146 | |
147 | ret->h = state->h; |
148 | ret->w = state->w; |
149 | ret->solved = state->solved; |
150 | ret->cheated = state->cheated; |
151 | |
152 | ret->clues = snewn(SQUARE_COUNT(state), char); |
153 | memcpy(ret->clues, state->clues, SQUARE_COUNT(state)); |
154 | |
155 | ret->hl = snewn(HL_COUNT(state), char); |
156 | memcpy(ret->hl, state->hl, HL_COUNT(state)); |
157 | |
158 | ret->vl = snewn(VL_COUNT(state), char); |
159 | memcpy(ret->vl, state->vl, VL_COUNT(state)); |
160 | |
161 | ret->recursion_depth = state->recursion_depth; |
162 | |
163 | return ret; |
164 | } |
165 | |
166 | static void free_game(game_state *state) |
167 | { |
168 | if (state) { |
169 | sfree(state->clues); |
170 | sfree(state->hl); |
171 | sfree(state->vl); |
172 | sfree(state); |
173 | } |
174 | } |
175 | |
176 | enum solver_status { |
177 | SOLVER_SOLVED, /* This is the only solution the solver could find */ |
178 | SOLVER_MISTAKE, /* This is definitely not a solution */ |
179 | SOLVER_AMBIGUOUS, /* This _might_ be an ambiguous solution */ |
180 | SOLVER_INCOMPLETE /* This may be a partial solution */ |
181 | }; |
182 | |
183 | typedef struct solver_state { |
184 | game_state *state; |
185 | /* XXX dot_atleastone[i,j, dline] is equivalent to */ |
186 | /* dot_atmostone[i,j,OPP_DLINE(dline)] */ |
187 | char *dot_atleastone; |
188 | char *dot_atmostone; |
189 | /* char *dline_identical; */ |
190 | int recursion_remaining; |
191 | enum solver_status solver_status; |
192 | int *dotdsf, *looplen; |
193 | } solver_state; |
194 | |
195 | static solver_state *new_solver_state(game_state *state) { |
196 | solver_state *ret = snew(solver_state); |
197 | int i; |
198 | |
199 | ret->state = dup_game(state); |
200 | |
201 | ret->dot_atmostone = snewn(DOT_COUNT(state), char); |
202 | memset(ret->dot_atmostone, 0, DOT_COUNT(state)); |
203 | ret->dot_atleastone = snewn(DOT_COUNT(state), char); |
204 | memset(ret->dot_atleastone, 0, DOT_COUNT(state)); |
205 | |
206 | #if 0 |
207 | dline_identical = snewn(DOT_COUNT(state), char); |
208 | memset(dline_identical, 0, DOT_COUNT(state)); |
209 | #endif |
210 | |
211 | ret->recursion_remaining = state->recursion_depth; |
212 | ret->solver_status = SOLVER_INCOMPLETE; /* XXX This may be a lie */ |
213 | |
214 | ret->dotdsf = snewn(DOT_COUNT(state), int); |
215 | ret->looplen = snewn(DOT_COUNT(state), int); |
216 | for (i = 0; i < DOT_COUNT(state); i++) { |
217 | ret->dotdsf[i] = i; |
218 | ret->looplen[i] = 1; |
219 | } |
220 | |
221 | return ret; |
222 | } |
223 | |
224 | static void free_solver_state(solver_state *sstate) { |
225 | if (sstate) { |
226 | free_game(sstate->state); |
227 | sfree(sstate->dot_atleastone); |
228 | sfree(sstate->dot_atmostone); |
229 | /* sfree(sstate->dline_identical); */ |
9cfc03b7 |
230 | sfree(sstate->dotdsf); |
231 | sfree(sstate->looplen); |
232 | sfree(sstate); |
6193da8d |
233 | } |
234 | } |
235 | |
236 | static solver_state *dup_solver_state(solver_state *sstate) { |
9cfc03b7 |
237 | game_state *state; |
6193da8d |
238 | |
239 | solver_state *ret = snew(solver_state); |
240 | |
9cfc03b7 |
241 | ret->state = state = dup_game(sstate->state); |
6193da8d |
242 | |
243 | ret->dot_atmostone = snewn(DOT_COUNT(state), char); |
244 | memcpy(ret->dot_atmostone, sstate->dot_atmostone, DOT_COUNT(state)); |
245 | |
246 | ret->dot_atleastone = snewn(DOT_COUNT(state), char); |
247 | memcpy(ret->dot_atleastone, sstate->dot_atleastone, DOT_COUNT(state)); |
248 | |
249 | #if 0 |
250 | ret->dline_identical = snewn((state->w + 1) * (state->h + 1), char); |
251 | memcpy(ret->dline_identical, state->dot_atmostone, |
252 | (state->w + 1) * (state->h + 1)); |
253 | #endif |
254 | |
255 | ret->recursion_remaining = sstate->recursion_remaining; |
256 | ret->solver_status = sstate->solver_status; |
257 | |
258 | ret->dotdsf = snewn(DOT_COUNT(state), int); |
259 | ret->looplen = snewn(DOT_COUNT(state), int); |
260 | memcpy(ret->dotdsf, sstate->dotdsf, DOT_COUNT(state) * sizeof(int)); |
261 | memcpy(ret->looplen, sstate->looplen, DOT_COUNT(state) * sizeof(int)); |
262 | |
263 | return ret; |
264 | } |
265 | |
266 | /* |
267 | * Merge two dots due to the existence of an edge between them. |
268 | * Updates the dsf tracking equivalence classes, and keeps track of |
269 | * the length of path each dot is currently a part of. |
270 | */ |
271 | static void merge_dots(solver_state *sstate, int x1, int y1, int x2, int y2) |
272 | { |
273 | int i, j, len; |
274 | |
275 | i = y1 * (sstate->state->w + 1) + x1; |
276 | j = y2 * (sstate->state->w + 1) + x2; |
277 | |
278 | i = dsf_canonify(sstate->dotdsf, i); |
279 | j = dsf_canonify(sstate->dotdsf, j); |
280 | |
281 | if (i != j) { |
282 | len = sstate->looplen[i] + sstate->looplen[j]; |
283 | dsf_merge(sstate->dotdsf, i, j); |
284 | i = dsf_canonify(sstate->dotdsf, i); |
285 | sstate->looplen[i] = len; |
286 | } |
287 | } |
288 | |
289 | /* Count the number of lines of a particular type currently going into the |
290 | * given dot. Lines going off the edge of the board are assumed fixed no. */ |
291 | static int dot_order(const game_state* state, int i, int j, char line_type) |
292 | { |
293 | int n = 0; |
294 | |
295 | if (i > 0) { |
296 | if (LEFTOF_DOT(state, i, j) == line_type) |
297 | ++n; |
298 | } else { |
299 | if (line_type == LINE_NO) |
300 | ++n; |
301 | } |
302 | if (i < state->w) { |
303 | if (RIGHTOF_DOT(state, i, j) == line_type) |
304 | ++n; |
305 | } else { |
306 | if (line_type == LINE_NO) |
307 | ++n; |
308 | } |
309 | if (j > 0) { |
310 | if (ABOVE_DOT(state, i, j) == line_type) |
311 | ++n; |
312 | } else { |
313 | if (line_type == LINE_NO) |
314 | ++n; |
315 | } |
316 | if (j < state->h) { |
317 | if (BELOW_DOT(state, i, j) == line_type) |
318 | ++n; |
319 | } else { |
320 | if (line_type == LINE_NO) |
321 | ++n; |
322 | } |
323 | |
324 | return n; |
325 | } |
326 | /* Count the number of lines of a particular type currently surrounding the |
327 | * given square */ |
328 | static int square_order(const game_state* state, int i, int j, char line_type) |
329 | { |
330 | int n = 0; |
331 | |
332 | if (ABOVE_SQUARE(state, i, j) == line_type) |
333 | ++n; |
334 | if (BELOW_SQUARE(state, i, j) == line_type) |
335 | ++n; |
336 | if (LEFTOF_SQUARE(state, i, j) == line_type) |
337 | ++n; |
338 | if (RIGHTOF_SQUARE(state, i, j) == line_type) |
339 | ++n; |
340 | |
341 | return n; |
342 | } |
343 | |
344 | /* Set all lines bordering a dot of type old_type to type new_type */ |
345 | static void dot_setall(game_state *state, int i, int j, |
346 | char old_type, char new_type) |
347 | { |
348 | /* printf("dot_setall([%d,%d], %d, %d)\n", i, j, old_type, new_type); */ |
349 | if (i > 0 && LEFTOF_DOT(state, i, j) == old_type) |
350 | LV_LEFTOF_DOT(state, i, j) = new_type; |
351 | if (i < state->w && RIGHTOF_DOT(state, i, j) == old_type) |
352 | LV_RIGHTOF_DOT(state, i, j) = new_type; |
353 | if (j > 0 && ABOVE_DOT(state, i, j) == old_type) |
354 | LV_ABOVE_DOT(state, i, j) = new_type; |
355 | if (j < state->h && BELOW_DOT(state, i, j) == old_type) |
356 | LV_BELOW_DOT(state, i, j) = new_type; |
357 | } |
358 | /* Set all lines bordering a square of type old_type to type new_type */ |
359 | static void square_setall(game_state *state, int i, int j, |
360 | char old_type, char new_type) |
361 | { |
362 | if (ABOVE_SQUARE(state, i, j) == old_type) |
363 | ABOVE_SQUARE(state, i, j) = new_type; |
364 | if (BELOW_SQUARE(state, i, j) == old_type) |
365 | BELOW_SQUARE(state, i, j) = new_type; |
366 | if (LEFTOF_SQUARE(state, i, j) == old_type) |
367 | LEFTOF_SQUARE(state, i, j) = new_type; |
368 | if (RIGHTOF_SQUARE(state, i, j) == old_type) |
369 | RIGHTOF_SQUARE(state, i, j) = new_type; |
370 | } |
371 | |
372 | static game_params *default_params(void) |
373 | { |
374 | game_params *ret = snew(game_params); |
375 | |
3c0ebbe5 |
376 | #ifdef SLOW_SYSTEM |
377 | ret->h = 4; |
378 | ret->w = 4; |
379 | #else |
6193da8d |
380 | ret->h = 10; |
381 | ret->w = 10; |
3c0ebbe5 |
382 | #endif |
383 | ret->rec = 0; |
6193da8d |
384 | |
385 | return ret; |
386 | } |
387 | |
388 | static game_params *dup_params(game_params *params) |
389 | { |
390 | game_params *ret = snew(game_params); |
391 | *ret = *params; /* structure copy */ |
392 | return ret; |
393 | } |
394 | |
395 | static const struct { |
396 | char *desc; |
397 | game_params params; |
398 | } loopy_presets[] = { |
399 | { "4x4 Easy", { 4, 4, 0 } }, |
400 | { "4x4 Hard", { 4, 4, 2 } }, |
401 | { "7x7 Easy", { 7, 7, 0 } }, |
69ac242c |
402 | { "7x7 Hard", { 7, 7, 2 } }, |
6193da8d |
403 | { "10x10 Easy", { 10, 10, 0 } }, |
3c0ebbe5 |
404 | #ifndef SLOW_SYSTEM |
6193da8d |
405 | { "10x10 Hard", { 10, 10, 2 } }, |
406 | { "15x15 Easy", { 15, 15, 0 } }, |
4404e5c4 |
407 | { "30x20 Easy", { 30, 20, 0 } } |
3c0ebbe5 |
408 | #endif |
6193da8d |
409 | }; |
410 | |
411 | static int game_fetch_preset(int i, char **name, game_params **params) |
412 | { |
413 | game_params tmppar; |
414 | |
415 | if (i < 0 || i >= lenof(loopy_presets)) |
416 | return FALSE; |
417 | |
418 | tmppar = loopy_presets[i].params; |
419 | *params = dup_params(&tmppar); |
420 | *name = dupstr(loopy_presets[i].desc); |
421 | |
422 | return TRUE; |
423 | } |
424 | |
425 | static void free_params(game_params *params) |
426 | { |
427 | sfree(params); |
428 | } |
429 | |
430 | static void decode_params(game_params *params, char const *string) |
431 | { |
432 | params->h = params->w = atoi(string); |
433 | params->rec = 0; |
434 | while (*string && isdigit((unsigned char)*string)) string++; |
435 | if (*string == 'x') { |
436 | string++; |
437 | params->h = atoi(string); |
438 | while (*string && isdigit((unsigned char)*string)) string++; |
439 | } |
440 | if (*string == 'r') { |
441 | string++; |
442 | params->rec = atoi(string); |
443 | while (*string && isdigit((unsigned char)*string)) string++; |
444 | } |
445 | } |
446 | |
447 | static char *encode_params(game_params *params, int full) |
448 | { |
449 | char str[80]; |
450 | sprintf(str, "%dx%d", params->w, params->h); |
451 | if (full) |
452 | sprintf(str + strlen(str), "r%d", params->rec); |
453 | return dupstr(str); |
454 | } |
455 | |
456 | static config_item *game_configure(game_params *params) |
457 | { |
458 | config_item *ret; |
459 | char buf[80]; |
460 | |
461 | ret = snewn(4, config_item); |
462 | |
463 | ret[0].name = "Width"; |
464 | ret[0].type = C_STRING; |
465 | sprintf(buf, "%d", params->w); |
466 | ret[0].sval = dupstr(buf); |
467 | ret[0].ival = 0; |
468 | |
469 | ret[1].name = "Height"; |
470 | ret[1].type = C_STRING; |
471 | sprintf(buf, "%d", params->h); |
472 | ret[1].sval = dupstr(buf); |
473 | ret[1].ival = 0; |
474 | |
475 | ret[2].name = "Recursion depth"; |
476 | ret[2].type = C_STRING; |
477 | sprintf(buf, "%d", params->rec); |
478 | ret[2].sval = dupstr(buf); |
479 | ret[2].ival = 0; |
480 | |
481 | ret[3].name = NULL; |
482 | ret[3].type = C_END; |
483 | ret[3].sval = NULL; |
484 | ret[3].ival = 0; |
485 | |
486 | return ret; |
487 | } |
488 | |
489 | static game_params *custom_params(config_item *cfg) |
490 | { |
491 | game_params *ret = snew(game_params); |
492 | |
493 | ret->w = atoi(cfg[0].sval); |
494 | ret->h = atoi(cfg[1].sval); |
495 | ret->rec = atoi(cfg[2].sval); |
496 | |
497 | return ret; |
498 | } |
499 | |
500 | static char *validate_params(game_params *params, int full) |
501 | { |
502 | if (params->w < 4 || params->h < 4) |
503 | return "Width and height must both be at least 4"; |
504 | if (params->rec < 0) |
505 | return "Recursion depth can't be negative"; |
506 | return NULL; |
507 | } |
508 | |
509 | /* We're going to store a list of current candidate squares for lighting. |
510 | * Each square gets a 'score', which tells us how adding that square right |
511 | * now would affect the length of the solution loop. We're trying to |
512 | * maximise that quantity so will bias our random selection of squares to |
513 | * light towards those with high scores */ |
514 | struct square { |
515 | int score; |
3c0ebbe5 |
516 | unsigned long random; |
6193da8d |
517 | int x, y; |
518 | }; |
519 | |
520 | static int get_square_cmpfn(void *v1, void *v2) |
521 | { |
522 | struct square *s1 = (struct square *)v1; |
523 | struct square *s2 = (struct square *)v2; |
524 | int r; |
525 | |
526 | r = s1->x - s2->x; |
527 | if (r) |
528 | return r; |
529 | |
530 | r = s1->y - s2->y; |
531 | if (r) |
532 | return r; |
533 | |
534 | return 0; |
535 | } |
536 | |
537 | static int square_sort_cmpfn(void *v1, void *v2) |
538 | { |
539 | struct square *s1 = (struct square *)v1; |
540 | struct square *s2 = (struct square *)v2; |
541 | int r; |
542 | |
543 | r = s2->score - s1->score; |
544 | if (r) { |
545 | return r; |
546 | } |
547 | |
3c0ebbe5 |
548 | if (s1->random < s2->random) |
549 | return -1; |
550 | else if (s1->random > s2->random) |
551 | return 1; |
6193da8d |
552 | |
553 | /* |
554 | * It's _just_ possible that two squares might have been given |
555 | * the same random value. In that situation, fall back to |
556 | * comparing based on the coordinates. This introduces a tiny |
557 | * directional bias, but not a significant one. |
558 | */ |
559 | return get_square_cmpfn(v1, v2); |
560 | } |
561 | |
562 | static void print_tree(tree234 *tree) |
563 | { |
564 | #if 0 |
565 | int i = 0; |
566 | struct square *s; |
567 | printf("Print tree:\n"); |
568 | while (i < count234(tree)) { |
569 | s = (struct square *)index234(tree, i); |
570 | assert(s); |
571 | printf(" [%d,%d], %d, %d\n", s->x, s->y, s->score, s->random); |
572 | ++i; |
573 | } |
574 | #endif |
575 | } |
576 | |
577 | enum { SQUARE_LIT, SQUARE_UNLIT }; |
578 | |
579 | #define SQUARE_STATE(i, j) \ |
580 | (((i) < 0 || (i) >= params->w || \ |
581 | (j) < 0 || (j) >= params->h) ? \ |
582 | SQUARE_UNLIT : LV_SQUARE_STATE(i,j)) |
583 | |
584 | #define LV_SQUARE_STATE(i, j) board[(i) + params->w * (j)] |
585 | |
586 | static void print_board(const game_params *params, const char *board) |
587 | { |
588 | #if 0 |
589 | int i,j; |
590 | |
591 | printf(" "); |
592 | for (i = 0; i < params->w; i++) { |
593 | printf("%d", i%10); |
594 | } |
595 | printf("\n"); |
596 | for (j = 0; j < params->h; j++) { |
597 | printf("%d", j%10); |
598 | for (i = 0; i < params->w; i++) { |
599 | printf("%c", SQUARE_STATE(i, j) ? ' ' : 'O'); |
600 | } |
601 | printf("\n"); |
602 | } |
603 | #endif |
604 | } |
605 | |
606 | static char *new_fullyclued_board(game_params *params, random_state *rs) |
607 | { |
608 | char *clues; |
609 | char *board; |
610 | int i, j, a, b, c; |
611 | game_state s; |
612 | game_state *state = &s; |
613 | int board_area = SQUARE_COUNT(params); |
614 | int t; |
615 | |
616 | struct square *square, *tmpsquare, *sq; |
617 | struct square square_pos; |
618 | |
619 | /* These will contain exactly the same information, sorted into different |
620 | * orders */ |
621 | tree234 *lightable_squares_sorted, *lightable_squares_gettable; |
622 | |
623 | #define SQUARE_REACHABLE(i,j) \ |
624 | (t = (SQUARE_STATE(i-1, j) == SQUARE_LIT || \ |
625 | SQUARE_STATE(i+1, j) == SQUARE_LIT || \ |
626 | SQUARE_STATE(i, j-1) == SQUARE_LIT || \ |
627 | SQUARE_STATE(i, j+1) == SQUARE_LIT), \ |
628 | /* printf("SQUARE_REACHABLE(%d,%d) = %d\n", i, j, t), */ \ |
629 | t) |
630 | |
631 | |
632 | /* One situation in which we may not light a square is if that'll leave one |
633 | * square above/below and one left/right of us unlit, separated by a lit |
634 | * square diagnonal from us */ |
635 | #define SQUARE_DIAGONAL_VIOLATION(i, j, h, v) \ |
636 | (t = (SQUARE_STATE((i)+(h), (j)) == SQUARE_UNLIT && \ |
637 | SQUARE_STATE((i), (j)+(v)) == SQUARE_UNLIT && \ |
638 | SQUARE_STATE((i)+(h), (j)+(v)) == SQUARE_LIT), \ |
639 | /* t ? printf("SQUARE_DIAGONAL_VIOLATION(%d, %d, %d, %d)\n", |
640 | i, j, h, v) : 0,*/ \ |
641 | t) |
642 | |
643 | /* We also may not light a square if it will form a loop of lit squares |
644 | * around some unlit squares, as then the game soln won't have a single |
645 | * loop */ |
646 | #define SQUARE_LOOP_VIOLATION(i, j, lit1, lit2) \ |
647 | (SQUARE_STATE((i)+1, (j)) == lit1 && \ |
648 | SQUARE_STATE((i)-1, (j)) == lit1 && \ |
649 | SQUARE_STATE((i), (j)+1) == lit2 && \ |
650 | SQUARE_STATE((i), (j)-1) == lit2) |
651 | |
652 | #define CAN_LIGHT_SQUARE(i, j) \ |
653 | (SQUARE_REACHABLE(i, j) && \ |
654 | !SQUARE_DIAGONAL_VIOLATION(i, j, -1, -1) && \ |
655 | !SQUARE_DIAGONAL_VIOLATION(i, j, +1, -1) && \ |
656 | !SQUARE_DIAGONAL_VIOLATION(i, j, -1, +1) && \ |
657 | !SQUARE_DIAGONAL_VIOLATION(i, j, +1, +1) && \ |
658 | !SQUARE_LOOP_VIOLATION(i, j, SQUARE_LIT, SQUARE_UNLIT) && \ |
659 | !SQUARE_LOOP_VIOLATION(i, j, SQUARE_UNLIT, SQUARE_LIT)) |
660 | |
661 | #define IS_LIGHTING_CANDIDATE(i, j) \ |
662 | (SQUARE_STATE(i, j) == SQUARE_UNLIT && \ |
663 | CAN_LIGHT_SQUARE(i,j)) |
664 | |
665 | /* The 'score' of a square reflects its current desirability for selection |
666 | * as the next square to light. We want to encourage moving into uncharted |
667 | * areas so we give scores according to how many of the square's neighbours |
668 | * are currently unlit. */ |
669 | |
670 | /* UNLIT SCORE |
671 | * 3 2 |
672 | * 2 0 |
673 | * 1 -2 |
674 | */ |
675 | #define SQUARE_SCORE(i,j) \ |
676 | (2*((SQUARE_STATE(i-1, j) == SQUARE_UNLIT) + \ |
677 | (SQUARE_STATE(i+1, j) == SQUARE_UNLIT) + \ |
678 | (SQUARE_STATE(i, j-1) == SQUARE_UNLIT) + \ |
679 | (SQUARE_STATE(i, j+1) == SQUARE_UNLIT)) - 4) |
680 | |
681 | /* When a square gets lit, this defines how far away from that square we |
682 | * need to go recomputing scores */ |
683 | #define SCORE_DISTANCE 1 |
684 | |
685 | board = snewn(board_area, char); |
686 | clues = snewn(board_area, char); |
687 | |
688 | state->h = params->h; |
689 | state->w = params->w; |
690 | state->clues = clues; |
691 | |
692 | /* Make a board */ |
693 | memset(board, SQUARE_UNLIT, board_area); |
694 | |
695 | /* Seed the board with a single lit square near the middle */ |
696 | i = params->w / 2; |
697 | j = params->h / 2; |
698 | if (params->w & 1 && random_bits(rs, 1)) |
699 | ++i; |
700 | if (params->h & 1 && random_bits(rs, 1)) |
701 | ++j; |
702 | |
703 | LV_SQUARE_STATE(i, j) = SQUARE_LIT; |
704 | |
705 | /* We need a way of favouring squares that will increase our loopiness. |
706 | * We do this by maintaining a list of all candidate squares sorted by |
707 | * their score and choose randomly from that with appropriate skew. |
708 | * In order to avoid consistently biasing towards particular squares, we |
709 | * need the sort order _within_ each group of scores to be completely |
710 | * random. But it would be abusing the hospitality of the tree234 data |
711 | * structure if our comparison function were nondeterministic :-). So with |
712 | * each square we associate a random number that does not change during a |
713 | * particular run of the generator, and use that as a secondary sort key. |
714 | * Yes, this means we will be biased towards particular random squares in |
715 | * any one run but that doesn't actually matter. */ |
716 | |
717 | lightable_squares_sorted = newtree234(square_sort_cmpfn); |
718 | lightable_squares_gettable = newtree234(get_square_cmpfn); |
719 | #define ADD_SQUARE(s) \ |
720 | do { \ |
721 | /* printf("ADD SQUARE: [%d,%d], %d, %d\n", |
722 | s->x, s->y, s->score, s->random);*/ \ |
723 | sq = add234(lightable_squares_sorted, s); \ |
724 | assert(sq == s); \ |
725 | sq = add234(lightable_squares_gettable, s); \ |
726 | assert(sq == s); \ |
727 | } while (0) |
728 | |
729 | #define REMOVE_SQUARE(s) \ |
730 | do { \ |
731 | /* printf("DELETE SQUARE: [%d,%d], %d, %d\n", |
732 | s->x, s->y, s->score, s->random);*/ \ |
733 | sq = del234(lightable_squares_sorted, s); \ |
734 | assert(sq); \ |
735 | sq = del234(lightable_squares_gettable, s); \ |
736 | assert(sq); \ |
737 | } while (0) |
738 | |
739 | #define HANDLE_DIR(a, b) \ |
740 | square = snew(struct square); \ |
741 | square->x = (i)+(a); \ |
742 | square->y = (j)+(b); \ |
743 | square->score = 2; \ |
744 | square->random = random_bits(rs, 31); \ |
745 | ADD_SQUARE(square); |
746 | HANDLE_DIR(-1, 0); |
747 | HANDLE_DIR( 1, 0); |
748 | HANDLE_DIR( 0,-1); |
749 | HANDLE_DIR( 0, 1); |
750 | #undef HANDLE_DIR |
751 | |
752 | /* Light squares one at a time until the board is interesting enough */ |
753 | while (TRUE) |
754 | { |
755 | /* We have count234(lightable_squares) possibilities, and in |
756 | * lightable_squares_sorted they are sorted with the most desirable |
757 | * first. */ |
758 | c = count234(lightable_squares_sorted); |
759 | if (c == 0) |
760 | break; |
761 | assert(c == count234(lightable_squares_gettable)); |
762 | |
763 | /* Check that the best square available is any good */ |
764 | square = (struct square *)index234(lightable_squares_sorted, 0); |
765 | assert(square); |
766 | |
c0c8f264 |
767 | /* |
768 | * We never want to _decrease_ the loop's perimeter. Making |
769 | * moves that leave the perimeter the same is occasionally |
770 | * useful: if it were _never_ done then the user would be |
771 | * able to deduce illicitly that any degree-zero vertex was |
772 | * on the outside of the loop. So we do it sometimes but |
773 | * not always. |
774 | */ |
775 | if (square->score < 0 || (square->score == 0 && |
776 | random_upto(rs, 2) == 0)) |
6193da8d |
777 | break; |
778 | |
779 | print_tree(lightable_squares_sorted); |
780 | assert(square->score == SQUARE_SCORE(square->x, square->y)); |
781 | assert(SQUARE_STATE(square->x, square->y) == SQUARE_UNLIT); |
782 | assert(square->x >= 0 && square->x < params->w); |
783 | assert(square->y >= 0 && square->y < params->h); |
784 | /* printf("LIGHT SQUARE: [%d,%d], score = %d\n", square->x, square->y, square->score); */ |
785 | |
786 | /* Update data structures */ |
787 | LV_SQUARE_STATE(square->x, square->y) = SQUARE_LIT; |
788 | REMOVE_SQUARE(square); |
789 | |
790 | print_board(params, board); |
791 | |
792 | /* We might have changed the score of any squares up to 2 units away in |
793 | * any direction */ |
794 | for (b = -SCORE_DISTANCE; b <= SCORE_DISTANCE; b++) { |
795 | for (a = -SCORE_DISTANCE; a <= SCORE_DISTANCE; a++) { |
796 | if (!a && !b) |
797 | continue; |
798 | square_pos.x = square->x + a; |
799 | square_pos.y = square->y + b; |
800 | /* printf("Refreshing score for [%d,%d]:\n", square_pos.x, square_pos.y); */ |
801 | if (square_pos.x < 0 || square_pos.x >= params->w || |
802 | square_pos.y < 0 || square_pos.y >= params->h) { |
803 | /* printf(" Out of bounds\n"); */ |
804 | continue; |
805 | } |
806 | tmpsquare = find234(lightable_squares_gettable, &square_pos, |
807 | NULL); |
808 | if (tmpsquare) { |
809 | /* printf(" Removing\n"); */ |
810 | assert(tmpsquare->x == square_pos.x); |
811 | assert(tmpsquare->y == square_pos.y); |
812 | assert(SQUARE_STATE(tmpsquare->x, tmpsquare->y) == |
813 | SQUARE_UNLIT); |
814 | REMOVE_SQUARE(tmpsquare); |
815 | } else { |
816 | /* printf(" Creating\n"); */ |
817 | tmpsquare = snew(struct square); |
818 | tmpsquare->x = square_pos.x; |
819 | tmpsquare->y = square_pos.y; |
820 | tmpsquare->random = random_bits(rs, 31); |
821 | } |
822 | tmpsquare->score = SQUARE_SCORE(tmpsquare->x, tmpsquare->y); |
823 | |
824 | if (IS_LIGHTING_CANDIDATE(tmpsquare->x, tmpsquare->y)) { |
825 | /* printf(" Adding\n"); */ |
826 | ADD_SQUARE(tmpsquare); |
827 | } else { |
828 | /* printf(" Destroying\n"); */ |
829 | sfree(tmpsquare); |
830 | } |
831 | } |
832 | } |
9cfc03b7 |
833 | sfree(square); |
6193da8d |
834 | /* printf("\n\n"); */ |
835 | } |
836 | |
837 | while ((square = delpos234(lightable_squares_gettable, 0)) != NULL) |
838 | sfree(square); |
839 | freetree234(lightable_squares_gettable); |
840 | freetree234(lightable_squares_sorted); |
841 | |
842 | /* Copy out all the clues */ |
843 | for (j = 0; j < params->h; ++j) { |
844 | for (i = 0; i < params->w; ++i) { |
845 | c = SQUARE_STATE(i, j); |
846 | LV_CLUE_AT(state, i, j) = '0'; |
847 | if (SQUARE_STATE(i-1, j) != c) ++LV_CLUE_AT(state, i, j); |
848 | if (SQUARE_STATE(i+1, j) != c) ++LV_CLUE_AT(state, i, j); |
849 | if (SQUARE_STATE(i, j-1) != c) ++LV_CLUE_AT(state, i, j); |
850 | if (SQUARE_STATE(i, j+1) != c) ++LV_CLUE_AT(state, i, j); |
851 | } |
852 | } |
853 | |
854 | sfree(board); |
855 | return clues; |
856 | } |
857 | |
858 | static solver_state *solve_game_rec(const solver_state *sstate); |
859 | |
860 | static int game_has_unique_soln(const game_state *state) |
861 | { |
862 | int ret; |
863 | solver_state *sstate_new; |
864 | solver_state *sstate = new_solver_state((game_state *)state); |
865 | |
866 | sstate_new = solve_game_rec(sstate); |
867 | |
868 | ret = (sstate_new->solver_status == SOLVER_SOLVED); |
869 | |
870 | free_solver_state(sstate_new); |
871 | free_solver_state(sstate); |
872 | |
873 | return ret; |
874 | } |
875 | |
876 | /* Remove clues one at a time at random. */ |
877 | static game_state *remove_clues(game_state *state, random_state *rs) |
878 | { |
879 | int *square_list, squares; |
880 | game_state *ret = dup_game(state), *saved_ret; |
881 | int n; |
882 | |
883 | /* We need to remove some clues. We'll do this by forming a list of all |
884 | * available equivalence classes, shuffling it, then going along one at a |
885 | * time clearing every member of each equivalence class, where removing a |
886 | * class doesn't render the board unsolvable. */ |
887 | squares = state->w * state->h; |
888 | square_list = snewn(squares, int); |
889 | for (n = 0; n < squares; ++n) { |
890 | square_list[n] = n; |
891 | } |
892 | |
893 | shuffle(square_list, squares, sizeof(int), rs); |
894 | |
895 | for (n = 0; n < squares; ++n) { |
896 | saved_ret = dup_game(ret); |
897 | LV_CLUE_AT(ret, square_list[n] % state->w, |
898 | square_list[n] / state->w) = ' '; |
899 | if (game_has_unique_soln(ret)) { |
900 | free_game(saved_ret); |
901 | } else { |
902 | free_game(ret); |
903 | ret = saved_ret; |
904 | } |
905 | } |
9cfc03b7 |
906 | sfree(square_list); |
6193da8d |
907 | |
908 | return ret; |
909 | } |
910 | |
911 | static char *validate_desc(game_params *params, char *desc); |
912 | |
913 | static char *new_game_desc(game_params *params, random_state *rs, |
914 | char **aux, int interactive) |
915 | { |
916 | /* solution and description both use run-length encoding in obvious ways */ |
917 | char *retval; |
918 | char *description = snewn(SQUARE_COUNT(params) + 1, char); |
919 | char *dp = description; |
920 | int i, j; |
921 | int empty_count; |
922 | game_state *state = snew(game_state), *state_new; |
923 | |
924 | state->h = params->h; |
925 | state->w = params->w; |
926 | |
927 | state->hl = snewn(HL_COUNT(params), char); |
928 | state->vl = snewn(VL_COUNT(params), char); |
929 | memset(state->hl, LINE_UNKNOWN, HL_COUNT(params)); |
930 | memset(state->vl, LINE_UNKNOWN, VL_COUNT(params)); |
931 | |
932 | state->solved = state->cheated = FALSE; |
933 | state->recursion_depth = params->rec; |
934 | |
935 | /* Get a new random solvable board with all its clues filled in. Yes, this |
936 | * can loop for ever if the params are suitably unfavourable, but |
937 | * preventing games smaller than 4x4 seems to stop this happening */ |
938 | do { |
939 | state->clues = new_fullyclued_board(params, rs); |
940 | } while (!game_has_unique_soln(state)); |
941 | |
942 | state_new = remove_clues(state, rs); |
943 | free_game(state); |
944 | state = state_new; |
945 | |
946 | empty_count = 0; |
947 | for (j = 0; j < params->h; ++j) { |
948 | for (i = 0; i < params->w; ++i) { |
949 | if (CLUE_AT(state, i, j) == ' ') { |
950 | if (empty_count > 25) { |
9cfc03b7 |
951 | dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); |
6193da8d |
952 | empty_count = 0; |
953 | } |
954 | empty_count++; |
955 | } else { |
956 | if (empty_count) { |
9cfc03b7 |
957 | dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); |
6193da8d |
958 | empty_count = 0; |
959 | } |
9cfc03b7 |
960 | dp += sprintf(dp, "%c", (int)(CLUE_AT(state, i, j))); |
6193da8d |
961 | } |
962 | } |
963 | } |
964 | if (empty_count) |
9cfc03b7 |
965 | dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); |
6193da8d |
966 | |
9cfc03b7 |
967 | free_game(state); |
6193da8d |
968 | retval = dupstr(description); |
969 | sfree(description); |
970 | |
971 | assert(!validate_desc(params, retval)); |
972 | |
973 | return retval; |
974 | } |
975 | |
976 | /* We require that the params pass the test in validate_params and that the |
977 | * description fills the entire game area */ |
978 | static char *validate_desc(game_params *params, char *desc) |
979 | { |
980 | int count = 0; |
981 | |
982 | for (; *desc; ++desc) { |
983 | if (*desc >= '0' && *desc <= '9') { |
984 | count++; |
985 | continue; |
986 | } |
987 | if (*desc >= 'a') { |
988 | count += *desc - 'a' + 1; |
989 | continue; |
990 | } |
991 | return "Unknown character in description"; |
992 | } |
993 | |
994 | if (count < SQUARE_COUNT(params)) |
995 | return "Description too short for board size"; |
996 | if (count > SQUARE_COUNT(params)) |
997 | return "Description too long for board size"; |
998 | |
999 | return NULL; |
1000 | } |
1001 | |
1002 | static game_state *new_game(midend *me, game_params *params, char *desc) |
1003 | { |
1004 | int i,j; |
1005 | game_state *state = snew(game_state); |
1006 | int empties_to_make = 0; |
1007 | int n; |
1008 | const char *dp = desc; |
1009 | |
1010 | state->recursion_depth = params->rec; |
1011 | |
1012 | state->h = params->h; |
1013 | state->w = params->w; |
1014 | |
1015 | state->clues = snewn(SQUARE_COUNT(params), char); |
1016 | state->hl = snewn(HL_COUNT(params), char); |
1017 | state->vl = snewn(VL_COUNT(params), char); |
1018 | |
1019 | state->solved = state->cheated = FALSE; |
1020 | |
1021 | for (j = 0 ; j < params->h; ++j) { |
1022 | for (i = 0 ; i < params->w; ++i) { |
1023 | if (empties_to_make) { |
1024 | empties_to_make--; |
1025 | LV_CLUE_AT(state, i, j) = ' '; |
1026 | continue; |
1027 | } |
1028 | |
1029 | assert(*dp); |
1030 | n = *dp - '0'; |
1031 | if (n >=0 && n < 10) { |
1032 | LV_CLUE_AT(state, i, j) = *dp; |
1033 | } else { |
1034 | n = *dp - 'a' + 1; |
1035 | assert(n > 0); |
1036 | LV_CLUE_AT(state, i, j) = ' '; |
1037 | empties_to_make = n - 1; |
1038 | } |
1039 | ++dp; |
1040 | } |
1041 | } |
1042 | |
1043 | memset(state->hl, LINE_UNKNOWN, HL_COUNT(params)); |
1044 | memset(state->vl, LINE_UNKNOWN, VL_COUNT(params)); |
1045 | |
1046 | return state; |
1047 | } |
1048 | |
1049 | enum { LOOP_NONE=0, LOOP_SOLN, LOOP_NOT_SOLN }; |
1050 | |
1051 | /* Starting at dot [i,j] moves around 'state' removing lines until it's clear |
1052 | * whether or not the starting dot was on a loop. Returns boolean specifying |
1053 | * whether a loop was found. loop_status calls this and assumes that if state |
1054 | * has any lines set, this function will always remove at least one. */ |
1055 | static int destructively_find_loop(game_state *state) |
1056 | { |
1057 | int a, b, i, j, new_i, new_j, n; |
1058 | char *lp; |
1059 | |
1060 | lp = (char *)memchr(state->hl, LINE_YES, HL_COUNT(state)); |
1061 | if (!lp) { |
1062 | /* We know we're going to return false but we have to fulfil our |
1063 | * contract */ |
1064 | lp = (char *)memchr(state->vl, LINE_YES, VL_COUNT(state)); |
1065 | if (lp) |
1066 | *lp = LINE_NO; |
1067 | |
1068 | return FALSE; |
1069 | } |
1070 | |
1071 | n = lp - state->hl; |
1072 | |
1073 | i = n % state->w; |
1074 | j = n / state->w; |
1075 | |
1076 | assert(i + j * state->w == n); /* because I'm feeling stupid */ |
1077 | /* Save start position */ |
1078 | a = i; |
1079 | b = j; |
1080 | |
1081 | /* Delete one line from the potential loop */ |
1082 | if (LEFTOF_DOT(state, i, j) == LINE_YES) { |
1083 | LV_LEFTOF_DOT(state, i, j) = LINE_NO; |
1084 | i--; |
1085 | } else if (ABOVE_DOT(state, i, j) == LINE_YES) { |
1086 | LV_ABOVE_DOT(state, i, j) = LINE_NO; |
1087 | j--; |
1088 | } else if (RIGHTOF_DOT(state, i, j) == LINE_YES) { |
1089 | LV_RIGHTOF_DOT(state, i, j) = LINE_NO; |
1090 | i++; |
1091 | } else if (BELOW_DOT(state, i, j) == LINE_YES) { |
1092 | LV_BELOW_DOT(state, i, j) = LINE_NO; |
1093 | j++; |
1094 | } else { |
1095 | return FALSE; |
1096 | } |
1097 | |
1098 | do { |
1099 | /* From the current position of [i,j] there needs to be exactly one |
1100 | * line */ |
1101 | new_i = new_j = -1; |
1102 | |
1103 | #define HANDLE_DIR(dir_dot, x, y) \ |
1104 | if (dir_dot(state, i, j) == LINE_YES) { \ |
1105 | if (new_i != -1 || new_j != -1) \ |
1106 | return FALSE; \ |
1107 | new_i = (i)+(x); \ |
1108 | new_j = (j)+(y); \ |
1109 | LV_##dir_dot(state, i, j) = LINE_NO; \ |
1110 | } |
1111 | HANDLE_DIR(ABOVE_DOT, 0, -1); |
1112 | HANDLE_DIR(BELOW_DOT, 0, +1); |
1113 | HANDLE_DIR(LEFTOF_DOT, -1, 0); |
1114 | HANDLE_DIR(RIGHTOF_DOT, +1, 0); |
1115 | #undef HANDLE_DIR |
1116 | if (new_i == -1 || new_j == -1) { |
1117 | return FALSE; |
1118 | } |
1119 | |
1120 | i = new_i; |
1121 | j = new_j; |
1122 | } while (i != a || j != b); |
1123 | |
1124 | return TRUE; |
1125 | } |
1126 | |
1127 | static int loop_status(game_state *state) |
1128 | { |
1129 | int i, j, n; |
1130 | game_state *tmpstate; |
1131 | int loop_found = FALSE, non_loop_found = FALSE, any_lines_found = FALSE; |
1132 | |
1133 | #define BAD_LOOP_FOUND \ |
1134 | do { free_game(tmpstate); return LOOP_NOT_SOLN; } while(0) |
1135 | |
1136 | /* Repeatedly look for loops until we either run out of lines to consider |
1137 | * or discover for sure that the board fails on the grounds of having no |
1138 | * loop */ |
1139 | tmpstate = dup_game(state); |
1140 | |
1141 | while (TRUE) { |
1142 | if (!memchr(tmpstate->hl, LINE_YES, HL_COUNT(tmpstate)) && |
1143 | !memchr(tmpstate->vl, LINE_YES, VL_COUNT(tmpstate))) { |
1144 | break; |
1145 | } |
1146 | any_lines_found = TRUE; |
1147 | |
1148 | if (loop_found) |
1149 | BAD_LOOP_FOUND; |
1150 | if (destructively_find_loop(tmpstate)) { |
1151 | loop_found = TRUE; |
1152 | if (non_loop_found) |
1153 | BAD_LOOP_FOUND; |
1154 | } else { |
1155 | non_loop_found = TRUE; |
1156 | } |
1157 | } |
1158 | |
1159 | free_game(tmpstate); |
1160 | |
1161 | if (!any_lines_found) |
1162 | return LOOP_NONE; |
1163 | |
1164 | if (non_loop_found) { |
1165 | assert(!loop_found); /* should have dealt with this already */ |
1166 | return LOOP_NONE; |
1167 | } |
1168 | |
1169 | /* Check that every clue is satisfied */ |
1170 | for (j = 0; j < state->h; ++j) { |
1171 | for (i = 0; i < state->w; ++i) { |
1172 | n = CLUE_AT(state, i, j); |
1173 | if (n != ' ') { |
1174 | if (square_order(state, i, j, LINE_YES) != n - '0') { |
1175 | return LOOP_NOT_SOLN; |
1176 | } |
1177 | } |
1178 | } |
1179 | } |
1180 | |
1181 | return LOOP_SOLN; |
1182 | } |
1183 | |
1184 | /* Sums the lengths of the numbers in range [0,n) */ |
1185 | /* See equivalent function in solo.c for justification of this. */ |
9cfc03b7 |
1186 | static int len_0_to_n(int n) |
6193da8d |
1187 | { |
1188 | int len = 1; /* Counting 0 as a bit of a special case */ |
1189 | int i; |
1190 | |
1191 | for (i = 1; i < n; i *= 10) { |
1192 | len += max(n - i, 0); |
1193 | } |
1194 | |
1195 | return len; |
1196 | } |
1197 | |
1198 | static char *encode_solve_move(const game_state *state) |
1199 | { |
1200 | int len, i, j; |
1201 | char *ret, *p; |
1202 | /* This is going to return a string representing the moves needed to set |
1203 | * every line in a grid to be the same as the ones in 'state'. The exact |
1204 | * length of this string is predictable. */ |
1205 | |
1206 | len = 1; /* Count the 'S' prefix */ |
1207 | /* Numbers in horizontal lines */ |
1208 | /* Horizontal lines, x position */ |
1209 | len += len_0_to_n(state->w) * (state->h + 1); |
1210 | /* Horizontal lines, y position */ |
1211 | len += len_0_to_n(state->h + 1) * (state->w); |
1212 | /* Vertical lines, y position */ |
1213 | len += len_0_to_n(state->h) * (state->w + 1); |
1214 | /* Vertical lines, x position */ |
1215 | len += len_0_to_n(state->w + 1) * (state->h); |
1216 | /* For each line we also have two letters and a comma */ |
1217 | len += 3 * (HL_COUNT(state) + VL_COUNT(state)); |
1218 | |
1219 | ret = snewn(len + 1, char); |
1220 | p = ret; |
1221 | |
1222 | p += sprintf(p, "S"); |
1223 | |
1224 | for (j = 0; j < state->h + 1; ++j) { |
1225 | for (i = 0; i < state->w; ++i) { |
1226 | switch (RIGHTOF_DOT(state, i, j)) { |
1227 | case LINE_YES: |
1228 | p += sprintf(p, "%d,%dhy", i, j); |
1229 | break; |
1230 | case LINE_NO: |
1231 | p += sprintf(p, "%d,%dhn", i, j); |
1232 | break; |
1233 | /* default: */ |
1234 | /* I'm going to forgive this because I think the results |
1235 | * are cute. */ |
1236 | /* assert(!"Solver produced incomplete solution!"); */ |
1237 | } |
1238 | } |
1239 | } |
1240 | |
1241 | for (j = 0; j < state->h; ++j) { |
1242 | for (i = 0; i < state->w + 1; ++i) { |
1243 | switch (BELOW_DOT(state, i, j)) { |
1244 | case LINE_YES: |
1245 | p += sprintf(p, "%d,%dvy", i, j); |
1246 | break; |
1247 | case LINE_NO: |
1248 | p += sprintf(p, "%d,%dvn", i, j); |
1249 | break; |
1250 | /* default: */ |
1251 | /* I'm going to forgive this because I think the results |
1252 | * are cute. */ |
1253 | /* assert(!"Solver produced incomplete solution!"); */ |
1254 | } |
1255 | } |
1256 | } |
1257 | |
1258 | /* No point in doing sums like that if they're going to be wrong */ |
1259 | assert(strlen(ret) <= (size_t)len); |
9cfc03b7 |
1260 | return ret; |
6193da8d |
1261 | } |
1262 | |
1263 | /* BEGIN SOLVER IMPLEMENTATION */ |
1264 | |
1265 | /* For each pair of lines through each dot we store a bit for whether |
1266 | * exactly one of those lines is ON, and in separate arrays we store whether |
1267 | * at least one is on and whether at most 1 is on. (If we know both or |
1268 | * neither is on that's already stored more directly.) That's six bits per |
1269 | * dot. Bit number n represents the lines shown in dot_type_dirs[n]. */ |
1270 | |
1271 | enum dline { |
1272 | DLINE_VERT = 0, |
1273 | DLINE_HORIZ = 1, |
1274 | DLINE_UL = 2, |
1275 | DLINE_DR = 3, |
1276 | DLINE_UR = 4, |
1277 | DLINE_DL = 5 |
1278 | }; |
1279 | |
1280 | #define OPP_DLINE(dline) (dline ^ 1) |
1281 | |
1282 | |
1283 | #define SQUARE_DLINES \ |
1284 | HANDLE_DLINE(DLINE_UL, RIGHTOF_SQUARE, BELOW_SQUARE, 1, 1); \ |
1285 | HANDLE_DLINE(DLINE_UR, LEFTOF_SQUARE, BELOW_SQUARE, 0, 1); \ |
1286 | HANDLE_DLINE(DLINE_DL, RIGHTOF_SQUARE, ABOVE_SQUARE, 1, 0); \ |
1287 | HANDLE_DLINE(DLINE_DR, LEFTOF_SQUARE, ABOVE_SQUARE, 0, 0); |
1288 | |
1289 | #define DOT_DLINES \ |
1290 | HANDLE_DLINE(DLINE_VERT, ABOVE_DOT, BELOW_DOT); \ |
1291 | HANDLE_DLINE(DLINE_HORIZ, LEFTOF_DOT, RIGHTOF_DOT); \ |
1292 | HANDLE_DLINE(DLINE_UL, ABOVE_DOT, LEFTOF_DOT); \ |
1293 | HANDLE_DLINE(DLINE_UR, ABOVE_DOT, RIGHTOF_DOT); \ |
1294 | HANDLE_DLINE(DLINE_DL, BELOW_DOT, LEFTOF_DOT); \ |
1295 | HANDLE_DLINE(DLINE_DR, BELOW_DOT, RIGHTOF_DOT); |
1296 | |
1297 | static void array_setall(char *array, char from, char to, int len) |
1298 | { |
1299 | char *p = array, *p_old = p; |
1300 | int len_remaining = len; |
1301 | |
1302 | while ((p = memchr(p, from, len_remaining))) { |
1303 | *p = to; |
1304 | len_remaining -= p - p_old; |
1305 | p_old = p; |
1306 | } |
1307 | } |
1308 | |
1309 | |
1310 | static int game_states_equal(const game_state *state1, |
1311 | const game_state *state2) |
1312 | { |
1313 | /* This deliberately doesn't check _all_ fields, just the ones that make a |
1314 | * game state 'interesting' from the POV of the solver */ |
1315 | /* XXX review this */ |
1316 | if (state1 == state2) |
1317 | return 1; |
1318 | |
1319 | if (!state1 || !state2) |
1320 | return 0; |
1321 | |
1322 | if (state1->w != state2->w || state1->h != state2->h) |
1323 | return 0; |
1324 | |
1325 | if (memcmp(state1->hl, state2->hl, HL_COUNT(state1))) |
1326 | return 0; |
1327 | |
1328 | if (memcmp(state1->vl, state2->vl, VL_COUNT(state1))) |
1329 | return 0; |
1330 | |
1331 | return 1; |
1332 | } |
1333 | |
1334 | static int solver_states_equal(const solver_state *sstate1, |
1335 | const solver_state *sstate2) |
1336 | { |
1337 | if (!sstate1) { |
1338 | if (!sstate2) |
1339 | return TRUE; |
1340 | else |
1341 | return FALSE; |
1342 | } |
1343 | |
1344 | if (!game_states_equal(sstate1->state, sstate2->state)) { |
1345 | return 0; |
1346 | } |
1347 | |
1348 | /* XXX fields missing, needs review */ |
1349 | /* XXX we're deliberately not looking at solver_state as it's only a cache */ |
1350 | |
1351 | if (memcmp(sstate1->dot_atleastone, sstate2->dot_atleastone, |
1352 | DOT_COUNT(sstate1->state))) { |
1353 | return 0; |
1354 | } |
1355 | |
1356 | if (memcmp(sstate1->dot_atmostone, sstate2->dot_atmostone, |
1357 | DOT_COUNT(sstate1->state))) { |
1358 | return 0; |
1359 | } |
1360 | |
1361 | /* handle dline_identical here */ |
1362 | |
1363 | return 1; |
1364 | } |
1365 | |
1366 | static void dot_setall_dlines(solver_state *sstate, enum dline dl, int i, int j, |
1367 | enum line_state line_old, enum line_state line_new) |
1368 | { |
1369 | game_state *state = sstate->state; |
1370 | |
1371 | /* First line in dline */ |
1372 | switch (dl) { |
1373 | case DLINE_UL: |
1374 | case DLINE_UR: |
1375 | case DLINE_VERT: |
1376 | if (j > 0 && ABOVE_DOT(state, i, j) == line_old) |
1377 | LV_ABOVE_DOT(state, i, j) = line_new; |
1378 | break; |
1379 | case DLINE_DL: |
1380 | case DLINE_DR: |
1381 | if (j <= (state)->h && BELOW_DOT(state, i, j) == line_old) |
1382 | LV_BELOW_DOT(state, i, j) = line_new; |
1383 | break; |
1384 | case DLINE_HORIZ: |
1385 | if (i > 0 && LEFTOF_DOT(state, i, j) == line_old) |
1386 | LV_LEFTOF_DOT(state, i, j) = line_new; |
1387 | break; |
1388 | } |
1389 | |
1390 | /* Second line in dline */ |
1391 | switch (dl) { |
1392 | case DLINE_UL: |
1393 | case DLINE_DL: |
1394 | if (i > 0 && LEFTOF_DOT(state, i, j) == line_old) |
1395 | LV_LEFTOF_DOT(state, i, j) = line_new; |
1396 | break; |
1397 | case DLINE_UR: |
1398 | case DLINE_DR: |
1399 | case DLINE_HORIZ: |
1400 | if (i <= (state)->w && RIGHTOF_DOT(state, i, j) == line_old) |
1401 | LV_RIGHTOF_DOT(state, i, j) = line_new; |
1402 | break; |
1403 | case DLINE_VERT: |
1404 | if (j <= (state)->h && BELOW_DOT(state, i, j) == line_old) |
1405 | LV_BELOW_DOT(state, i, j) = line_new; |
1406 | break; |
1407 | } |
1408 | } |
1409 | |
1410 | static void update_solver_status(solver_state *sstate) |
1411 | { |
1412 | if (sstate->solver_status == SOLVER_INCOMPLETE) { |
1413 | switch (loop_status(sstate->state)) { |
1414 | case LOOP_NONE: |
1415 | sstate->solver_status = SOLVER_INCOMPLETE; |
1416 | break; |
1417 | case LOOP_SOLN: |
1418 | if (sstate->solver_status != SOLVER_AMBIGUOUS) |
1419 | sstate->solver_status = SOLVER_SOLVED; |
1420 | break; |
1421 | case LOOP_NOT_SOLN: |
1422 | sstate->solver_status = SOLVER_MISTAKE; |
1423 | break; |
1424 | } |
1425 | } |
1426 | } |
1427 | |
1428 | |
1429 | /* This will return a dynamically allocated solver_state containing the (more) |
1430 | * solved grid */ |
1431 | static solver_state *solve_game_rec(const solver_state *sstate_start) |
1432 | { |
1433 | int i, j; |
1434 | int current_yes, current_no, desired; |
1435 | solver_state *sstate, *sstate_saved, *sstate_tmp; |
1436 | int t; |
1437 | /* char *text; */ |
1438 | solver_state *sstate_rec_solved; |
1439 | int recursive_soln_count; |
1440 | |
1441 | #if 0 |
1442 | printf("solve_game_rec: recursion_remaining = %d\n", |
1443 | sstate_start->recursion_remaining); |
1444 | #endif |
1445 | |
1446 | sstate = dup_solver_state((solver_state *)sstate_start); |
1447 | |
1448 | #if 0 |
1449 | text = game_text_format(sstate->state); |
1450 | printf("%s\n", text); |
1451 | sfree(text); |
1452 | #endif |
1453 | |
1454 | #define RETURN_IF_SOLVED \ |
1455 | do { \ |
1456 | update_solver_status(sstate); \ |
1457 | if (sstate->solver_status != SOLVER_INCOMPLETE) { \ |
1458 | free_solver_state(sstate_saved); \ |
1459 | return sstate; \ |
1460 | } \ |
1461 | } while (0) |
1462 | |
1463 | sstate_saved = NULL; |
1464 | RETURN_IF_SOLVED; |
1465 | |
1466 | nonrecursive_solver: |
1467 | |
1468 | while (1) { |
1469 | sstate_saved = dup_solver_state(sstate); |
1470 | |
1471 | /* First we do the 'easy' work, that might cause concrete results */ |
1472 | |
1473 | /* Per-square deductions */ |
1474 | for (j = 0; j < sstate->state->h; ++j) { |
1475 | for (i = 0; i < sstate->state->w; ++i) { |
1476 | /* Begin rules that look at the clue (if there is one) */ |
1477 | desired = CLUE_AT(sstate->state, i, j); |
1478 | if (desired == ' ') |
1479 | continue; |
1480 | desired = desired - '0'; |
1481 | current_yes = square_order(sstate->state, i, j, LINE_YES); |
1482 | current_no = square_order(sstate->state, i, j, LINE_NO); |
1483 | |
1484 | if (desired <= current_yes) { |
1485 | square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_NO); |
1486 | continue; |
1487 | } |
1488 | |
1489 | if (4 - desired <= current_no) { |
1490 | square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_YES); |
1491 | } |
1492 | } |
1493 | } |
1494 | |
1495 | RETURN_IF_SOLVED; |
1496 | |
1497 | /* Per-dot deductions */ |
1498 | for (j = 0; j < sstate->state->h + 1; ++j) { |
1499 | for (i = 0; i < sstate->state->w + 1; ++i) { |
1500 | switch (dot_order(sstate->state, i, j, LINE_YES)) { |
1501 | case 0: |
1502 | if (dot_order(sstate->state, i, j, LINE_NO) == 3) { |
1503 | dot_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_NO); |
1504 | } |
1505 | break; |
1506 | case 1: |
1507 | switch (dot_order(sstate->state, i, j, LINE_NO)) { |
1508 | #define H1(dline, dir1_dot, dir2_dot, dot_howmany) \ |
1509 | if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN) { \ |
1510 | if (dir2_dot(sstate->state, i, j) == LINE_UNKNOWN){ \ |
1511 | sstate->dot_howmany \ |
1512 | [i + (sstate->state->w + 1) * j] |= 1<<dline; \ |
1513 | } \ |
1514 | } |
1515 | case 1: |
1516 | #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \ |
1517 | H1(dline, dir1_dot, dir2_dot, dot_atleastone) |
1518 | /* 1 yes, 1 no, so exactly one of unknowns is yes */ |
1519 | DOT_DLINES; |
1520 | #undef HANDLE_DLINE |
1521 | /* fall through */ |
1522 | case 0: |
1523 | #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \ |
1524 | H1(dline, dir1_dot, dir2_dot, dot_atmostone) |
1525 | /* 1 yes, fewer than 2 no, so at most one of |
1526 | * unknowns is yes */ |
1527 | DOT_DLINES; |
1528 | #undef HANDLE_DLINE |
1529 | #undef H1 |
1530 | break; |
1531 | case 2: /* 1 yes, 2 no */ |
1532 | dot_setall(sstate->state, i, j, |
1533 | LINE_UNKNOWN, LINE_YES); |
1534 | break; |
1535 | } |
1536 | break; |
1537 | case 2: |
1538 | case 3: |
1539 | dot_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_NO); |
1540 | } |
1541 | #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \ |
1542 | if (sstate->dot_atleastone \ |
1543 | [i + (sstate->state->w + 1) * j] & 1<<dline) { \ |
1544 | sstate->dot_atmostone \ |
1545 | [i + (sstate->state->w + 1) * j] |= 1<<OPP_DLINE(dline); \ |
1546 | } |
1547 | /* If at least one of a dline in a dot is YES, at most one of |
1548 | * the opposite dline to that dot must be YES. */ |
1549 | DOT_DLINES; |
1550 | #undef HANDLE_DLINE |
1551 | } |
1552 | } |
1553 | |
1554 | /* More obscure per-square operations */ |
1555 | for (j = 0; j < sstate->state->h; ++j) { |
1556 | for (i = 0; i < sstate->state->w; ++i) { |
1557 | #define H1(dline, dir1_sq, dir2_sq, a, b, dot_howmany, line_query, line_set) \ |
1558 | if (sstate->dot_howmany[i+a + (sstate->state->w + 1) * (j+b)] &\ |
1559 | 1<<dline) { \ |
1560 | t = dir1_sq(sstate->state, i, j); \ |
1561 | if (t == line_query) \ |
1562 | dir2_sq(sstate->state, i, j) = line_set; \ |
1563 | else { \ |
1564 | t = dir2_sq(sstate->state, i, j); \ |
1565 | if (t == line_query) \ |
1566 | dir1_sq(sstate->state, i, j) = line_set; \ |
1567 | } \ |
1568 | } |
1569 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1570 | H1(dline, dir1_sq, dir2_sq, a, b, dot_atmostone, \ |
1571 | LINE_YES, LINE_NO) |
1572 | /* If at most one of the DLINE is on, and one is definitely on, |
1573 | * set the other to definitely off */ |
1574 | SQUARE_DLINES; |
1575 | #undef HANDLE_DLINE |
1576 | |
1577 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1578 | H1(dline, dir1_sq, dir2_sq, a, b, dot_atleastone, \ |
1579 | LINE_NO, LINE_YES) |
1580 | /* If at least one of the DLINE is on, and one is definitely |
1581 | * off, set the other to definitely on */ |
1582 | SQUARE_DLINES; |
1583 | #undef HANDLE_DLINE |
1584 | #undef H1 |
1585 | |
1586 | switch (CLUE_AT(sstate->state, i, j)) { |
1587 | case '0': |
1588 | case '1': |
1589 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1590 | /* At most one of any DLINE can be set */ \ |
1591 | sstate->dot_atmostone \ |
1592 | [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \ |
1593 | /* This DLINE provides enough YESes to solve the clue */\ |
1594 | if (sstate->dot_atleastone \ |
1595 | [i+a + (sstate->state->w + 1) * (j+b)] & \ |
1596 | 1<<dline) { \ |
1597 | dot_setall_dlines(sstate, OPP_DLINE(dline), \ |
1598 | i+(1-a), j+(1-b), \ |
1599 | LINE_UNKNOWN, LINE_NO); \ |
1600 | } |
1601 | SQUARE_DLINES; |
1602 | #undef HANDLE_DLINE |
1603 | break; |
1604 | case '2': |
1605 | #define H1(dline, dot_at1one, dot_at2one, a, b) \ |
1606 | if (sstate->dot_at1one \ |
1607 | [i+a + (sstate->state->w + 1) * (j+b)] & \ |
1608 | 1<<dline) { \ |
1609 | sstate->dot_at2one \ |
1610 | [i+(1-a) + (sstate->state->w + 1) * (j+(1-b))] |= \ |
1611 | 1<<OPP_DLINE(dline); \ |
1612 | } |
1613 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1614 | H1(dline, dot_atleastone, dot_atmostone, a, b); \ |
1615 | H1(dline, dot_atmostone, dot_atleastone, a, b); |
1616 | /* If at least one of one DLINE is set, at most one of |
1617 | * the opposing one is and vice versa */ |
1618 | SQUARE_DLINES; |
1619 | #undef HANDLE_DLINE |
1620 | #undef H1 |
1621 | break; |
1622 | case '3': |
1623 | case '4': |
1624 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1625 | /* At least one of any DLINE can be set */ \ |
1626 | sstate->dot_atleastone \ |
1627 | [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \ |
1628 | /* This DLINE provides enough NOs to solve the clue */ \ |
1629 | if (sstate->dot_atmostone \ |
1630 | [i+a + (sstate->state->w + 1) * (j+b)] & \ |
1631 | 1<<dline) { \ |
1632 | dot_setall_dlines(sstate, OPP_DLINE(dline), \ |
1633 | i+(1-a), j+(1-b), \ |
1634 | LINE_UNKNOWN, LINE_YES); \ |
1635 | } |
1636 | SQUARE_DLINES; |
1637 | #undef HANDLE_DLINE |
1638 | break; |
1639 | } |
1640 | } |
1641 | } |
1642 | |
1643 | if (solver_states_equal(sstate, sstate_saved)) { |
1644 | int edgecount = 0, clues = 0, satclues = 0, sm1clues = 0; |
1645 | int d; |
1646 | |
1647 | /* |
1648 | * Go through the grid and update for all the new edges. |
1649 | * Since merge_dots() is idempotent, the simplest way to |
1650 | * do this is just to update for _all_ the edges. |
1651 | * |
1652 | * Also, while we're here, we count the edges, count the |
1653 | * clues, count the satisfied clues, and count the |
1654 | * satisfied-minus-one clues. |
1655 | */ |
1656 | for (j = 0; j <= sstate->state->h; ++j) { |
1657 | for (i = 0; i <= sstate->state->w; ++i) { |
1658 | if (RIGHTOF_DOT(sstate->state, i, j) == LINE_YES) { |
1659 | merge_dots(sstate, i, j, i+1, j); |
1660 | edgecount++; |
1661 | } |
1662 | if (BELOW_DOT(sstate->state, i, j) == LINE_YES) { |
1663 | merge_dots(sstate, i, j, i, j+1); |
1664 | edgecount++; |
1665 | } |
1666 | |
1667 | if (CLUE_AT(sstate->state, i, j) != ' ') { |
1668 | int c = CLUE_AT(sstate->state, i, j) - '0'; |
1669 | int o = square_order(sstate->state, i, j, LINE_YES); |
1670 | if (o == c) |
1671 | satclues++; |
1672 | else if (o == c-1) |
1673 | sm1clues++; |
1674 | clues++; |
1675 | } |
1676 | } |
1677 | } |
1678 | |
1679 | /* |
1680 | * Now go through looking for LINE_UNKNOWN edges which |
1681 | * connect two dots that are already in the same |
1682 | * equivalence class. If we find one, test to see if the |
1683 | * loop it would create is a solution. |
1684 | */ |
1685 | for (j = 0; j <= sstate->state->h; ++j) { |
1686 | for (i = 0; i <= sstate->state->w; ++i) { |
1687 | for (d = 0; d < 2; d++) { |
1688 | int i2, j2, eqclass, val; |
1689 | |
1690 | if (d == 0) { |
1691 | if (RIGHTOF_DOT(sstate->state, i, j) != |
1692 | LINE_UNKNOWN) |
1693 | continue; |
1694 | i2 = i+1; |
1695 | j2 = j; |
1696 | } else { |
1697 | if (BELOW_DOT(sstate->state, i, j) != |
1698 | LINE_UNKNOWN) |
1699 | continue; |
1700 | i2 = i; |
1701 | j2 = j+1; |
1702 | } |
1703 | |
1704 | eqclass = dsf_canonify(sstate->dotdsf, |
1705 | j * (sstate->state->w+1) + i); |
1706 | if (eqclass != dsf_canonify(sstate->dotdsf, |
1707 | j2 * (sstate->state->w+1) + |
1708 | i2)) |
1709 | continue; |
1710 | |
1711 | val = LINE_NO; /* loop is bad until proven otherwise */ |
1712 | |
1713 | /* |
1714 | * This edge would form a loop. Next |
1715 | * question: how long would the loop be? |
1716 | * Would it equal the total number of edges |
1717 | * (plus the one we'd be adding if we added |
1718 | * it)? |
1719 | */ |
1720 | if (sstate->looplen[eqclass] == edgecount + 1) { |
1721 | int sm1_nearby; |
1722 | int cx, cy; |
1723 | |
1724 | /* |
1725 | * This edge would form a loop which |
1726 | * took in all the edges in the entire |
1727 | * grid. So now we need to work out |
1728 | * whether it would be a valid solution |
1729 | * to the puzzle, which means we have to |
1730 | * check if it satisfies all the clues. |
1731 | * This means that every clue must be |
1732 | * either satisfied or satisfied-minus- |
1733 | * 1, and also that the number of |
1734 | * satisfied-minus-1 clues must be at |
1735 | * most two and they must lie on either |
1736 | * side of this edge. |
1737 | */ |
1738 | sm1_nearby = 0; |
1739 | cx = i - (j2-j); |
1740 | cy = j - (i2-i); |
1741 | if (CLUE_AT(sstate->state, cx,cy) != ' ' && |
1742 | square_order(sstate->state, cx,cy, LINE_YES) == |
1743 | CLUE_AT(sstate->state, cx,cy) - '0' - 1) |
1744 | sm1_nearby++; |
1745 | if (CLUE_AT(sstate->state, i, j) != ' ' && |
1746 | square_order(sstate->state, i, j, LINE_YES) == |
1747 | CLUE_AT(sstate->state, i, j) - '0' - 1) |
1748 | sm1_nearby++; |
1749 | if (sm1clues == sm1_nearby && |
1750 | sm1clues + satclues == clues) |
1751 | val = LINE_YES; /* loop is good! */ |
1752 | } |
1753 | |
1754 | /* |
1755 | * Right. Now we know that adding this edge |
1756 | * would form a loop, and we know whether |
1757 | * that loop would be a viable solution or |
1758 | * not. |
1759 | * |
1760 | * If adding this edge produces a solution, |
1761 | * then we know we've found _a_ solution but |
1762 | * we don't know that it's _the_ solution - |
1763 | * if it were provably the solution then |
1764 | * we'd have deduced this edge some time ago |
1765 | * without the need to do loop detection. So |
1766 | * in this state we return SOLVER_AMBIGUOUS, |
1767 | * which has the effect that hitting Solve |
1768 | * on a user-provided puzzle will fill in a |
1769 | * solution but using the solver to |
1770 | * construct new puzzles won't consider this |
1771 | * a reasonable deduction for the user to |
1772 | * make. |
1773 | */ |
1774 | if (d == 0) |
1775 | LV_RIGHTOF_DOT(sstate->state, i, j) = val; |
1776 | else |
1777 | LV_BELOW_DOT(sstate->state, i, j) = val; |
1778 | if (val == LINE_YES) { |
1779 | sstate->solver_status = SOLVER_AMBIGUOUS; |
1780 | goto finished_loop_checking; |
1781 | } |
1782 | } |
1783 | } |
1784 | } |
1785 | |
1786 | finished_loop_checking: |
1787 | |
1788 | RETURN_IF_SOLVED; |
1789 | } |
1790 | |
1791 | if (solver_states_equal(sstate, sstate_saved)) { |
1792 | /* Solver has stopped making progress so we terminate */ |
1793 | free_solver_state(sstate_saved); |
1794 | break; |
1795 | } |
1796 | |
1797 | free_solver_state(sstate_saved); |
1798 | } |
1799 | |
1800 | if (sstate->solver_status == SOLVER_SOLVED || |
1801 | sstate->solver_status == SOLVER_AMBIGUOUS) { |
1802 | /* s/LINE_UNKNOWN/LINE_NO/g */ |
1803 | array_setall(sstate->state->hl, LINE_UNKNOWN, LINE_NO, |
1804 | HL_COUNT(sstate->state)); |
1805 | array_setall(sstate->state->vl, LINE_UNKNOWN, LINE_NO, |
1806 | VL_COUNT(sstate->state)); |
1807 | return sstate; |
1808 | } |
1809 | |
1810 | /* Perform recursive calls */ |
1811 | if (sstate->recursion_remaining) { |
1812 | sstate->recursion_remaining--; |
1813 | |
1814 | sstate_saved = dup_solver_state(sstate); |
1815 | |
1816 | recursive_soln_count = 0; |
1817 | sstate_rec_solved = NULL; |
1818 | |
1819 | /* Memory management: |
1820 | * sstate_saved won't be modified but needs to be freed when we have |
1821 | * finished with it. |
1822 | * sstate is expected to contain our 'best' solution by the time we |
1823 | * finish this section of code. It's the thing we'll try adding lines |
1824 | * to, seeing if they make it more solvable. |
1825 | * If sstate_rec_solved is non-NULL, it will supersede sstate |
1826 | * eventually. sstate_tmp should not hold a value persistently. |
1827 | */ |
1828 | |
1829 | /* NB SOLVER_AMBIGUOUS is like SOLVER_SOLVED except the solver is aware |
1830 | * of the possibility of additional solutions. So as soon as we have a |
1831 | * SOLVER_AMBIGUOUS we can safely propagate it back to our caller, but |
1832 | * if we get a SOLVER_SOLVED we want to keep trying in case we find |
1833 | * further solutions and have to mark it ambiguous. |
1834 | */ |
1835 | |
1836 | #define DO_RECURSIVE_CALL(dir_dot) \ |
1837 | if (dir_dot(sstate->state, i, j) == LINE_UNKNOWN) { \ |
1838 | debug(("Trying " #dir_dot " at [%d,%d]\n", i, j)); \ |
1839 | LV_##dir_dot(sstate->state, i, j) = LINE_YES; \ |
1840 | sstate_tmp = solve_game_rec(sstate); \ |
1841 | switch (sstate_tmp->solver_status) { \ |
1842 | case SOLVER_AMBIGUOUS: \ |
1843 | debug(("Solver ambiguous, returning\n")); \ |
1844 | sstate_rec_solved = sstate_tmp; \ |
1845 | goto finished_recursion; \ |
1846 | case SOLVER_SOLVED: \ |
1847 | switch (++recursive_soln_count) { \ |
1848 | case 1: \ |
1849 | debug(("One solution found\n")); \ |
1850 | sstate_rec_solved = sstate_tmp; \ |
1851 | break; \ |
1852 | case 2: \ |
1853 | debug(("Ambiguous solutions found\n")); \ |
1854 | free_solver_state(sstate_tmp); \ |
1855 | sstate_rec_solved->solver_status = SOLVER_AMBIGUOUS;\ |
1856 | goto finished_recursion; \ |
1857 | default: \ |
1858 | assert(!"recursive_soln_count out of range"); \ |
1859 | break; \ |
1860 | } \ |
1861 | break; \ |
1862 | case SOLVER_MISTAKE: \ |
1863 | debug(("Non-solution found\n")); \ |
1864 | free_solver_state(sstate_tmp); \ |
1865 | free_solver_state(sstate_saved); \ |
1866 | LV_##dir_dot(sstate->state, i, j) = LINE_NO; \ |
1867 | goto nonrecursive_solver; \ |
1868 | case SOLVER_INCOMPLETE: \ |
1869 | debug(("Recursive step inconclusive\n")); \ |
1870 | free_solver_state(sstate_tmp); \ |
1871 | break; \ |
1872 | } \ |
1873 | free_solver_state(sstate); \ |
1874 | sstate = dup_solver_state(sstate_saved); \ |
1875 | } |
1876 | |
1877 | for (j = 0; j < sstate->state->h + 1; ++j) { |
1878 | for (i = 0; i < sstate->state->w + 1; ++i) { |
1879 | /* Only perform recursive calls on 'loose ends' */ |
1880 | if (dot_order(sstate->state, i, j, LINE_YES) == 1) { |
1881 | if (LEFTOF_DOT(sstate->state, i, j) == LINE_UNKNOWN) |
1882 | DO_RECURSIVE_CALL(LEFTOF_DOT); |
1883 | if (RIGHTOF_DOT(sstate->state, i, j) == LINE_UNKNOWN) |
1884 | DO_RECURSIVE_CALL(RIGHTOF_DOT); |
1885 | if (ABOVE_DOT(sstate->state, i, j) == LINE_UNKNOWN) |
1886 | DO_RECURSIVE_CALL(ABOVE_DOT); |
1887 | if (BELOW_DOT(sstate->state, i, j) == LINE_UNKNOWN) |
1888 | DO_RECURSIVE_CALL(BELOW_DOT); |
1889 | } |
1890 | } |
1891 | } |
1892 | |
1893 | finished_recursion: |
1894 | |
1895 | if (sstate_rec_solved) { |
1896 | free_solver_state(sstate); |
1897 | sstate = sstate_rec_solved; |
1898 | } |
1899 | } |
1900 | |
1901 | return sstate; |
1902 | } |
1903 | |
1904 | /* XXX bits of solver that may come in handy one day */ |
1905 | #if 0 |
1906 | #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \ |
1907 | /* dline from this dot that's entirely unknown must have |
1908 | * both lines identical */ \ |
1909 | if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN && \ |
1910 | dir2_dot(sstate->state, i, j) == LINE_UNKNOWN) { \ |
1911 | sstate->dline_identical[i + (sstate->state->w + 1) * j] |= \ |
1912 | 1<<dline; \ |
1913 | } else if (sstate->dline_identical[i + |
1914 | (sstate->state->w + 1) * j] &\ |
1915 | 1<<dline) { \ |
1916 | /* If they're identical and one is known do the obvious |
1917 | * thing */ \ |
1918 | t = dir1_dot(sstate->state, i, j); \ |
1919 | if (t != LINE_UNKNOWN) \ |
1920 | dir2_dot(sstate->state, i, j) = t; \ |
1921 | else { \ |
1922 | t = dir2_dot(sstate->state, i, j); \ |
1923 | if (t != LINE_UNKNOWN) \ |
1924 | dir1_dot(sstate->state, i, j) = t; \ |
1925 | } \ |
1926 | } \ |
1927 | DOT_DLINES; |
1928 | #undef HANDLE_DLINE |
1929 | #endif |
1930 | |
1931 | #if 0 |
1932 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1933 | if (sstate->dline_identical[i+a + \ |
1934 | (sstate->state->w + 1) * (j+b)] &\ |
1935 | 1<<dline) { \ |
1936 | dir1_sq(sstate->state, i, j) = LINE_YES; \ |
1937 | dir2_sq(sstate->state, i, j) = LINE_YES; \ |
1938 | } |
1939 | /* If two lines are the same they must be on */ |
1940 | SQUARE_DLINES; |
1941 | #undef HANDLE_DLINE |
1942 | #endif |
1943 | |
1944 | |
1945 | #if 0 |
1946 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1947 | if (sstate->dot_atmostone[i+a + (sstate->state->w + 1) * (j+b)] & \ |
1948 | 1<<dline) { \ |
1949 | if (square_order(sstate->state, i, j, LINE_UNKNOWN) - 1 == \ |
1950 | CLUE_AT(sstate->state, i, j) - '0') { \ |
1951 | square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_YES); \ |
1952 | /* XXX the following may overwrite known data! */ \ |
1953 | dir1_sq(sstate->state, i, j) = LINE_UNKNOWN; \ |
1954 | dir2_sq(sstate->state, i, j) = LINE_UNKNOWN; \ |
1955 | } \ |
1956 | } |
1957 | SQUARE_DLINES; |
1958 | #undef HANDLE_DLINE |
1959 | #endif |
1960 | |
1961 | #if 0 |
1962 | #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \ |
1963 | if (sstate->dline_identical[i+a + |
1964 | (sstate->state->w + 1) * (j+b)] &\ |
1965 | 1<<dline) { \ |
1966 | dir1_sq(sstate->state, i, j) = LINE_NO; \ |
1967 | dir2_sq(sstate->state, i, j) = LINE_NO; \ |
1968 | } |
1969 | /* If two lines are the same they must be off */ |
1970 | SQUARE_DLINES; |
1971 | #undef HANDLE_DLINE |
1972 | #endif |
1973 | |
1974 | static char *solve_game(game_state *state, game_state *currstate, |
1975 | char *aux, char **error) |
1976 | { |
1977 | char *soln = NULL; |
1978 | solver_state *sstate, *new_sstate; |
1979 | |
1980 | sstate = new_solver_state(state); |
1981 | new_sstate = solve_game_rec(sstate); |
1982 | |
1983 | if (new_sstate->solver_status == SOLVER_SOLVED) { |
1984 | soln = encode_solve_move(new_sstate->state); |
1985 | } else if (new_sstate->solver_status == SOLVER_AMBIGUOUS) { |
1986 | soln = encode_solve_move(new_sstate->state); |
1987 | /**error = "Solver found ambiguous solutions"; */ |
1988 | } else { |
1989 | soln = encode_solve_move(new_sstate->state); |
1990 | /**error = "Solver failed"; */ |
1991 | } |
1992 | |
1993 | free_solver_state(new_sstate); |
1994 | free_solver_state(sstate); |
1995 | |
1996 | return soln; |
1997 | } |
1998 | |
1999 | static char *game_text_format(game_state *state) |
2000 | { |
2001 | int i, j; |
2002 | int len; |
2003 | char *ret, *rp; |
2004 | |
2005 | len = (2 * state->w + 2) * (2 * state->h + 1); |
2006 | rp = ret = snewn(len + 1, char); |
2007 | |
2008 | #define DRAW_HL \ |
2009 | switch (ABOVE_SQUARE(state, i, j)) { \ |
2010 | case LINE_YES: \ |
2011 | rp += sprintf(rp, " -"); \ |
2012 | break; \ |
2013 | case LINE_NO: \ |
2014 | rp += sprintf(rp, " x"); \ |
2015 | break; \ |
2016 | case LINE_UNKNOWN: \ |
2017 | rp += sprintf(rp, " "); \ |
2018 | break; \ |
2019 | default: \ |
2020 | assert(!"Illegal line state for HL");\ |
2021 | } |
2022 | |
2023 | #define DRAW_VL \ |
2024 | switch (LEFTOF_SQUARE(state, i, j)) {\ |
2025 | case LINE_YES: \ |
2026 | rp += sprintf(rp, "|"); \ |
2027 | break; \ |
2028 | case LINE_NO: \ |
2029 | rp += sprintf(rp, "x"); \ |
2030 | break; \ |
2031 | case LINE_UNKNOWN: \ |
2032 | rp += sprintf(rp, " "); \ |
2033 | break; \ |
2034 | default: \ |
2035 | assert(!"Illegal line state for VL");\ |
2036 | } |
2037 | |
2038 | for (j = 0; j < state->h; ++j) { |
2039 | for (i = 0; i < state->w; ++i) { |
2040 | DRAW_HL; |
2041 | } |
2042 | rp += sprintf(rp, " \n"); |
2043 | for (i = 0; i < state->w; ++i) { |
2044 | DRAW_VL; |
9cfc03b7 |
2045 | rp += sprintf(rp, "%c", (int)(CLUE_AT(state, i, j))); |
6193da8d |
2046 | } |
2047 | DRAW_VL; |
2048 | rp += sprintf(rp, "\n"); |
2049 | } |
2050 | for (i = 0; i < state->w; ++i) { |
2051 | DRAW_HL; |
2052 | } |
2053 | rp += sprintf(rp, " \n"); |
2054 | |
2055 | assert(strlen(ret) == len); |
2056 | return ret; |
2057 | } |
2058 | |
2059 | static game_ui *new_ui(game_state *state) |
2060 | { |
2061 | return NULL; |
2062 | } |
2063 | |
2064 | static void free_ui(game_ui *ui) |
2065 | { |
2066 | } |
2067 | |
2068 | static char *encode_ui(game_ui *ui) |
2069 | { |
2070 | return NULL; |
2071 | } |
2072 | |
2073 | static void decode_ui(game_ui *ui, char *encoding) |
2074 | { |
2075 | } |
2076 | |
2077 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
2078 | game_state *newstate) |
2079 | { |
2080 | } |
2081 | |
2082 | struct game_drawstate { |
2083 | int started; |
2084 | int tilesize; |
2085 | int flashing; |
2086 | char *hl, *vl; |
2087 | }; |
2088 | |
2089 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
2090 | int x, int y, int button) |
2091 | { |
2092 | int hl_selected; |
2093 | int i, j, p, q; |
2094 | char *ret, buf[80]; |
2095 | char button_char = ' '; |
2096 | enum line_state old_state; |
2097 | |
2098 | button &= ~MOD_MASK; |
2099 | |
2100 | /* Around each line is a diamond-shaped region where points within that |
2101 | * region are closer to this line than any other. We assume any click |
2102 | * within a line's diamond was meant for that line. It would all be a lot |
2103 | * simpler if the / and % operators respected modulo arithmetic properly |
2104 | * for negative numbers. */ |
2105 | |
2106 | x -= BORDER; |
2107 | y -= BORDER; |
2108 | |
2109 | /* Get the coordinates of the square the click was in */ |
2110 | i = (x + TILE_SIZE) / TILE_SIZE - 1; |
2111 | j = (y + TILE_SIZE) / TILE_SIZE - 1; |
2112 | |
2113 | /* Get the precise position inside square [i,j] */ |
2114 | p = (x + TILE_SIZE) % TILE_SIZE; |
2115 | q = (y + TILE_SIZE) % TILE_SIZE; |
2116 | |
2117 | /* After this bit of magic [i,j] will correspond to the point either above |
2118 | * or to the left of the line selected */ |
2119 | if (p > q) { |
2120 | if (TILE_SIZE - p > q) { |
2121 | hl_selected = TRUE; |
2122 | } else { |
2123 | hl_selected = FALSE; |
2124 | ++i; |
2125 | } |
2126 | } else { |
2127 | if (TILE_SIZE - q > p) { |
2128 | hl_selected = FALSE; |
2129 | } else { |
2130 | hl_selected = TRUE; |
2131 | ++j; |
2132 | } |
2133 | } |
2134 | |
2135 | if (i < 0 || j < 0) |
2136 | return NULL; |
2137 | |
2138 | if (hl_selected) { |
2139 | if (i >= state->w || j >= state->h + 1) |
2140 | return NULL; |
2141 | } else { |
2142 | if (i >= state->w + 1 || j >= state->h) |
2143 | return NULL; |
2144 | } |
2145 | |
2146 | /* I think it's only possible to play this game with mouse clicks, sorry */ |
2147 | /* Maybe will add mouse drag support some time */ |
2148 | if (hl_selected) |
2149 | old_state = RIGHTOF_DOT(state, i, j); |
2150 | else |
2151 | old_state = BELOW_DOT(state, i, j); |
2152 | |
2153 | switch (button) { |
2154 | case LEFT_BUTTON: |
2155 | switch (old_state) { |
2156 | case LINE_UNKNOWN: |
2157 | button_char = 'y'; |
2158 | break; |
2159 | case LINE_YES: |
2160 | case LINE_NO: |
2161 | button_char = 'u'; |
2162 | break; |
2163 | } |
2164 | break; |
2165 | case MIDDLE_BUTTON: |
2166 | button_char = 'u'; |
2167 | break; |
2168 | case RIGHT_BUTTON: |
2169 | switch (old_state) { |
2170 | case LINE_UNKNOWN: |
2171 | button_char = 'n'; |
2172 | break; |
2173 | case LINE_NO: |
2174 | case LINE_YES: |
2175 | button_char = 'u'; |
2176 | break; |
2177 | } |
2178 | break; |
2179 | default: |
2180 | return NULL; |
2181 | } |
2182 | |
2183 | |
9cfc03b7 |
2184 | sprintf(buf, "%d,%d%c%c", i, j, (int)(hl_selected ? 'h' : 'v'), (int)button_char); |
6193da8d |
2185 | ret = dupstr(buf); |
2186 | |
2187 | return ret; |
2188 | } |
2189 | |
2190 | static game_state *execute_move(game_state *state, char *move) |
2191 | { |
2192 | int i, j; |
2193 | game_state *newstate = dup_game(state); |
2194 | |
2195 | if (move[0] == 'S') { |
2196 | move++; |
2197 | newstate->cheated = TRUE; |
2198 | } |
2199 | |
2200 | while (*move) { |
2201 | i = atoi(move); |
2202 | move = strchr(move, ','); |
2203 | if (!move) |
2204 | goto fail; |
2205 | j = atoi(++move); |
2206 | move += strspn(move, "1234567890"); |
2207 | switch (*(move++)) { |
2208 | case 'h': |
2209 | if (i >= newstate->w || j > newstate->h) |
2210 | goto fail; |
2211 | switch (*(move++)) { |
2212 | case 'y': |
2213 | LV_RIGHTOF_DOT(newstate, i, j) = LINE_YES; |
2214 | break; |
2215 | case 'n': |
2216 | LV_RIGHTOF_DOT(newstate, i, j) = LINE_NO; |
2217 | break; |
2218 | case 'u': |
2219 | LV_RIGHTOF_DOT(newstate, i, j) = LINE_UNKNOWN; |
2220 | break; |
2221 | default: |
2222 | goto fail; |
2223 | } |
2224 | break; |
2225 | case 'v': |
2226 | if (i > newstate->w || j >= newstate->h) |
2227 | goto fail; |
2228 | switch (*(move++)) { |
2229 | case 'y': |
2230 | LV_BELOW_DOT(newstate, i, j) = LINE_YES; |
2231 | break; |
2232 | case 'n': |
2233 | LV_BELOW_DOT(newstate, i, j) = LINE_NO; |
2234 | break; |
2235 | case 'u': |
2236 | LV_BELOW_DOT(newstate, i, j) = LINE_UNKNOWN; |
2237 | break; |
2238 | default: |
2239 | goto fail; |
2240 | } |
2241 | break; |
2242 | default: |
2243 | goto fail; |
2244 | } |
2245 | } |
2246 | |
2247 | /* |
2248 | * Check for completion. |
2249 | */ |
67d5174b |
2250 | i = 0; /* placate optimiser */ |
6193da8d |
2251 | for (j = 0; j <= newstate->h; j++) { |
2252 | for (i = 0; i < newstate->w; i++) |
2253 | if (LV_RIGHTOF_DOT(newstate, i, j) == LINE_YES) |
2254 | break; |
2255 | if (i < newstate->w) |
2256 | break; |
2257 | } |
2258 | if (j <= newstate->h) { |
2259 | int prevdir = 'R'; |
2260 | int x = i, y = j; |
2261 | int looplen, count; |
2262 | |
2263 | /* |
2264 | * We've found a horizontal edge at (i,j). Follow it round |
2265 | * to see if it's part of a loop. |
2266 | */ |
2267 | looplen = 0; |
2268 | while (1) { |
2269 | int order = dot_order(newstate, x, y, LINE_YES); |
2270 | if (order != 2) |
2271 | goto completion_check_done; |
2272 | |
2273 | if (LEFTOF_DOT(newstate, x, y) == LINE_YES && prevdir != 'L') { |
2274 | x--; |
2275 | prevdir = 'R'; |
2276 | } else if (RIGHTOF_DOT(newstate, x, y) == LINE_YES && |
2277 | prevdir != 'R') { |
2278 | x++; |
2279 | prevdir = 'L'; |
2280 | } else if (ABOVE_DOT(newstate, x, y) == LINE_YES && |
2281 | prevdir != 'U') { |
2282 | y--; |
2283 | prevdir = 'D'; |
2284 | } else if (BELOW_DOT(newstate, x, y) == LINE_YES && |
2285 | prevdir != 'D') { |
2286 | y++; |
2287 | prevdir = 'U'; |
2288 | } else { |
2289 | assert(!"Can't happen"); /* dot_order guarantees success */ |
2290 | } |
2291 | |
2292 | looplen++; |
2293 | |
2294 | if (x == i && y == j) |
2295 | break; |
2296 | } |
2297 | |
2298 | if (x != i || y != j || looplen == 0) |
2299 | goto completion_check_done; |
2300 | |
2301 | /* |
2302 | * We've traced our way round a loop, and we know how many |
2303 | * line segments were involved. Count _all_ the line |
2304 | * segments in the grid, to see if the loop includes them |
2305 | * all. |
2306 | */ |
2307 | count = 0; |
2308 | for (j = 0; j <= newstate->h; j++) |
2309 | for (i = 0; i <= newstate->w; i++) |
2310 | count += ((RIGHTOF_DOT(newstate, i, j) == LINE_YES) + |
2311 | (BELOW_DOT(newstate, i, j) == LINE_YES)); |
2312 | assert(count >= looplen); |
2313 | if (count != looplen) |
2314 | goto completion_check_done; |
2315 | |
2316 | /* |
2317 | * The grid contains one closed loop and nothing else. |
2318 | * Check that all the clues are satisfied. |
2319 | */ |
2320 | for (j = 0; j < newstate->h; ++j) { |
2321 | for (i = 0; i < newstate->w; ++i) { |
2322 | int n = CLUE_AT(newstate, i, j); |
2323 | if (n != ' ') { |
2324 | if (square_order(newstate, i, j, LINE_YES) != n - '0') { |
2325 | goto completion_check_done; |
2326 | } |
2327 | } |
2328 | } |
2329 | } |
2330 | |
2331 | /* |
2332 | * Completed! |
2333 | */ |
2334 | newstate->solved = TRUE; |
2335 | } |
2336 | |
2337 | completion_check_done: |
2338 | return newstate; |
2339 | |
2340 | fail: |
2341 | free_game(newstate); |
2342 | return NULL; |
2343 | } |
2344 | |
2345 | /* ---------------------------------------------------------------------- |
2346 | * Drawing routines. |
2347 | */ |
2348 | |
2349 | #define SIZE(d) ((d) * TILE_SIZE + 2 * BORDER + 1) |
2350 | |
2351 | static void game_compute_size(game_params *params, int tilesize, |
2352 | int *x, int *y) |
2353 | { |
2354 | struct { int tilesize; } ads, *ds = &ads; |
2355 | ads.tilesize = tilesize; |
2356 | |
2357 | *x = SIZE(params->w); |
2358 | *y = SIZE(params->h); |
2359 | } |
2360 | |
2361 | static void game_set_size(drawing *dr, game_drawstate *ds, |
2362 | game_params *params, int tilesize) |
2363 | { |
2364 | ds->tilesize = tilesize; |
2365 | } |
2366 | |
2367 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
2368 | { |
2369 | float *ret = snewn(4 * NCOLOURS, float); |
2370 | |
2371 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
2372 | |
2373 | ret[COL_FOREGROUND * 3 + 0] = 0.0F; |
2374 | ret[COL_FOREGROUND * 3 + 1] = 0.0F; |
2375 | ret[COL_FOREGROUND * 3 + 2] = 0.0F; |
2376 | |
2377 | ret[COL_HIGHLIGHT * 3 + 0] = 1.0F; |
2378 | ret[COL_HIGHLIGHT * 3 + 1] = 1.0F; |
2379 | ret[COL_HIGHLIGHT * 3 + 2] = 1.0F; |
2380 | |
2381 | *ncolours = NCOLOURS; |
2382 | return ret; |
2383 | } |
2384 | |
2385 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
2386 | { |
2387 | struct game_drawstate *ds = snew(struct game_drawstate); |
2388 | |
2389 | ds->tilesize = 0; |
2390 | ds->started = 0; |
2391 | ds->hl = snewn(HL_COUNT(state), char); |
2392 | ds->vl = snewn(VL_COUNT(state), char); |
2393 | ds->flashing = 0; |
2394 | |
2395 | memset(ds->hl, LINE_UNKNOWN, HL_COUNT(state)); |
2396 | memset(ds->vl, LINE_UNKNOWN, VL_COUNT(state)); |
2397 | |
2398 | return ds; |
2399 | } |
2400 | |
2401 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
2402 | { |
2403 | sfree(ds->hl); |
2404 | sfree(ds->vl); |
2405 | sfree(ds); |
2406 | } |
2407 | |
2408 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
2409 | game_state *state, int dir, game_ui *ui, |
2410 | float animtime, float flashtime) |
2411 | { |
2412 | int i, j; |
2413 | int w = state->w, h = state->h; |
2414 | char c[2]; |
2415 | int line_colour, flash_changed; |
2416 | |
2417 | if (!ds->started) { |
2418 | /* |
2419 | * The initial contents of the window are not guaranteed and |
2420 | * can vary with front ends. To be on the safe side, all games |
2421 | * should start by drawing a big background-colour rectangle |
2422 | * covering the whole window. |
2423 | */ |
2424 | draw_rect(dr, 0, 0, SIZE(state->w), SIZE(state->h), COL_BACKGROUND); |
2425 | |
2426 | /* Draw dots */ |
2427 | for (j = 0; j < h + 1; ++j) { |
2428 | for (i = 0; i < w + 1; ++i) { |
2429 | draw_rect(dr, |
2430 | BORDER + i * TILE_SIZE - LINEWIDTH/2, |
2431 | BORDER + j * TILE_SIZE - LINEWIDTH/2, |
2432 | LINEWIDTH, LINEWIDTH, COL_FOREGROUND); |
2433 | } |
2434 | } |
2435 | |
2436 | /* Draw clues */ |
2437 | for (j = 0; j < h; ++j) { |
2438 | for (i = 0; i < w; ++i) { |
2439 | c[0] = CLUE_AT(state, i, j); |
2440 | c[1] = '\0'; |
2441 | draw_text(dr, |
2442 | BORDER + i * TILE_SIZE + TILE_SIZE/2, |
2443 | BORDER + j * TILE_SIZE + TILE_SIZE/2, |
2444 | FONT_VARIABLE, TILE_SIZE/2, |
2445 | ALIGN_VCENTRE | ALIGN_HCENTRE, COL_FOREGROUND, c); |
2446 | } |
2447 | } |
2448 | draw_update(dr, 0, 0, |
2449 | state->w * TILE_SIZE + 2*BORDER + 1, |
2450 | state->h * TILE_SIZE + 2*BORDER + 1); |
2451 | ds->started = TRUE; |
2452 | } |
2453 | |
2454 | if (flashtime > 0 && |
2455 | (flashtime <= FLASH_TIME/3 || |
2456 | flashtime >= FLASH_TIME*2/3)) { |
2457 | flash_changed = !ds->flashing; |
2458 | ds->flashing = TRUE; |
2459 | line_colour = COL_HIGHLIGHT; |
2460 | } else { |
2461 | flash_changed = ds->flashing; |
2462 | ds->flashing = FALSE; |
2463 | line_colour = COL_FOREGROUND; |
2464 | } |
2465 | |
2466 | #define CROSS_SIZE (3 * LINEWIDTH / 2) |
2467 | |
2468 | #define CLEAR_VL(i, j) do { \ |
2469 | draw_rect(dr, \ |
2470 | BORDER + i * TILE_SIZE - CROSS_SIZE, \ |
444203b4 |
2471 | BORDER + j * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, \ |
6193da8d |
2472 | CROSS_SIZE * 2, \ |
2473 | TILE_SIZE - LINEWIDTH, \ |
2474 | COL_BACKGROUND); \ |
2475 | draw_update(dr, \ |
2476 | BORDER + i * TILE_SIZE - CROSS_SIZE, \ |
2477 | BORDER + j * TILE_SIZE - CROSS_SIZE, \ |
2478 | CROSS_SIZE*2, \ |
2479 | TILE_SIZE + CROSS_SIZE*2); \ |
2480 | } while (0) |
2481 | |
2482 | #define CLEAR_HL(i, j) do { \ |
2483 | draw_rect(dr, \ |
444203b4 |
2484 | BORDER + i * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, \ |
6193da8d |
2485 | BORDER + j * TILE_SIZE - CROSS_SIZE, \ |
2486 | TILE_SIZE - LINEWIDTH, \ |
2487 | CROSS_SIZE * 2, \ |
2488 | COL_BACKGROUND); \ |
2489 | draw_update(dr, \ |
2490 | BORDER + i * TILE_SIZE - CROSS_SIZE, \ |
2491 | BORDER + j * TILE_SIZE - CROSS_SIZE, \ |
2492 | TILE_SIZE + CROSS_SIZE*2, \ |
2493 | CROSS_SIZE*2); \ |
2494 | } while (0) |
2495 | |
2496 | /* Vertical lines */ |
2497 | for (j = 0; j < h; ++j) { |
2498 | for (i = 0; i < w + 1; ++i) { |
2499 | switch (BELOW_DOT(state, i, j)) { |
2500 | case LINE_UNKNOWN: |
2501 | if (ds->vl[i + (w + 1) * j] != BELOW_DOT(state, i, j)) { |
2502 | CLEAR_VL(i, j); |
2503 | } |
2504 | break; |
2505 | case LINE_YES: |
2506 | if (ds->vl[i + (w + 1) * j] != BELOW_DOT(state, i, j) || |
2507 | flash_changed) { |
2508 | CLEAR_VL(i, j); |
2509 | draw_rect(dr, |
2510 | BORDER + i * TILE_SIZE - LINEWIDTH/2, |
444203b4 |
2511 | BORDER + j * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, |
6193da8d |
2512 | LINEWIDTH, TILE_SIZE - LINEWIDTH, |
2513 | line_colour); |
2514 | } |
2515 | break; |
2516 | case LINE_NO: |
2517 | if (ds->vl[i + (w + 1) * j] != BELOW_DOT(state, i, j)) { |
2518 | CLEAR_VL(i, j); |
2519 | draw_line(dr, |
2520 | BORDER + i * TILE_SIZE - CROSS_SIZE, |
2521 | BORDER + j * TILE_SIZE + TILE_SIZE/2 - CROSS_SIZE, |
2522 | BORDER + i * TILE_SIZE + CROSS_SIZE - 1, |
2523 | BORDER + j * TILE_SIZE + TILE_SIZE/2 + CROSS_SIZE - 1, |
2524 | COL_FOREGROUND); |
2525 | draw_line(dr, |
2526 | BORDER + i * TILE_SIZE + CROSS_SIZE - 1, |
2527 | BORDER + j * TILE_SIZE + TILE_SIZE/2 - CROSS_SIZE, |
2528 | BORDER + i * TILE_SIZE - CROSS_SIZE, |
2529 | BORDER + j * TILE_SIZE + TILE_SIZE/2 + CROSS_SIZE - 1, |
2530 | COL_FOREGROUND); |
2531 | } |
2532 | break; |
2533 | } |
2534 | ds->vl[i + (w + 1) * j] = BELOW_DOT(state, i, j); |
2535 | } |
2536 | } |
2537 | |
2538 | /* Horizontal lines */ |
2539 | for (j = 0; j < h + 1; ++j) { |
2540 | for (i = 0; i < w; ++i) { |
2541 | switch (RIGHTOF_DOT(state, i, j)) { |
2542 | case LINE_UNKNOWN: |
2543 | if (ds->hl[i + w * j] != RIGHTOF_DOT(state, i, j)) { |
2544 | CLEAR_HL(i, j); |
2545 | } |
2546 | break; |
2547 | case LINE_YES: |
2548 | if (ds->hl[i + w * j] != RIGHTOF_DOT(state, i, j) || |
2549 | flash_changed) { |
2550 | CLEAR_HL(i, j); |
2551 | draw_rect(dr, |
444203b4 |
2552 | BORDER + i * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, |
6193da8d |
2553 | BORDER + j * TILE_SIZE - LINEWIDTH/2, |
2554 | TILE_SIZE - LINEWIDTH, LINEWIDTH, |
2555 | line_colour); |
2556 | break; |
2557 | } |
2558 | case LINE_NO: |
2559 | if (ds->hl[i + w * j] != RIGHTOF_DOT(state, i, j)) { |
2560 | CLEAR_HL(i, j); |
2561 | draw_line(dr, |
2562 | BORDER + i * TILE_SIZE + TILE_SIZE/2 - CROSS_SIZE, |
2563 | BORDER + j * TILE_SIZE + CROSS_SIZE - 1, |
2564 | BORDER + i * TILE_SIZE + TILE_SIZE/2 + CROSS_SIZE - 1, |
2565 | BORDER + j * TILE_SIZE - CROSS_SIZE, |
2566 | COL_FOREGROUND); |
2567 | draw_line(dr, |
2568 | BORDER + i * TILE_SIZE + TILE_SIZE/2 - CROSS_SIZE, |
2569 | BORDER + j * TILE_SIZE - CROSS_SIZE, |
2570 | BORDER + i * TILE_SIZE + TILE_SIZE/2 + CROSS_SIZE - 1, |
2571 | BORDER + j * TILE_SIZE + CROSS_SIZE - 1, |
2572 | COL_FOREGROUND); |
2573 | break; |
2574 | } |
2575 | } |
2576 | ds->hl[i + w * j] = RIGHTOF_DOT(state, i, j); |
2577 | } |
2578 | } |
2579 | } |
2580 | |
2581 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
2582 | int dir, game_ui *ui) |
2583 | { |
2584 | return 0.0F; |
2585 | } |
2586 | |
2587 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
2588 | int dir, game_ui *ui) |
2589 | { |
2590 | if (!oldstate->solved && newstate->solved && |
2591 | !oldstate->cheated && !newstate->cheated) { |
2592 | return FLASH_TIME; |
2593 | } |
2594 | |
2595 | return 0.0F; |
2596 | } |
2597 | |
2598 | static int game_wants_statusbar(void) |
2599 | { |
2600 | return FALSE; |
2601 | } |
2602 | |
2603 | static int game_timing_state(game_state *state, game_ui *ui) |
2604 | { |
2605 | return TRUE; |
2606 | } |
2607 | |
2608 | static void game_print_size(game_params *params, float *x, float *y) |
2609 | { |
2610 | int pw, ph; |
2611 | |
2612 | /* |
2613 | * I'll use 7mm squares by default. |
2614 | */ |
2615 | game_compute_size(params, 700, &pw, &ph); |
2616 | *x = pw / 100.0F; |
2617 | *y = ph / 100.0F; |
2618 | } |
2619 | |
2620 | static void game_print(drawing *dr, game_state *state, int tilesize) |
2621 | { |
2622 | int w = state->w, h = state->h; |
2623 | int ink = print_mono_colour(dr, 0); |
2624 | int x, y; |
2625 | game_drawstate ads, *ds = &ads; |
2626 | ds->tilesize = tilesize; |
2627 | |
2628 | /* |
2629 | * Dots. I'll deliberately make the dots a bit wider than the |
2630 | * lines, so you can still see them. (And also because it's |
2631 | * annoyingly tricky to make them _exactly_ the same size...) |
2632 | */ |
2633 | for (y = 0; y <= h; y++) |
2634 | for (x = 0; x <= w; x++) |
2635 | draw_circle(dr, BORDER + x * TILE_SIZE, BORDER + y * TILE_SIZE, |
2636 | LINEWIDTH, ink, ink); |
2637 | |
2638 | /* |
2639 | * Clues. |
2640 | */ |
2641 | for (y = 0; y < h; y++) |
2642 | for (x = 0; x < w; x++) |
2643 | if (CLUE_AT(state, x, y) != ' ') { |
2644 | char c[2]; |
2645 | |
2646 | c[0] = CLUE_AT(state, x, y); |
2647 | c[1] = '\0'; |
2648 | draw_text(dr, |
2649 | BORDER + x * TILE_SIZE + TILE_SIZE/2, |
2650 | BORDER + y * TILE_SIZE + TILE_SIZE/2, |
2651 | FONT_VARIABLE, TILE_SIZE/2, |
2652 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, c); |
2653 | } |
2654 | |
2655 | /* |
2656 | * Lines. (At the moment, I'm not bothering with crosses.) |
2657 | */ |
2658 | for (y = 0; y <= h; y++) |
2659 | for (x = 0; x < w; x++) |
2660 | if (RIGHTOF_DOT(state, x, y) == LINE_YES) |
2661 | draw_rect(dr, BORDER + x * TILE_SIZE, |
2662 | BORDER + y * TILE_SIZE - LINEWIDTH/2, |
2663 | TILE_SIZE, (LINEWIDTH/2) * 2 + 1, ink); |
2664 | for (y = 0; y < h; y++) |
2665 | for (x = 0; x <= w; x++) |
2666 | if (BELOW_DOT(state, x, y) == LINE_YES) |
2667 | draw_rect(dr, BORDER + x * TILE_SIZE - LINEWIDTH/2, |
2668 | BORDER + y * TILE_SIZE, |
2669 | (LINEWIDTH/2) * 2 + 1, TILE_SIZE, ink); |
2670 | } |
2671 | |
2672 | #ifdef COMBINED |
2673 | #define thegame loopy |
2674 | #endif |
2675 | |
2676 | const struct game thegame = { |
2677 | "Loopy", "games.loopy", |
2678 | default_params, |
2679 | game_fetch_preset, |
2680 | decode_params, |
2681 | encode_params, |
2682 | free_params, |
2683 | dup_params, |
2684 | TRUE, game_configure, custom_params, |
2685 | validate_params, |
2686 | new_game_desc, |
2687 | validate_desc, |
2688 | new_game, |
2689 | dup_game, |
2690 | free_game, |
2691 | 1, solve_game, |
2692 | TRUE, game_text_format, |
2693 | new_ui, |
2694 | free_ui, |
2695 | encode_ui, |
2696 | decode_ui, |
2697 | game_changed_state, |
2698 | interpret_move, |
2699 | execute_move, |
2700 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
2701 | game_colours, |
2702 | game_new_drawstate, |
2703 | game_free_drawstate, |
2704 | game_redraw, |
2705 | game_anim_length, |
2706 | game_flash_length, |
2707 | TRUE, FALSE, game_print_size, game_print, |
2708 | game_wants_statusbar, |
2709 | FALSE, game_timing_state, |
2710 | 0, /* mouse_priorities */ |
2711 | }; |