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