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