6193da8d |
1 | /* |
7c95608a |
2 | * loopy.c: |
3 | * |
4 | * An implementation of the Nikoli game 'Loop the loop'. |
121aae4b |
5 | * (c) Mike Pinna, 2005, 2006 |
7c95608a |
6 | * Substantially rewritten to allowing for more general types of grid. |
7 | * (c) Lambros Lambrou 2008 |
6193da8d |
8 | * |
9 | * vim: set shiftwidth=4 :set textwidth=80: |
7c95608a |
10 | */ |
6193da8d |
11 | |
12 | /* |
a36a26d7 |
13 | * Possible future solver enhancements: |
14 | * |
15 | * - There's an interesting deductive technique which makes use |
16 | * of topology rather than just graph theory. Each _face_ in |
17 | * the grid is either inside or outside the loop; you can tell |
18 | * that two faces are on the same side of the loop if they're |
19 | * separated by a LINE_NO (or, more generally, by a path |
20 | * crossing no LINE_UNKNOWNs and an even number of LINE_YESes), |
21 | * and on the opposite side of the loop if they're separated by |
22 | * a LINE_YES (or an odd number of LINE_YESes and no |
23 | * LINE_UNKNOWNs). Oh, and any face separated from the outside |
24 | * of the grid by a LINE_YES or a LINE_NO is on the inside or |
25 | * outside respectively. So if you can track this for all |
26 | * faces, you figure out the state of the line between a pair |
27 | * once their relative insideness is known. |
28 | * + The way I envisage this working is simply to keep an edsf |
29 | * of all _faces_, which indicates whether they're on |
30 | * opposite sides of the loop from one another. We also |
31 | * include a special entry in the edsf for the infinite |
32 | * exterior "face". |
33 | * + So, the simple way to do this is to just go through the |
34 | * edges: every time we see an edge in a state other than |
35 | * LINE_UNKNOWN which separates two faces that aren't in the |
36 | * same edsf class, we can rectify that by merging the |
37 | * classes. Then, conversely, an edge in LINE_UNKNOWN state |
38 | * which separates two faces that _are_ in the same edsf |
39 | * class can immediately have its state determined. |
40 | * + But you can go one better, if you're prepared to loop |
41 | * over all _pairs_ of edges. Suppose we have edges A and B, |
42 | * which respectively separate faces A1,A2 and B1,B2. |
43 | * Suppose that A,B are in the same edge-edsf class and that |
44 | * A1,B1 (wlog) are in the same face-edsf class; then we can |
45 | * immediately place A2,B2 into the same face-edsf class (as |
46 | * each other, not as A1 and A2) one way round or the other. |
47 | * And conversely again, if A1,B1 are in the same face-edsf |
48 | * class and so are A2,B2, then we can put A,B into the same |
49 | * face-edsf class. |
50 | * * Of course, this deduction requires a quadratic-time |
51 | * loop over all pairs of edges in the grid, so it should |
52 | * be reserved until there's nothing easier left to be |
53 | * done. |
54 | * |
55 | * - The generalised grid support has made me (SGT) notice a |
56 | * possible extension to the loop-avoidance code. When you have |
57 | * a path of connected edges such that no other edges at all |
58 | * are incident on any vertex in the middle of the path - or, |
59 | * alternatively, such that any such edges are already known to |
60 | * be LINE_NO - then you know those edges are either all |
61 | * LINE_YES or all LINE_NO. Hence you can mentally merge the |
62 | * entire path into a single long curly edge for the purposes |
63 | * of loop avoidance, and look directly at whether or not the |
64 | * extreme endpoints of the path are connected by some other |
65 | * route. I find this coming up fairly often when I play on the |
66 | * octagonal grid setting, so it might be worth implementing in |
67 | * the solver. |
121aae4b |
68 | * |
69 | * - (Just a speed optimisation.) Consider some todo list queue where every |
70 | * time we modify something we mark it for consideration by other bits of |
71 | * the solver, to save iteration over things that have already been done. |
6193da8d |
72 | */ |
73 | |
74 | #include <stdio.h> |
75 | #include <stdlib.h> |
7126ca41 |
76 | #include <stddef.h> |
6193da8d |
77 | #include <string.h> |
78 | #include <assert.h> |
79 | #include <ctype.h> |
80 | #include <math.h> |
81 | |
82 | #include "puzzles.h" |
83 | #include "tree234.h" |
7c95608a |
84 | #include "grid.h" |
b760b8bd |
85 | #include "loopgen.h" |
6193da8d |
86 | |
121aae4b |
87 | /* Debugging options */ |
7c95608a |
88 | |
89 | /* |
90 | #define DEBUG_CACHES |
91 | #define SHOW_WORKING |
92 | #define DEBUG_DLINES |
93 | */ |
121aae4b |
94 | |
95 | /* ---------------------------------------------------------------------- |
96 | * Struct, enum and function declarations |
97 | */ |
98 | |
99 | enum { |
100 | COL_BACKGROUND, |
101 | COL_FOREGROUND, |
7c95608a |
102 | COL_LINEUNKNOWN, |
121aae4b |
103 | COL_HIGHLIGHT, |
104 | COL_MISTAKE, |
7c95608a |
105 | COL_SATISFIED, |
ec909c7a |
106 | COL_FAINT, |
121aae4b |
107 | NCOLOURS |
108 | }; |
109 | |
110 | struct game_state { |
cebf0b0d |
111 | grid *game_grid; /* ref-counted (internally) */ |
7c95608a |
112 | |
113 | /* Put -1 in a face that doesn't get a clue */ |
aa8ccc55 |
114 | signed char *clues; |
7c95608a |
115 | |
116 | /* Array of line states, to store whether each line is |
117 | * YES, NO or UNKNOWN */ |
118 | char *lines; |
121aae4b |
119 | |
b6bf0adc |
120 | unsigned char *line_errors; |
121 | |
121aae4b |
122 | int solved; |
123 | int cheated; |
124 | |
7c95608a |
125 | /* Used in game_text_format(), so that it knows what type of |
126 | * grid it's trying to render as ASCII text. */ |
127 | int grid_type; |
121aae4b |
128 | }; |
129 | |
130 | enum solver_status { |
131 | SOLVER_SOLVED, /* This is the only solution the solver could find */ |
132 | SOLVER_MISTAKE, /* This is definitely not a solution */ |
133 | SOLVER_AMBIGUOUS, /* This _might_ be an ambiguous solution */ |
134 | SOLVER_INCOMPLETE /* This may be a partial solution */ |
135 | }; |
136 | |
7c95608a |
137 | /* ------ Solver state ------ */ |
121aae4b |
138 | typedef struct solver_state { |
139 | game_state *state; |
121aae4b |
140 | enum solver_status solver_status; |
141 | /* NB looplen is the number of dots that are joined together at a point, ie a |
142 | * looplen of 1 means there are no lines to a particular dot */ |
143 | int *looplen; |
144 | |
315e47b9 |
145 | /* Difficulty level of solver. Used by solver functions that want to |
146 | * vary their behaviour depending on the requested difficulty level. */ |
147 | int diff; |
148 | |
121aae4b |
149 | /* caches */ |
7c95608a |
150 | char *dot_yes_count; |
151 | char *dot_no_count; |
152 | char *face_yes_count; |
153 | char *face_no_count; |
154 | char *dot_solved, *face_solved; |
121aae4b |
155 | int *dotdsf; |
156 | |
315e47b9 |
157 | /* Information for Normal level deductions: |
158 | * For each dline, store a bitmask for whether we know: |
159 | * (bit 0) at least one is YES |
160 | * (bit 1) at most one is YES */ |
161 | char *dlines; |
162 | |
163 | /* Hard level information */ |
164 | int *linedsf; |
121aae4b |
165 | } solver_state; |
166 | |
167 | /* |
168 | * Difficulty levels. I do some macro ickery here to ensure that my |
169 | * enum and the various forms of my name list always match up. |
170 | */ |
171 | |
172 | #define DIFFLIST(A) \ |
315e47b9 |
173 | A(EASY,Easy,e) \ |
174 | A(NORMAL,Normal,n) \ |
175 | A(TRICKY,Tricky,t) \ |
176 | A(HARD,Hard,h) |
177 | #define ENUM(upper,title,lower) DIFF_ ## upper, |
178 | #define TITLE(upper,title,lower) #title, |
179 | #define ENCODE(upper,title,lower) #lower |
180 | #define CONFIG(upper,title,lower) ":" #title |
1a739e2f |
181 | enum { DIFFLIST(ENUM) DIFF_MAX }; |
121aae4b |
182 | static char const *const diffnames[] = { DIFFLIST(TITLE) }; |
183 | static char const diffchars[] = DIFFLIST(ENCODE); |
184 | #define DIFFCONFIG DIFFLIST(CONFIG) |
315e47b9 |
185 | |
186 | /* |
187 | * Solver routines, sorted roughly in order of computational cost. |
188 | * The solver will run the faster deductions first, and slower deductions are |
189 | * only invoked when the faster deductions are unable to make progress. |
190 | * Each function is associated with a difficulty level, so that the generated |
191 | * puzzles are solvable by applying only the functions with the chosen |
192 | * difficulty level or lower. |
193 | */ |
194 | #define SOLVERLIST(A) \ |
195 | A(trivial_deductions, DIFF_EASY) \ |
196 | A(dline_deductions, DIFF_NORMAL) \ |
197 | A(linedsf_deductions, DIFF_HARD) \ |
198 | A(loop_deductions, DIFF_EASY) |
199 | #define SOLVER_FN_DECL(fn,diff) static int fn(solver_state *); |
200 | #define SOLVER_FN(fn,diff) &fn, |
201 | #define SOLVER_DIFF(fn,diff) diff, |
202 | SOLVERLIST(SOLVER_FN_DECL) |
203 | static int (*(solver_fns[]))(solver_state *) = { SOLVERLIST(SOLVER_FN) }; |
204 | static int const solver_diffs[] = { SOLVERLIST(SOLVER_DIFF) }; |
fd66a01d |
205 | static const int NUM_SOLVERS = sizeof(solver_diffs)/sizeof(*solver_diffs); |
121aae4b |
206 | |
207 | struct game_params { |
208 | int w, h; |
1a739e2f |
209 | int diff; |
7c95608a |
210 | int type; |
121aae4b |
211 | }; |
212 | |
b6bf0adc |
213 | /* line_drawstate is the same as line_state, but with the extra ERROR |
214 | * possibility. The drawing code copies line_state to line_drawstate, |
215 | * except in the case that the line is an error. */ |
121aae4b |
216 | enum line_state { LINE_YES, LINE_UNKNOWN, LINE_NO }; |
b6bf0adc |
217 | enum line_drawstate { DS_LINE_YES, DS_LINE_UNKNOWN, |
218 | DS_LINE_NO, DS_LINE_ERROR }; |
121aae4b |
219 | |
7c95608a |
220 | #define OPP(line_state) \ |
221 | (2 - line_state) |
121aae4b |
222 | |
121aae4b |
223 | |
224 | struct game_drawstate { |
225 | int started; |
7c95608a |
226 | int tilesize; |
121aae4b |
227 | int flashing; |
e0936bbd |
228 | int *textx, *texty; |
7c95608a |
229 | char *lines; |
121aae4b |
230 | char *clue_error; |
7c95608a |
231 | char *clue_satisfied; |
121aae4b |
232 | }; |
233 | |
121aae4b |
234 | static char *validate_desc(game_params *params, char *desc); |
7c95608a |
235 | static int dot_order(const game_state* state, int i, char line_type); |
236 | static int face_order(const game_state* state, int i, char line_type); |
315e47b9 |
237 | static solver_state *solve_game_rec(const solver_state *sstate); |
121aae4b |
238 | |
239 | #ifdef DEBUG_CACHES |
240 | static void check_caches(const solver_state* sstate); |
241 | #else |
242 | #define check_caches(s) |
243 | #endif |
244 | |
7c95608a |
245 | /* ------- List of grid generators ------- */ |
246 | #define GRIDLIST(A) \ |
cebf0b0d |
247 | A(Squares,GRID_SQUARE,3,3) \ |
248 | A(Triangular,GRID_TRIANGULAR,3,3) \ |
249 | A(Honeycomb,GRID_HONEYCOMB,3,3) \ |
250 | A(Snub-Square,GRID_SNUBSQUARE,3,3) \ |
251 | A(Cairo,GRID_CAIRO,3,4) \ |
252 | A(Great-Hexagonal,GRID_GREATHEXAGONAL,3,3) \ |
253 | A(Octagonal,GRID_OCTAGONAL,3,3) \ |
254 | A(Kites,GRID_KITE,3,3) \ |
255 | A(Floret,GRID_FLORET,1,2) \ |
256 | A(Dodecagonal,GRID_DODECAGONAL,2,2) \ |
257 | A(Great-Dodecagonal,GRID_GREATDODECAGONAL,2,2) \ |
258 | A(Penrose (kite/dart),GRID_PENROSE_P2,3,3) \ |
f875ca4d |
259 | A(Penrose (rhombs),GRID_PENROSE_P3,3,3) |
cebf0b0d |
260 | |
261 | #define GRID_NAME(title,type,amin,omin) #title, |
262 | #define GRID_CONFIG(title,type,amin,omin) ":" #title |
263 | #define GRID_TYPE(title,type,amin,omin) type, |
264 | #define GRID_SIZES(title,type,amin,omin) \ |
e3c9e042 |
265 | {amin, omin, \ |
266 | "Width and height for this grid type must both be at least " #amin, \ |
267 | "At least one of width and height for this grid type must be at least " #omin,}, |
7c95608a |
268 | static char const *const gridnames[] = { GRIDLIST(GRID_NAME) }; |
269 | #define GRID_CONFIGS GRIDLIST(GRID_CONFIG) |
cebf0b0d |
270 | static grid_type grid_types[] = { GRIDLIST(GRID_TYPE) }; |
271 | #define NUM_GRID_TYPES (sizeof(grid_types) / sizeof(grid_types[0])) |
e3c9e042 |
272 | static const struct { |
273 | int amin, omin; |
274 | char *aerr, *oerr; |
275 | } grid_size_limits[] = { GRIDLIST(GRID_SIZES) }; |
7c95608a |
276 | |
277 | /* Generates a (dynamically allocated) new grid, according to the |
278 | * type and size requested in params. Does nothing if the grid is already |
cebf0b0d |
279 | * generated. */ |
280 | static grid *loopy_generate_grid(game_params *params, char *grid_desc) |
7c95608a |
281 | { |
f875ca4d |
282 | return grid_new(grid_types[params->type], params->w, params->h, grid_desc); |
7c95608a |
283 | } |
284 | |
121aae4b |
285 | /* ---------------------------------------------------------------------- |
7c95608a |
286 | * Preprocessor magic |
121aae4b |
287 | */ |
288 | |
289 | /* General constants */ |
6193da8d |
290 | #define PREFERRED_TILE_SIZE 32 |
7c95608a |
291 | #define BORDER(tilesize) ((tilesize) / 2) |
c0eb17ce |
292 | #define FLASH_TIME 0.5F |
6193da8d |
293 | |
121aae4b |
294 | #define BIT_SET(field, bit) ((field) & (1<<(bit))) |
295 | |
296 | #define SET_BIT(field, bit) (BIT_SET(field, bit) ? FALSE : \ |
297 | ((field) |= (1<<(bit)), TRUE)) |
298 | |
299 | #define CLEAR_BIT(field, bit) (BIT_SET(field, bit) ? \ |
300 | ((field) &= ~(1<<(bit)), TRUE) : FALSE) |
301 | |
121aae4b |
302 | #define CLUE2CHAR(c) \ |
918a098a |
303 | ((c < 0) ? ' ' : c < 10 ? c + '0' : c - 10 + 'A') |
121aae4b |
304 | |
121aae4b |
305 | /* ---------------------------------------------------------------------- |
306 | * General struct manipulation and other straightforward code |
307 | */ |
6193da8d |
308 | |
309 | static game_state *dup_game(game_state *state) |
310 | { |
311 | game_state *ret = snew(game_state); |
312 | |
7c95608a |
313 | ret->game_grid = state->game_grid; |
314 | ret->game_grid->refcount++; |
315 | |
6193da8d |
316 | ret->solved = state->solved; |
317 | ret->cheated = state->cheated; |
318 | |
7c95608a |
319 | ret->clues = snewn(state->game_grid->num_faces, signed char); |
320 | memcpy(ret->clues, state->clues, state->game_grid->num_faces); |
6193da8d |
321 | |
7c95608a |
322 | ret->lines = snewn(state->game_grid->num_edges, char); |
323 | memcpy(ret->lines, state->lines, state->game_grid->num_edges); |
6193da8d |
324 | |
b6bf0adc |
325 | ret->line_errors = snewn(state->game_grid->num_edges, unsigned char); |
326 | memcpy(ret->line_errors, state->line_errors, state->game_grid->num_edges); |
327 | |
7c95608a |
328 | ret->grid_type = state->grid_type; |
6193da8d |
329 | return ret; |
330 | } |
331 | |
332 | static void free_game(game_state *state) |
333 | { |
334 | if (state) { |
7c95608a |
335 | grid_free(state->game_grid); |
6193da8d |
336 | sfree(state->clues); |
7c95608a |
337 | sfree(state->lines); |
b6bf0adc |
338 | sfree(state->line_errors); |
6193da8d |
339 | sfree(state); |
340 | } |
341 | } |
342 | |
7c95608a |
343 | static solver_state *new_solver_state(game_state *state, int diff) { |
344 | int i; |
345 | int num_dots = state->game_grid->num_dots; |
346 | int num_faces = state->game_grid->num_faces; |
347 | int num_edges = state->game_grid->num_edges; |
6193da8d |
348 | solver_state *ret = snew(solver_state); |
6193da8d |
349 | |
7c95608a |
350 | ret->state = dup_game(state); |
351 | |
352 | ret->solver_status = SOLVER_INCOMPLETE; |
315e47b9 |
353 | ret->diff = diff; |
6193da8d |
354 | |
7c95608a |
355 | ret->dotdsf = snew_dsf(num_dots); |
356 | ret->looplen = snewn(num_dots, int); |
121aae4b |
357 | |
7c95608a |
358 | for (i = 0; i < num_dots; i++) { |
121aae4b |
359 | ret->looplen[i] = 1; |
360 | } |
361 | |
7c95608a |
362 | ret->dot_solved = snewn(num_dots, char); |
363 | ret->face_solved = snewn(num_faces, char); |
364 | memset(ret->dot_solved, FALSE, num_dots); |
365 | memset(ret->face_solved, FALSE, num_faces); |
121aae4b |
366 | |
7c95608a |
367 | ret->dot_yes_count = snewn(num_dots, char); |
368 | memset(ret->dot_yes_count, 0, num_dots); |
369 | ret->dot_no_count = snewn(num_dots, char); |
370 | memset(ret->dot_no_count, 0, num_dots); |
371 | ret->face_yes_count = snewn(num_faces, char); |
372 | memset(ret->face_yes_count, 0, num_faces); |
373 | ret->face_no_count = snewn(num_faces, char); |
374 | memset(ret->face_no_count, 0, num_faces); |
121aae4b |
375 | |
376 | if (diff < DIFF_NORMAL) { |
315e47b9 |
377 | ret->dlines = NULL; |
121aae4b |
378 | } else { |
315e47b9 |
379 | ret->dlines = snewn(2*num_edges, char); |
380 | memset(ret->dlines, 0, 2*num_edges); |
121aae4b |
381 | } |
382 | |
383 | if (diff < DIFF_HARD) { |
315e47b9 |
384 | ret->linedsf = NULL; |
121aae4b |
385 | } else { |
315e47b9 |
386 | ret->linedsf = snew_dsf(state->game_grid->num_edges); |
6193da8d |
387 | } |
388 | |
389 | return ret; |
390 | } |
391 | |
392 | static void free_solver_state(solver_state *sstate) { |
393 | if (sstate) { |
394 | free_game(sstate->state); |
9cfc03b7 |
395 | sfree(sstate->dotdsf); |
396 | sfree(sstate->looplen); |
121aae4b |
397 | sfree(sstate->dot_solved); |
7c95608a |
398 | sfree(sstate->face_solved); |
399 | sfree(sstate->dot_yes_count); |
400 | sfree(sstate->dot_no_count); |
401 | sfree(sstate->face_yes_count); |
402 | sfree(sstate->face_no_count); |
121aae4b |
403 | |
315e47b9 |
404 | /* OK, because sfree(NULL) is a no-op */ |
405 | sfree(sstate->dlines); |
406 | sfree(sstate->linedsf); |
121aae4b |
407 | |
9cfc03b7 |
408 | sfree(sstate); |
6193da8d |
409 | } |
410 | } |
411 | |
121aae4b |
412 | static solver_state *dup_solver_state(const solver_state *sstate) { |
7c95608a |
413 | game_state *state = sstate->state; |
414 | int num_dots = state->game_grid->num_dots; |
415 | int num_faces = state->game_grid->num_faces; |
416 | int num_edges = state->game_grid->num_edges; |
6193da8d |
417 | solver_state *ret = snew(solver_state); |
418 | |
9cfc03b7 |
419 | ret->state = state = dup_game(sstate->state); |
6193da8d |
420 | |
6193da8d |
421 | ret->solver_status = sstate->solver_status; |
315e47b9 |
422 | ret->diff = sstate->diff; |
6193da8d |
423 | |
7c95608a |
424 | ret->dotdsf = snewn(num_dots, int); |
425 | ret->looplen = snewn(num_dots, int); |
426 | memcpy(ret->dotdsf, sstate->dotdsf, |
427 | num_dots * sizeof(int)); |
428 | memcpy(ret->looplen, sstate->looplen, |
429 | num_dots * sizeof(int)); |
430 | |
431 | ret->dot_solved = snewn(num_dots, char); |
432 | ret->face_solved = snewn(num_faces, char); |
433 | memcpy(ret->dot_solved, sstate->dot_solved, num_dots); |
434 | memcpy(ret->face_solved, sstate->face_solved, num_faces); |
435 | |
436 | ret->dot_yes_count = snewn(num_dots, char); |
437 | memcpy(ret->dot_yes_count, sstate->dot_yes_count, num_dots); |
438 | ret->dot_no_count = snewn(num_dots, char); |
439 | memcpy(ret->dot_no_count, sstate->dot_no_count, num_dots); |
440 | |
441 | ret->face_yes_count = snewn(num_faces, char); |
442 | memcpy(ret->face_yes_count, sstate->face_yes_count, num_faces); |
443 | ret->face_no_count = snewn(num_faces, char); |
444 | memcpy(ret->face_no_count, sstate->face_no_count, num_faces); |
121aae4b |
445 | |
315e47b9 |
446 | if (sstate->dlines) { |
447 | ret->dlines = snewn(2*num_edges, char); |
448 | memcpy(ret->dlines, sstate->dlines, |
7c95608a |
449 | 2*num_edges); |
121aae4b |
450 | } else { |
315e47b9 |
451 | ret->dlines = NULL; |
121aae4b |
452 | } |
453 | |
315e47b9 |
454 | if (sstate->linedsf) { |
455 | ret->linedsf = snewn(num_edges, int); |
456 | memcpy(ret->linedsf, sstate->linedsf, |
7c95608a |
457 | num_edges * sizeof(int)); |
121aae4b |
458 | } else { |
315e47b9 |
459 | ret->linedsf = NULL; |
121aae4b |
460 | } |
6193da8d |
461 | |
462 | return ret; |
463 | } |
464 | |
121aae4b |
465 | static game_params *default_params(void) |
6193da8d |
466 | { |
121aae4b |
467 | game_params *ret = snew(game_params); |
6193da8d |
468 | |
121aae4b |
469 | #ifdef SLOW_SYSTEM |
7c95608a |
470 | ret->h = 7; |
471 | ret->w = 7; |
121aae4b |
472 | #else |
473 | ret->h = 10; |
474 | ret->w = 10; |
475 | #endif |
476 | ret->diff = DIFF_EASY; |
7c95608a |
477 | ret->type = 0; |
478 | |
121aae4b |
479 | return ret; |
6193da8d |
480 | } |
481 | |
121aae4b |
482 | static game_params *dup_params(game_params *params) |
6193da8d |
483 | { |
121aae4b |
484 | game_params *ret = snew(game_params); |
7c95608a |
485 | |
121aae4b |
486 | *ret = *params; /* structure copy */ |
487 | return ret; |
488 | } |
6193da8d |
489 | |
121aae4b |
490 | static const game_params presets[] = { |
b1535c90 |
491 | #ifdef SMALL_SCREEN |
f875ca4d |
492 | { 7, 7, DIFF_EASY, 0 }, |
493 | { 7, 7, DIFF_NORMAL, 0 }, |
494 | { 7, 7, DIFF_HARD, 0 }, |
495 | { 7, 7, DIFF_HARD, 1 }, |
496 | { 7, 7, DIFF_HARD, 2 }, |
497 | { 5, 5, DIFF_HARD, 3 }, |
498 | { 7, 7, DIFF_HARD, 4 }, |
499 | { 5, 4, DIFF_HARD, 5 }, |
500 | { 5, 5, DIFF_HARD, 6 }, |
501 | { 5, 5, DIFF_HARD, 7 }, |
502 | { 3, 3, DIFF_HARD, 8 }, |
503 | { 3, 3, DIFF_HARD, 9 }, |
504 | { 3, 3, DIFF_HARD, 10 }, |
505 | { 6, 6, DIFF_HARD, 11 }, |
506 | { 6, 6, DIFF_HARD, 12 }, |
b1535c90 |
507 | #else |
f875ca4d |
508 | { 7, 7, DIFF_EASY, 0 }, |
509 | { 10, 10, DIFF_EASY, 0 }, |
510 | { 7, 7, DIFF_NORMAL, 0 }, |
511 | { 10, 10, DIFF_NORMAL, 0 }, |
512 | { 7, 7, DIFF_HARD, 0 }, |
513 | { 10, 10, DIFF_HARD, 0 }, |
514 | { 10, 10, DIFF_HARD, 1 }, |
515 | { 12, 10, DIFF_HARD, 2 }, |
516 | { 7, 7, DIFF_HARD, 3 }, |
517 | { 9, 9, DIFF_HARD, 4 }, |
518 | { 5, 4, DIFF_HARD, 5 }, |
519 | { 7, 7, DIFF_HARD, 6 }, |
520 | { 5, 5, DIFF_HARD, 7 }, |
521 | { 5, 5, DIFF_HARD, 8 }, |
522 | { 5, 4, DIFF_HARD, 9 }, |
523 | { 5, 4, DIFF_HARD, 10 }, |
524 | { 10, 10, DIFF_HARD, 11 }, |
525 | { 10, 10, DIFF_HARD, 12 } |
b1535c90 |
526 | #endif |
121aae4b |
527 | }; |
6193da8d |
528 | |
121aae4b |
529 | static int game_fetch_preset(int i, char **name, game_params **params) |
6193da8d |
530 | { |
1a739e2f |
531 | game_params *tmppar; |
121aae4b |
532 | char buf[80]; |
6193da8d |
533 | |
121aae4b |
534 | if (i < 0 || i >= lenof(presets)) |
535 | return FALSE; |
6193da8d |
536 | |
1a739e2f |
537 | tmppar = snew(game_params); |
538 | *tmppar = presets[i]; |
539 | *params = tmppar; |
f875ca4d |
540 | sprintf(buf, "%dx%d %s - %s", tmppar->h, tmppar->w, |
541 | gridnames[tmppar->type], diffnames[tmppar->diff]); |
121aae4b |
542 | *name = dupstr(buf); |
543 | |
544 | return TRUE; |
6193da8d |
545 | } |
546 | |
547 | static void free_params(game_params *params) |
548 | { |
549 | sfree(params); |
550 | } |
551 | |
552 | static void decode_params(game_params *params, char const *string) |
553 | { |
554 | params->h = params->w = atoi(string); |
c0eb17ce |
555 | params->diff = DIFF_EASY; |
6193da8d |
556 | while (*string && isdigit((unsigned char)*string)) string++; |
557 | if (*string == 'x') { |
558 | string++; |
559 | params->h = atoi(string); |
121aae4b |
560 | while (*string && isdigit((unsigned char)*string)) string++; |
6193da8d |
561 | } |
7c95608a |
562 | if (*string == 't') { |
6193da8d |
563 | string++; |
7c95608a |
564 | params->type = atoi(string); |
121aae4b |
565 | while (*string && isdigit((unsigned char)*string)) string++; |
6193da8d |
566 | } |
c0eb17ce |
567 | if (*string == 'd') { |
568 | int i; |
c0eb17ce |
569 | string++; |
121aae4b |
570 | for (i = 0; i < DIFF_MAX; i++) |
571 | if (*string == diffchars[i]) |
572 | params->diff = i; |
573 | if (*string) string++; |
c0eb17ce |
574 | } |
6193da8d |
575 | } |
576 | |
577 | static char *encode_params(game_params *params, int full) |
578 | { |
579 | char str[80]; |
f875ca4d |
580 | sprintf(str, "%dx%dt%d", params->w, params->h, params->type); |
6193da8d |
581 | if (full) |
7c95608a |
582 | sprintf(str + strlen(str), "d%c", diffchars[params->diff]); |
6193da8d |
583 | return dupstr(str); |
584 | } |
585 | |
586 | static config_item *game_configure(game_params *params) |
587 | { |
588 | config_item *ret; |
589 | char buf[80]; |
590 | |
f875ca4d |
591 | ret = snewn(5, config_item); |
6193da8d |
592 | |
593 | ret[0].name = "Width"; |
594 | ret[0].type = C_STRING; |
595 | sprintf(buf, "%d", params->w); |
596 | ret[0].sval = dupstr(buf); |
597 | ret[0].ival = 0; |
598 | |
599 | ret[1].name = "Height"; |
600 | ret[1].type = C_STRING; |
601 | sprintf(buf, "%d", params->h); |
602 | ret[1].sval = dupstr(buf); |
603 | ret[1].ival = 0; |
604 | |
7c95608a |
605 | ret[2].name = "Grid type"; |
c0eb17ce |
606 | ret[2].type = C_CHOICES; |
7c95608a |
607 | ret[2].sval = GRID_CONFIGS; |
608 | ret[2].ival = params->type; |
6193da8d |
609 | |
7c95608a |
610 | ret[3].name = "Difficulty"; |
611 | ret[3].type = C_CHOICES; |
612 | ret[3].sval = DIFFCONFIG; |
613 | ret[3].ival = params->diff; |
614 | |
f875ca4d |
615 | ret[4].name = NULL; |
616 | ret[4].type = C_END; |
7c95608a |
617 | ret[4].sval = NULL; |
f875ca4d |
618 | ret[4].ival = 0; |
6193da8d |
619 | |
620 | return ret; |
621 | } |
622 | |
623 | static game_params *custom_params(config_item *cfg) |
624 | { |
625 | game_params *ret = snew(game_params); |
626 | |
627 | ret->w = atoi(cfg[0].sval); |
628 | ret->h = atoi(cfg[1].sval); |
7c95608a |
629 | ret->type = cfg[2].ival; |
630 | ret->diff = cfg[3].ival; |
6193da8d |
631 | |
632 | return ret; |
633 | } |
634 | |
635 | static char *validate_params(game_params *params, int full) |
636 | { |
7c95608a |
637 | if (params->type < 0 || params->type >= NUM_GRID_TYPES) |
638 | return "Illegal grid type"; |
e3c9e042 |
639 | if (params->w < grid_size_limits[params->type].amin || |
640 | params->h < grid_size_limits[params->type].amin) |
641 | return grid_size_limits[params->type].aerr; |
642 | if (params->w < grid_size_limits[params->type].omin && |
643 | params->h < grid_size_limits[params->type].omin) |
644 | return grid_size_limits[params->type].oerr; |
c0eb17ce |
645 | |
646 | /* |
647 | * This shouldn't be able to happen at all, since decode_params |
648 | * and custom_params will never generate anything that isn't |
649 | * within range. |
650 | */ |
1a739e2f |
651 | assert(params->diff < DIFF_MAX); |
c0eb17ce |
652 | |
6193da8d |
653 | return NULL; |
654 | } |
655 | |
121aae4b |
656 | /* Returns a newly allocated string describing the current puzzle */ |
657 | static char *state_to_text(const game_state *state) |
6193da8d |
658 | { |
7c95608a |
659 | grid *g = state->game_grid; |
121aae4b |
660 | char *retval; |
7c95608a |
661 | int num_faces = g->num_faces; |
662 | char *description = snewn(num_faces + 1, char); |
121aae4b |
663 | char *dp = description; |
664 | int empty_count = 0; |
7c95608a |
665 | int i; |
6193da8d |
666 | |
7c95608a |
667 | for (i = 0; i < num_faces; i++) { |
668 | if (state->clues[i] < 0) { |
121aae4b |
669 | if (empty_count > 25) { |
670 | dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); |
671 | empty_count = 0; |
672 | } |
673 | empty_count++; |
674 | } else { |
675 | if (empty_count) { |
676 | dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); |
677 | empty_count = 0; |
678 | } |
7c95608a |
679 | dp += sprintf(dp, "%c", (int)CLUE2CHAR(state->clues[i])); |
121aae4b |
680 | } |
681 | } |
6193da8d |
682 | |
121aae4b |
683 | if (empty_count) |
1a739e2f |
684 | dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); |
121aae4b |
685 | |
686 | retval = dupstr(description); |
687 | sfree(description); |
688 | |
689 | return retval; |
6193da8d |
690 | } |
691 | |
cebf0b0d |
692 | #define GRID_DESC_SEP '_' |
693 | |
694 | /* Splits up a (optional) grid_desc from the game desc. Returns the |
695 | * grid_desc (which needs freeing) and updates the desc pointer to |
696 | * start of real desc, or returns NULL if no desc. */ |
697 | static char *extract_grid_desc(char **desc) |
698 | { |
699 | char *sep = strchr(*desc, GRID_DESC_SEP), *gd; |
700 | int gd_len; |
701 | |
702 | if (!sep) return NULL; |
703 | |
704 | gd_len = sep - (*desc); |
705 | gd = snewn(gd_len+1, char); |
706 | memcpy(gd, *desc, gd_len); |
707 | gd[gd_len] = '\0'; |
708 | |
709 | *desc = sep+1; |
710 | |
711 | return gd; |
712 | } |
713 | |
121aae4b |
714 | /* We require that the params pass the test in validate_params and that the |
715 | * description fills the entire game area */ |
716 | static char *validate_desc(game_params *params, char *desc) |
6193da8d |
717 | { |
121aae4b |
718 | int count = 0; |
7c95608a |
719 | grid *g; |
cebf0b0d |
720 | char *grid_desc, *ret; |
721 | |
722 | /* It's pretty inefficient to do this just for validation. All we need to |
723 | * know is the precise number of faces. */ |
724 | grid_desc = extract_grid_desc(&desc); |
f875ca4d |
725 | ret = grid_validate_desc(grid_types[params->type], params->w, params->h, grid_desc); |
cebf0b0d |
726 | if (ret) return ret; |
727 | |
728 | g = loopy_generate_grid(params, grid_desc); |
729 | if (grid_desc) sfree(grid_desc); |
6193da8d |
730 | |
121aae4b |
731 | for (; *desc; ++desc) { |
918a098a |
732 | if ((*desc >= '0' && *desc <= '9') || (*desc >= 'A' && *desc <= 'Z')) { |
121aae4b |
733 | count++; |
734 | continue; |
735 | } |
736 | if (*desc >= 'a') { |
737 | count += *desc - 'a' + 1; |
738 | continue; |
739 | } |
740 | return "Unknown character in description"; |
6193da8d |
741 | } |
742 | |
7c95608a |
743 | if (count < g->num_faces) |
121aae4b |
744 | return "Description too short for board size"; |
7c95608a |
745 | if (count > g->num_faces) |
121aae4b |
746 | return "Description too long for board size"; |
6193da8d |
747 | |
cebf0b0d |
748 | grid_free(g); |
749 | |
121aae4b |
750 | return NULL; |
6193da8d |
751 | } |
752 | |
121aae4b |
753 | /* Sums the lengths of the numbers in range [0,n) */ |
754 | /* See equivalent function in solo.c for justification of this. */ |
755 | static int len_0_to_n(int n) |
6193da8d |
756 | { |
121aae4b |
757 | int len = 1; /* Counting 0 as a bit of a special case */ |
758 | int i; |
759 | |
760 | for (i = 1; i < n; i *= 10) { |
761 | len += max(n - i, 0); |
6193da8d |
762 | } |
121aae4b |
763 | |
764 | return len; |
6193da8d |
765 | } |
766 | |
121aae4b |
767 | static char *encode_solve_move(const game_state *state) |
768 | { |
7c95608a |
769 | int len; |
121aae4b |
770 | char *ret, *p; |
7c95608a |
771 | int i; |
772 | int num_edges = state->game_grid->num_edges; |
773 | |
121aae4b |
774 | /* This is going to return a string representing the moves needed to set |
775 | * every line in a grid to be the same as the ones in 'state'. The exact |
776 | * length of this string is predictable. */ |
6193da8d |
777 | |
121aae4b |
778 | len = 1; /* Count the 'S' prefix */ |
7c95608a |
779 | /* Numbers in all lines */ |
780 | len += len_0_to_n(num_edges); |
781 | /* For each line we also have a letter */ |
782 | len += num_edges; |
6193da8d |
783 | |
121aae4b |
784 | ret = snewn(len + 1, char); |
785 | p = ret; |
6193da8d |
786 | |
121aae4b |
787 | p += sprintf(p, "S"); |
6193da8d |
788 | |
7c95608a |
789 | for (i = 0; i < num_edges; i++) { |
790 | switch (state->lines[i]) { |
791 | case LINE_YES: |
792 | p += sprintf(p, "%dy", i); |
793 | break; |
794 | case LINE_NO: |
795 | p += sprintf(p, "%dn", i); |
796 | break; |
6193da8d |
797 | } |
6193da8d |
798 | } |
121aae4b |
799 | |
800 | /* No point in doing sums like that if they're going to be wrong */ |
801 | assert(strlen(ret) <= (size_t)len); |
802 | return ret; |
6193da8d |
803 | } |
804 | |
121aae4b |
805 | static game_ui *new_ui(game_state *state) |
6193da8d |
806 | { |
121aae4b |
807 | return NULL; |
808 | } |
6193da8d |
809 | |
121aae4b |
810 | static void free_ui(game_ui *ui) |
811 | { |
812 | } |
6193da8d |
813 | |
121aae4b |
814 | static char *encode_ui(game_ui *ui) |
815 | { |
816 | return NULL; |
817 | } |
6193da8d |
818 | |
121aae4b |
819 | static void decode_ui(game_ui *ui, char *encoding) |
820 | { |
821 | } |
6193da8d |
822 | |
121aae4b |
823 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
824 | game_state *newstate) |
825 | { |
826 | } |
6193da8d |
827 | |
121aae4b |
828 | static void game_compute_size(game_params *params, int tilesize, |
829 | int *x, int *y) |
830 | { |
1515b973 |
831 | int grid_width, grid_height, rendered_width, rendered_height; |
cebf0b0d |
832 | int g_tilesize; |
833 | |
834 | grid_compute_size(grid_types[params->type], params->w, params->h, |
835 | &g_tilesize, &grid_width, &grid_height); |
1515b973 |
836 | |
7c95608a |
837 | /* multiply first to minimise rounding error on integer division */ |
cebf0b0d |
838 | rendered_width = grid_width * tilesize / g_tilesize; |
839 | rendered_height = grid_height * tilesize / g_tilesize; |
7c95608a |
840 | *x = rendered_width + 2 * BORDER(tilesize) + 1; |
841 | *y = rendered_height + 2 * BORDER(tilesize) + 1; |
121aae4b |
842 | } |
6193da8d |
843 | |
121aae4b |
844 | static void game_set_size(drawing *dr, game_drawstate *ds, |
7c95608a |
845 | game_params *params, int tilesize) |
121aae4b |
846 | { |
847 | ds->tilesize = tilesize; |
121aae4b |
848 | } |
6193da8d |
849 | |
121aae4b |
850 | static float *game_colours(frontend *fe, int *ncolours) |
851 | { |
852 | float *ret = snewn(4 * NCOLOURS, float); |
6193da8d |
853 | |
121aae4b |
854 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
855 | |
856 | ret[COL_FOREGROUND * 3 + 0] = 0.0F; |
857 | ret[COL_FOREGROUND * 3 + 1] = 0.0F; |
858 | ret[COL_FOREGROUND * 3 + 2] = 0.0F; |
859 | |
32c231bb |
860 | /* |
861 | * We want COL_LINEUNKNOWN to be a yellow which is a bit darker |
862 | * than the background. (I previously set it to 0.8,0.8,0, but |
863 | * found that this went badly with the 0.8,0.8,0.8 favoured as a |
864 | * background by the Java frontend.) |
865 | */ |
866 | ret[COL_LINEUNKNOWN * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.9F; |
867 | ret[COL_LINEUNKNOWN * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.9F; |
7c95608a |
868 | ret[COL_LINEUNKNOWN * 3 + 2] = 0.0F; |
869 | |
121aae4b |
870 | ret[COL_HIGHLIGHT * 3 + 0] = 1.0F; |
871 | ret[COL_HIGHLIGHT * 3 + 1] = 1.0F; |
872 | ret[COL_HIGHLIGHT * 3 + 2] = 1.0F; |
873 | |
874 | ret[COL_MISTAKE * 3 + 0] = 1.0F; |
875 | ret[COL_MISTAKE * 3 + 1] = 0.0F; |
876 | ret[COL_MISTAKE * 3 + 2] = 0.0F; |
877 | |
7c95608a |
878 | ret[COL_SATISFIED * 3 + 0] = 0.0F; |
879 | ret[COL_SATISFIED * 3 + 1] = 0.0F; |
880 | ret[COL_SATISFIED * 3 + 2] = 0.0F; |
881 | |
ec909c7a |
882 | /* We want the faint lines to be a bit darker than the background. |
883 | * Except if the background is pretty dark already; then it ought to be a |
884 | * bit lighter. Oy vey. |
885 | */ |
886 | ret[COL_FAINT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.9F; |
887 | ret[COL_FAINT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.9F; |
888 | ret[COL_FAINT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.9F; |
889 | |
121aae4b |
890 | *ncolours = NCOLOURS; |
891 | return ret; |
892 | } |
893 | |
894 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
895 | { |
896 | struct game_drawstate *ds = snew(struct game_drawstate); |
7c95608a |
897 | int num_faces = state->game_grid->num_faces; |
898 | int num_edges = state->game_grid->num_edges; |
e0936bbd |
899 | int i; |
121aae4b |
900 | |
7c95608a |
901 | ds->tilesize = 0; |
121aae4b |
902 | ds->started = 0; |
7c95608a |
903 | ds->lines = snewn(num_edges, char); |
904 | ds->clue_error = snewn(num_faces, char); |
905 | ds->clue_satisfied = snewn(num_faces, char); |
e0936bbd |
906 | ds->textx = snewn(num_faces, int); |
907 | ds->texty = snewn(num_faces, int); |
121aae4b |
908 | ds->flashing = 0; |
909 | |
7c95608a |
910 | memset(ds->lines, LINE_UNKNOWN, num_edges); |
911 | memset(ds->clue_error, 0, num_faces); |
912 | memset(ds->clue_satisfied, 0, num_faces); |
e0936bbd |
913 | for (i = 0; i < num_faces; i++) |
914 | ds->textx[i] = ds->texty[i] = -1; |
121aae4b |
915 | |
916 | return ds; |
917 | } |
918 | |
919 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
920 | { |
a6bd4b9c |
921 | sfree(ds->textx); |
922 | sfree(ds->texty); |
121aae4b |
923 | sfree(ds->clue_error); |
7c95608a |
924 | sfree(ds->clue_satisfied); |
925 | sfree(ds->lines); |
121aae4b |
926 | sfree(ds); |
927 | } |
928 | |
929 | static int game_timing_state(game_state *state, game_ui *ui) |
930 | { |
931 | return TRUE; |
932 | } |
933 | |
934 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
935 | int dir, game_ui *ui) |
936 | { |
937 | return 0.0F; |
938 | } |
939 | |
7c95608a |
940 | static int game_can_format_as_text_now(game_params *params) |
941 | { |
942 | if (params->type != 0) |
943 | return FALSE; |
944 | return TRUE; |
945 | } |
946 | |
121aae4b |
947 | static char *game_text_format(game_state *state) |
948 | { |
7c95608a |
949 | int w, h, W, H; |
950 | int x, y, i; |
951 | int cell_size; |
952 | char *ret; |
953 | grid *g = state->game_grid; |
954 | grid_face *f; |
955 | |
956 | assert(state->grid_type == 0); |
957 | |
958 | /* Work out the basic size unit */ |
959 | f = g->faces; /* first face */ |
960 | assert(f->order == 4); |
961 | /* The dots are ordered clockwise, so the two opposite |
962 | * corners are guaranteed to span the square */ |
963 | cell_size = abs(f->dots[0]->x - f->dots[2]->x); |
964 | |
965 | w = (g->highest_x - g->lowest_x) / cell_size; |
966 | h = (g->highest_y - g->lowest_y) / cell_size; |
967 | |
968 | /* Create a blank "canvas" to "draw" on */ |
969 | W = 2 * w + 2; |
970 | H = 2 * h + 1; |
971 | ret = snewn(W * H + 1, char); |
972 | for (y = 0; y < H; y++) { |
973 | for (x = 0; x < W-1; x++) { |
974 | ret[y*W + x] = ' '; |
121aae4b |
975 | } |
7c95608a |
976 | ret[y*W + W-1] = '\n'; |
977 | } |
978 | ret[H*W] = '\0'; |
979 | |
980 | /* Fill in edge info */ |
981 | for (i = 0; i < g->num_edges; i++) { |
982 | grid_edge *e = g->edges + i; |
983 | /* Cell coordinates, from (0,0) to (w-1,h-1) */ |
984 | int x1 = (e->dot1->x - g->lowest_x) / cell_size; |
985 | int x2 = (e->dot2->x - g->lowest_x) / cell_size; |
986 | int y1 = (e->dot1->y - g->lowest_y) / cell_size; |
987 | int y2 = (e->dot2->y - g->lowest_y) / cell_size; |
988 | /* Midpoint, in canvas coordinates (canvas coordinates are just twice |
989 | * cell coordinates) */ |
990 | x = x1 + x2; |
991 | y = y1 + y2; |
992 | switch (state->lines[i]) { |
993 | case LINE_YES: |
994 | ret[y*W + x] = (y1 == y2) ? '-' : '|'; |
995 | break; |
996 | case LINE_NO: |
997 | ret[y*W + x] = 'x'; |
998 | break; |
999 | case LINE_UNKNOWN: |
1000 | break; /* already a space */ |
1001 | default: |
1002 | assert(!"Illegal line state"); |
121aae4b |
1003 | } |
121aae4b |
1004 | } |
7c95608a |
1005 | |
1006 | /* Fill in clues */ |
1007 | for (i = 0; i < g->num_faces; i++) { |
1515b973 |
1008 | int x1, x2, y1, y2; |
1009 | |
7c95608a |
1010 | f = g->faces + i; |
1011 | assert(f->order == 4); |
1012 | /* Cell coordinates, from (0,0) to (w-1,h-1) */ |
1515b973 |
1013 | x1 = (f->dots[0]->x - g->lowest_x) / cell_size; |
1014 | x2 = (f->dots[2]->x - g->lowest_x) / cell_size; |
1015 | y1 = (f->dots[0]->y - g->lowest_y) / cell_size; |
1016 | y2 = (f->dots[2]->y - g->lowest_y) / cell_size; |
7c95608a |
1017 | /* Midpoint, in canvas coordinates */ |
1018 | x = x1 + x2; |
1019 | y = y1 + y2; |
1020 | ret[y*W + x] = CLUE2CHAR(state->clues[i]); |
121aae4b |
1021 | } |
121aae4b |
1022 | return ret; |
1023 | } |
1024 | |
1025 | /* ---------------------------------------------------------------------- |
1026 | * Debug code |
1027 | */ |
1028 | |
1029 | #ifdef DEBUG_CACHES |
1030 | static void check_caches(const solver_state* sstate) |
1031 | { |
7c95608a |
1032 | int i; |
121aae4b |
1033 | const game_state *state = sstate->state; |
7c95608a |
1034 | const grid *g = state->game_grid; |
121aae4b |
1035 | |
7c95608a |
1036 | for (i = 0; i < g->num_dots; i++) { |
1037 | assert(dot_order(state, i, LINE_YES) == sstate->dot_yes_count[i]); |
1038 | assert(dot_order(state, i, LINE_NO) == sstate->dot_no_count[i]); |
121aae4b |
1039 | } |
1040 | |
7c95608a |
1041 | for (i = 0; i < g->num_faces; i++) { |
1042 | assert(face_order(state, i, LINE_YES) == sstate->face_yes_count[i]); |
1043 | assert(face_order(state, i, LINE_NO) == sstate->face_no_count[i]); |
121aae4b |
1044 | } |
1045 | } |
1046 | |
1047 | #if 0 |
1048 | #define check_caches(s) \ |
1049 | do { \ |
1050 | fprintf(stderr, "check_caches at line %d\n", __LINE__); \ |
1051 | check_caches(s); \ |
1052 | } while (0) |
1053 | #endif |
1054 | #endif /* DEBUG_CACHES */ |
1055 | |
1056 | /* ---------------------------------------------------------------------- |
1057 | * Solver utility functions |
1058 | */ |
1059 | |
7c95608a |
1060 | /* Sets the line (with index i) to the new state 'line_new', and updates |
1061 | * the cached counts of any affected faces and dots. |
1062 | * Returns TRUE if this actually changed the line's state. */ |
1063 | static int solver_set_line(solver_state *sstate, int i, |
1064 | enum line_state line_new |
121aae4b |
1065 | #ifdef SHOW_WORKING |
7c95608a |
1066 | , const char *reason |
121aae4b |
1067 | #endif |
7c95608a |
1068 | ) |
121aae4b |
1069 | { |
1070 | game_state *state = sstate->state; |
7c95608a |
1071 | grid *g; |
1072 | grid_edge *e; |
121aae4b |
1073 | |
1074 | assert(line_new != LINE_UNKNOWN); |
1075 | |
1076 | check_caches(sstate); |
1077 | |
7c95608a |
1078 | if (state->lines[i] == line_new) { |
1079 | return FALSE; /* nothing changed */ |
121aae4b |
1080 | } |
7c95608a |
1081 | state->lines[i] = line_new; |
121aae4b |
1082 | |
1083 | #ifdef SHOW_WORKING |
7c95608a |
1084 | fprintf(stderr, "solver: set line [%d] to %s (%s)\n", |
1085 | i, line_new == LINE_YES ? "YES" : "NO", |
121aae4b |
1086 | reason); |
1087 | #endif |
1088 | |
7c95608a |
1089 | g = state->game_grid; |
1090 | e = g->edges + i; |
1091 | |
1092 | /* Update the cache for both dots and both faces affected by this. */ |
121aae4b |
1093 | if (line_new == LINE_YES) { |
7c95608a |
1094 | sstate->dot_yes_count[e->dot1 - g->dots]++; |
1095 | sstate->dot_yes_count[e->dot2 - g->dots]++; |
1096 | if (e->face1) { |
1097 | sstate->face_yes_count[e->face1 - g->faces]++; |
1098 | } |
1099 | if (e->face2) { |
1100 | sstate->face_yes_count[e->face2 - g->faces]++; |
1101 | } |
121aae4b |
1102 | } else { |
7c95608a |
1103 | sstate->dot_no_count[e->dot1 - g->dots]++; |
1104 | sstate->dot_no_count[e->dot2 - g->dots]++; |
1105 | if (e->face1) { |
1106 | sstate->face_no_count[e->face1 - g->faces]++; |
1107 | } |
1108 | if (e->face2) { |
1109 | sstate->face_no_count[e->face2 - g->faces]++; |
1110 | } |
1111 | } |
1112 | |
121aae4b |
1113 | check_caches(sstate); |
7c95608a |
1114 | return TRUE; |
121aae4b |
1115 | } |
1116 | |
1117 | #ifdef SHOW_WORKING |
7c95608a |
1118 | #define solver_set_line(a, b, c) \ |
1119 | solver_set_line(a, b, c, __FUNCTION__) |
121aae4b |
1120 | #endif |
1121 | |
1122 | /* |
1123 | * Merge two dots due to the existence of an edge between them. |
1124 | * Updates the dsf tracking equivalence classes, and keeps track of |
1125 | * the length of path each dot is currently a part of. |
1126 | * Returns TRUE if the dots were already linked, ie if they are part of a |
1127 | * closed loop, and false otherwise. |
1128 | */ |
7c95608a |
1129 | static int merge_dots(solver_state *sstate, int edge_index) |
121aae4b |
1130 | { |
1131 | int i, j, len; |
7c95608a |
1132 | grid *g = sstate->state->game_grid; |
1133 | grid_edge *e = g->edges + edge_index; |
121aae4b |
1134 | |
7c95608a |
1135 | i = e->dot1 - g->dots; |
1136 | j = e->dot2 - g->dots; |
121aae4b |
1137 | |
1138 | i = dsf_canonify(sstate->dotdsf, i); |
1139 | j = dsf_canonify(sstate->dotdsf, j); |
1140 | |
1141 | if (i == j) { |
1142 | return TRUE; |
1143 | } else { |
1144 | len = sstate->looplen[i] + sstate->looplen[j]; |
1145 | dsf_merge(sstate->dotdsf, i, j); |
1146 | i = dsf_canonify(sstate->dotdsf, i); |
1147 | sstate->looplen[i] = len; |
1148 | return FALSE; |
1149 | } |
1150 | } |
1151 | |
121aae4b |
1152 | /* Merge two lines because the solver has deduced that they must be either |
1153 | * identical or opposite. Returns TRUE if this is new information, otherwise |
1154 | * FALSE. */ |
7c95608a |
1155 | static int merge_lines(solver_state *sstate, int i, int j, int inverse |
121aae4b |
1156 | #ifdef SHOW_WORKING |
1157 | , const char *reason |
1158 | #endif |
7c95608a |
1159 | ) |
121aae4b |
1160 | { |
7c95608a |
1161 | int inv_tmp; |
121aae4b |
1162 | |
7c95608a |
1163 | assert(i < sstate->state->game_grid->num_edges); |
1164 | assert(j < sstate->state->game_grid->num_edges); |
121aae4b |
1165 | |
315e47b9 |
1166 | i = edsf_canonify(sstate->linedsf, i, &inv_tmp); |
121aae4b |
1167 | inverse ^= inv_tmp; |
315e47b9 |
1168 | j = edsf_canonify(sstate->linedsf, j, &inv_tmp); |
121aae4b |
1169 | inverse ^= inv_tmp; |
1170 | |
315e47b9 |
1171 | edsf_merge(sstate->linedsf, i, j, inverse); |
121aae4b |
1172 | |
1173 | #ifdef SHOW_WORKING |
1174 | if (i != j) { |
7c95608a |
1175 | fprintf(stderr, "%s [%d] [%d] %s(%s)\n", |
1176 | __FUNCTION__, i, j, |
121aae4b |
1177 | inverse ? "inverse " : "", reason); |
1178 | } |
1179 | #endif |
1180 | return (i != j); |
1181 | } |
1182 | |
1183 | #ifdef SHOW_WORKING |
7c95608a |
1184 | #define merge_lines(a, b, c, d) \ |
1185 | merge_lines(a, b, c, d, __FUNCTION__) |
121aae4b |
1186 | #endif |
1187 | |
1188 | /* Count the number of lines of a particular type currently going into the |
7c95608a |
1189 | * given dot. */ |
1190 | static int dot_order(const game_state* state, int dot, char line_type) |
121aae4b |
1191 | { |
1192 | int n = 0; |
7c95608a |
1193 | grid *g = state->game_grid; |
1194 | grid_dot *d = g->dots + dot; |
1195 | int i; |
121aae4b |
1196 | |
7c95608a |
1197 | for (i = 0; i < d->order; i++) { |
1198 | grid_edge *e = d->edges[i]; |
1199 | if (state->lines[e - g->edges] == line_type) |
121aae4b |
1200 | ++n; |
1201 | } |
121aae4b |
1202 | return n; |
1203 | } |
1204 | |
1205 | /* Count the number of lines of a particular type currently surrounding the |
7c95608a |
1206 | * given face */ |
1207 | static int face_order(const game_state* state, int face, char line_type) |
121aae4b |
1208 | { |
1209 | int n = 0; |
7c95608a |
1210 | grid *g = state->game_grid; |
1211 | grid_face *f = g->faces + face; |
1212 | int i; |
121aae4b |
1213 | |
7c95608a |
1214 | for (i = 0; i < f->order; i++) { |
1215 | grid_edge *e = f->edges[i]; |
1216 | if (state->lines[e - g->edges] == line_type) |
1217 | ++n; |
1218 | } |
121aae4b |
1219 | return n; |
1220 | } |
1221 | |
7c95608a |
1222 | /* Set all lines bordering a dot of type old_type to type new_type |
121aae4b |
1223 | * Return value tells caller whether this function actually did anything */ |
7c95608a |
1224 | static int dot_setall(solver_state *sstate, int dot, |
1225 | char old_type, char new_type) |
121aae4b |
1226 | { |
1227 | int retval = FALSE, r; |
1228 | game_state *state = sstate->state; |
7c95608a |
1229 | grid *g; |
1230 | grid_dot *d; |
1231 | int i; |
1232 | |
121aae4b |
1233 | if (old_type == new_type) |
1234 | return FALSE; |
1235 | |
7c95608a |
1236 | g = state->game_grid; |
1237 | d = g->dots + dot; |
121aae4b |
1238 | |
7c95608a |
1239 | for (i = 0; i < d->order; i++) { |
1240 | int line_index = d->edges[i] - g->edges; |
1241 | if (state->lines[line_index] == old_type) { |
1242 | r = solver_set_line(sstate, line_index, new_type); |
1243 | assert(r == TRUE); |
1244 | retval = TRUE; |
1245 | } |
121aae4b |
1246 | } |
121aae4b |
1247 | return retval; |
1248 | } |
1249 | |
7c95608a |
1250 | /* Set all lines bordering a face of type old_type to type new_type */ |
1251 | static int face_setall(solver_state *sstate, int face, |
1252 | char old_type, char new_type) |
121aae4b |
1253 | { |
7c95608a |
1254 | int retval = FALSE, r; |
121aae4b |
1255 | game_state *state = sstate->state; |
7c95608a |
1256 | grid *g; |
1257 | grid_face *f; |
1258 | int i; |
121aae4b |
1259 | |
7c95608a |
1260 | if (old_type == new_type) |
1261 | return FALSE; |
1262 | |
1263 | g = state->game_grid; |
1264 | f = g->faces + face; |
121aae4b |
1265 | |
7c95608a |
1266 | for (i = 0; i < f->order; i++) { |
1267 | int line_index = f->edges[i] - g->edges; |
1268 | if (state->lines[line_index] == old_type) { |
1269 | r = solver_set_line(sstate, line_index, new_type); |
1270 | assert(r == TRUE); |
1271 | retval = TRUE; |
1272 | } |
1273 | } |
1274 | return retval; |
121aae4b |
1275 | } |
1276 | |
1277 | /* ---------------------------------------------------------------------- |
1278 | * Loop generation and clue removal |
1279 | */ |
1280 | |
121aae4b |
1281 | static void add_full_clues(game_state *state, random_state *rs) |
1282 | { |
7c95608a |
1283 | signed char *clues = state->clues; |
7c95608a |
1284 | grid *g = state->game_grid; |
b760b8bd |
1285 | char *board = snewn(g->num_faces, char); |
1286 | int i; |
7126ca41 |
1287 | |
b760b8bd |
1288 | generate_loop(g, board, rs, NULL, NULL); |
7c95608a |
1289 | |
1290 | /* Fill out all the clues by initialising to 0, then iterating over |
1291 | * all edges and incrementing each clue as we find edges that border |
7126ca41 |
1292 | * between BLACK/WHITE faces. While we're at it, we verify that the |
1293 | * algorithm does work, and there aren't any GREY faces still there. */ |
b760b8bd |
1294 | memset(clues, 0, g->num_faces); |
7c95608a |
1295 | for (i = 0; i < g->num_edges; i++) { |
1296 | grid_edge *e = g->edges + i; |
1297 | grid_face *f1 = e->face1; |
1298 | grid_face *f2 = e->face2; |
7126ca41 |
1299 | enum face_colour c1 = FACE_COLOUR(f1); |
1300 | enum face_colour c2 = FACE_COLOUR(f2); |
1301 | assert(c1 != FACE_GREY); |
1302 | assert(c2 != FACE_GREY); |
1303 | if (c1 != c2) { |
7c95608a |
1304 | if (f1) clues[f1 - g->faces]++; |
1305 | if (f2) clues[f2 - g->faces]++; |
1306 | } |
121aae4b |
1307 | } |
121aae4b |
1308 | sfree(board); |
1309 | } |
1310 | |
7c95608a |
1311 | |
1a739e2f |
1312 | static int game_has_unique_soln(const game_state *state, int diff) |
121aae4b |
1313 | { |
1314 | int ret; |
1315 | solver_state *sstate_new; |
1316 | solver_state *sstate = new_solver_state((game_state *)state, diff); |
7c95608a |
1317 | |
315e47b9 |
1318 | sstate_new = solve_game_rec(sstate); |
121aae4b |
1319 | |
1320 | assert(sstate_new->solver_status != SOLVER_MISTAKE); |
1321 | ret = (sstate_new->solver_status == SOLVER_SOLVED); |
1322 | |
1323 | free_solver_state(sstate_new); |
1324 | free_solver_state(sstate); |
1325 | |
1326 | return ret; |
1327 | } |
1328 | |
7c95608a |
1329 | |
121aae4b |
1330 | /* Remove clues one at a time at random. */ |
7c95608a |
1331 | static game_state *remove_clues(game_state *state, random_state *rs, |
1a739e2f |
1332 | int diff) |
121aae4b |
1333 | { |
7c95608a |
1334 | int *face_list; |
1335 | int num_faces = state->game_grid->num_faces; |
121aae4b |
1336 | game_state *ret = dup_game(state), *saved_ret; |
1337 | int n; |
121aae4b |
1338 | |
1339 | /* We need to remove some clues. We'll do this by forming a list of all |
1340 | * available clues, shuffling it, then going along one at a |
1341 | * time clearing each clue in turn for which doing so doesn't render the |
1342 | * board unsolvable. */ |
7c95608a |
1343 | face_list = snewn(num_faces, int); |
1344 | for (n = 0; n < num_faces; ++n) { |
1345 | face_list[n] = n; |
121aae4b |
1346 | } |
1347 | |
7c95608a |
1348 | shuffle(face_list, num_faces, sizeof(int), rs); |
121aae4b |
1349 | |
7c95608a |
1350 | for (n = 0; n < num_faces; ++n) { |
1351 | saved_ret = dup_game(ret); |
1352 | ret->clues[face_list[n]] = -1; |
121aae4b |
1353 | |
1354 | if (game_has_unique_soln(ret, diff)) { |
1355 | free_game(saved_ret); |
1356 | } else { |
1357 | free_game(ret); |
1358 | ret = saved_ret; |
1359 | } |
1360 | } |
7c95608a |
1361 | sfree(face_list); |
121aae4b |
1362 | |
1363 | return ret; |
1364 | } |
1365 | |
7c95608a |
1366 | |
121aae4b |
1367 | static char *new_game_desc(game_params *params, random_state *rs, |
1368 | char **aux, int interactive) |
1369 | { |
1370 | /* solution and description both use run-length encoding in obvious ways */ |
cebf0b0d |
1371 | char *retval, *game_desc, *grid_desc; |
7c95608a |
1372 | grid *g; |
1373 | game_state *state = snew(game_state); |
1374 | game_state *state_new; |
cebf0b0d |
1375 | |
f875ca4d |
1376 | grid_desc = grid_new_desc(grid_types[params->type], params->w, params->h, rs); |
cebf0b0d |
1377 | state->game_grid = g = loopy_generate_grid(params, grid_desc); |
1378 | |
7c95608a |
1379 | state->clues = snewn(g->num_faces, signed char); |
1380 | state->lines = snewn(g->num_edges, char); |
b6bf0adc |
1381 | state->line_errors = snewn(g->num_edges, unsigned char); |
121aae4b |
1382 | |
7c95608a |
1383 | state->grid_type = params->type; |
121aae4b |
1384 | |
7c95608a |
1385 | newboard_please: |
121aae4b |
1386 | |
7c95608a |
1387 | memset(state->lines, LINE_UNKNOWN, g->num_edges); |
b6bf0adc |
1388 | memset(state->line_errors, 0, g->num_edges); |
121aae4b |
1389 | |
1390 | state->solved = state->cheated = FALSE; |
121aae4b |
1391 | |
1392 | /* Get a new random solvable board with all its clues filled in. Yes, this |
1393 | * can loop for ever if the params are suitably unfavourable, but |
1394 | * preventing games smaller than 4x4 seems to stop this happening */ |
121aae4b |
1395 | do { |
1396 | add_full_clues(state, rs); |
1397 | } while (!game_has_unique_soln(state, params->diff)); |
1398 | |
1399 | state_new = remove_clues(state, rs, params->diff); |
1400 | free_game(state); |
1401 | state = state_new; |
1402 | |
7c95608a |
1403 | |
121aae4b |
1404 | if (params->diff > 0 && game_has_unique_soln(state, params->diff-1)) { |
1a739e2f |
1405 | #ifdef SHOW_WORKING |
121aae4b |
1406 | fprintf(stderr, "Rejecting board, it is too easy\n"); |
1a739e2f |
1407 | #endif |
121aae4b |
1408 | goto newboard_please; |
1409 | } |
1410 | |
cebf0b0d |
1411 | game_desc = state_to_text(state); |
121aae4b |
1412 | |
1413 | free_game(state); |
7c95608a |
1414 | |
cebf0b0d |
1415 | if (grid_desc) { |
1416 | retval = snewn(strlen(grid_desc) + 1 + strlen(game_desc) + 1, char); |
fd66a01d |
1417 | sprintf(retval, "%s%c%s", grid_desc, (int)GRID_DESC_SEP, game_desc); |
cebf0b0d |
1418 | sfree(grid_desc); |
1419 | sfree(game_desc); |
1420 | } else { |
1421 | retval = game_desc; |
1422 | } |
1423 | |
121aae4b |
1424 | assert(!validate_desc(params, retval)); |
1425 | |
1426 | return retval; |
1427 | } |
1428 | |
1429 | static game_state *new_game(midend *me, game_params *params, char *desc) |
1430 | { |
7c95608a |
1431 | int i; |
121aae4b |
1432 | game_state *state = snew(game_state); |
1433 | int empties_to_make = 0; |
918a098a |
1434 | int n,n2; |
cebf0b0d |
1435 | const char *dp; |
1436 | char *grid_desc; |
7c95608a |
1437 | grid *g; |
1515b973 |
1438 | int num_faces, num_edges; |
1439 | |
cebf0b0d |
1440 | grid_desc = extract_grid_desc(&desc); |
1441 | state->game_grid = g = loopy_generate_grid(params, grid_desc); |
1442 | if (grid_desc) sfree(grid_desc); |
1443 | |
1444 | dp = desc; |
1445 | |
1515b973 |
1446 | num_faces = g->num_faces; |
1447 | num_edges = g->num_edges; |
121aae4b |
1448 | |
7c95608a |
1449 | state->clues = snewn(num_faces, signed char); |
1450 | state->lines = snewn(num_edges, char); |
b6bf0adc |
1451 | state->line_errors = snewn(num_edges, unsigned char); |
121aae4b |
1452 | |
1453 | state->solved = state->cheated = FALSE; |
1454 | |
7c95608a |
1455 | state->grid_type = params->type; |
1456 | |
1457 | for (i = 0; i < num_faces; i++) { |
121aae4b |
1458 | if (empties_to_make) { |
1459 | empties_to_make--; |
7c95608a |
1460 | state->clues[i] = -1; |
121aae4b |
1461 | continue; |
1462 | } |
1463 | |
1464 | assert(*dp); |
1465 | n = *dp - '0'; |
918a098a |
1466 | n2 = *dp - 'A' + 10; |
121aae4b |
1467 | if (n >= 0 && n < 10) { |
7c95608a |
1468 | state->clues[i] = n; |
918a098a |
1469 | } else if (n2 >= 10 && n2 < 36) { |
1470 | state->clues[i] = n2; |
121aae4b |
1471 | } else { |
1472 | n = *dp - 'a' + 1; |
1473 | assert(n > 0); |
7c95608a |
1474 | state->clues[i] = -1; |
121aae4b |
1475 | empties_to_make = n - 1; |
1476 | } |
1477 | ++dp; |
1478 | } |
1479 | |
7c95608a |
1480 | memset(state->lines, LINE_UNKNOWN, num_edges); |
b6bf0adc |
1481 | memset(state->line_errors, 0, num_edges); |
121aae4b |
1482 | return state; |
1483 | } |
1484 | |
b6bf0adc |
1485 | /* Calculates the line_errors data, and checks if the current state is a |
1486 | * solution */ |
1487 | static int check_completion(game_state *state) |
1488 | { |
1489 | grid *g = state->game_grid; |
1490 | int *dsf; |
1491 | int num_faces = g->num_faces; |
1492 | int i; |
1493 | int infinite_area, finite_area; |
1494 | int loops_found = 0; |
1495 | int found_edge_not_in_loop = FALSE; |
1496 | |
1497 | memset(state->line_errors, 0, g->num_edges); |
1498 | |
1499 | /* LL implementation of SGT's idea: |
1500 | * A loop will partition the grid into an inside and an outside. |
1501 | * If there is more than one loop, the grid will be partitioned into |
1502 | * even more distinct regions. We can therefore track equivalence of |
1503 | * faces, by saying that two faces are equivalent when there is a non-YES |
1504 | * edge between them. |
1505 | * We could keep track of the number of connected components, by counting |
1506 | * the number of dsf-merges that aren't no-ops. |
1507 | * But we're only interested in 3 separate cases: |
1508 | * no loops, one loop, more than one loop. |
1509 | * |
1510 | * No loops: all faces are equivalent to the infinite face. |
1511 | * One loop: only two equivalence classes - finite and infinite. |
1512 | * >= 2 loops: there are 2 distinct finite regions. |
1513 | * |
1514 | * So we simply make two passes through all the edges. |
1515 | * In the first pass, we dsf-merge the two faces bordering each non-YES |
1516 | * edge. |
1517 | * In the second pass, we look for YES-edges bordering: |
1518 | * a) two non-equivalent faces. |
1519 | * b) two non-equivalent faces, and one of them is part of a different |
1520 | * finite area from the first finite area we've seen. |
1521 | * |
1522 | * An occurrence of a) means there is at least one loop. |
1523 | * An occurrence of b) means there is more than one loop. |
1524 | * Edges satisfying a) are marked as errors. |
1525 | * |
1526 | * While we're at it, we set a flag if we find a YES edge that is not |
1527 | * part of a loop. |
1528 | * This information will help decide, if there's a single loop, whether it |
1529 | * is a candidate for being a solution (that is, all YES edges are part of |
1530 | * this loop). |
1531 | * |
1532 | * If there is a candidate loop, we then go through all clues and check |
1533 | * they are all satisfied. If so, we have found a solution and we can |
1534 | * unmark all line_errors. |
1535 | */ |
1536 | |
1537 | /* Infinite face is at the end - its index is num_faces. |
1538 | * This macro is just to make this obvious! */ |
1539 | #define INF_FACE num_faces |
1540 | dsf = snewn(num_faces + 1, int); |
1541 | dsf_init(dsf, num_faces + 1); |
1542 | |
1543 | /* First pass */ |
1544 | for (i = 0; i < g->num_edges; i++) { |
1545 | grid_edge *e = g->edges + i; |
1546 | int f1 = e->face1 ? e->face1 - g->faces : INF_FACE; |
1547 | int f2 = e->face2 ? e->face2 - g->faces : INF_FACE; |
1548 | if (state->lines[i] != LINE_YES) |
1549 | dsf_merge(dsf, f1, f2); |
1550 | } |
1551 | |
1552 | /* Second pass */ |
1553 | infinite_area = dsf_canonify(dsf, INF_FACE); |
1554 | finite_area = -1; |
1555 | for (i = 0; i < g->num_edges; i++) { |
1556 | grid_edge *e = g->edges + i; |
1557 | int f1 = e->face1 ? e->face1 - g->faces : INF_FACE; |
1558 | int can1 = dsf_canonify(dsf, f1); |
1559 | int f2 = e->face2 ? e->face2 - g->faces : INF_FACE; |
1560 | int can2 = dsf_canonify(dsf, f2); |
1561 | if (state->lines[i] != LINE_YES) continue; |
1562 | |
1563 | if (can1 == can2) { |
1564 | /* Faces are equivalent, so this edge not part of a loop */ |
1565 | found_edge_not_in_loop = TRUE; |
1566 | continue; |
1567 | } |
1568 | state->line_errors[i] = TRUE; |
1569 | if (loops_found == 0) loops_found = 1; |
1570 | |
1571 | /* Don't bother with further checks if we've already found 2 loops */ |
1572 | if (loops_found == 2) continue; |
1573 | |
1574 | if (finite_area == -1) { |
1575 | /* Found our first finite area */ |
1576 | if (can1 != infinite_area) |
1577 | finite_area = can1; |
1578 | else |
1579 | finite_area = can2; |
1580 | } |
1581 | |
1582 | /* Have we found a second area? */ |
1583 | if (finite_area != -1) { |
1584 | if (can1 != infinite_area && can1 != finite_area) { |
1585 | loops_found = 2; |
1586 | continue; |
1587 | } |
1588 | if (can2 != infinite_area && can2 != finite_area) { |
1589 | loops_found = 2; |
1590 | } |
1591 | } |
1592 | } |
1593 | |
1594 | /* |
1595 | printf("loops_found = %d\n", loops_found); |
1596 | printf("found_edge_not_in_loop = %s\n", |
1597 | found_edge_not_in_loop ? "TRUE" : "FALSE"); |
1598 | */ |
1599 | |
1600 | sfree(dsf); /* No longer need the dsf */ |
1601 | |
1602 | /* Have we found a candidate loop? */ |
1603 | if (loops_found == 1 && !found_edge_not_in_loop) { |
1604 | /* Yes, so check all clues are satisfied */ |
1605 | int found_clue_violation = FALSE; |
1606 | for (i = 0; i < num_faces; i++) { |
1607 | int c = state->clues[i]; |
1608 | if (c >= 0) { |
1609 | if (face_order(state, i, LINE_YES) != c) { |
1610 | found_clue_violation = TRUE; |
1611 | break; |
1612 | } |
1613 | } |
1614 | } |
1615 | |
1616 | if (!found_clue_violation) { |
1617 | /* The loop is good */ |
1618 | memset(state->line_errors, 0, g->num_edges); |
1619 | return TRUE; /* No need to bother checking for dot violations */ |
1620 | } |
1621 | } |
1622 | |
1623 | /* Check for dot violations */ |
1624 | for (i = 0; i < g->num_dots; i++) { |
1625 | int yes = dot_order(state, i, LINE_YES); |
1626 | int unknown = dot_order(state, i, LINE_UNKNOWN); |
1627 | if ((yes == 1 && unknown == 0) || (yes >= 3)) { |
1628 | /* violation, so mark all YES edges as errors */ |
1629 | grid_dot *d = g->dots + i; |
1630 | int j; |
1631 | for (j = 0; j < d->order; j++) { |
1632 | int e = d->edges[j] - g->edges; |
1633 | if (state->lines[e] == LINE_YES) |
1634 | state->line_errors[e] = TRUE; |
1635 | } |
1636 | } |
1637 | } |
1638 | return FALSE; |
1639 | } |
121aae4b |
1640 | |
1641 | /* ---------------------------------------------------------------------- |
1642 | * Solver logic |
1643 | * |
1644 | * Our solver modes operate as follows. Each mode also uses the modes above it. |
1645 | * |
1646 | * Easy Mode |
1647 | * Just implement the rules of the game. |
1648 | * |
315e47b9 |
1649 | * Normal and Tricky Modes |
7c95608a |
1650 | * For each (adjacent) pair of lines through each dot we store a bit for |
1651 | * whether at least one of them is on and whether at most one is on. (If we |
1652 | * know both or neither is on that's already stored more directly.) |
121aae4b |
1653 | * |
1654 | * Advanced Mode |
1655 | * Use edsf data structure to make equivalence classes of lines that are |
1656 | * known identical to or opposite to one another. |
1657 | */ |
1658 | |
121aae4b |
1659 | |
7c95608a |
1660 | /* DLines: |
1661 | * For general grids, we consider "dlines" to be pairs of lines joined |
1662 | * at a dot. The lines must be adjacent around the dot, so we can think of |
1663 | * a dline as being a dot+face combination. Or, a dot+edge combination where |
1664 | * the second edge is taken to be the next clockwise edge from the dot. |
1665 | * Original loopy code didn't have this extra restriction of the lines being |
1666 | * adjacent. From my tests with square grids, this extra restriction seems to |
1667 | * take little, if anything, away from the quality of the puzzles. |
1668 | * A dline can be uniquely identified by an edge/dot combination, given that |
1669 | * a dline-pair always goes clockwise around its common dot. The edge/dot |
1670 | * combination can be represented by an edge/bool combination - if bool is |
1671 | * TRUE, use edge->dot1 else use edge->dot2. So the total number of dlines is |
1672 | * exactly twice the number of edges in the grid - although the dlines |
1673 | * spanning the infinite face are not all that useful to the solver. |
1674 | * Note that, by convention, a dline goes clockwise around its common dot, |
1675 | * which means the dline goes anti-clockwise around its common face. |
1676 | */ |
121aae4b |
1677 | |
7c95608a |
1678 | /* Helper functions for obtaining an index into an array of dlines, given |
1679 | * various information. We assume the grid layout conventions about how |
1680 | * the various lists are interleaved - see grid_make_consistent() for |
1681 | * details. */ |
121aae4b |
1682 | |
7c95608a |
1683 | /* i points to the first edge of the dline pair, reading clockwise around |
1684 | * the dot. */ |
1685 | static int dline_index_from_dot(grid *g, grid_dot *d, int i) |
121aae4b |
1686 | { |
7c95608a |
1687 | grid_edge *e = d->edges[i]; |
121aae4b |
1688 | int ret; |
7c95608a |
1689 | #ifdef DEBUG_DLINES |
1690 | grid_edge *e2; |
1691 | int i2 = i+1; |
1692 | if (i2 == d->order) i2 = 0; |
1693 | e2 = d->edges[i2]; |
1694 | #endif |
1695 | ret = 2 * (e - g->edges) + ((e->dot1 == d) ? 1 : 0); |
1696 | #ifdef DEBUG_DLINES |
1697 | printf("dline_index_from_dot: d=%d,i=%d, edges [%d,%d] - %d\n", |
1698 | (int)(d - g->dots), i, (int)(e - g->edges), |
1699 | (int)(e2 - g->edges), ret); |
121aae4b |
1700 | #endif |
1701 | return ret; |
1702 | } |
7c95608a |
1703 | /* i points to the second edge of the dline pair, reading clockwise around |
1704 | * the face. That is, the edges of the dline, starting at edge{i}, read |
1705 | * anti-clockwise around the face. By layout conventions, the common dot |
1706 | * of the dline will be f->dots[i] */ |
1707 | static int dline_index_from_face(grid *g, grid_face *f, int i) |
121aae4b |
1708 | { |
7c95608a |
1709 | grid_edge *e = f->edges[i]; |
1710 | grid_dot *d = f->dots[i]; |
121aae4b |
1711 | int ret; |
7c95608a |
1712 | #ifdef DEBUG_DLINES |
1713 | grid_edge *e2; |
1714 | int i2 = i - 1; |
1715 | if (i2 < 0) i2 += f->order; |
1716 | e2 = f->edges[i2]; |
1717 | #endif |
1718 | ret = 2 * (e - g->edges) + ((e->dot1 == d) ? 1 : 0); |
1719 | #ifdef DEBUG_DLINES |
1720 | printf("dline_index_from_face: f=%d,i=%d, edges [%d,%d] - %d\n", |
1721 | (int)(f - g->faces), i, (int)(e - g->edges), |
1722 | (int)(e2 - g->edges), ret); |
121aae4b |
1723 | #endif |
1724 | return ret; |
1725 | } |
7c95608a |
1726 | static int is_atleastone(const char *dline_array, int index) |
121aae4b |
1727 | { |
7c95608a |
1728 | return BIT_SET(dline_array[index], 0); |
121aae4b |
1729 | } |
7c95608a |
1730 | static int set_atleastone(char *dline_array, int index) |
121aae4b |
1731 | { |
7c95608a |
1732 | return SET_BIT(dline_array[index], 0); |
121aae4b |
1733 | } |
7c95608a |
1734 | static int is_atmostone(const char *dline_array, int index) |
121aae4b |
1735 | { |
7c95608a |
1736 | return BIT_SET(dline_array[index], 1); |
1737 | } |
1738 | static int set_atmostone(char *dline_array, int index) |
1739 | { |
1740 | return SET_BIT(dline_array[index], 1); |
121aae4b |
1741 | } |
121aae4b |
1742 | |
1743 | static void array_setall(char *array, char from, char to, int len) |
1744 | { |
1745 | char *p = array, *p_old = p; |
1746 | int len_remaining = len; |
1747 | |
1748 | while ((p = memchr(p, from, len_remaining))) { |
1749 | *p = to; |
1750 | len_remaining -= p - p_old; |
1751 | p_old = p; |
1752 | } |
1753 | } |
6193da8d |
1754 | |
7c95608a |
1755 | /* Helper, called when doing dline dot deductions, in the case where we |
1756 | * have 4 UNKNOWNs, and two of them (adjacent) have *exactly* one YES between |
1757 | * them (because of dline atmostone/atleastone). |
1758 | * On entry, edge points to the first of these two UNKNOWNs. This function |
1759 | * will find the opposite UNKNOWNS (if they are adjacent to one another) |
1760 | * and set their corresponding dline to atleastone. (Setting atmostone |
1761 | * already happens in earlier dline deductions) */ |
1762 | static int dline_set_opp_atleastone(solver_state *sstate, |
1763 | grid_dot *d, int edge) |
121aae4b |
1764 | { |
7c95608a |
1765 | game_state *state = sstate->state; |
1766 | grid *g = state->game_grid; |
1767 | int N = d->order; |
1768 | int opp, opp2; |
1769 | for (opp = 0; opp < N; opp++) { |
1770 | int opp_dline_index; |
1771 | if (opp == edge || opp == edge+1 || opp == edge-1) |
1772 | continue; |
1773 | if (opp == 0 && edge == N-1) |
1774 | continue; |
1775 | if (opp == N-1 && edge == 0) |
1776 | continue; |
1777 | opp2 = opp + 1; |
1778 | if (opp2 == N) opp2 = 0; |
1779 | /* Check if opp, opp2 point to LINE_UNKNOWNs */ |
1780 | if (state->lines[d->edges[opp] - g->edges] != LINE_UNKNOWN) |
1781 | continue; |
1782 | if (state->lines[d->edges[opp2] - g->edges] != LINE_UNKNOWN) |
1783 | continue; |
1784 | /* Found opposite UNKNOWNS and they're next to each other */ |
1785 | opp_dline_index = dline_index_from_dot(g, d, opp); |
315e47b9 |
1786 | return set_atleastone(sstate->dlines, opp_dline_index); |
121aae4b |
1787 | } |
7c95608a |
1788 | return FALSE; |
121aae4b |
1789 | } |
6193da8d |
1790 | |
121aae4b |
1791 | |
7c95608a |
1792 | /* Set pairs of lines around this face which are known to be identical, to |
121aae4b |
1793 | * the given line_state */ |
7c95608a |
1794 | static int face_setall_identical(solver_state *sstate, int face_index, |
1795 | enum line_state line_new) |
121aae4b |
1796 | { |
1797 | /* can[dir] contains the canonical line associated with the line in |
1798 | * direction dir from the square in question. Similarly inv[dir] is |
1799 | * whether or not the line in question is inverse to its canonical |
1800 | * element. */ |
121aae4b |
1801 | int retval = FALSE; |
7c95608a |
1802 | game_state *state = sstate->state; |
1803 | grid *g = state->game_grid; |
1804 | grid_face *f = g->faces + face_index; |
1805 | int N = f->order; |
1806 | int i, j; |
1807 | int can1, can2, inv1, inv2; |
6193da8d |
1808 | |
7c95608a |
1809 | for (i = 0; i < N; i++) { |
1810 | int line1_index = f->edges[i] - g->edges; |
1811 | if (state->lines[line1_index] != LINE_UNKNOWN) |
1812 | continue; |
1813 | for (j = i + 1; j < N; j++) { |
1814 | int line2_index = f->edges[j] - g->edges; |
1815 | if (state->lines[line2_index] != LINE_UNKNOWN) |
121aae4b |
1816 | continue; |
6193da8d |
1817 | |
7c95608a |
1818 | /* Found two UNKNOWNS */ |
315e47b9 |
1819 | can1 = edsf_canonify(sstate->linedsf, line1_index, &inv1); |
1820 | can2 = edsf_canonify(sstate->linedsf, line2_index, &inv2); |
7c95608a |
1821 | if (can1 == can2 && inv1 == inv2) { |
1822 | solver_set_line(sstate, line1_index, line_new); |
1823 | solver_set_line(sstate, line2_index, line_new); |
6193da8d |
1824 | } |
1825 | } |
6193da8d |
1826 | } |
121aae4b |
1827 | return retval; |
1828 | } |
1829 | |
7c95608a |
1830 | /* Given a dot or face, and a count of LINE_UNKNOWNs, find them and |
1831 | * return the edge indices into e. */ |
1832 | static void find_unknowns(game_state *state, |
1833 | grid_edge **edge_list, /* Edge list to search (from a face or a dot) */ |
1834 | int expected_count, /* Number of UNKNOWNs (comes from solver's cache) */ |
1835 | int *e /* Returned edge indices */) |
1836 | { |
1837 | int c = 0; |
1838 | grid *g = state->game_grid; |
1839 | while (c < expected_count) { |
1840 | int line_index = *edge_list - g->edges; |
1841 | if (state->lines[line_index] == LINE_UNKNOWN) { |
1842 | e[c] = line_index; |
1843 | c++; |
6193da8d |
1844 | } |
7c95608a |
1845 | ++edge_list; |
6193da8d |
1846 | } |
6193da8d |
1847 | } |
1848 | |
7c95608a |
1849 | /* If we have a list of edges, and we know whether the number of YESs should |
1850 | * be odd or even, and there are only a few UNKNOWNs, we can do some simple |
1851 | * linedsf deductions. This can be used for both face and dot deductions. |
1852 | * Returns the difficulty level of the next solver that should be used, |
1853 | * or DIFF_MAX if no progress was made. */ |
1854 | static int parity_deductions(solver_state *sstate, |
1855 | grid_edge **edge_list, /* Edge list (from a face or a dot) */ |
1856 | int total_parity, /* Expected number of YESs modulo 2 (either 0 or 1) */ |
1857 | int unknown_count) |
6193da8d |
1858 | { |
121aae4b |
1859 | game_state *state = sstate->state; |
7c95608a |
1860 | int diff = DIFF_MAX; |
315e47b9 |
1861 | int *linedsf = sstate->linedsf; |
7c95608a |
1862 | |
1863 | if (unknown_count == 2) { |
1864 | /* Lines are known alike/opposite, depending on inv. */ |
1865 | int e[2]; |
1866 | find_unknowns(state, edge_list, 2, e); |
1867 | if (merge_lines(sstate, e[0], e[1], total_parity)) |
1868 | diff = min(diff, DIFF_HARD); |
1869 | } else if (unknown_count == 3) { |
1870 | int e[3]; |
1871 | int can[3]; /* canonical edges */ |
1872 | int inv[3]; /* whether can[x] is inverse to e[x] */ |
1873 | find_unknowns(state, edge_list, 3, e); |
1874 | can[0] = edsf_canonify(linedsf, e[0], inv); |
1875 | can[1] = edsf_canonify(linedsf, e[1], inv+1); |
1876 | can[2] = edsf_canonify(linedsf, e[2], inv+2); |
1877 | if (can[0] == can[1]) { |
1878 | if (solver_set_line(sstate, e[2], (total_parity^inv[0]^inv[1]) ? |
1879 | LINE_YES : LINE_NO)) |
1880 | diff = min(diff, DIFF_EASY); |
1881 | } |
1882 | if (can[0] == can[2]) { |
1883 | if (solver_set_line(sstate, e[1], (total_parity^inv[0]^inv[2]) ? |
1884 | LINE_YES : LINE_NO)) |
1885 | diff = min(diff, DIFF_EASY); |
1886 | } |
1887 | if (can[1] == can[2]) { |
1888 | if (solver_set_line(sstate, e[0], (total_parity^inv[1]^inv[2]) ? |
1889 | LINE_YES : LINE_NO)) |
1890 | diff = min(diff, DIFF_EASY); |
1891 | } |
1892 | } else if (unknown_count == 4) { |
1893 | int e[4]; |
1894 | int can[4]; /* canonical edges */ |
1895 | int inv[4]; /* whether can[x] is inverse to e[x] */ |
1896 | find_unknowns(state, edge_list, 4, e); |
1897 | can[0] = edsf_canonify(linedsf, e[0], inv); |
1898 | can[1] = edsf_canonify(linedsf, e[1], inv+1); |
1899 | can[2] = edsf_canonify(linedsf, e[2], inv+2); |
1900 | can[3] = edsf_canonify(linedsf, e[3], inv+3); |
1901 | if (can[0] == can[1]) { |
1902 | if (merge_lines(sstate, e[2], e[3], total_parity^inv[0]^inv[1])) |
1903 | diff = min(diff, DIFF_HARD); |
1904 | } else if (can[0] == can[2]) { |
1905 | if (merge_lines(sstate, e[1], e[3], total_parity^inv[0]^inv[2])) |
1906 | diff = min(diff, DIFF_HARD); |
1907 | } else if (can[0] == can[3]) { |
1908 | if (merge_lines(sstate, e[1], e[2], total_parity^inv[0]^inv[3])) |
1909 | diff = min(diff, DIFF_HARD); |
1910 | } else if (can[1] == can[2]) { |
1911 | if (merge_lines(sstate, e[0], e[3], total_parity^inv[1]^inv[2])) |
1912 | diff = min(diff, DIFF_HARD); |
1913 | } else if (can[1] == can[3]) { |
1914 | if (merge_lines(sstate, e[0], e[2], total_parity^inv[1]^inv[3])) |
1915 | diff = min(diff, DIFF_HARD); |
1916 | } else if (can[2] == can[3]) { |
1917 | if (merge_lines(sstate, e[0], e[1], total_parity^inv[2]^inv[3])) |
1918 | diff = min(diff, DIFF_HARD); |
6193da8d |
1919 | } |
1920 | } |
7c95608a |
1921 | return diff; |
6193da8d |
1922 | } |
1923 | |
7c95608a |
1924 | |
121aae4b |
1925 | /* |
7c95608a |
1926 | * These are the main solver functions. |
121aae4b |
1927 | * |
1928 | * Their return values are diff values corresponding to the lowest mode solver |
1929 | * that would notice the work that they have done. For example if the normal |
1930 | * mode solver adds actual lines or crosses, it will return DIFF_EASY as the |
1931 | * easy mode solver might be able to make progress using that. It doesn't make |
1932 | * sense for one of them to return a diff value higher than that of the |
7c95608a |
1933 | * function itself. |
121aae4b |
1934 | * |
1935 | * Each function returns the lowest value it can, as early as possible, in |
1936 | * order to try and pass as much work as possible back to the lower level |
1937 | * solvers which progress more quickly. |
1938 | */ |
6193da8d |
1939 | |
121aae4b |
1940 | /* PROPOSED NEW DESIGN: |
1941 | * We have a work queue consisting of 'events' notifying us that something has |
1942 | * happened that a particular solver mode might be interested in. For example |
1943 | * the hard mode solver might do something that helps the normal mode solver at |
1944 | * dot [x,y] in which case it will enqueue an event recording this fact. Then |
1945 | * we pull events off the work queue, and hand each in turn to the solver that |
1946 | * is interested in them. If a solver reports that it failed we pass the same |
1947 | * event on to progressively more advanced solvers and the loop detector. Once |
1948 | * we've exhausted an event, or it has helped us progress, we drop it and |
1949 | * continue to the next one. The events are sorted first in order of solver |
1950 | * complexity (easy first) then order of insertion (oldest first). |
1951 | * Once we run out of events we loop over each permitted solver in turn |
1952 | * (easiest first) until either a deduction is made (and an event therefore |
1953 | * emerges) or no further deductions can be made (in which case we've failed). |
1954 | * |
7c95608a |
1955 | * QUESTIONS: |
121aae4b |
1956 | * * How do we 'loop over' a solver when both dots and squares are concerned. |
1957 | * Answer: first all squares then all dots. |
1958 | */ |
1959 | |
315e47b9 |
1960 | static int trivial_deductions(solver_state *sstate) |
6193da8d |
1961 | { |
7c95608a |
1962 | int i, current_yes, current_no; |
1963 | game_state *state = sstate->state; |
1964 | grid *g = state->game_grid; |
1a739e2f |
1965 | int diff = DIFF_MAX; |
6193da8d |
1966 | |
7c95608a |
1967 | /* Per-face deductions */ |
1968 | for (i = 0; i < g->num_faces; i++) { |
1969 | grid_face *f = g->faces + i; |
1970 | |
1971 | if (sstate->face_solved[i]) |
121aae4b |
1972 | continue; |
6193da8d |
1973 | |
7c95608a |
1974 | current_yes = sstate->face_yes_count[i]; |
1975 | current_no = sstate->face_no_count[i]; |
c0eb17ce |
1976 | |
7c95608a |
1977 | if (current_yes + current_no == f->order) { |
1978 | sstate->face_solved[i] = TRUE; |
121aae4b |
1979 | continue; |
1980 | } |
6193da8d |
1981 | |
7c95608a |
1982 | if (state->clues[i] < 0) |
121aae4b |
1983 | continue; |
6193da8d |
1984 | |
dba1fdaf |
1985 | /* |
1986 | * This code checks whether the numeric clue on a face is so |
1987 | * large as to permit all its remaining LINE_UNKNOWNs to be |
1988 | * filled in as LINE_YES, or alternatively so small as to |
1989 | * permit them all to be filled in as LINE_NO. |
1990 | */ |
1991 | |
7c95608a |
1992 | if (state->clues[i] < current_yes) { |
121aae4b |
1993 | sstate->solver_status = SOLVER_MISTAKE; |
1994 | return DIFF_EASY; |
1995 | } |
7c95608a |
1996 | if (state->clues[i] == current_yes) { |
1997 | if (face_setall(sstate, i, LINE_UNKNOWN, LINE_NO)) |
121aae4b |
1998 | diff = min(diff, DIFF_EASY); |
7c95608a |
1999 | sstate->face_solved[i] = TRUE; |
121aae4b |
2000 | continue; |
2001 | } |
c0eb17ce |
2002 | |
7c95608a |
2003 | if (f->order - state->clues[i] < current_no) { |
121aae4b |
2004 | sstate->solver_status = SOLVER_MISTAKE; |
2005 | return DIFF_EASY; |
2006 | } |
7c95608a |
2007 | if (f->order - state->clues[i] == current_no) { |
2008 | if (face_setall(sstate, i, LINE_UNKNOWN, LINE_YES)) |
121aae4b |
2009 | diff = min(diff, DIFF_EASY); |
7c95608a |
2010 | sstate->face_solved[i] = TRUE; |
121aae4b |
2011 | continue; |
2012 | } |
dba1fdaf |
2013 | |
2014 | if (f->order - state->clues[i] == current_no + 1 && |
2015 | f->order - current_yes - current_no > 2) { |
2016 | /* |
2017 | * One small refinement to the above: we also look for any |
2018 | * adjacent pair of LINE_UNKNOWNs around the face with |
2019 | * some LINE_YES incident on it from elsewhere. If we find |
2020 | * one, then we know that pair of LINE_UNKNOWNs can't |
2021 | * _both_ be LINE_YES, and hence that pushes us one line |
2022 | * closer to being able to determine all the rest. |
2023 | */ |
2024 | int j, k, e1, e2, e, d; |
2025 | |
2026 | for (j = 0; j < f->order; j++) { |
2027 | e1 = f->edges[j] - g->edges; |
2028 | e2 = f->edges[j+1 < f->order ? j+1 : 0] - g->edges; |
2029 | |
2030 | if (g->edges[e1].dot1 == g->edges[e2].dot1 || |
2031 | g->edges[e1].dot1 == g->edges[e2].dot2) { |
2032 | d = g->edges[e1].dot1 - g->dots; |
2033 | } else { |
2034 | assert(g->edges[e1].dot2 == g->edges[e2].dot1 || |
2035 | g->edges[e1].dot2 == g->edges[e2].dot2); |
2036 | d = g->edges[e1].dot2 - g->dots; |
2037 | } |
2038 | |
2039 | if (state->lines[e1] == LINE_UNKNOWN && |
2040 | state->lines[e2] == LINE_UNKNOWN) { |
2041 | for (k = 0; k < g->dots[d].order; k++) { |
2042 | int e = g->dots[d].edges[k] - g->edges; |
2043 | if (state->lines[e] == LINE_YES) |
2044 | goto found; /* multi-level break */ |
2045 | } |
2046 | } |
2047 | } |
2048 | continue; |
2049 | |
2050 | found: |
2051 | /* |
2052 | * If we get here, we've found such a pair of edges, and |
2053 | * they're e1 and e2. |
2054 | */ |
2055 | for (j = 0; j < f->order; j++) { |
2056 | e = f->edges[j] - g->edges; |
2057 | if (state->lines[e] == LINE_UNKNOWN && e != e1 && e != e2) { |
2058 | int r = solver_set_line(sstate, e, LINE_YES); |
2059 | assert(r); |
2060 | diff = min(diff, DIFF_EASY); |
2061 | } |
2062 | } |
2063 | } |
121aae4b |
2064 | } |
6193da8d |
2065 | |
121aae4b |
2066 | check_caches(sstate); |
6193da8d |
2067 | |
121aae4b |
2068 | /* Per-dot deductions */ |
7c95608a |
2069 | for (i = 0; i < g->num_dots; i++) { |
2070 | grid_dot *d = g->dots + i; |
2071 | int yes, no, unknown; |
2072 | |
2073 | if (sstate->dot_solved[i]) |
121aae4b |
2074 | continue; |
c0eb17ce |
2075 | |
7c95608a |
2076 | yes = sstate->dot_yes_count[i]; |
2077 | no = sstate->dot_no_count[i]; |
2078 | unknown = d->order - yes - no; |
2079 | |
2080 | if (yes == 0) { |
2081 | if (unknown == 0) { |
2082 | sstate->dot_solved[i] = TRUE; |
2083 | } else if (unknown == 1) { |
2084 | dot_setall(sstate, i, LINE_UNKNOWN, LINE_NO); |
121aae4b |
2085 | diff = min(diff, DIFF_EASY); |
7c95608a |
2086 | sstate->dot_solved[i] = TRUE; |
2087 | } |
2088 | } else if (yes == 1) { |
2089 | if (unknown == 0) { |
121aae4b |
2090 | sstate->solver_status = SOLVER_MISTAKE; |
2091 | return DIFF_EASY; |
7c95608a |
2092 | } else if (unknown == 1) { |
2093 | dot_setall(sstate, i, LINE_UNKNOWN, LINE_YES); |
2094 | diff = min(diff, DIFF_EASY); |
2095 | } |
2096 | } else if (yes == 2) { |
2097 | if (unknown > 0) { |
2098 | dot_setall(sstate, i, LINE_UNKNOWN, LINE_NO); |
2099 | diff = min(diff, DIFF_EASY); |
2100 | } |
2101 | sstate->dot_solved[i] = TRUE; |
2102 | } else { |
2103 | sstate->solver_status = SOLVER_MISTAKE; |
2104 | return DIFF_EASY; |
6193da8d |
2105 | } |
2106 | } |
6193da8d |
2107 | |
121aae4b |
2108 | check_caches(sstate); |
6193da8d |
2109 | |
121aae4b |
2110 | return diff; |
6193da8d |
2111 | } |
2112 | |
315e47b9 |
2113 | static int dline_deductions(solver_state *sstate) |
6193da8d |
2114 | { |
121aae4b |
2115 | game_state *state = sstate->state; |
7c95608a |
2116 | grid *g = state->game_grid; |
315e47b9 |
2117 | char *dlines = sstate->dlines; |
7c95608a |
2118 | int i; |
1a739e2f |
2119 | int diff = DIFF_MAX; |
6193da8d |
2120 | |
7c95608a |
2121 | /* ------ Face deductions ------ */ |
2122 | |
2123 | /* Given a set of dline atmostone/atleastone constraints, need to figure |
2124 | * out if we can deduce any further info. For more general faces than |
2125 | * squares, this turns out to be a tricky problem. |
2126 | * The approach taken here is to define (per face) NxN matrices: |
2127 | * "maxs" and "mins". |
2128 | * The entries maxs(j,k) and mins(j,k) define the upper and lower limits |
2129 | * for the possible number of edges that are YES between positions j and k |
2130 | * going clockwise around the face. Can think of j and k as marking dots |
2131 | * around the face (recall the labelling scheme: edge0 joins dot0 to dot1, |
2132 | * edge1 joins dot1 to dot2 etc). |
2133 | * Trivially, mins(j,j) = maxs(j,j) = 0, and we don't even bother storing |
2134 | * these. mins(j,j+1) and maxs(j,j+1) are determined by whether edge{j} |
2135 | * is YES, NO or UNKNOWN. mins(j,j+2) and maxs(j,j+2) are related to |
2136 | * the dline atmostone/atleastone status for edges j and j+1. |
2137 | * |
2138 | * Then we calculate the remaining entries recursively. We definitely |
2139 | * know that |
2140 | * mins(j,k) >= { mins(j,u) + mins(u,k) } for any u between j and k. |
2141 | * This is because any valid placement of YESs between j and k must give |
2142 | * a valid placement between j and u, and also between u and k. |
2143 | * I believe it's sufficient to use just the two values of u: |
2144 | * j+1 and j+2. Seems to work well in practice - the bounds we compute |
2145 | * are rigorous, even if they might not be best-possible. |
2146 | * |
2147 | * Once we have maxs and mins calculated, we can make inferences about |
2148 | * each dline{j,j+1} by looking at the possible complementary edge-counts |
2149 | * mins(j+2,j) and maxs(j+2,j) and comparing these with the face clue. |
2150 | * As well as dlines, we can make similar inferences about single edges. |
2151 | * For example, consider a pentagon with clue 3, and we know at most one |
2152 | * of (edge0, edge1) is YES, and at most one of (edge2, edge3) is YES. |
2153 | * We could then deduce edge4 is YES, because maxs(0,4) would be 2, so |
2154 | * that final edge would have to be YES to make the count up to 3. |
2155 | */ |
121aae4b |
2156 | |
7c95608a |
2157 | /* Much quicker to allocate arrays on the stack than the heap, so |
2158 | * define the largest possible face size, and base our array allocations |
2159 | * on that. We check this with an assertion, in case someone decides to |
2160 | * make a grid which has larger faces than this. Note, this algorithm |
2161 | * could get quite expensive if there are many large faces. */ |
918a098a |
2162 | #define MAX_FACE_SIZE 12 |
7c95608a |
2163 | |
2164 | for (i = 0; i < g->num_faces; i++) { |
2165 | int maxs[MAX_FACE_SIZE][MAX_FACE_SIZE]; |
2166 | int mins[MAX_FACE_SIZE][MAX_FACE_SIZE]; |
2167 | grid_face *f = g->faces + i; |
2168 | int N = f->order; |
2169 | int j,m; |
2170 | int clue = state->clues[i]; |
2171 | assert(N <= MAX_FACE_SIZE); |
2172 | if (sstate->face_solved[i]) |
6193da8d |
2173 | continue; |
7c95608a |
2174 | if (clue < 0) continue; |
2175 | |
2176 | /* Calculate the (j,j+1) entries */ |
2177 | for (j = 0; j < N; j++) { |
2178 | int edge_index = f->edges[j] - g->edges; |
2179 | int dline_index; |
2180 | enum line_state line1 = state->lines[edge_index]; |
2181 | enum line_state line2; |
2182 | int tmp; |
2183 | int k = j + 1; |
2184 | if (k >= N) k = 0; |
2185 | maxs[j][k] = (line1 == LINE_NO) ? 0 : 1; |
2186 | mins[j][k] = (line1 == LINE_YES) ? 1 : 0; |
2187 | /* Calculate the (j,j+2) entries */ |
2188 | dline_index = dline_index_from_face(g, f, k); |
2189 | edge_index = f->edges[k] - g->edges; |
2190 | line2 = state->lines[edge_index]; |
2191 | k++; |
2192 | if (k >= N) k = 0; |
2193 | |
2194 | /* max */ |
2195 | tmp = 2; |
2196 | if (line1 == LINE_NO) tmp--; |
2197 | if (line2 == LINE_NO) tmp--; |
2198 | if (tmp == 2 && is_atmostone(dlines, dline_index)) |
2199 | tmp = 1; |
2200 | maxs[j][k] = tmp; |
2201 | |
2202 | /* min */ |
2203 | tmp = 0; |
2204 | if (line1 == LINE_YES) tmp++; |
2205 | if (line2 == LINE_YES) tmp++; |
2206 | if (tmp == 0 && is_atleastone(dlines, dline_index)) |
2207 | tmp = 1; |
2208 | mins[j][k] = tmp; |
2209 | } |
121aae4b |
2210 | |
7c95608a |
2211 | /* Calculate the (j,j+m) entries for m between 3 and N-1 */ |
2212 | for (m = 3; m < N; m++) { |
2213 | for (j = 0; j < N; j++) { |
2214 | int k = j + m; |
2215 | int u = j + 1; |
2216 | int v = j + 2; |
2217 | int tmp; |
2218 | if (k >= N) k -= N; |
2219 | if (u >= N) u -= N; |
2220 | if (v >= N) v -= N; |
2221 | maxs[j][k] = maxs[j][u] + maxs[u][k]; |
2222 | mins[j][k] = mins[j][u] + mins[u][k]; |
2223 | tmp = maxs[j][v] + maxs[v][k]; |
2224 | maxs[j][k] = min(maxs[j][k], tmp); |
2225 | tmp = mins[j][v] + mins[v][k]; |
2226 | mins[j][k] = max(mins[j][k], tmp); |
2227 | } |
2228 | } |
121aae4b |
2229 | |
7c95608a |
2230 | /* See if we can make any deductions */ |
2231 | for (j = 0; j < N; j++) { |
2232 | int k; |
2233 | grid_edge *e = f->edges[j]; |
2234 | int line_index = e - g->edges; |
2235 | int dline_index; |
121aae4b |
2236 | |
7c95608a |
2237 | if (state->lines[line_index] != LINE_UNKNOWN) |
2238 | continue; |
2239 | k = j + 1; |
2240 | if (k >= N) k = 0; |
121aae4b |
2241 | |
7c95608a |
2242 | /* minimum YESs in the complement of this edge */ |
2243 | if (mins[k][j] > clue) { |
2244 | sstate->solver_status = SOLVER_MISTAKE; |
2245 | return DIFF_EASY; |
2246 | } |
2247 | if (mins[k][j] == clue) { |
2248 | /* setting this edge to YES would make at least |
2249 | * (clue+1) edges - contradiction */ |
2250 | solver_set_line(sstate, line_index, LINE_NO); |
2251 | diff = min(diff, DIFF_EASY); |
2252 | } |
2253 | if (maxs[k][j] < clue - 1) { |
2254 | sstate->solver_status = SOLVER_MISTAKE; |
2255 | return DIFF_EASY; |
2256 | } |
2257 | if (maxs[k][j] == clue - 1) { |
2258 | /* Only way to satisfy the clue is to set edge{j} as YES */ |
2259 | solver_set_line(sstate, line_index, LINE_YES); |
2260 | diff = min(diff, DIFF_EASY); |
2261 | } |
2262 | |
315e47b9 |
2263 | /* More advanced deduction that allows propagation along diagonal |
2264 | * chains of faces connected by dots, for example, 3-2-...-2-3 |
2265 | * in square grids. */ |
2266 | if (sstate->diff >= DIFF_TRICKY) { |
2267 | /* Now see if we can make dline deduction for edges{j,j+1} */ |
2268 | e = f->edges[k]; |
2269 | if (state->lines[e - g->edges] != LINE_UNKNOWN) |
2270 | /* Only worth doing this for an UNKNOWN,UNKNOWN pair. |
2271 | * Dlines where one of the edges is known, are handled in the |
2272 | * dot-deductions */ |
2273 | continue; |
2274 | |
2275 | dline_index = dline_index_from_face(g, f, k); |
2276 | k++; |
2277 | if (k >= N) k = 0; |
2278 | |
2279 | /* minimum YESs in the complement of this dline */ |
2280 | if (mins[k][j] > clue - 2) { |
2281 | /* Adding 2 YESs would break the clue */ |
2282 | if (set_atmostone(dlines, dline_index)) |
2283 | diff = min(diff, DIFF_NORMAL); |
2284 | } |
2285 | /* maximum YESs in the complement of this dline */ |
2286 | if (maxs[k][j] < clue) { |
2287 | /* Adding 2 NOs would mean not enough YESs */ |
2288 | if (set_atleastone(dlines, dline_index)) |
2289 | diff = min(diff, DIFF_NORMAL); |
2290 | } |
7c95608a |
2291 | } |
6193da8d |
2292 | } |
6193da8d |
2293 | } |
2294 | |
121aae4b |
2295 | if (diff < DIFF_NORMAL) |
2296 | return diff; |
6193da8d |
2297 | |
7c95608a |
2298 | /* ------ Dot deductions ------ */ |
6193da8d |
2299 | |
7c95608a |
2300 | for (i = 0; i < g->num_dots; i++) { |
2301 | grid_dot *d = g->dots + i; |
2302 | int N = d->order; |
2303 | int yes, no, unknown; |
2304 | int j; |
2305 | if (sstate->dot_solved[i]) |
2306 | continue; |
2307 | yes = sstate->dot_yes_count[i]; |
2308 | no = sstate->dot_no_count[i]; |
2309 | unknown = N - yes - no; |
2310 | |
2311 | for (j = 0; j < N; j++) { |
2312 | int k; |
2313 | int dline_index; |
2314 | int line1_index, line2_index; |
2315 | enum line_state line1, line2; |
2316 | k = j + 1; |
2317 | if (k >= N) k = 0; |
2318 | dline_index = dline_index_from_dot(g, d, j); |
2319 | line1_index = d->edges[j] - g->edges; |
2320 | line2_index = d->edges[k] - g->edges; |
2321 | line1 = state->lines[line1_index]; |
2322 | line2 = state->lines[line2_index]; |
2323 | |
2324 | /* Infer dline state from line state */ |
2325 | if (line1 == LINE_NO || line2 == LINE_NO) { |
2326 | if (set_atmostone(dlines, dline_index)) |
2327 | diff = min(diff, DIFF_NORMAL); |
2328 | } |
2329 | if (line1 == LINE_YES || line2 == LINE_YES) { |
2330 | if (set_atleastone(dlines, dline_index)) |
2331 | diff = min(diff, DIFF_NORMAL); |
2332 | } |
2333 | /* Infer line state from dline state */ |
2334 | if (is_atmostone(dlines, dline_index)) { |
2335 | if (line1 == LINE_YES && line2 == LINE_UNKNOWN) { |
2336 | solver_set_line(sstate, line2_index, LINE_NO); |
2337 | diff = min(diff, DIFF_EASY); |
2338 | } |
2339 | if (line2 == LINE_YES && line1 == LINE_UNKNOWN) { |
2340 | solver_set_line(sstate, line1_index, LINE_NO); |
2341 | diff = min(diff, DIFF_EASY); |
2342 | } |
2343 | } |
2344 | if (is_atleastone(dlines, dline_index)) { |
2345 | if (line1 == LINE_NO && line2 == LINE_UNKNOWN) { |
2346 | solver_set_line(sstate, line2_index, LINE_YES); |
2347 | diff = min(diff, DIFF_EASY); |
2348 | } |
2349 | if (line2 == LINE_NO && line1 == LINE_UNKNOWN) { |
2350 | solver_set_line(sstate, line1_index, LINE_YES); |
2351 | diff = min(diff, DIFF_EASY); |
2352 | } |
2353 | } |
2354 | /* Deductions that depend on the numbers of lines. |
2355 | * Only bother if both lines are UNKNOWN, otherwise the |
2356 | * easy-mode solver (or deductions above) would have taken |
2357 | * care of it. */ |
2358 | if (line1 != LINE_UNKNOWN || line2 != LINE_UNKNOWN) |
2359 | continue; |
6193da8d |
2360 | |
7c95608a |
2361 | if (yes == 0 && unknown == 2) { |
2362 | /* Both these unknowns must be identical. If we know |
2363 | * atmostone or atleastone, we can make progress. */ |
2364 | if (is_atmostone(dlines, dline_index)) { |
2365 | solver_set_line(sstate, line1_index, LINE_NO); |
2366 | solver_set_line(sstate, line2_index, LINE_NO); |
2367 | diff = min(diff, DIFF_EASY); |
2368 | } |
2369 | if (is_atleastone(dlines, dline_index)) { |
2370 | solver_set_line(sstate, line1_index, LINE_YES); |
2371 | solver_set_line(sstate, line2_index, LINE_YES); |
2372 | diff = min(diff, DIFF_EASY); |
2373 | } |
2374 | } |
2375 | if (yes == 1) { |
2376 | if (set_atmostone(dlines, dline_index)) |
2377 | diff = min(diff, DIFF_NORMAL); |
2378 | if (unknown == 2) { |
2379 | if (set_atleastone(dlines, dline_index)) |
2380 | diff = min(diff, DIFF_NORMAL); |
2381 | } |
121aae4b |
2382 | } |
6193da8d |
2383 | |
315e47b9 |
2384 | /* More advanced deduction that allows propagation along diagonal |
2385 | * chains of faces connected by dots, for example: 3-2-...-2-3 |
2386 | * in square grids. */ |
2387 | if (sstate->diff >= DIFF_TRICKY) { |
2388 | /* If we have atleastone set for this dline, infer |
2389 | * atmostone for each "opposite" dline (that is, each |
2390 | * dline without edges in common with this one). |
2391 | * Again, this test is only worth doing if both these |
2392 | * lines are UNKNOWN. For if one of these lines were YES, |
2393 | * the (yes == 1) test above would kick in instead. */ |
2394 | if (is_atleastone(dlines, dline_index)) { |
2395 | int opp; |
2396 | for (opp = 0; opp < N; opp++) { |
2397 | int opp_dline_index; |
2398 | if (opp == j || opp == j+1 || opp == j-1) |
2399 | continue; |
2400 | if (j == 0 && opp == N-1) |
2401 | continue; |
2402 | if (j == N-1 && opp == 0) |
2403 | continue; |
2404 | opp_dline_index = dline_index_from_dot(g, d, opp); |
2405 | if (set_atmostone(dlines, opp_dline_index)) |
2406 | diff = min(diff, DIFF_NORMAL); |
2407 | } |
2408 | if (yes == 0 && is_atmostone(dlines, dline_index)) { |
2409 | /* This dline has *exactly* one YES and there are no |
2410 | * other YESs. This allows more deductions. */ |
2411 | if (unknown == 3) { |
2412 | /* Third unknown must be YES */ |
2413 | for (opp = 0; opp < N; opp++) { |
2414 | int opp_index; |
2415 | if (opp == j || opp == k) |
2416 | continue; |
2417 | opp_index = d->edges[opp] - g->edges; |
2418 | if (state->lines[opp_index] == LINE_UNKNOWN) { |
2419 | solver_set_line(sstate, opp_index, |
2420 | LINE_YES); |
2421 | diff = min(diff, DIFF_EASY); |
2422 | } |
121aae4b |
2423 | } |
315e47b9 |
2424 | } else if (unknown == 4) { |
2425 | /* Exactly one of opposite UNKNOWNS is YES. We've |
2426 | * already set atmostone, so set atleastone as |
2427 | * well. |
2428 | */ |
2429 | if (dline_set_opp_atleastone(sstate, d, j)) |
2430 | diff = min(diff, DIFF_NORMAL); |
121aae4b |
2431 | } |
2432 | } |
121aae4b |
2433 | } |
6193da8d |
2434 | } |
6193da8d |
2435 | } |
121aae4b |
2436 | } |
121aae4b |
2437 | return diff; |
6193da8d |
2438 | } |
2439 | |
315e47b9 |
2440 | static int linedsf_deductions(solver_state *sstate) |
6193da8d |
2441 | { |
121aae4b |
2442 | game_state *state = sstate->state; |
7c95608a |
2443 | grid *g = state->game_grid; |
315e47b9 |
2444 | char *dlines = sstate->dlines; |
7c95608a |
2445 | int i; |
1a739e2f |
2446 | int diff = DIFF_MAX; |
7c95608a |
2447 | int diff_tmp; |
121aae4b |
2448 | |
7c95608a |
2449 | /* ------ Face deductions ------ */ |
6193da8d |
2450 | |
7c95608a |
2451 | /* A fully-general linedsf deduction seems overly complicated |
2452 | * (I suspect the problem is NP-complete, though in practice it might just |
2453 | * be doable because faces are limited in size). |
2454 | * For simplicity, we only consider *pairs* of LINE_UNKNOWNS that are |
2455 | * known to be identical. If setting them both to YES (or NO) would break |
2456 | * the clue, set them to NO (or YES). */ |
121aae4b |
2457 | |
7c95608a |
2458 | for (i = 0; i < g->num_faces; i++) { |
2459 | int N, yes, no, unknown; |
2460 | int clue; |
6193da8d |
2461 | |
7c95608a |
2462 | if (sstate->face_solved[i]) |
121aae4b |
2463 | continue; |
7c95608a |
2464 | clue = state->clues[i]; |
2465 | if (clue < 0) |
121aae4b |
2466 | continue; |
6193da8d |
2467 | |
7c95608a |
2468 | N = g->faces[i].order; |
2469 | yes = sstate->face_yes_count[i]; |
2470 | if (yes + 1 == clue) { |
2471 | if (face_setall_identical(sstate, i, LINE_NO)) |
2472 | diff = min(diff, DIFF_EASY); |
121aae4b |
2473 | } |
7c95608a |
2474 | no = sstate->face_no_count[i]; |
2475 | if (no + 1 == N - clue) { |
2476 | if (face_setall_identical(sstate, i, LINE_YES)) |
2477 | diff = min(diff, DIFF_EASY); |
6193da8d |
2478 | } |
6193da8d |
2479 | |
7c95608a |
2480 | /* Reload YES count, it might have changed */ |
2481 | yes = sstate->face_yes_count[i]; |
2482 | unknown = N - no - yes; |
2483 | |
2484 | /* Deductions with small number of LINE_UNKNOWNs, based on overall |
2485 | * parity of lines. */ |
2486 | diff_tmp = parity_deductions(sstate, g->faces[i].edges, |
2487 | (clue - yes) % 2, unknown); |
2488 | diff = min(diff, diff_tmp); |
2489 | } |
2490 | |
2491 | /* ------ Dot deductions ------ */ |
2492 | for (i = 0; i < g->num_dots; i++) { |
2493 | grid_dot *d = g->dots + i; |
2494 | int N = d->order; |
2495 | int j; |
2496 | int yes, no, unknown; |
2497 | /* Go through dlines, and do any dline<->linedsf deductions wherever |
2498 | * we find two UNKNOWNS. */ |
2499 | for (j = 0; j < N; j++) { |
2500 | int dline_index = dline_index_from_dot(g, d, j); |
2501 | int line1_index; |
2502 | int line2_index; |
2503 | int can1, can2, inv1, inv2; |
2504 | int j2; |
2505 | line1_index = d->edges[j] - g->edges; |
2506 | if (state->lines[line1_index] != LINE_UNKNOWN) |
121aae4b |
2507 | continue; |
7c95608a |
2508 | j2 = j + 1; |
2509 | if (j2 == N) j2 = 0; |
2510 | line2_index = d->edges[j2] - g->edges; |
2511 | if (state->lines[line2_index] != LINE_UNKNOWN) |
121aae4b |
2512 | continue; |
7c95608a |
2513 | /* Infer dline flags from linedsf */ |
315e47b9 |
2514 | can1 = edsf_canonify(sstate->linedsf, line1_index, &inv1); |
2515 | can2 = edsf_canonify(sstate->linedsf, line2_index, &inv2); |
7c95608a |
2516 | if (can1 == can2 && inv1 != inv2) { |
2517 | /* These are opposites, so set dline atmostone/atleastone */ |
2518 | if (set_atmostone(dlines, dline_index)) |
2519 | diff = min(diff, DIFF_NORMAL); |
2520 | if (set_atleastone(dlines, dline_index)) |
2521 | diff = min(diff, DIFF_NORMAL); |
121aae4b |
2522 | continue; |
7c95608a |
2523 | } |
2524 | /* Infer linedsf from dline flags */ |
2525 | if (is_atmostone(dlines, dline_index) |
2526 | && is_atleastone(dlines, dline_index)) { |
2527 | if (merge_lines(sstate, line1_index, line2_index, 1)) |
121aae4b |
2528 | diff = min(diff, DIFF_HARD); |
121aae4b |
2529 | } |
2530 | } |
7c95608a |
2531 | |
2532 | /* Deductions with small number of LINE_UNKNOWNs, based on overall |
2533 | * parity of lines. */ |
2534 | yes = sstate->dot_yes_count[i]; |
2535 | no = sstate->dot_no_count[i]; |
2536 | unknown = N - yes - no; |
2537 | diff_tmp = parity_deductions(sstate, d->edges, |
2538 | yes % 2, unknown); |
2539 | diff = min(diff, diff_tmp); |
121aae4b |
2540 | } |
6193da8d |
2541 | |
7c95608a |
2542 | /* ------ Edge dsf deductions ------ */ |
2543 | |
2544 | /* If the state of a line is known, deduce the state of its canonical line |
2545 | * too, and vice versa. */ |
2546 | for (i = 0; i < g->num_edges; i++) { |
2547 | int can, inv; |
2548 | enum line_state s; |
315e47b9 |
2549 | can = edsf_canonify(sstate->linedsf, i, &inv); |
7c95608a |
2550 | if (can == i) |
2551 | continue; |
2552 | s = sstate->state->lines[can]; |
2553 | if (s != LINE_UNKNOWN) { |
2554 | if (solver_set_line(sstate, i, inv ? OPP(s) : s)) |
2555 | diff = min(diff, DIFF_EASY); |
2556 | } else { |
2557 | s = sstate->state->lines[i]; |
2558 | if (s != LINE_UNKNOWN) { |
2559 | if (solver_set_line(sstate, can, inv ? OPP(s) : s)) |
121aae4b |
2560 | diff = min(diff, DIFF_EASY); |
2561 | } |
2562 | } |
2563 | } |
6193da8d |
2564 | |
121aae4b |
2565 | return diff; |
2566 | } |
6193da8d |
2567 | |
121aae4b |
2568 | static int loop_deductions(solver_state *sstate) |
2569 | { |
2570 | int edgecount = 0, clues = 0, satclues = 0, sm1clues = 0; |
2571 | game_state *state = sstate->state; |
7c95608a |
2572 | grid *g = state->game_grid; |
2573 | int shortest_chainlen = g->num_dots; |
121aae4b |
2574 | int loop_found = FALSE; |
121aae4b |
2575 | int dots_connected; |
2576 | int progress = FALSE; |
7c95608a |
2577 | int i; |
6193da8d |
2578 | |
121aae4b |
2579 | /* |
2580 | * Go through the grid and update for all the new edges. |
2581 | * Since merge_dots() is idempotent, the simplest way to |
2582 | * do this is just to update for _all_ the edges. |
7c95608a |
2583 | * Also, while we're here, we count the edges. |
121aae4b |
2584 | */ |
7c95608a |
2585 | for (i = 0; i < g->num_edges; i++) { |
2586 | if (state->lines[i] == LINE_YES) { |
2587 | loop_found |= merge_dots(sstate, i); |
121aae4b |
2588 | edgecount++; |
2589 | } |
7c95608a |
2590 | } |
6193da8d |
2591 | |
7c95608a |
2592 | /* |
2593 | * Count the clues, count the satisfied clues, and count the |
2594 | * satisfied-minus-one clues. |
2595 | */ |
2596 | for (i = 0; i < g->num_faces; i++) { |
2597 | int c = state->clues[i]; |
2598 | if (c >= 0) { |
2599 | int o = sstate->face_yes_count[i]; |
121aae4b |
2600 | if (o == c) |
2601 | satclues++; |
2602 | else if (o == c-1) |
2603 | sm1clues++; |
2604 | clues++; |
2605 | } |
2606 | } |
6193da8d |
2607 | |
7c95608a |
2608 | for (i = 0; i < g->num_dots; ++i) { |
2609 | dots_connected = |
121aae4b |
2610 | sstate->looplen[dsf_canonify(sstate->dotdsf, i)]; |
2611 | if (dots_connected > 1) |
2612 | shortest_chainlen = min(shortest_chainlen, dots_connected); |
6193da8d |
2613 | } |
6193da8d |
2614 | |
121aae4b |
2615 | assert(sstate->solver_status == SOLVER_INCOMPLETE); |
6c42c563 |
2616 | |
121aae4b |
2617 | if (satclues == clues && shortest_chainlen == edgecount) { |
2618 | sstate->solver_status = SOLVER_SOLVED; |
2619 | /* This discovery clearly counts as progress, even if we haven't |
2620 | * just added any lines or anything */ |
7c95608a |
2621 | progress = TRUE; |
121aae4b |
2622 | goto finished_loop_deductionsing; |
2623 | } |
6193da8d |
2624 | |
121aae4b |
2625 | /* |
2626 | * Now go through looking for LINE_UNKNOWN edges which |
2627 | * connect two dots that are already in the same |
2628 | * equivalence class. If we find one, test to see if the |
2629 | * loop it would create is a solution. |
2630 | */ |
7c95608a |
2631 | for (i = 0; i < g->num_edges; i++) { |
2632 | grid_edge *e = g->edges + i; |
2633 | int d1 = e->dot1 - g->dots; |
2634 | int d2 = e->dot2 - g->dots; |
2635 | int eqclass, val; |
2636 | if (state->lines[i] != LINE_UNKNOWN) |
2637 | continue; |
121aae4b |
2638 | |
7c95608a |
2639 | eqclass = dsf_canonify(sstate->dotdsf, d1); |
2640 | if (eqclass != dsf_canonify(sstate->dotdsf, d2)) |
2641 | continue; |
121aae4b |
2642 | |
7c95608a |
2643 | val = LINE_NO; /* loop is bad until proven otherwise */ |
6193da8d |
2644 | |
7c95608a |
2645 | /* |
2646 | * This edge would form a loop. Next |
2647 | * question: how long would the loop be? |
2648 | * Would it equal the total number of edges |
2649 | * (plus the one we'd be adding if we added |
2650 | * it)? |
2651 | */ |
2652 | if (sstate->looplen[eqclass] == edgecount + 1) { |
2653 | int sm1_nearby; |
121aae4b |
2654 | |
2655 | /* |
7c95608a |
2656 | * This edge would form a loop which |
2657 | * took in all the edges in the entire |
2658 | * grid. So now we need to work out |
2659 | * whether it would be a valid solution |
2660 | * to the puzzle, which means we have to |
2661 | * check if it satisfies all the clues. |
2662 | * This means that every clue must be |
2663 | * either satisfied or satisfied-minus- |
2664 | * 1, and also that the number of |
2665 | * satisfied-minus-1 clues must be at |
2666 | * most two and they must lie on either |
2667 | * side of this edge. |
121aae4b |
2668 | */ |
7c95608a |
2669 | sm1_nearby = 0; |
2670 | if (e->face1) { |
2671 | int f = e->face1 - g->faces; |
2672 | int c = state->clues[f]; |
2673 | if (c >= 0 && sstate->face_yes_count[f] == c - 1) |
121aae4b |
2674 | sm1_nearby++; |
6c42c563 |
2675 | } |
7c95608a |
2676 | if (e->face2) { |
2677 | int f = e->face2 - g->faces; |
2678 | int c = state->clues[f]; |
2679 | if (c >= 0 && sstate->face_yes_count[f] == c - 1) |
2680 | sm1_nearby++; |
6c42c563 |
2681 | } |
7c95608a |
2682 | if (sm1clues == sm1_nearby && |
2683 | sm1clues + satclues == clues) { |
2684 | val = LINE_YES; /* loop is good! */ |
6c42c563 |
2685 | } |
121aae4b |
2686 | } |
7c95608a |
2687 | |
2688 | /* |
2689 | * Right. Now we know that adding this edge |
2690 | * would form a loop, and we know whether |
2691 | * that loop would be a viable solution or |
2692 | * not. |
2693 | * |
2694 | * If adding this edge produces a solution, |
2695 | * then we know we've found _a_ solution but |
2696 | * we don't know that it's _the_ solution - |
2697 | * if it were provably the solution then |
2698 | * we'd have deduced this edge some time ago |
2699 | * without the need to do loop detection. So |
2700 | * in this state we return SOLVER_AMBIGUOUS, |
2701 | * which has the effect that hitting Solve |
2702 | * on a user-provided puzzle will fill in a |
2703 | * solution but using the solver to |
2704 | * construct new puzzles won't consider this |
2705 | * a reasonable deduction for the user to |
2706 | * make. |
2707 | */ |
2708 | progress = solver_set_line(sstate, i, val); |
2709 | assert(progress == TRUE); |
2710 | if (val == LINE_YES) { |
2711 | sstate->solver_status = SOLVER_AMBIGUOUS; |
2712 | goto finished_loop_deductionsing; |
2713 | } |
6193da8d |
2714 | } |
6193da8d |
2715 | |
7c95608a |
2716 | finished_loop_deductionsing: |
121aae4b |
2717 | return progress ? DIFF_EASY : DIFF_MAX; |
c0eb17ce |
2718 | } |
6193da8d |
2719 | |
2720 | /* This will return a dynamically allocated solver_state containing the (more) |
2721 | * solved grid */ |
315e47b9 |
2722 | static solver_state *solve_game_rec(const solver_state *sstate_start) |
121aae4b |
2723 | { |
315e47b9 |
2724 | solver_state *sstate; |
6193da8d |
2725 | |
315e47b9 |
2726 | /* Index of the solver we should call next. */ |
2727 | int i = 0; |
2728 | |
2729 | /* As a speed-optimisation, we avoid re-running solvers that we know |
2730 | * won't make any progress. This happens when a high-difficulty |
2731 | * solver makes a deduction that can only help other high-difficulty |
2732 | * solvers. |
2733 | * For example: if a new 'dline' flag is set by dline_deductions, the |
2734 | * trivial_deductions solver cannot do anything with this information. |
2735 | * If we've already run the trivial_deductions solver (because it's |
2736 | * earlier in the list), there's no point running it again. |
2737 | * |
2738 | * Therefore: if a solver is earlier in the list than "threshold_index", |
2739 | * we don't bother running it if it's difficulty level is less than |
2740 | * "threshold_diff". |
2741 | */ |
2742 | int threshold_diff = 0; |
2743 | int threshold_index = 0; |
2744 | |
121aae4b |
2745 | sstate = dup_solver_state(sstate_start); |
7c95608a |
2746 | |
121aae4b |
2747 | check_caches(sstate); |
6193da8d |
2748 | |
315e47b9 |
2749 | while (i < NUM_SOLVERS) { |
121aae4b |
2750 | if (sstate->solver_status == SOLVER_MISTAKE) |
2751 | return sstate; |
7c95608a |
2752 | if (sstate->solver_status == SOLVER_SOLVED || |
121aae4b |
2753 | sstate->solver_status == SOLVER_AMBIGUOUS) { |
315e47b9 |
2754 | /* solver finished */ |
121aae4b |
2755 | break; |
2756 | } |
99dd160e |
2757 | |
315e47b9 |
2758 | if ((solver_diffs[i] >= threshold_diff || i >= threshold_index) |
2759 | && solver_diffs[i] <= sstate->diff) { |
2760 | /* current_solver is eligible, so use it */ |
2761 | int next_diff = solver_fns[i](sstate); |
2762 | if (next_diff != DIFF_MAX) { |
2763 | /* solver made progress, so use new thresholds and |
2764 | * start again at top of list. */ |
2765 | threshold_diff = next_diff; |
2766 | threshold_index = i; |
2767 | i = 0; |
2768 | continue; |
2769 | } |
2770 | } |
2771 | /* current_solver is ineligible, or failed to make progress, so |
2772 | * go to the next solver in the list */ |
2773 | i++; |
2774 | } |
121aae4b |
2775 | |
2776 | if (sstate->solver_status == SOLVER_SOLVED || |
2777 | sstate->solver_status == SOLVER_AMBIGUOUS) { |
2778 | /* s/LINE_UNKNOWN/LINE_NO/g */ |
7c95608a |
2779 | array_setall(sstate->state->lines, LINE_UNKNOWN, LINE_NO, |
2780 | sstate->state->game_grid->num_edges); |
121aae4b |
2781 | return sstate; |
2782 | } |
6193da8d |
2783 | |
121aae4b |
2784 | return sstate; |
6193da8d |
2785 | } |
2786 | |
6193da8d |
2787 | static char *solve_game(game_state *state, game_state *currstate, |
2788 | char *aux, char **error) |
2789 | { |
2790 | char *soln = NULL; |
2791 | solver_state *sstate, *new_sstate; |
2792 | |
121aae4b |
2793 | sstate = new_solver_state(state, DIFF_MAX); |
315e47b9 |
2794 | new_sstate = solve_game_rec(sstate); |
6193da8d |
2795 | |
2796 | if (new_sstate->solver_status == SOLVER_SOLVED) { |
2797 | soln = encode_solve_move(new_sstate->state); |
2798 | } else if (new_sstate->solver_status == SOLVER_AMBIGUOUS) { |
2799 | soln = encode_solve_move(new_sstate->state); |
2800 | /**error = "Solver found ambiguous solutions"; */ |
2801 | } else { |
2802 | soln = encode_solve_move(new_sstate->state); |
2803 | /**error = "Solver failed"; */ |
2804 | } |
2805 | |
2806 | free_solver_state(new_sstate); |
2807 | free_solver_state(sstate); |
2808 | |
2809 | return soln; |
2810 | } |
2811 | |
121aae4b |
2812 | /* ---------------------------------------------------------------------- |
2813 | * Drawing and mouse-handling |
2814 | */ |
6193da8d |
2815 | |
2816 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
2817 | int x, int y, int button) |
2818 | { |
7c95608a |
2819 | grid *g = state->game_grid; |
2820 | grid_edge *e; |
2821 | int i; |
6193da8d |
2822 | char *ret, buf[80]; |
2823 | char button_char = ' '; |
2824 | enum line_state old_state; |
2825 | |
2826 | button &= ~MOD_MASK; |
2827 | |
7c95608a |
2828 | /* Convert mouse-click (x,y) to grid coordinates */ |
2829 | x -= BORDER(ds->tilesize); |
2830 | y -= BORDER(ds->tilesize); |
2831 | x = x * g->tilesize / ds->tilesize; |
2832 | y = y * g->tilesize / ds->tilesize; |
2833 | x += g->lowest_x; |
2834 | y += g->lowest_y; |
6193da8d |
2835 | |
7c95608a |
2836 | e = grid_nearest_edge(g, x, y); |
2837 | if (e == NULL) |
6193da8d |
2838 | return NULL; |
2839 | |
7c95608a |
2840 | i = e - g->edges; |
6193da8d |
2841 | |
2842 | /* I think it's only possible to play this game with mouse clicks, sorry */ |
2843 | /* Maybe will add mouse drag support some time */ |
7c95608a |
2844 | old_state = state->lines[i]; |
6193da8d |
2845 | |
2846 | switch (button) { |
7c95608a |
2847 | case LEFT_BUTTON: |
2848 | switch (old_state) { |
2849 | case LINE_UNKNOWN: |
2850 | button_char = 'y'; |
2851 | break; |
2852 | case LINE_YES: |
80e7e37c |
2853 | #ifdef STYLUS_BASED |
2854 | button_char = 'n'; |
2855 | break; |
2856 | #endif |
7c95608a |
2857 | case LINE_NO: |
2858 | button_char = 'u'; |
2859 | break; |
2860 | } |
2861 | break; |
2862 | case MIDDLE_BUTTON: |
2863 | button_char = 'u'; |
2864 | break; |
2865 | case RIGHT_BUTTON: |
2866 | switch (old_state) { |
2867 | case LINE_UNKNOWN: |
2868 | button_char = 'n'; |
2869 | break; |
2870 | case LINE_NO: |
80e7e37c |
2871 | #ifdef STYLUS_BASED |
2872 | button_char = 'y'; |
2873 | break; |
2874 | #endif |
7c95608a |
2875 | case LINE_YES: |
2876 | button_char = 'u'; |
2877 | break; |
2878 | } |
2879 | break; |
2880 | default: |
2881 | return NULL; |
2882 | } |
2883 | |
2884 | |
2885 | sprintf(buf, "%d%c", i, (int)button_char); |
6193da8d |
2886 | ret = dupstr(buf); |
2887 | |
2888 | return ret; |
2889 | } |
2890 | |
2891 | static game_state *execute_move(game_state *state, char *move) |
2892 | { |
7c95608a |
2893 | int i; |
6193da8d |
2894 | game_state *newstate = dup_game(state); |
2895 | |
2896 | if (move[0] == 'S') { |
2897 | move++; |
2898 | newstate->cheated = TRUE; |
2899 | } |
2900 | |
2901 | while (*move) { |
2902 | i = atoi(move); |
8719c2e7 |
2903 | if (i < 0 || i >= newstate->game_grid->num_edges) |
2904 | goto fail; |
6193da8d |
2905 | move += strspn(move, "1234567890"); |
2906 | switch (*(move++)) { |
7c95608a |
2907 | case 'y': |
2908 | newstate->lines[i] = LINE_YES; |
2909 | break; |
2910 | case 'n': |
2911 | newstate->lines[i] = LINE_NO; |
2912 | break; |
2913 | case 'u': |
2914 | newstate->lines[i] = LINE_UNKNOWN; |
2915 | break; |
2916 | default: |
2917 | goto fail; |
6193da8d |
2918 | } |
2919 | } |
2920 | |
2921 | /* |
2922 | * Check for completion. |
2923 | */ |
b6bf0adc |
2924 | if (check_completion(newstate)) |
121aae4b |
2925 | newstate->solved = TRUE; |
6193da8d |
2926 | |
6193da8d |
2927 | return newstate; |
2928 | |
7c95608a |
2929 | fail: |
6193da8d |
2930 | free_game(newstate); |
2931 | return NULL; |
2932 | } |
2933 | |
2934 | /* ---------------------------------------------------------------------- |
2935 | * Drawing routines. |
2936 | */ |
7c95608a |
2937 | |
2938 | /* Convert from grid coordinates to screen coordinates */ |
2939 | static void grid_to_screen(const game_drawstate *ds, const grid *g, |
2940 | int grid_x, int grid_y, int *x, int *y) |
2941 | { |
2942 | *x = grid_x - g->lowest_x; |
2943 | *y = grid_y - g->lowest_y; |
2944 | *x = *x * ds->tilesize / g->tilesize; |
2945 | *y = *y * ds->tilesize / g->tilesize; |
2946 | *x += BORDER(ds->tilesize); |
2947 | *y += BORDER(ds->tilesize); |
2948 | } |
2949 | |
2950 | /* Returns (into x,y) position of centre of face for rendering the text clue. |
2951 | */ |
2952 | static void face_text_pos(const game_drawstate *ds, const grid *g, |
e64991db |
2953 | grid_face *f, int *xret, int *yret) |
7c95608a |
2954 | { |
e0936bbd |
2955 | int faceindex = f - g->faces; |
7c95608a |
2956 | |
e0936bbd |
2957 | /* |
2958 | * Return the cached position for this face, if we've already |
2959 | * worked it out. |
2960 | */ |
2961 | if (ds->textx[faceindex] >= 0) { |
2962 | *xret = ds->textx[faceindex]; |
2963 | *yret = ds->texty[faceindex]; |
2964 | return; |
2965 | } |
7c95608a |
2966 | |
e0936bbd |
2967 | /* |
e64991db |
2968 | * Otherwise, use the incentre computed by grid.c and convert it |
2969 | * to screen coordinates. |
e0936bbd |
2970 | */ |
e64991db |
2971 | grid_find_incentre(f); |
2972 | grid_to_screen(ds, g, f->ix, f->iy, |
e0936bbd |
2973 | &ds->textx[faceindex], &ds->texty[faceindex]); |
2974 | |
2975 | *xret = ds->textx[faceindex]; |
2976 | *yret = ds->texty[faceindex]; |
7c95608a |
2977 | } |
2978 | |
1463f9f1 |
2979 | static void face_text_bbox(game_drawstate *ds, grid *g, grid_face *f, |
2980 | int *x, int *y, int *w, int *h) |
2981 | { |
2982 | int xx, yy; |
2983 | face_text_pos(ds, g, f, &xx, &yy); |
2984 | |
2985 | /* There seems to be a certain amount of trial-and-error involved |
2986 | * in working out the correct bounding-box for the text. */ |
2987 | |
2988 | *x = xx - ds->tilesize/4 - 1; |
2989 | *y = yy - ds->tilesize/4 - 3; |
2990 | *w = ds->tilesize/2 + 2; |
2991 | *h = ds->tilesize/2 + 5; |
2992 | } |
2993 | |
d68b2c10 |
2994 | static void game_redraw_clue(drawing *dr, game_drawstate *ds, |
2995 | game_state *state, int i) |
2996 | { |
2997 | grid *g = state->game_grid; |
2998 | grid_face *f = g->faces + i; |
2999 | int x, y; |
918a098a |
3000 | char c[3]; |
d68b2c10 |
3001 | |
918a098a |
3002 | if (state->clues[i] < 10) { |
3003 | c[0] = CLUE2CHAR(state->clues[i]); |
3004 | c[1] = '\0'; |
3005 | } else { |
3006 | sprintf(c, "%d", state->clues[i]); |
3007 | } |
d68b2c10 |
3008 | |
3009 | face_text_pos(ds, g, f, &x, &y); |
3010 | draw_text(dr, x, y, |
3011 | FONT_VARIABLE, ds->tilesize/2, |
3012 | ALIGN_VCENTRE | ALIGN_HCENTRE, |
3013 | ds->clue_error[i] ? COL_MISTAKE : |
3014 | ds->clue_satisfied[i] ? COL_SATISFIED : COL_FOREGROUND, c); |
3015 | } |
3016 | |
1463f9f1 |
3017 | static void edge_bbox(game_drawstate *ds, grid *g, grid_edge *e, |
3018 | int *x, int *y, int *w, int *h) |
3019 | { |
3020 | int x1 = e->dot1->x; |
3021 | int y1 = e->dot1->y; |
3022 | int x2 = e->dot2->x; |
3023 | int y2 = e->dot2->y; |
3024 | int xmin, xmax, ymin, ymax; |
3025 | |
3026 | grid_to_screen(ds, g, x1, y1, &x1, &y1); |
3027 | grid_to_screen(ds, g, x2, y2, &x2, &y2); |
3028 | /* Allow extra margin for dots, and thickness of lines */ |
3029 | xmin = min(x1, x2) - 2; |
3030 | xmax = max(x1, x2) + 2; |
3031 | ymin = min(y1, y2) - 2; |
3032 | ymax = max(y1, y2) + 2; |
3033 | |
3034 | *x = xmin; |
3035 | *y = ymin; |
3036 | *w = xmax - xmin + 1; |
3037 | *h = ymax - ymin + 1; |
3038 | } |
3039 | |
3040 | static void dot_bbox(game_drawstate *ds, grid *g, grid_dot *d, |
3041 | int *x, int *y, int *w, int *h) |
3042 | { |
3043 | int x1, y1; |
3044 | |
3045 | grid_to_screen(ds, g, d->x, d->y, &x1, &y1); |
3046 | |
3047 | *x = x1 - 2; |
3048 | *y = y1 - 2; |
3049 | *w = 5; |
3050 | *h = 5; |
3051 | } |
3052 | |
b0a2ee96 |
3053 | static const int loopy_line_redraw_phases[] = { |
3054 | COL_FAINT, COL_LINEUNKNOWN, COL_FOREGROUND, COL_HIGHLIGHT, COL_MISTAKE |
3055 | }; |
3056 | #define NPHASES lenof(loopy_line_redraw_phases) |
3057 | |
d68b2c10 |
3058 | static void game_redraw_line(drawing *dr, game_drawstate *ds, |
b0a2ee96 |
3059 | game_state *state, int i, int phase) |
d68b2c10 |
3060 | { |
3061 | grid *g = state->game_grid; |
3062 | grid_edge *e = g->edges + i; |
3063 | int x1, x2, y1, y2; |
d68b2c10 |
3064 | int line_colour; |
3065 | |
3066 | if (state->line_errors[i]) |
3067 | line_colour = COL_MISTAKE; |
3068 | else if (state->lines[i] == LINE_UNKNOWN) |
3069 | line_colour = COL_LINEUNKNOWN; |
3070 | else if (state->lines[i] == LINE_NO) |
3071 | line_colour = COL_FAINT; |
3072 | else if (ds->flashing) |
3073 | line_colour = COL_HIGHLIGHT; |
3074 | else |
3075 | line_colour = COL_FOREGROUND; |
b0a2ee96 |
3076 | if (line_colour != loopy_line_redraw_phases[phase]) |
3077 | return; |
d68b2c10 |
3078 | |
3079 | /* Convert from grid to screen coordinates */ |
3080 | grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1); |
3081 | grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2); |
3082 | |
d68b2c10 |
3083 | if (line_colour == COL_FAINT) { |
3084 | static int draw_faint_lines = -1; |
3085 | if (draw_faint_lines < 0) { |
3086 | char *env = getenv("LOOPY_FAINT_LINES"); |
3087 | draw_faint_lines = (!env || (env[0] == 'y' || |
3088 | env[0] == 'Y')); |
3089 | } |
3090 | if (draw_faint_lines) |
3091 | draw_line(dr, x1, y1, x2, y2, line_colour); |
3092 | } else { |
3093 | draw_thick_line(dr, 3.0, |
3094 | x1 + 0.5, y1 + 0.5, |
3095 | x2 + 0.5, y2 + 0.5, |
3096 | line_colour); |
3097 | } |
3098 | } |
3099 | |
3100 | static void game_redraw_dot(drawing *dr, game_drawstate *ds, |
3101 | game_state *state, int i) |
3102 | { |
3103 | grid *g = state->game_grid; |
3104 | grid_dot *d = g->dots + i; |
3105 | int x, y; |
3106 | |
3107 | grid_to_screen(ds, g, d->x, d->y, &x, &y); |
3108 | draw_circle(dr, x, y, 2, COL_FOREGROUND, COL_FOREGROUND); |
3109 | } |
3110 | |
1463f9f1 |
3111 | static int boxes_intersect(int x0, int y0, int w0, int h0, |
3112 | int x1, int y1, int w1, int h1) |
3113 | { |
3114 | /* |
3115 | * Two intervals intersect iff neither is wholly on one side of |
3116 | * the other. Two boxes intersect iff their horizontal and |
3117 | * vertical intervals both intersect. |
3118 | */ |
3119 | return (x0 < x1+w1 && x1 < x0+w0 && y0 < y1+h1 && y1 < y0+h0); |
3120 | } |
3121 | |
3122 | static void game_redraw_in_rect(drawing *dr, game_drawstate *ds, |
3123 | game_state *state, int x, int y, int w, int h) |
3124 | { |
3125 | grid *g = state->game_grid; |
3126 | int i, phase; |
3127 | int bx, by, bw, bh; |
3128 | |
3129 | clip(dr, x, y, w, h); |
3130 | draw_rect(dr, x, y, w, h, COL_BACKGROUND); |
3131 | |
3132 | for (i = 0; i < g->num_faces; i++) { |
75a52b16 |
3133 | if (state->clues[i] >= 0) { |
3134 | face_text_bbox(ds, g, &g->faces[i], &bx, &by, &bw, &bh); |
3135 | if (boxes_intersect(x, y, w, h, bx, by, bw, bh)) |
3136 | game_redraw_clue(dr, ds, state, i); |
3137 | } |
1463f9f1 |
3138 | } |
3139 | for (phase = 0; phase < NPHASES; phase++) { |
3140 | for (i = 0; i < g->num_edges; i++) { |
3141 | edge_bbox(ds, g, &g->edges[i], &bx, &by, &bw, &bh); |
3142 | if (boxes_intersect(x, y, w, h, bx, by, bw, bh)) |
3143 | game_redraw_line(dr, ds, state, i, phase); |
3144 | } |
3145 | } |
3146 | for (i = 0; i < g->num_dots; i++) { |
3147 | dot_bbox(ds, g, &g->dots[i], &bx, &by, &bw, &bh); |
3148 | if (boxes_intersect(x, y, w, h, bx, by, bw, bh)) |
3149 | game_redraw_dot(dr, ds, state, i); |
3150 | } |
3151 | |
3152 | unclip(dr); |
3153 | draw_update(dr, x, y, w, h); |
3154 | } |
3155 | |
6193da8d |
3156 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
3157 | game_state *state, int dir, game_ui *ui, |
3158 | float animtime, float flashtime) |
3159 | { |
d68b2c10 |
3160 | #define REDRAW_OBJECTS_LIMIT 16 /* Somewhat arbitrary tradeoff */ |
3161 | |
7c95608a |
3162 | grid *g = state->game_grid; |
3163 | int border = BORDER(ds->tilesize); |
1463f9f1 |
3164 | int i; |
d68b2c10 |
3165 | int flash_changed; |
3166 | int redraw_everything = FALSE; |
3167 | |
3168 | int edges[REDRAW_OBJECTS_LIMIT], nedges = 0; |
3169 | int faces[REDRAW_OBJECTS_LIMIT], nfaces = 0; |
3170 | |
3171 | /* Redrawing is somewhat involved. |
3172 | * |
3173 | * An update can theoretically affect an arbitrary number of edges |
3174 | * (consider, for example, completing or breaking a cycle which doesn't |
3175 | * satisfy all the clues -- we'll switch many edges between error and |
3176 | * normal states). On the other hand, redrawing the whole grid takes a |
3177 | * while, making the game feel sluggish, and many updates are actually |
3178 | * quite well localized. |
3179 | * |
3180 | * This redraw algorithm attempts to cope with both situations gracefully |
3181 | * and correctly. For localized changes, we set a clip rectangle, fill |
3182 | * it with background, and then redraw (a plausible but conservative |
3183 | * guess at) the objects which intersect the rectangle; if several |
3184 | * objects need redrawing, we'll do them individually. However, if lots |
3185 | * of objects are affected, we'll just redraw everything. |
3186 | * |
3187 | * The reason for all of this is that it's just not safe to do the redraw |
3188 | * piecemeal. If you try to draw an antialiased diagonal line over |
3189 | * itself, you get a slightly thicker antialiased diagonal line, which |
3190 | * looks rather ugly after a while. |
3191 | * |
3192 | * So, we take two passes over the grid. The first attempts to work out |
3193 | * what needs doing, and the second actually does it. |
3194 | */ |
3195 | |
3196 | if (!ds->started) |
3197 | redraw_everything = TRUE; |
3198 | else { |
3199 | |
3200 | /* First, trundle through the faces. */ |
3201 | for (i = 0; i < g->num_faces; i++) { |
3202 | grid_face *f = g->faces + i; |
3203 | int sides = f->order; |
3204 | int clue_mistake; |
3205 | int clue_satisfied; |
3206 | int n = state->clues[i]; |
3207 | if (n < 0) |
3208 | continue; |
3209 | |
3210 | clue_mistake = (face_order(state, i, LINE_YES) > n || |
3211 | face_order(state, i, LINE_NO ) > (sides-n)); |
3212 | clue_satisfied = (face_order(state, i, LINE_YES) == n && |
3213 | face_order(state, i, LINE_NO ) == (sides-n)); |
3214 | |
3215 | if (clue_mistake != ds->clue_error[i] || |
3216 | clue_satisfied != ds->clue_satisfied[i]) { |
3217 | ds->clue_error[i] = clue_mistake; |
3218 | ds->clue_satisfied[i] = clue_satisfied; |
3219 | if (nfaces == REDRAW_OBJECTS_LIMIT) |
3220 | redraw_everything = TRUE; |
3221 | else |
3222 | faces[nfaces++] = i; |
3223 | } |
3224 | } |
3225 | |
3226 | /* Work out what the flash state needs to be. */ |
3227 | if (flashtime > 0 && |
3228 | (flashtime <= FLASH_TIME/3 || |
3229 | flashtime >= FLASH_TIME*2/3)) { |
3230 | flash_changed = !ds->flashing; |
3231 | ds->flashing = TRUE; |
3232 | } else { |
3233 | flash_changed = ds->flashing; |
3234 | ds->flashing = FALSE; |
3235 | } |
3236 | |
3237 | /* Now, trundle through the edges. */ |
3238 | for (i = 0; i < g->num_edges; i++) { |
3239 | char new_ds = |
3240 | state->line_errors[i] ? DS_LINE_ERROR : state->lines[i]; |
3241 | if (new_ds != ds->lines[i] || |
3242 | (flash_changed && state->lines[i] == LINE_YES)) { |
3243 | ds->lines[i] = new_ds; |
3244 | if (nedges == REDRAW_OBJECTS_LIMIT) |
3245 | redraw_everything = TRUE; |
3246 | else |
3247 | edges[nedges++] = i; |
3248 | } |
3249 | } |
3250 | } |
3251 | |
3252 | /* Pass one is now done. Now we do the actual drawing. */ |
3253 | if (redraw_everything) { |
7c95608a |
3254 | int grid_width = g->highest_x - g->lowest_x; |
3255 | int grid_height = g->highest_y - g->lowest_y; |
3256 | int w = grid_width * ds->tilesize / g->tilesize; |
3257 | int h = grid_height * ds->tilesize / g->tilesize; |
6193da8d |
3258 | |
1463f9f1 |
3259 | game_redraw_in_rect(dr, ds, state, |
3260 | 0, 0, w + 2*border + 1, h + 2*border + 1); |
d68b2c10 |
3261 | } else { |
c0eb17ce |
3262 | |
d68b2c10 |
3263 | /* Right. Now we roll up our sleeves. */ |
3264 | |
3265 | for (i = 0; i < nfaces; i++) { |
3266 | grid_face *f = g->faces + faces[i]; |
d68b2c10 |
3267 | int x, y, w, h; |
1463f9f1 |
3268 | |
3269 | face_text_bbox(ds, g, f, &x, &y, &w, &h); |
3270 | game_redraw_in_rect(dr, ds, state, x, y, w, h); |
d68b2c10 |
3271 | } |
c0eb17ce |
3272 | |
d68b2c10 |
3273 | for (i = 0; i < nedges; i++) { |
1463f9f1 |
3274 | grid_edge *e = g->edges + edges[i]; |
3275 | int x, y, w, h; |
6193da8d |
3276 | |
1463f9f1 |
3277 | edge_bbox(ds, g, e, &x, &y, &w, &h); |
3278 | game_redraw_in_rect(dr, ds, state, x, y, w, h); |
d68b2c10 |
3279 | } |
6193da8d |
3280 | } |
d68b2c10 |
3281 | |
7c95608a |
3282 | ds->started = TRUE; |
6193da8d |
3283 | } |
3284 | |
6193da8d |
3285 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
3286 | int dir, game_ui *ui) |
3287 | { |
3288 | if (!oldstate->solved && newstate->solved && |
3289 | !oldstate->cheated && !newstate->cheated) { |
3290 | return FLASH_TIME; |
3291 | } |
3292 | |
3293 | return 0.0F; |
3294 | } |
3295 | |
1cea529f |
3296 | static int game_status(game_state *state) |
4496362f |
3297 | { |
1cea529f |
3298 | return state->solved ? +1 : 0; |
4496362f |
3299 | } |
3300 | |
6193da8d |
3301 | static void game_print_size(game_params *params, float *x, float *y) |
3302 | { |
3303 | int pw, ph; |
3304 | |
3305 | /* |
7c95608a |
3306 | * I'll use 7mm "squares" by default. |
6193da8d |
3307 | */ |
3308 | game_compute_size(params, 700, &pw, &ph); |
3309 | *x = pw / 100.0F; |
3310 | *y = ph / 100.0F; |
3311 | } |
3312 | |
3313 | static void game_print(drawing *dr, game_state *state, int tilesize) |
3314 | { |
6193da8d |
3315 | int ink = print_mono_colour(dr, 0); |
7c95608a |
3316 | int i; |
6193da8d |
3317 | game_drawstate ads, *ds = &ads; |
7c95608a |
3318 | grid *g = state->game_grid; |
4413ef0f |
3319 | |
092e9395 |
3320 | ds->tilesize = tilesize; |
8acc8766 |
3321 | ds->textx = snewn(g->num_faces, int); |
3322 | ds->texty = snewn(g->num_faces, int); |
3323 | for (i = 0; i < g->num_faces; i++) |
3324 | ds->textx[i] = ds->texty[i] = -1; |
6193da8d |
3325 | |
7c95608a |
3326 | for (i = 0; i < g->num_dots; i++) { |
3327 | int x, y; |
3328 | grid_to_screen(ds, g, g->dots[i].x, g->dots[i].y, &x, &y); |
3329 | draw_circle(dr, x, y, ds->tilesize / 15, ink, ink); |
121aae4b |
3330 | } |
6193da8d |
3331 | |
3332 | /* |
3333 | * Clues. |
3334 | */ |
7c95608a |
3335 | for (i = 0; i < g->num_faces; i++) { |
3336 | grid_face *f = g->faces + i; |
3337 | int clue = state->clues[i]; |
3338 | if (clue >= 0) { |
121aae4b |
3339 | char c[2]; |
7c95608a |
3340 | int x, y; |
3341 | c[0] = CLUE2CHAR(clue); |
121aae4b |
3342 | c[1] = '\0'; |
7c95608a |
3343 | face_text_pos(ds, g, f, &x, &y); |
3344 | draw_text(dr, x, y, |
3345 | FONT_VARIABLE, ds->tilesize / 2, |
121aae4b |
3346 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, c); |
3347 | } |
3348 | } |
6193da8d |
3349 | |
3350 | /* |
7c95608a |
3351 | * Lines. |
6193da8d |
3352 | */ |
7c95608a |
3353 | for (i = 0; i < g->num_edges; i++) { |
3354 | int thickness = (state->lines[i] == LINE_YES) ? 30 : 150; |
3355 | grid_edge *e = g->edges + i; |
3356 | int x1, y1, x2, y2; |
3357 | grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1); |
3358 | grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2); |
3359 | if (state->lines[i] == LINE_YES) |
3360 | { |
3361 | /* (dx, dy) points from (x1, y1) to (x2, y2). |
3362 | * The line is then "fattened" in a perpendicular |
3363 | * direction to create a thin rectangle. */ |
3364 | double d = sqrt(SQ((double)x1 - x2) + SQ((double)y1 - y2)); |
3365 | double dx = (x2 - x1) / d; |
3366 | double dy = (y2 - y1) / d; |
1515b973 |
3367 | int points[8]; |
3368 | |
7c95608a |
3369 | dx = (dx * ds->tilesize) / thickness; |
3370 | dy = (dy * ds->tilesize) / thickness; |
b1535c90 |
3371 | points[0] = x1 + (int)dy; |
3372 | points[1] = y1 - (int)dx; |
3373 | points[2] = x1 - (int)dy; |
3374 | points[3] = y1 + (int)dx; |
3375 | points[4] = x2 - (int)dy; |
3376 | points[5] = y2 + (int)dx; |
3377 | points[6] = x2 + (int)dy; |
3378 | points[7] = y2 - (int)dx; |
7c95608a |
3379 | draw_polygon(dr, points, 4, ink, ink); |
3380 | } |
3381 | else |
3382 | { |
3383 | /* Draw a dotted line */ |
3384 | int divisions = 6; |
3385 | int j; |
3386 | for (j = 1; j < divisions; j++) { |
3387 | /* Weighted average */ |
3388 | int x = (x1 * (divisions -j) + x2 * j) / divisions; |
3389 | int y = (y1 * (divisions -j) + y2 * j) / divisions; |
3390 | draw_circle(dr, x, y, ds->tilesize / thickness, ink, ink); |
3391 | } |
3392 | } |
121aae4b |
3393 | } |
8acc8766 |
3394 | |
3395 | sfree(ds->textx); |
3396 | sfree(ds->texty); |
6193da8d |
3397 | } |
3398 | |
3399 | #ifdef COMBINED |
3400 | #define thegame loopy |
3401 | #endif |
3402 | |
3403 | const struct game thegame = { |
750037d7 |
3404 | "Loopy", "games.loopy", "loopy", |
6193da8d |
3405 | default_params, |
3406 | game_fetch_preset, |
3407 | decode_params, |
3408 | encode_params, |
3409 | free_params, |
3410 | dup_params, |
3411 | TRUE, game_configure, custom_params, |
3412 | validate_params, |
3413 | new_game_desc, |
3414 | validate_desc, |
3415 | new_game, |
3416 | dup_game, |
3417 | free_game, |
3418 | 1, solve_game, |
fa3abef5 |
3419 | TRUE, game_can_format_as_text_now, game_text_format, |
6193da8d |
3420 | new_ui, |
3421 | free_ui, |
3422 | encode_ui, |
3423 | decode_ui, |
3424 | game_changed_state, |
3425 | interpret_move, |
3426 | execute_move, |
3427 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
3428 | game_colours, |
3429 | game_new_drawstate, |
3430 | game_free_drawstate, |
3431 | game_redraw, |
3432 | game_anim_length, |
3433 | game_flash_length, |
1cea529f |
3434 | game_status, |
6193da8d |
3435 | TRUE, FALSE, game_print_size, game_print, |
121aae4b |
3436 | FALSE /* wants_statusbar */, |
6193da8d |
3437 | FALSE, game_timing_state, |
121aae4b |
3438 | 0, /* mouse_priorities */ |
6193da8d |
3439 | }; |
5ca89681 |
3440 | |
3441 | #ifdef STANDALONE_SOLVER |
3442 | |
3443 | /* |
3444 | * Half-hearted standalone solver. It can't output the solution to |
3445 | * anything but a square puzzle, and it can't log the deductions |
3446 | * it makes either. But it can solve square puzzles, and more |
3447 | * importantly it can use its solver to grade the difficulty of |
3448 | * any puzzle you give it. |
3449 | */ |
3450 | |
3451 | #include <stdarg.h> |
3452 | |
3453 | int main(int argc, char **argv) |
3454 | { |
3455 | game_params *p; |
3456 | game_state *s; |
3457 | char *id = NULL, *desc, *err; |
3458 | int grade = FALSE; |
3459 | int ret, diff; |
3460 | #if 0 /* verbose solver not supported here (yet) */ |
3461 | int really_verbose = FALSE; |
3462 | #endif |
3463 | |
3464 | while (--argc > 0) { |
3465 | char *p = *++argv; |
3466 | #if 0 /* verbose solver not supported here (yet) */ |
3467 | if (!strcmp(p, "-v")) { |
3468 | really_verbose = TRUE; |
3469 | } else |
3470 | #endif |
3471 | if (!strcmp(p, "-g")) { |
3472 | grade = TRUE; |
3473 | } else if (*p == '-') { |
3474 | fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); |
3475 | return 1; |
3476 | } else { |
3477 | id = p; |
3478 | } |
3479 | } |
3480 | |
3481 | if (!id) { |
3482 | fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]); |
3483 | return 1; |
3484 | } |
3485 | |
3486 | desc = strchr(id, ':'); |
3487 | if (!desc) { |
3488 | fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); |
3489 | return 1; |
3490 | } |
3491 | *desc++ = '\0'; |
3492 | |
3493 | p = default_params(); |
3494 | decode_params(p, id); |
3495 | err = validate_desc(p, desc); |
3496 | if (err) { |
3497 | fprintf(stderr, "%s: %s\n", argv[0], err); |
3498 | return 1; |
3499 | } |
3500 | s = new_game(NULL, p, desc); |
3501 | |
3502 | /* |
3503 | * When solving an Easy puzzle, we don't want to bother the |
3504 | * user with Hard-level deductions. For this reason, we grade |
3505 | * the puzzle internally before doing anything else. |
3506 | */ |
3507 | ret = -1; /* placate optimiser */ |
3508 | for (diff = 0; diff < DIFF_MAX; diff++) { |
3509 | solver_state *sstate_new; |
3510 | solver_state *sstate = new_solver_state((game_state *)s, diff); |
3511 | |
315e47b9 |
3512 | sstate_new = solve_game_rec(sstate); |
5ca89681 |
3513 | |
3514 | if (sstate_new->solver_status == SOLVER_MISTAKE) |
3515 | ret = 0; |
3516 | else if (sstate_new->solver_status == SOLVER_SOLVED) |
3517 | ret = 1; |
3518 | else |
3519 | ret = 2; |
3520 | |
3521 | free_solver_state(sstate_new); |
3522 | free_solver_state(sstate); |
3523 | |
3524 | if (ret < 2) |
3525 | break; |
3526 | } |
3527 | |
3528 | if (diff == DIFF_MAX) { |
3529 | if (grade) |
3530 | printf("Difficulty rating: harder than Hard, or ambiguous\n"); |
3531 | else |
3532 | printf("Unable to find a unique solution\n"); |
3533 | } else { |
3534 | if (grade) { |
3535 | if (ret == 0) |
3536 | printf("Difficulty rating: impossible (no solution exists)\n"); |
3537 | else if (ret == 1) |
3538 | printf("Difficulty rating: %s\n", diffnames[diff]); |
3539 | } else { |
3540 | solver_state *sstate_new; |
3541 | solver_state *sstate = new_solver_state((game_state *)s, diff); |
3542 | |
3543 | /* If we supported a verbose solver, we'd set verbosity here */ |
3544 | |
315e47b9 |
3545 | sstate_new = solve_game_rec(sstate); |
5ca89681 |
3546 | |
3547 | if (sstate_new->solver_status == SOLVER_MISTAKE) |
3548 | printf("Puzzle is inconsistent\n"); |
3549 | else { |
3550 | assert(sstate_new->solver_status == SOLVER_SOLVED); |
3551 | if (s->grid_type == 0) { |
3552 | fputs(game_text_format(sstate_new->state), stdout); |
3553 | } else { |
3554 | printf("Unable to output non-square grids\n"); |
3555 | } |
3556 | } |
3557 | |
3558 | free_solver_state(sstate_new); |
3559 | free_solver_state(sstate); |
3560 | } |
3561 | } |
3562 | |
3563 | return 0; |
3564 | } |
3565 | |
3566 | #endif |
cebf0b0d |
3567 | |
3568 | /* vim: set shiftwidth=4 tabstop=8: */ |