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