4 * An implementation of the Nikoli game 'Loop the loop'.
5 * (c) Mike Pinna, 2005, 2006
6 * Substantially rewritten to allowing for more general types of grid.
7 * (c) Lambros Lambrou 2008
9 * vim: set shiftwidth=4 :set textwidth=80:
13 * Possible future solver enhancements:
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
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
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
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
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.
87 /* Debugging options */
95 /* ----------------------------------------------------------------------
96 * Struct, enum and function declarations
111 grid *game_grid; /* ref-counted (internally) */
113 /* Put -1 in a face that doesn't get a clue */
116 /* Array of line states, to store whether each line is
117 * YES, NO or UNKNOWN */
120 unsigned char *line_errors;
121 int exactly_one_loop;
126 /* Used in game_text_format(), so that it knows what type of
127 * grid it's trying to render as ASCII text. */
132 SOLVER_SOLVED, /* This is the only solution the solver could find */
133 SOLVER_MISTAKE, /* This is definitely not a solution */
134 SOLVER_AMBIGUOUS, /* This _might_ be an ambiguous solution */
135 SOLVER_INCOMPLETE /* This may be a partial solution */
138 /* ------ Solver state ------ */
139 typedef struct solver_state {
141 enum solver_status solver_status;
142 /* NB looplen is the number of dots that are joined together at a point, ie a
143 * looplen of 1 means there are no lines to a particular dot */
146 /* Difficulty level of solver. Used by solver functions that want to
147 * vary their behaviour depending on the requested difficulty level. */
153 char *face_yes_count;
155 char *dot_solved, *face_solved;
158 /* Information for Normal level deductions:
159 * For each dline, store a bitmask for whether we know:
160 * (bit 0) at least one is YES
161 * (bit 1) at most one is YES */
164 /* Hard level information */
169 * Difficulty levels. I do some macro ickery here to ensure that my
170 * enum and the various forms of my name list always match up.
173 #define DIFFLIST(A) \
178 #define ENUM(upper,title,lower) DIFF_ ## upper,
179 #define TITLE(upper,title,lower) #title,
180 #define ENCODE(upper,title,lower) #lower
181 #define CONFIG(upper,title,lower) ":" #title
182 enum { DIFFLIST(ENUM) DIFF_MAX };
183 static char const *const diffnames[] = { DIFFLIST(TITLE) };
184 static char const diffchars[] = DIFFLIST(ENCODE);
185 #define DIFFCONFIG DIFFLIST(CONFIG)
188 * Solver routines, sorted roughly in order of computational cost.
189 * The solver will run the faster deductions first, and slower deductions are
190 * only invoked when the faster deductions are unable to make progress.
191 * Each function is associated with a difficulty level, so that the generated
192 * puzzles are solvable by applying only the functions with the chosen
193 * difficulty level or lower.
195 #define SOLVERLIST(A) \
196 A(trivial_deductions, DIFF_EASY) \
197 A(dline_deductions, DIFF_NORMAL) \
198 A(linedsf_deductions, DIFF_HARD) \
199 A(loop_deductions, DIFF_EASY)
200 #define SOLVER_FN_DECL(fn,diff) static int fn(solver_state *);
201 #define SOLVER_FN(fn,diff) &fn,
202 #define SOLVER_DIFF(fn,diff) diff,
203 SOLVERLIST(SOLVER_FN_DECL)
204 static int (*(solver_fns[]))(solver_state *) = { SOLVERLIST(SOLVER_FN) };
205 static int const solver_diffs[] = { SOLVERLIST(SOLVER_DIFF) };
206 static const int NUM_SOLVERS = sizeof(solver_diffs)/sizeof(*solver_diffs);
214 /* line_drawstate is the same as line_state, but with the extra ERROR
215 * possibility. The drawing code copies line_state to line_drawstate,
216 * except in the case that the line is an error. */
217 enum line_state { LINE_YES, LINE_UNKNOWN, LINE_NO };
218 enum line_drawstate { DS_LINE_YES, DS_LINE_UNKNOWN,
219 DS_LINE_NO, DS_LINE_ERROR };
221 #define OPP(line_state) \
225 struct game_drawstate {
232 char *clue_satisfied;
235 static char *validate_desc(const game_params *params, const char *desc);
236 static int dot_order(const game_state* state, int i, char line_type);
237 static int face_order(const game_state* state, int i, char line_type);
238 static solver_state *solve_game_rec(const solver_state *sstate);
241 static void check_caches(const solver_state* sstate);
243 #define check_caches(s)
246 /* ------- List of grid generators ------- */
247 #define GRIDLIST(A) \
248 A(Squares,GRID_SQUARE,3,3) \
249 A(Triangular,GRID_TRIANGULAR,3,3) \
250 A(Honeycomb,GRID_HONEYCOMB,3,3) \
251 A(Snub-Square,GRID_SNUBSQUARE,3,3) \
252 A(Cairo,GRID_CAIRO,3,4) \
253 A(Great-Hexagonal,GRID_GREATHEXAGONAL,3,3) \
254 A(Octagonal,GRID_OCTAGONAL,3,3) \
255 A(Kites,GRID_KITE,3,3) \
256 A(Floret,GRID_FLORET,1,2) \
257 A(Dodecagonal,GRID_DODECAGONAL,2,2) \
258 A(Great-Dodecagonal,GRID_GREATDODECAGONAL,2,2) \
259 A(Penrose (kite/dart),GRID_PENROSE_P2,3,3) \
260 A(Penrose (rhombs),GRID_PENROSE_P3,3,3)
262 #define GRID_NAME(title,type,amin,omin) #title,
263 #define GRID_CONFIG(title,type,amin,omin) ":" #title
264 #define GRID_TYPE(title,type,amin,omin) type,
265 #define GRID_SIZES(title,type,amin,omin) \
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,},
269 static char const *const gridnames[] = { GRIDLIST(GRID_NAME) };
270 #define GRID_CONFIGS GRIDLIST(GRID_CONFIG)
271 static grid_type grid_types[] = { GRIDLIST(GRID_TYPE) };
272 #define NUM_GRID_TYPES (sizeof(grid_types) / sizeof(grid_types[0]))
273 static const struct {
276 } grid_size_limits[] = { GRIDLIST(GRID_SIZES) };
278 /* Generates a (dynamically allocated) new grid, according to the
279 * type and size requested in params. Does nothing if the grid is already
281 static grid *loopy_generate_grid(const game_params *params,
282 const char *grid_desc)
284 return grid_new(grid_types[params->type], params->w, params->h, grid_desc);
287 /* ----------------------------------------------------------------------
291 /* General constants */
292 #define PREFERRED_TILE_SIZE 32
293 #define BORDER(tilesize) ((tilesize) / 2)
294 #define FLASH_TIME 0.5F
296 #define BIT_SET(field, bit) ((field) & (1<<(bit)))
298 #define SET_BIT(field, bit) (BIT_SET(field, bit) ? FALSE : \
299 ((field) |= (1<<(bit)), TRUE))
301 #define CLEAR_BIT(field, bit) (BIT_SET(field, bit) ? \
302 ((field) &= ~(1<<(bit)), TRUE) : FALSE)
304 #define CLUE2CHAR(c) \
305 ((c < 0) ? ' ' : c < 10 ? c + '0' : c - 10 + 'A')
307 /* ----------------------------------------------------------------------
308 * General struct manipulation and other straightforward code
311 static game_state *dup_game(const game_state *state)
313 game_state *ret = snew(game_state);
315 ret->game_grid = state->game_grid;
316 ret->game_grid->refcount++;
318 ret->solved = state->solved;
319 ret->cheated = state->cheated;
321 ret->clues = snewn(state->game_grid->num_faces, signed char);
322 memcpy(ret->clues, state->clues, state->game_grid->num_faces);
324 ret->lines = snewn(state->game_grid->num_edges, char);
325 memcpy(ret->lines, state->lines, state->game_grid->num_edges);
327 ret->line_errors = snewn(state->game_grid->num_edges, unsigned char);
328 memcpy(ret->line_errors, state->line_errors, state->game_grid->num_edges);
329 ret->exactly_one_loop = state->exactly_one_loop;
331 ret->grid_type = state->grid_type;
335 static void free_game(game_state *state)
338 grid_free(state->game_grid);
341 sfree(state->line_errors);
346 static solver_state *new_solver_state(const game_state *state, int diff) {
348 int num_dots = state->game_grid->num_dots;
349 int num_faces = state->game_grid->num_faces;
350 int num_edges = state->game_grid->num_edges;
351 solver_state *ret = snew(solver_state);
353 ret->state = dup_game(state);
355 ret->solver_status = SOLVER_INCOMPLETE;
358 ret->dotdsf = snew_dsf(num_dots);
359 ret->looplen = snewn(num_dots, int);
361 for (i = 0; i < num_dots; i++) {
365 ret->dot_solved = snewn(num_dots, char);
366 ret->face_solved = snewn(num_faces, char);
367 memset(ret->dot_solved, FALSE, num_dots);
368 memset(ret->face_solved, FALSE, num_faces);
370 ret->dot_yes_count = snewn(num_dots, char);
371 memset(ret->dot_yes_count, 0, num_dots);
372 ret->dot_no_count = snewn(num_dots, char);
373 memset(ret->dot_no_count, 0, num_dots);
374 ret->face_yes_count = snewn(num_faces, char);
375 memset(ret->face_yes_count, 0, num_faces);
376 ret->face_no_count = snewn(num_faces, char);
377 memset(ret->face_no_count, 0, num_faces);
379 if (diff < DIFF_NORMAL) {
382 ret->dlines = snewn(2*num_edges, char);
383 memset(ret->dlines, 0, 2*num_edges);
386 if (diff < DIFF_HARD) {
389 ret->linedsf = snew_dsf(state->game_grid->num_edges);
395 static void free_solver_state(solver_state *sstate) {
397 free_game(sstate->state);
398 sfree(sstate->dotdsf);
399 sfree(sstate->looplen);
400 sfree(sstate->dot_solved);
401 sfree(sstate->face_solved);
402 sfree(sstate->dot_yes_count);
403 sfree(sstate->dot_no_count);
404 sfree(sstate->face_yes_count);
405 sfree(sstate->face_no_count);
407 /* OK, because sfree(NULL) is a no-op */
408 sfree(sstate->dlines);
409 sfree(sstate->linedsf);
415 static solver_state *dup_solver_state(const solver_state *sstate) {
416 game_state *state = sstate->state;
417 int num_dots = state->game_grid->num_dots;
418 int num_faces = state->game_grid->num_faces;
419 int num_edges = state->game_grid->num_edges;
420 solver_state *ret = snew(solver_state);
422 ret->state = state = dup_game(sstate->state);
424 ret->solver_status = sstate->solver_status;
425 ret->diff = sstate->diff;
427 ret->dotdsf = snewn(num_dots, int);
428 ret->looplen = snewn(num_dots, int);
429 memcpy(ret->dotdsf, sstate->dotdsf,
430 num_dots * sizeof(int));
431 memcpy(ret->looplen, sstate->looplen,
432 num_dots * sizeof(int));
434 ret->dot_solved = snewn(num_dots, char);
435 ret->face_solved = snewn(num_faces, char);
436 memcpy(ret->dot_solved, sstate->dot_solved, num_dots);
437 memcpy(ret->face_solved, sstate->face_solved, num_faces);
439 ret->dot_yes_count = snewn(num_dots, char);
440 memcpy(ret->dot_yes_count, sstate->dot_yes_count, num_dots);
441 ret->dot_no_count = snewn(num_dots, char);
442 memcpy(ret->dot_no_count, sstate->dot_no_count, num_dots);
444 ret->face_yes_count = snewn(num_faces, char);
445 memcpy(ret->face_yes_count, sstate->face_yes_count, num_faces);
446 ret->face_no_count = snewn(num_faces, char);
447 memcpy(ret->face_no_count, sstate->face_no_count, num_faces);
449 if (sstate->dlines) {
450 ret->dlines = snewn(2*num_edges, char);
451 memcpy(ret->dlines, sstate->dlines,
457 if (sstate->linedsf) {
458 ret->linedsf = snewn(num_edges, int);
459 memcpy(ret->linedsf, sstate->linedsf,
460 num_edges * sizeof(int));
468 static game_params *default_params(void)
470 game_params *ret = snew(game_params);
479 ret->diff = DIFF_EASY;
485 static game_params *dup_params(const game_params *params)
487 game_params *ret = snew(game_params);
489 *ret = *params; /* structure copy */
493 static const game_params presets[] = {
495 { 7, 7, DIFF_EASY, 0 },
496 { 7, 7, DIFF_NORMAL, 0 },
497 { 7, 7, DIFF_HARD, 0 },
498 { 7, 7, DIFF_HARD, 1 },
499 { 7, 7, DIFF_HARD, 2 },
500 { 5, 5, DIFF_HARD, 3 },
501 { 7, 7, DIFF_HARD, 4 },
502 { 5, 4, DIFF_HARD, 5 },
503 { 5, 5, DIFF_HARD, 6 },
504 { 5, 5, DIFF_HARD, 7 },
505 { 3, 3, DIFF_HARD, 8 },
506 { 3, 3, DIFF_HARD, 9 },
507 { 3, 3, DIFF_HARD, 10 },
508 { 6, 6, DIFF_HARD, 11 },
509 { 6, 6, DIFF_HARD, 12 },
511 { 7, 7, DIFF_EASY, 0 },
512 { 10, 10, DIFF_EASY, 0 },
513 { 7, 7, DIFF_NORMAL, 0 },
514 { 10, 10, DIFF_NORMAL, 0 },
515 { 7, 7, DIFF_HARD, 0 },
516 { 10, 10, DIFF_HARD, 0 },
517 { 10, 10, DIFF_HARD, 1 },
518 { 12, 10, DIFF_HARD, 2 },
519 { 7, 7, DIFF_HARD, 3 },
520 { 9, 9, DIFF_HARD, 4 },
521 { 5, 4, DIFF_HARD, 5 },
522 { 7, 7, DIFF_HARD, 6 },
523 { 5, 5, DIFF_HARD, 7 },
524 { 5, 5, DIFF_HARD, 8 },
525 { 5, 4, DIFF_HARD, 9 },
526 { 5, 4, DIFF_HARD, 10 },
527 { 10, 10, DIFF_HARD, 11 },
528 { 10, 10, DIFF_HARD, 12 }
532 static int game_fetch_preset(int i, char **name, game_params **params)
537 if (i < 0 || i >= lenof(presets))
540 tmppar = snew(game_params);
541 *tmppar = presets[i];
543 sprintf(buf, "%dx%d %s - %s", tmppar->h, tmppar->w,
544 gridnames[tmppar->type], diffnames[tmppar->diff]);
550 static void free_params(game_params *params)
555 static void decode_params(game_params *params, char const *string)
557 params->h = params->w = atoi(string);
558 params->diff = DIFF_EASY;
559 while (*string && isdigit((unsigned char)*string)) string++;
560 if (*string == 'x') {
562 params->h = atoi(string);
563 while (*string && isdigit((unsigned char)*string)) string++;
565 if (*string == 't') {
567 params->type = atoi(string);
568 while (*string && isdigit((unsigned char)*string)) string++;
570 if (*string == 'd') {
573 for (i = 0; i < DIFF_MAX; i++)
574 if (*string == diffchars[i])
576 if (*string) string++;
580 static char *encode_params(const game_params *params, int full)
583 sprintf(str, "%dx%dt%d", params->w, params->h, params->type);
585 sprintf(str + strlen(str), "d%c", diffchars[params->diff]);
589 static config_item *game_configure(const game_params *params)
594 ret = snewn(5, config_item);
596 ret[0].name = "Width";
597 ret[0].type = C_STRING;
598 sprintf(buf, "%d", params->w);
599 ret[0].sval = dupstr(buf);
602 ret[1].name = "Height";
603 ret[1].type = C_STRING;
604 sprintf(buf, "%d", params->h);
605 ret[1].sval = dupstr(buf);
608 ret[2].name = "Grid type";
609 ret[2].type = C_CHOICES;
610 ret[2].sval = GRID_CONFIGS;
611 ret[2].ival = params->type;
613 ret[3].name = "Difficulty";
614 ret[3].type = C_CHOICES;
615 ret[3].sval = DIFFCONFIG;
616 ret[3].ival = params->diff;
626 static game_params *custom_params(const config_item *cfg)
628 game_params *ret = snew(game_params);
630 ret->w = atoi(cfg[0].sval);
631 ret->h = atoi(cfg[1].sval);
632 ret->type = cfg[2].ival;
633 ret->diff = cfg[3].ival;
638 static char *validate_params(const game_params *params, int full)
640 if (params->type < 0 || params->type >= NUM_GRID_TYPES)
641 return "Illegal grid type";
642 if (params->w < grid_size_limits[params->type].amin ||
643 params->h < grid_size_limits[params->type].amin)
644 return grid_size_limits[params->type].aerr;
645 if (params->w < grid_size_limits[params->type].omin &&
646 params->h < grid_size_limits[params->type].omin)
647 return grid_size_limits[params->type].oerr;
650 * This shouldn't be able to happen at all, since decode_params
651 * and custom_params will never generate anything that isn't
654 assert(params->diff < DIFF_MAX);
659 /* Returns a newly allocated string describing the current puzzle */
660 static char *state_to_text(const game_state *state)
662 grid *g = state->game_grid;
664 int num_faces = g->num_faces;
665 char *description = snewn(num_faces + 1, char);
666 char *dp = description;
670 for (i = 0; i < num_faces; i++) {
671 if (state->clues[i] < 0) {
672 if (empty_count > 25) {
673 dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1));
679 dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1));
682 dp += sprintf(dp, "%c", (int)CLUE2CHAR(state->clues[i]));
687 dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1));
689 retval = dupstr(description);
695 #define GRID_DESC_SEP '_'
697 /* Splits up a (optional) grid_desc from the game desc. Returns the
698 * grid_desc (which needs freeing) and updates the desc pointer to
699 * start of real desc, or returns NULL if no desc. */
700 static char *extract_grid_desc(const char **desc)
702 char *sep = strchr(*desc, GRID_DESC_SEP), *gd;
705 if (!sep) return NULL;
707 gd_len = sep - (*desc);
708 gd = snewn(gd_len+1, char);
709 memcpy(gd, *desc, gd_len);
717 /* We require that the params pass the test in validate_params and that the
718 * description fills the entire game area */
719 static char *validate_desc(const game_params *params, const char *desc)
723 char *grid_desc, *ret;
725 /* It's pretty inefficient to do this just for validation. All we need to
726 * know is the precise number of faces. */
727 grid_desc = extract_grid_desc(&desc);
728 ret = grid_validate_desc(grid_types[params->type], params->w, params->h, grid_desc);
731 g = loopy_generate_grid(params, grid_desc);
732 if (grid_desc) sfree(grid_desc);
734 for (; *desc; ++desc) {
735 if ((*desc >= '0' && *desc <= '9') || (*desc >= 'A' && *desc <= 'Z')) {
740 count += *desc - 'a' + 1;
743 return "Unknown character in description";
746 if (count < g->num_faces)
747 return "Description too short for board size";
748 if (count > g->num_faces)
749 return "Description too long for board size";
756 /* Sums the lengths of the numbers in range [0,n) */
757 /* See equivalent function in solo.c for justification of this. */
758 static int len_0_to_n(int n)
760 int len = 1; /* Counting 0 as a bit of a special case */
763 for (i = 1; i < n; i *= 10) {
764 len += max(n - i, 0);
770 static char *encode_solve_move(const game_state *state)
775 int num_edges = state->game_grid->num_edges;
777 /* This is going to return a string representing the moves needed to set
778 * every line in a grid to be the same as the ones in 'state'. The exact
779 * length of this string is predictable. */
781 len = 1; /* Count the 'S' prefix */
782 /* Numbers in all lines */
783 len += len_0_to_n(num_edges);
784 /* For each line we also have a letter */
787 ret = snewn(len + 1, char);
790 p += sprintf(p, "S");
792 for (i = 0; i < num_edges; i++) {
793 switch (state->lines[i]) {
795 p += sprintf(p, "%dy", i);
798 p += sprintf(p, "%dn", i);
803 /* No point in doing sums like that if they're going to be wrong */
804 assert(strlen(ret) <= (size_t)len);
808 static game_ui *new_ui(const game_state *state)
813 static void free_ui(game_ui *ui)
817 static char *encode_ui(const game_ui *ui)
822 static void decode_ui(game_ui *ui, const char *encoding)
826 static void game_changed_state(game_ui *ui, const game_state *oldstate,
827 const game_state *newstate)
831 static void game_compute_size(const game_params *params, int tilesize,
834 int grid_width, grid_height, rendered_width, rendered_height;
837 grid_compute_size(grid_types[params->type], params->w, params->h,
838 &g_tilesize, &grid_width, &grid_height);
840 /* multiply first to minimise rounding error on integer division */
841 rendered_width = grid_width * tilesize / g_tilesize;
842 rendered_height = grid_height * tilesize / g_tilesize;
843 *x = rendered_width + 2 * BORDER(tilesize) + 1;
844 *y = rendered_height + 2 * BORDER(tilesize) + 1;
847 static void game_set_size(drawing *dr, game_drawstate *ds,
848 const game_params *params, int tilesize)
850 ds->tilesize = tilesize;
853 static float *game_colours(frontend *fe, int *ncolours)
855 float *ret = snewn(3 * NCOLOURS, float);
857 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
859 ret[COL_FOREGROUND * 3 + 0] = 0.0F;
860 ret[COL_FOREGROUND * 3 + 1] = 0.0F;
861 ret[COL_FOREGROUND * 3 + 2] = 0.0F;
864 * We want COL_LINEUNKNOWN to be a yellow which is a bit darker
865 * than the background. (I previously set it to 0.8,0.8,0, but
866 * found that this went badly with the 0.8,0.8,0.8 favoured as a
867 * background by the Java frontend.)
869 ret[COL_LINEUNKNOWN * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.9F;
870 ret[COL_LINEUNKNOWN * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.9F;
871 ret[COL_LINEUNKNOWN * 3 + 2] = 0.0F;
873 ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
874 ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
875 ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
877 ret[COL_MISTAKE * 3 + 0] = 1.0F;
878 ret[COL_MISTAKE * 3 + 1] = 0.0F;
879 ret[COL_MISTAKE * 3 + 2] = 0.0F;
881 ret[COL_SATISFIED * 3 + 0] = 0.0F;
882 ret[COL_SATISFIED * 3 + 1] = 0.0F;
883 ret[COL_SATISFIED * 3 + 2] = 0.0F;
885 /* We want the faint lines to be a bit darker than the background.
886 * Except if the background is pretty dark already; then it ought to be a
887 * bit lighter. Oy vey.
889 ret[COL_FAINT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.9F;
890 ret[COL_FAINT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.9F;
891 ret[COL_FAINT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.9F;
893 *ncolours = NCOLOURS;
897 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
899 struct game_drawstate *ds = snew(struct game_drawstate);
900 int num_faces = state->game_grid->num_faces;
901 int num_edges = state->game_grid->num_edges;
906 ds->lines = snewn(num_edges, char);
907 ds->clue_error = snewn(num_faces, char);
908 ds->clue_satisfied = snewn(num_faces, char);
909 ds->textx = snewn(num_faces, int);
910 ds->texty = snewn(num_faces, int);
913 memset(ds->lines, LINE_UNKNOWN, num_edges);
914 memset(ds->clue_error, 0, num_faces);
915 memset(ds->clue_satisfied, 0, num_faces);
916 for (i = 0; i < num_faces; i++)
917 ds->textx[i] = ds->texty[i] = -1;
922 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
926 sfree(ds->clue_error);
927 sfree(ds->clue_satisfied);
932 static int game_timing_state(const game_state *state, game_ui *ui)
937 static float game_anim_length(const game_state *oldstate,
938 const game_state *newstate, int dir, game_ui *ui)
943 static int game_can_format_as_text_now(const game_params *params)
945 if (params->type != 0)
950 static char *game_text_format(const game_state *state)
956 grid *g = state->game_grid;
959 assert(state->grid_type == 0);
961 /* Work out the basic size unit */
962 f = g->faces; /* first face */
963 assert(f->order == 4);
964 /* The dots are ordered clockwise, so the two opposite
965 * corners are guaranteed to span the square */
966 cell_size = abs(f->dots[0]->x - f->dots[2]->x);
968 w = (g->highest_x - g->lowest_x) / cell_size;
969 h = (g->highest_y - g->lowest_y) / cell_size;
971 /* Create a blank "canvas" to "draw" on */
974 ret = snewn(W * H + 1, char);
975 for (y = 0; y < H; y++) {
976 for (x = 0; x < W-1; x++) {
979 ret[y*W + W-1] = '\n';
983 /* Fill in edge info */
984 for (i = 0; i < g->num_edges; i++) {
985 grid_edge *e = g->edges + i;
986 /* Cell coordinates, from (0,0) to (w-1,h-1) */
987 int x1 = (e->dot1->x - g->lowest_x) / cell_size;
988 int x2 = (e->dot2->x - g->lowest_x) / cell_size;
989 int y1 = (e->dot1->y - g->lowest_y) / cell_size;
990 int y2 = (e->dot2->y - g->lowest_y) / cell_size;
991 /* Midpoint, in canvas coordinates (canvas coordinates are just twice
992 * cell coordinates) */
995 switch (state->lines[i]) {
997 ret[y*W + x] = (y1 == y2) ? '-' : '|';
1003 break; /* already a space */
1005 assert(!"Illegal line state");
1010 for (i = 0; i < g->num_faces; i++) {
1014 assert(f->order == 4);
1015 /* Cell coordinates, from (0,0) to (w-1,h-1) */
1016 x1 = (f->dots[0]->x - g->lowest_x) / cell_size;
1017 x2 = (f->dots[2]->x - g->lowest_x) / cell_size;
1018 y1 = (f->dots[0]->y - g->lowest_y) / cell_size;
1019 y2 = (f->dots[2]->y - g->lowest_y) / cell_size;
1020 /* Midpoint, in canvas coordinates */
1023 ret[y*W + x] = CLUE2CHAR(state->clues[i]);
1028 /* ----------------------------------------------------------------------
1033 static void check_caches(const solver_state* sstate)
1036 const game_state *state = sstate->state;
1037 const grid *g = state->game_grid;
1039 for (i = 0; i < g->num_dots; i++) {
1040 assert(dot_order(state, i, LINE_YES) == sstate->dot_yes_count[i]);
1041 assert(dot_order(state, i, LINE_NO) == sstate->dot_no_count[i]);
1044 for (i = 0; i < g->num_faces; i++) {
1045 assert(face_order(state, i, LINE_YES) == sstate->face_yes_count[i]);
1046 assert(face_order(state, i, LINE_NO) == sstate->face_no_count[i]);
1051 #define check_caches(s) \
1053 fprintf(stderr, "check_caches at line %d\n", __LINE__); \
1057 #endif /* DEBUG_CACHES */
1059 /* ----------------------------------------------------------------------
1060 * Solver utility functions
1063 /* Sets the line (with index i) to the new state 'line_new', and updates
1064 * the cached counts of any affected faces and dots.
1065 * Returns TRUE if this actually changed the line's state. */
1066 static int solver_set_line(solver_state *sstate, int i,
1067 enum line_state line_new
1069 , const char *reason
1073 game_state *state = sstate->state;
1077 assert(line_new != LINE_UNKNOWN);
1079 check_caches(sstate);
1081 if (state->lines[i] == line_new) {
1082 return FALSE; /* nothing changed */
1084 state->lines[i] = line_new;
1087 fprintf(stderr, "solver: set line [%d] to %s (%s)\n",
1088 i, line_new == LINE_YES ? "YES" : "NO",
1092 g = state->game_grid;
1095 /* Update the cache for both dots and both faces affected by this. */
1096 if (line_new == LINE_YES) {
1097 sstate->dot_yes_count[e->dot1 - g->dots]++;
1098 sstate->dot_yes_count[e->dot2 - g->dots]++;
1100 sstate->face_yes_count[e->face1 - g->faces]++;
1103 sstate->face_yes_count[e->face2 - g->faces]++;
1106 sstate->dot_no_count[e->dot1 - g->dots]++;
1107 sstate->dot_no_count[e->dot2 - g->dots]++;
1109 sstate->face_no_count[e->face1 - g->faces]++;
1112 sstate->face_no_count[e->face2 - g->faces]++;
1116 check_caches(sstate);
1121 #define solver_set_line(a, b, c) \
1122 solver_set_line(a, b, c, __FUNCTION__)
1126 * Merge two dots due to the existence of an edge between them.
1127 * Updates the dsf tracking equivalence classes, and keeps track of
1128 * the length of path each dot is currently a part of.
1129 * Returns TRUE if the dots were already linked, ie if they are part of a
1130 * closed loop, and false otherwise.
1132 static int merge_dots(solver_state *sstate, int edge_index)
1135 grid *g = sstate->state->game_grid;
1136 grid_edge *e = g->edges + edge_index;
1138 i = e->dot1 - g->dots;
1139 j = e->dot2 - g->dots;
1141 i = dsf_canonify(sstate->dotdsf, i);
1142 j = dsf_canonify(sstate->dotdsf, j);
1147 len = sstate->looplen[i] + sstate->looplen[j];
1148 dsf_merge(sstate->dotdsf, i, j);
1149 i = dsf_canonify(sstate->dotdsf, i);
1150 sstate->looplen[i] = len;
1155 /* Merge two lines because the solver has deduced that they must be either
1156 * identical or opposite. Returns TRUE if this is new information, otherwise
1158 static int merge_lines(solver_state *sstate, int i, int j, int inverse
1160 , const char *reason
1166 assert(i < sstate->state->game_grid->num_edges);
1167 assert(j < sstate->state->game_grid->num_edges);
1169 i = edsf_canonify(sstate->linedsf, i, &inv_tmp);
1171 j = edsf_canonify(sstate->linedsf, j, &inv_tmp);
1174 edsf_merge(sstate->linedsf, i, j, inverse);
1178 fprintf(stderr, "%s [%d] [%d] %s(%s)\n",
1180 inverse ? "inverse " : "", reason);
1187 #define merge_lines(a, b, c, d) \
1188 merge_lines(a, b, c, d, __FUNCTION__)
1191 /* Count the number of lines of a particular type currently going into the
1193 static int dot_order(const game_state* state, int dot, char line_type)
1196 grid *g = state->game_grid;
1197 grid_dot *d = g->dots + dot;
1200 for (i = 0; i < d->order; i++) {
1201 grid_edge *e = d->edges[i];
1202 if (state->lines[e - g->edges] == line_type)
1208 /* Count the number of lines of a particular type currently surrounding the
1210 static int face_order(const game_state* state, int face, char line_type)
1213 grid *g = state->game_grid;
1214 grid_face *f = g->faces + face;
1217 for (i = 0; i < f->order; i++) {
1218 grid_edge *e = f->edges[i];
1219 if (state->lines[e - g->edges] == line_type)
1225 /* Set all lines bordering a dot of type old_type to type new_type
1226 * Return value tells caller whether this function actually did anything */
1227 static int dot_setall(solver_state *sstate, int dot,
1228 char old_type, char new_type)
1230 int retval = FALSE, r;
1231 game_state *state = sstate->state;
1236 if (old_type == new_type)
1239 g = state->game_grid;
1242 for (i = 0; i < d->order; i++) {
1243 int line_index = d->edges[i] - g->edges;
1244 if (state->lines[line_index] == old_type) {
1245 r = solver_set_line(sstate, line_index, new_type);
1253 /* Set all lines bordering a face of type old_type to type new_type */
1254 static int face_setall(solver_state *sstate, int face,
1255 char old_type, char new_type)
1257 int retval = FALSE, r;
1258 game_state *state = sstate->state;
1263 if (old_type == new_type)
1266 g = state->game_grid;
1267 f = g->faces + face;
1269 for (i = 0; i < f->order; i++) {
1270 int line_index = f->edges[i] - g->edges;
1271 if (state->lines[line_index] == old_type) {
1272 r = solver_set_line(sstate, line_index, new_type);
1280 /* ----------------------------------------------------------------------
1281 * Loop generation and clue removal
1284 static void add_full_clues(game_state *state, random_state *rs)
1286 signed char *clues = state->clues;
1287 grid *g = state->game_grid;
1288 char *board = snewn(g->num_faces, char);
1291 generate_loop(g, board, rs, NULL, NULL);
1293 /* Fill out all the clues by initialising to 0, then iterating over
1294 * all edges and incrementing each clue as we find edges that border
1295 * between BLACK/WHITE faces. While we're at it, we verify that the
1296 * algorithm does work, and there aren't any GREY faces still there. */
1297 memset(clues, 0, g->num_faces);
1298 for (i = 0; i < g->num_edges; i++) {
1299 grid_edge *e = g->edges + i;
1300 grid_face *f1 = e->face1;
1301 grid_face *f2 = e->face2;
1302 enum face_colour c1 = FACE_COLOUR(f1);
1303 enum face_colour c2 = FACE_COLOUR(f2);
1304 assert(c1 != FACE_GREY);
1305 assert(c2 != FACE_GREY);
1307 if (f1) clues[f1 - g->faces]++;
1308 if (f2) clues[f2 - g->faces]++;
1315 static int game_has_unique_soln(const game_state *state, int diff)
1318 solver_state *sstate_new;
1319 solver_state *sstate = new_solver_state((game_state *)state, diff);
1321 sstate_new = solve_game_rec(sstate);
1323 assert(sstate_new->solver_status != SOLVER_MISTAKE);
1324 ret = (sstate_new->solver_status == SOLVER_SOLVED);
1326 free_solver_state(sstate_new);
1327 free_solver_state(sstate);
1333 /* Remove clues one at a time at random. */
1334 static game_state *remove_clues(game_state *state, random_state *rs,
1338 int num_faces = state->game_grid->num_faces;
1339 game_state *ret = dup_game(state), *saved_ret;
1342 /* We need to remove some clues. We'll do this by forming a list of all
1343 * available clues, shuffling it, then going along one at a
1344 * time clearing each clue in turn for which doing so doesn't render the
1345 * board unsolvable. */
1346 face_list = snewn(num_faces, int);
1347 for (n = 0; n < num_faces; ++n) {
1351 shuffle(face_list, num_faces, sizeof(int), rs);
1353 for (n = 0; n < num_faces; ++n) {
1354 saved_ret = dup_game(ret);
1355 ret->clues[face_list[n]] = -1;
1357 if (game_has_unique_soln(ret, diff)) {
1358 free_game(saved_ret);
1370 static char *new_game_desc(const game_params *params, random_state *rs,
1371 char **aux, int interactive)
1373 /* solution and description both use run-length encoding in obvious ways */
1374 char *retval, *game_desc, *grid_desc;
1376 game_state *state = snew(game_state);
1377 game_state *state_new;
1379 grid_desc = grid_new_desc(grid_types[params->type], params->w, params->h, rs);
1380 state->game_grid = g = loopy_generate_grid(params, grid_desc);
1382 state->clues = snewn(g->num_faces, signed char);
1383 state->lines = snewn(g->num_edges, char);
1384 state->line_errors = snewn(g->num_edges, unsigned char);
1385 state->exactly_one_loop = FALSE;
1387 state->grid_type = params->type;
1391 memset(state->lines, LINE_UNKNOWN, g->num_edges);
1392 memset(state->line_errors, 0, g->num_edges);
1394 state->solved = state->cheated = FALSE;
1396 /* Get a new random solvable board with all its clues filled in. Yes, this
1397 * can loop for ever if the params are suitably unfavourable, but
1398 * preventing games smaller than 4x4 seems to stop this happening */
1400 add_full_clues(state, rs);
1401 } while (!game_has_unique_soln(state, params->diff));
1403 state_new = remove_clues(state, rs, params->diff);
1408 if (params->diff > 0 && game_has_unique_soln(state, params->diff-1)) {
1410 fprintf(stderr, "Rejecting board, it is too easy\n");
1412 goto newboard_please;
1415 game_desc = state_to_text(state);
1420 retval = snewn(strlen(grid_desc) + 1 + strlen(game_desc) + 1, char);
1421 sprintf(retval, "%s%c%s", grid_desc, (int)GRID_DESC_SEP, game_desc);
1428 assert(!validate_desc(params, retval));
1433 static game_state *new_game(midend *me, const game_params *params,
1437 game_state *state = snew(game_state);
1438 int empties_to_make = 0;
1443 int num_faces, num_edges;
1445 grid_desc = extract_grid_desc(&desc);
1446 state->game_grid = g = loopy_generate_grid(params, grid_desc);
1447 if (grid_desc) sfree(grid_desc);
1451 num_faces = g->num_faces;
1452 num_edges = g->num_edges;
1454 state->clues = snewn(num_faces, signed char);
1455 state->lines = snewn(num_edges, char);
1456 state->line_errors = snewn(num_edges, unsigned char);
1457 state->exactly_one_loop = FALSE;
1459 state->solved = state->cheated = FALSE;
1461 state->grid_type = params->type;
1463 for (i = 0; i < num_faces; i++) {
1464 if (empties_to_make) {
1466 state->clues[i] = -1;
1472 n2 = *dp - 'A' + 10;
1473 if (n >= 0 && n < 10) {
1474 state->clues[i] = n;
1475 } else if (n2 >= 10 && n2 < 36) {
1476 state->clues[i] = n2;
1480 state->clues[i] = -1;
1481 empties_to_make = n - 1;
1486 memset(state->lines, LINE_UNKNOWN, num_edges);
1487 memset(state->line_errors, 0, num_edges);
1491 /* Calculates the line_errors data, and checks if the current state is a
1493 static int check_completion(game_state *state)
1495 grid *g = state->game_grid;
1497 int *dsf, *component_state;
1498 int nsilly, nloop, npath, largest_comp, largest_size, total_pathsize;
1499 enum { COMP_NONE, COMP_LOOP, COMP_PATH, COMP_SILLY, COMP_EMPTY };
1501 memset(state->line_errors, 0, g->num_edges);
1504 * Find loops in the grid, and determine whether the puzzle is
1507 * Loopy is a bit more complicated than most puzzles that care
1508 * about loop detection. In most of them, loops are simply
1509 * _forbidden_; so the obviously right way to do
1510 * error-highlighting during play is to light up a graph edge red
1511 * iff it is part of a loop, which is exactly what the centralised
1512 * findloop.c makes easy.
1514 * But Loopy is unusual in that you're _supposed_ to be making a
1515 * loop - and yet _some_ loops are not the right loop. So we need
1516 * to be more discriminating, by identifying loops one by one and
1517 * then thinking about which ones to highlight, and so findloop.c
1518 * isn't quite the right tool for the job in this case.
1520 * Worse still, consider situations in which the grid contains a
1521 * loop and also some non-loop edges: there are some cases like
1522 * this in which the user's intuitive expectation would be to
1523 * highlight the loop (if you're only about half way through the
1524 * puzzle and have accidentally made a little loop in some corner
1525 * of the grid), and others in which they'd be more likely to
1526 * expect you to highlight the non-loop edges (if you've just
1527 * closed off a whole loop that you thought was the entire
1528 * solution, but forgot some disconnected edges in a corner
1529 * somewhere). So while it's easy enough to check whether the
1530 * solution is _right_, highlighting the wrong parts is a tricky
1531 * problem for this puzzle!
1533 * I'd quite like, in some situations, to identify the largest
1534 * loop among the player's YES edges, and then light up everything
1535 * other than that. But finding the longest cycle in a graph is an
1536 * NP-complete problem (because, in particular, it must return a
1537 * Hamilton cycle if one exists).
1539 * However, I think we can make the problem tractable by
1540 * exercising the Puzzles principle that it isn't absolutely
1541 * necessary to highlight _all_ errors: the key point is that by
1542 * the time the user has filled in the whole grid, they should
1543 * either have seen a completion flash, or have _some_ error
1544 * highlight showing them why the solution isn't right. So in
1545 * principle it would be *just about* good enough to highlight
1546 * just one error in the whole grid, if there was really no better
1547 * way. But we'd like to highlight as many errors as possible.
1549 * In this case, I think the simple approach is to make use of the
1550 * fact that no vertex may have degree > 2, and that's really
1551 * simple to detect. So the plan goes like this:
1553 * - Form the dsf of connected components of the graph vertices.
1555 * - Highlight an error at any vertex with degree > 2. (It so
1556 * happens that we do this by lighting up all the edges
1557 * incident to that vertex, but that's an output detail.)
1559 * - Any component that contains such a vertex is now excluded
1560 * from further consideration, because it already has a
1563 * - The remaining components have no vertex with degree > 2, and
1564 * hence they all consist of either a simple loop, or a simple
1565 * path with two endpoints.
1567 * - For these purposes, group together all the paths and imagine
1568 * them to be a single component (because in most normal
1569 * situations the player will gradually build up the solution
1570 * _not_ all in one connected segment, but as lots of separate
1571 * little path pieces that gradually connect to each other).
1573 * - After doing that, if there is exactly one (sensible)
1574 * component - be it a collection of paths or a loop - then
1575 * highlight no further edge errors. (The former case is normal
1576 * during play, and the latter is a potentially solved puzzle.)
1578 * - Otherwise, find the largest of the sensible components,
1579 * leave that one unhighlighted, and light the rest up in red.
1582 dsf = snew_dsf(g->num_dots);
1584 /* Build the dsf. */
1585 for (i = 0; i < g->num_edges; i++) {
1586 if (state->lines[i] == LINE_YES) {
1587 grid_edge *e = g->edges + i;
1588 int d1 = e->dot1 - g->dots, d2 = e->dot2 - g->dots;
1589 dsf_merge(dsf, d1, d2);
1593 /* Initialise a state variable for each connected component. */
1594 component_state = snewn(g->num_dots, int);
1595 for (i = 0; i < g->num_dots; i++) {
1596 if (dsf_canonify(dsf, i) == i)
1597 component_state[i] = COMP_LOOP;
1599 component_state[i] = COMP_NONE;
1602 /* Check for dots with degree > 3. Here we also spot dots of
1603 * degree 1 in which the user has marked all the non-edges as
1604 * LINE_NO, because those are also clear vertex-level errors, so
1605 * we give them the same treatment of excluding their connected
1606 * component from the subsequent loop analysis. */
1607 for (i = 0; i < g->num_dots; i++) {
1608 int comp = dsf_canonify(dsf, i);
1609 int yes = dot_order(state, i, LINE_YES);
1610 int unknown = dot_order(state, i, LINE_UNKNOWN);
1611 if ((yes == 1 && unknown == 0) || (yes >= 3)) {
1612 /* violation, so mark all YES edges as errors */
1613 grid_dot *d = g->dots + i;
1615 for (j = 0; j < d->order; j++) {
1616 int e = d->edges[j] - g->edges;
1617 if (state->lines[e] == LINE_YES)
1618 state->line_errors[e] = TRUE;
1620 /* And mark this component as not worthy of further
1622 component_state[comp] = COMP_SILLY;
1624 } else if (yes == 0) {
1625 /* A completely isolated dot must also be excluded it from
1626 * the subsequent loop highlighting pass, but we tag it
1627 * with a different enum value to avoid it counting
1628 * towards the components that inhibit returning a win
1630 component_state[comp] = COMP_EMPTY;
1631 } else if (yes == 1) {
1632 /* A dot with degree 1 that didn't fall into the 'clearly
1633 * erroneous' case above indicates that this connected
1634 * component will be a path rather than a loop - unless
1635 * something worse elsewhere in the component has
1636 * classified it as silly. */
1637 if (component_state[comp] != COMP_SILLY)
1638 component_state[comp] = COMP_PATH;
1642 /* Count up the components. Also, find the largest sensible
1643 * component. (Tie-breaking condition is derived from the order of
1644 * vertices in the grid data structure, which is fairly arbitrary
1645 * but at least stays stable throughout the game.) */
1646 nsilly = nloop = npath = 0;
1648 largest_comp = largest_size = -1;
1649 for (i = 0; i < g->num_dots; i++) {
1650 if (component_state[i] == COMP_SILLY) {
1652 } else if (component_state[i] == COMP_PATH) {
1653 total_pathsize += dsf_size(dsf, i);
1655 } else if (component_state[i] == COMP_LOOP) {
1660 if ((this_size = dsf_size(dsf, i)) > largest_size) {
1662 largest_size = this_size;
1666 if (largest_size < total_pathsize) {
1667 largest_comp = -1; /* means the paths */
1668 largest_size = total_pathsize;
1671 if (nloop > 0 && nloop + npath > 1) {
1673 * If there are at least two sensible components including at
1674 * least one loop, highlight all edges in every sensible
1675 * component that is not the largest one.
1677 for (i = 0; i < g->num_edges; i++) {
1678 if (state->lines[i] == LINE_YES) {
1679 grid_edge *e = g->edges + i;
1680 int d1 = e->dot1 - g->dots; /* either endpoint is good enough */
1681 int comp = dsf_canonify(dsf, d1);
1682 if ((component_state[comp] == COMP_PATH &&
1683 -1 != largest_comp) ||
1684 (component_state[comp] == COMP_LOOP &&
1685 comp != largest_comp))
1686 state->line_errors[i] = TRUE;
1691 if (nloop == 1 && npath == 0 && nsilly == 0) {
1693 * If there is exactly one component and it is a loop, then
1694 * the puzzle is potentially complete, so check the clues.
1698 for (i = 0; i < g->num_faces; i++) {
1699 int c = state->clues[i];
1700 if (c >= 0 && face_order(state, i, LINE_YES) != c) {
1707 * Also, whether or not the puzzle is actually complete, set
1708 * the flag that says this game_state has exactly one loop and
1709 * nothing else, which will be used to vary the semantics of
1710 * clue highlighting at display time.
1712 state->exactly_one_loop = TRUE;
1715 state->exactly_one_loop = FALSE;
1718 sfree(component_state);
1724 /* ----------------------------------------------------------------------
1727 * Our solver modes operate as follows. Each mode also uses the modes above it.
1730 * Just implement the rules of the game.
1732 * Normal and Tricky Modes
1733 * For each (adjacent) pair of lines through each dot we store a bit for
1734 * whether at least one of them is on and whether at most one is on. (If we
1735 * know both or neither is on that's already stored more directly.)
1738 * Use edsf data structure to make equivalence classes of lines that are
1739 * known identical to or opposite to one another.
1744 * For general grids, we consider "dlines" to be pairs of lines joined
1745 * at a dot. The lines must be adjacent around the dot, so we can think of
1746 * a dline as being a dot+face combination. Or, a dot+edge combination where
1747 * the second edge is taken to be the next clockwise edge from the dot.
1748 * Original loopy code didn't have this extra restriction of the lines being
1749 * adjacent. From my tests with square grids, this extra restriction seems to
1750 * take little, if anything, away from the quality of the puzzles.
1751 * A dline can be uniquely identified by an edge/dot combination, given that
1752 * a dline-pair always goes clockwise around its common dot. The edge/dot
1753 * combination can be represented by an edge/bool combination - if bool is
1754 * TRUE, use edge->dot1 else use edge->dot2. So the total number of dlines is
1755 * exactly twice the number of edges in the grid - although the dlines
1756 * spanning the infinite face are not all that useful to the solver.
1757 * Note that, by convention, a dline goes clockwise around its common dot,
1758 * which means the dline goes anti-clockwise around its common face.
1761 /* Helper functions for obtaining an index into an array of dlines, given
1762 * various information. We assume the grid layout conventions about how
1763 * the various lists are interleaved - see grid_make_consistent() for
1766 /* i points to the first edge of the dline pair, reading clockwise around
1768 static int dline_index_from_dot(grid *g, grid_dot *d, int i)
1770 grid_edge *e = d->edges[i];
1775 if (i2 == d->order) i2 = 0;
1778 ret = 2 * (e - g->edges) + ((e->dot1 == d) ? 1 : 0);
1780 printf("dline_index_from_dot: d=%d,i=%d, edges [%d,%d] - %d\n",
1781 (int)(d - g->dots), i, (int)(e - g->edges),
1782 (int)(e2 - g->edges), ret);
1786 /* i points to the second edge of the dline pair, reading clockwise around
1787 * the face. That is, the edges of the dline, starting at edge{i}, read
1788 * anti-clockwise around the face. By layout conventions, the common dot
1789 * of the dline will be f->dots[i] */
1790 static int dline_index_from_face(grid *g, grid_face *f, int i)
1792 grid_edge *e = f->edges[i];
1793 grid_dot *d = f->dots[i];
1798 if (i2 < 0) i2 += f->order;
1801 ret = 2 * (e - g->edges) + ((e->dot1 == d) ? 1 : 0);
1803 printf("dline_index_from_face: f=%d,i=%d, edges [%d,%d] - %d\n",
1804 (int)(f - g->faces), i, (int)(e - g->edges),
1805 (int)(e2 - g->edges), ret);
1809 static int is_atleastone(const char *dline_array, int index)
1811 return BIT_SET(dline_array[index], 0);
1813 static int set_atleastone(char *dline_array, int index)
1815 return SET_BIT(dline_array[index], 0);
1817 static int is_atmostone(const char *dline_array, int index)
1819 return BIT_SET(dline_array[index], 1);
1821 static int set_atmostone(char *dline_array, int index)
1823 return SET_BIT(dline_array[index], 1);
1826 static void array_setall(char *array, char from, char to, int len)
1828 char *p = array, *p_old = p;
1829 int len_remaining = len;
1831 while ((p = memchr(p, from, len_remaining))) {
1833 len_remaining -= p - p_old;
1838 /* Helper, called when doing dline dot deductions, in the case where we
1839 * have 4 UNKNOWNs, and two of them (adjacent) have *exactly* one YES between
1840 * them (because of dline atmostone/atleastone).
1841 * On entry, edge points to the first of these two UNKNOWNs. This function
1842 * will find the opposite UNKNOWNS (if they are adjacent to one another)
1843 * and set their corresponding dline to atleastone. (Setting atmostone
1844 * already happens in earlier dline deductions) */
1845 static int dline_set_opp_atleastone(solver_state *sstate,
1846 grid_dot *d, int edge)
1848 game_state *state = sstate->state;
1849 grid *g = state->game_grid;
1852 for (opp = 0; opp < N; opp++) {
1853 int opp_dline_index;
1854 if (opp == edge || opp == edge+1 || opp == edge-1)
1856 if (opp == 0 && edge == N-1)
1858 if (opp == N-1 && edge == 0)
1861 if (opp2 == N) opp2 = 0;
1862 /* Check if opp, opp2 point to LINE_UNKNOWNs */
1863 if (state->lines[d->edges[opp] - g->edges] != LINE_UNKNOWN)
1865 if (state->lines[d->edges[opp2] - g->edges] != LINE_UNKNOWN)
1867 /* Found opposite UNKNOWNS and they're next to each other */
1868 opp_dline_index = dline_index_from_dot(g, d, opp);
1869 return set_atleastone(sstate->dlines, opp_dline_index);
1875 /* Set pairs of lines around this face which are known to be identical, to
1876 * the given line_state */
1877 static int face_setall_identical(solver_state *sstate, int face_index,
1878 enum line_state line_new)
1880 /* can[dir] contains the canonical line associated with the line in
1881 * direction dir from the square in question. Similarly inv[dir] is
1882 * whether or not the line in question is inverse to its canonical
1885 game_state *state = sstate->state;
1886 grid *g = state->game_grid;
1887 grid_face *f = g->faces + face_index;
1890 int can1, can2, inv1, inv2;
1892 for (i = 0; i < N; i++) {
1893 int line1_index = f->edges[i] - g->edges;
1894 if (state->lines[line1_index] != LINE_UNKNOWN)
1896 for (j = i + 1; j < N; j++) {
1897 int line2_index = f->edges[j] - g->edges;
1898 if (state->lines[line2_index] != LINE_UNKNOWN)
1901 /* Found two UNKNOWNS */
1902 can1 = edsf_canonify(sstate->linedsf, line1_index, &inv1);
1903 can2 = edsf_canonify(sstate->linedsf, line2_index, &inv2);
1904 if (can1 == can2 && inv1 == inv2) {
1905 solver_set_line(sstate, line1_index, line_new);
1906 solver_set_line(sstate, line2_index, line_new);
1913 /* Given a dot or face, and a count of LINE_UNKNOWNs, find them and
1914 * return the edge indices into e. */
1915 static void find_unknowns(game_state *state,
1916 grid_edge **edge_list, /* Edge list to search (from a face or a dot) */
1917 int expected_count, /* Number of UNKNOWNs (comes from solver's cache) */
1918 int *e /* Returned edge indices */)
1921 grid *g = state->game_grid;
1922 while (c < expected_count) {
1923 int line_index = *edge_list - g->edges;
1924 if (state->lines[line_index] == LINE_UNKNOWN) {
1932 /* If we have a list of edges, and we know whether the number of YESs should
1933 * be odd or even, and there are only a few UNKNOWNs, we can do some simple
1934 * linedsf deductions. This can be used for both face and dot deductions.
1935 * Returns the difficulty level of the next solver that should be used,
1936 * or DIFF_MAX if no progress was made. */
1937 static int parity_deductions(solver_state *sstate,
1938 grid_edge **edge_list, /* Edge list (from a face or a dot) */
1939 int total_parity, /* Expected number of YESs modulo 2 (either 0 or 1) */
1942 game_state *state = sstate->state;
1943 int diff = DIFF_MAX;
1944 int *linedsf = sstate->linedsf;
1946 if (unknown_count == 2) {
1947 /* Lines are known alike/opposite, depending on inv. */
1949 find_unknowns(state, edge_list, 2, e);
1950 if (merge_lines(sstate, e[0], e[1], total_parity))
1951 diff = min(diff, DIFF_HARD);
1952 } else if (unknown_count == 3) {
1954 int can[3]; /* canonical edges */
1955 int inv[3]; /* whether can[x] is inverse to e[x] */
1956 find_unknowns(state, edge_list, 3, e);
1957 can[0] = edsf_canonify(linedsf, e[0], inv);
1958 can[1] = edsf_canonify(linedsf, e[1], inv+1);
1959 can[2] = edsf_canonify(linedsf, e[2], inv+2);
1960 if (can[0] == can[1]) {
1961 if (solver_set_line(sstate, e[2], (total_parity^inv[0]^inv[1]) ?
1962 LINE_YES : LINE_NO))
1963 diff = min(diff, DIFF_EASY);
1965 if (can[0] == can[2]) {
1966 if (solver_set_line(sstate, e[1], (total_parity^inv[0]^inv[2]) ?
1967 LINE_YES : LINE_NO))
1968 diff = min(diff, DIFF_EASY);
1970 if (can[1] == can[2]) {
1971 if (solver_set_line(sstate, e[0], (total_parity^inv[1]^inv[2]) ?
1972 LINE_YES : LINE_NO))
1973 diff = min(diff, DIFF_EASY);
1975 } else if (unknown_count == 4) {
1977 int can[4]; /* canonical edges */
1978 int inv[4]; /* whether can[x] is inverse to e[x] */
1979 find_unknowns(state, edge_list, 4, e);
1980 can[0] = edsf_canonify(linedsf, e[0], inv);
1981 can[1] = edsf_canonify(linedsf, e[1], inv+1);
1982 can[2] = edsf_canonify(linedsf, e[2], inv+2);
1983 can[3] = edsf_canonify(linedsf, e[3], inv+3);
1984 if (can[0] == can[1]) {
1985 if (merge_lines(sstate, e[2], e[3], total_parity^inv[0]^inv[1]))
1986 diff = min(diff, DIFF_HARD);
1987 } else if (can[0] == can[2]) {
1988 if (merge_lines(sstate, e[1], e[3], total_parity^inv[0]^inv[2]))
1989 diff = min(diff, DIFF_HARD);
1990 } else if (can[0] == can[3]) {
1991 if (merge_lines(sstate, e[1], e[2], total_parity^inv[0]^inv[3]))
1992 diff = min(diff, DIFF_HARD);
1993 } else if (can[1] == can[2]) {
1994 if (merge_lines(sstate, e[0], e[3], total_parity^inv[1]^inv[2]))
1995 diff = min(diff, DIFF_HARD);
1996 } else if (can[1] == can[3]) {
1997 if (merge_lines(sstate, e[0], e[2], total_parity^inv[1]^inv[3]))
1998 diff = min(diff, DIFF_HARD);
1999 } else if (can[2] == can[3]) {
2000 if (merge_lines(sstate, e[0], e[1], total_parity^inv[2]^inv[3]))
2001 diff = min(diff, DIFF_HARD);
2009 * These are the main solver functions.
2011 * Their return values are diff values corresponding to the lowest mode solver
2012 * that would notice the work that they have done. For example if the normal
2013 * mode solver adds actual lines or crosses, it will return DIFF_EASY as the
2014 * easy mode solver might be able to make progress using that. It doesn't make
2015 * sense for one of them to return a diff value higher than that of the
2018 * Each function returns the lowest value it can, as early as possible, in
2019 * order to try and pass as much work as possible back to the lower level
2020 * solvers which progress more quickly.
2023 /* PROPOSED NEW DESIGN:
2024 * We have a work queue consisting of 'events' notifying us that something has
2025 * happened that a particular solver mode might be interested in. For example
2026 * the hard mode solver might do something that helps the normal mode solver at
2027 * dot [x,y] in which case it will enqueue an event recording this fact. Then
2028 * we pull events off the work queue, and hand each in turn to the solver that
2029 * is interested in them. If a solver reports that it failed we pass the same
2030 * event on to progressively more advanced solvers and the loop detector. Once
2031 * we've exhausted an event, or it has helped us progress, we drop it and
2032 * continue to the next one. The events are sorted first in order of solver
2033 * complexity (easy first) then order of insertion (oldest first).
2034 * Once we run out of events we loop over each permitted solver in turn
2035 * (easiest first) until either a deduction is made (and an event therefore
2036 * emerges) or no further deductions can be made (in which case we've failed).
2039 * * How do we 'loop over' a solver when both dots and squares are concerned.
2040 * Answer: first all squares then all dots.
2043 static int trivial_deductions(solver_state *sstate)
2045 int i, current_yes, current_no;
2046 game_state *state = sstate->state;
2047 grid *g = state->game_grid;
2048 int diff = DIFF_MAX;
2050 /* Per-face deductions */
2051 for (i = 0; i < g->num_faces; i++) {
2052 grid_face *f = g->faces + i;
2054 if (sstate->face_solved[i])
2057 current_yes = sstate->face_yes_count[i];
2058 current_no = sstate->face_no_count[i];
2060 if (current_yes + current_no == f->order) {
2061 sstate->face_solved[i] = TRUE;
2065 if (state->clues[i] < 0)
2069 * This code checks whether the numeric clue on a face is so
2070 * large as to permit all its remaining LINE_UNKNOWNs to be
2071 * filled in as LINE_YES, or alternatively so small as to
2072 * permit them all to be filled in as LINE_NO.
2075 if (state->clues[i] < current_yes) {
2076 sstate->solver_status = SOLVER_MISTAKE;
2079 if (state->clues[i] == current_yes) {
2080 if (face_setall(sstate, i, LINE_UNKNOWN, LINE_NO))
2081 diff = min(diff, DIFF_EASY);
2082 sstate->face_solved[i] = TRUE;
2086 if (f->order - state->clues[i] < current_no) {
2087 sstate->solver_status = SOLVER_MISTAKE;
2090 if (f->order - state->clues[i] == current_no) {
2091 if (face_setall(sstate, i, LINE_UNKNOWN, LINE_YES))
2092 diff = min(diff, DIFF_EASY);
2093 sstate->face_solved[i] = TRUE;
2097 if (f->order - state->clues[i] == current_no + 1 &&
2098 f->order - current_yes - current_no > 2) {
2100 * One small refinement to the above: we also look for any
2101 * adjacent pair of LINE_UNKNOWNs around the face with
2102 * some LINE_YES incident on it from elsewhere. If we find
2103 * one, then we know that pair of LINE_UNKNOWNs can't
2104 * _both_ be LINE_YES, and hence that pushes us one line
2105 * closer to being able to determine all the rest.
2107 int j, k, e1, e2, e, d;
2109 for (j = 0; j < f->order; j++) {
2110 e1 = f->edges[j] - g->edges;
2111 e2 = f->edges[j+1 < f->order ? j+1 : 0] - g->edges;
2113 if (g->edges[e1].dot1 == g->edges[e2].dot1 ||
2114 g->edges[e1].dot1 == g->edges[e2].dot2) {
2115 d = g->edges[e1].dot1 - g->dots;
2117 assert(g->edges[e1].dot2 == g->edges[e2].dot1 ||
2118 g->edges[e1].dot2 == g->edges[e2].dot2);
2119 d = g->edges[e1].dot2 - g->dots;
2122 if (state->lines[e1] == LINE_UNKNOWN &&
2123 state->lines[e2] == LINE_UNKNOWN) {
2124 for (k = 0; k < g->dots[d].order; k++) {
2125 int e = g->dots[d].edges[k] - g->edges;
2126 if (state->lines[e] == LINE_YES)
2127 goto found; /* multi-level break */
2135 * If we get here, we've found such a pair of edges, and
2136 * they're e1 and e2.
2138 for (j = 0; j < f->order; j++) {
2139 e = f->edges[j] - g->edges;
2140 if (state->lines[e] == LINE_UNKNOWN && e != e1 && e != e2) {
2141 int r = solver_set_line(sstate, e, LINE_YES);
2143 diff = min(diff, DIFF_EASY);
2149 check_caches(sstate);
2151 /* Per-dot deductions */
2152 for (i = 0; i < g->num_dots; i++) {
2153 grid_dot *d = g->dots + i;
2154 int yes, no, unknown;
2156 if (sstate->dot_solved[i])
2159 yes = sstate->dot_yes_count[i];
2160 no = sstate->dot_no_count[i];
2161 unknown = d->order - yes - no;
2165 sstate->dot_solved[i] = TRUE;
2166 } else if (unknown == 1) {
2167 dot_setall(sstate, i, LINE_UNKNOWN, LINE_NO);
2168 diff = min(diff, DIFF_EASY);
2169 sstate->dot_solved[i] = TRUE;
2171 } else if (yes == 1) {
2173 sstate->solver_status = SOLVER_MISTAKE;
2175 } else if (unknown == 1) {
2176 dot_setall(sstate, i, LINE_UNKNOWN, LINE_YES);
2177 diff = min(diff, DIFF_EASY);
2179 } else if (yes == 2) {
2181 dot_setall(sstate, i, LINE_UNKNOWN, LINE_NO);
2182 diff = min(diff, DIFF_EASY);
2184 sstate->dot_solved[i] = TRUE;
2186 sstate->solver_status = SOLVER_MISTAKE;
2191 check_caches(sstate);
2196 static int dline_deductions(solver_state *sstate)
2198 game_state *state = sstate->state;
2199 grid *g = state->game_grid;
2200 char *dlines = sstate->dlines;
2202 int diff = DIFF_MAX;
2204 /* ------ Face deductions ------ */
2206 /* Given a set of dline atmostone/atleastone constraints, need to figure
2207 * out if we can deduce any further info. For more general faces than
2208 * squares, this turns out to be a tricky problem.
2209 * The approach taken here is to define (per face) NxN matrices:
2210 * "maxs" and "mins".
2211 * The entries maxs(j,k) and mins(j,k) define the upper and lower limits
2212 * for the possible number of edges that are YES between positions j and k
2213 * going clockwise around the face. Can think of j and k as marking dots
2214 * around the face (recall the labelling scheme: edge0 joins dot0 to dot1,
2215 * edge1 joins dot1 to dot2 etc).
2216 * Trivially, mins(j,j) = maxs(j,j) = 0, and we don't even bother storing
2217 * these. mins(j,j+1) and maxs(j,j+1) are determined by whether edge{j}
2218 * is YES, NO or UNKNOWN. mins(j,j+2) and maxs(j,j+2) are related to
2219 * the dline atmostone/atleastone status for edges j and j+1.
2221 * Then we calculate the remaining entries recursively. We definitely
2223 * mins(j,k) >= { mins(j,u) + mins(u,k) } for any u between j and k.
2224 * This is because any valid placement of YESs between j and k must give
2225 * a valid placement between j and u, and also between u and k.
2226 * I believe it's sufficient to use just the two values of u:
2227 * j+1 and j+2. Seems to work well in practice - the bounds we compute
2228 * are rigorous, even if they might not be best-possible.
2230 * Once we have maxs and mins calculated, we can make inferences about
2231 * each dline{j,j+1} by looking at the possible complementary edge-counts
2232 * mins(j+2,j) and maxs(j+2,j) and comparing these with the face clue.
2233 * As well as dlines, we can make similar inferences about single edges.
2234 * For example, consider a pentagon with clue 3, and we know at most one
2235 * of (edge0, edge1) is YES, and at most one of (edge2, edge3) is YES.
2236 * We could then deduce edge4 is YES, because maxs(0,4) would be 2, so
2237 * that final edge would have to be YES to make the count up to 3.
2240 /* Much quicker to allocate arrays on the stack than the heap, so
2241 * define the largest possible face size, and base our array allocations
2242 * on that. We check this with an assertion, in case someone decides to
2243 * make a grid which has larger faces than this. Note, this algorithm
2244 * could get quite expensive if there are many large faces. */
2245 #define MAX_FACE_SIZE 12
2247 for (i = 0; i < g->num_faces; i++) {
2248 int maxs[MAX_FACE_SIZE][MAX_FACE_SIZE];
2249 int mins[MAX_FACE_SIZE][MAX_FACE_SIZE];
2250 grid_face *f = g->faces + i;
2253 int clue = state->clues[i];
2254 assert(N <= MAX_FACE_SIZE);
2255 if (sstate->face_solved[i])
2257 if (clue < 0) continue;
2259 /* Calculate the (j,j+1) entries */
2260 for (j = 0; j < N; j++) {
2261 int edge_index = f->edges[j] - g->edges;
2263 enum line_state line1 = state->lines[edge_index];
2264 enum line_state line2;
2268 maxs[j][k] = (line1 == LINE_NO) ? 0 : 1;
2269 mins[j][k] = (line1 == LINE_YES) ? 1 : 0;
2270 /* Calculate the (j,j+2) entries */
2271 dline_index = dline_index_from_face(g, f, k);
2272 edge_index = f->edges[k] - g->edges;
2273 line2 = state->lines[edge_index];
2279 if (line1 == LINE_NO) tmp--;
2280 if (line2 == LINE_NO) tmp--;
2281 if (tmp == 2 && is_atmostone(dlines, dline_index))
2287 if (line1 == LINE_YES) tmp++;
2288 if (line2 == LINE_YES) tmp++;
2289 if (tmp == 0 && is_atleastone(dlines, dline_index))
2294 /* Calculate the (j,j+m) entries for m between 3 and N-1 */
2295 for (m = 3; m < N; m++) {
2296 for (j = 0; j < N; j++) {
2304 maxs[j][k] = maxs[j][u] + maxs[u][k];
2305 mins[j][k] = mins[j][u] + mins[u][k];
2306 tmp = maxs[j][v] + maxs[v][k];
2307 maxs[j][k] = min(maxs[j][k], tmp);
2308 tmp = mins[j][v] + mins[v][k];
2309 mins[j][k] = max(mins[j][k], tmp);
2313 /* See if we can make any deductions */
2314 for (j = 0; j < N; j++) {
2316 grid_edge *e = f->edges[j];
2317 int line_index = e - g->edges;
2320 if (state->lines[line_index] != LINE_UNKNOWN)
2325 /* minimum YESs in the complement of this edge */
2326 if (mins[k][j] > clue) {
2327 sstate->solver_status = SOLVER_MISTAKE;
2330 if (mins[k][j] == clue) {
2331 /* setting this edge to YES would make at least
2332 * (clue+1) edges - contradiction */
2333 solver_set_line(sstate, line_index, LINE_NO);
2334 diff = min(diff, DIFF_EASY);
2336 if (maxs[k][j] < clue - 1) {
2337 sstate->solver_status = SOLVER_MISTAKE;
2340 if (maxs[k][j] == clue - 1) {
2341 /* Only way to satisfy the clue is to set edge{j} as YES */
2342 solver_set_line(sstate, line_index, LINE_YES);
2343 diff = min(diff, DIFF_EASY);
2346 /* More advanced deduction that allows propagation along diagonal
2347 * chains of faces connected by dots, for example, 3-2-...-2-3
2348 * in square grids. */
2349 if (sstate->diff >= DIFF_TRICKY) {
2350 /* Now see if we can make dline deduction for edges{j,j+1} */
2352 if (state->lines[e - g->edges] != LINE_UNKNOWN)
2353 /* Only worth doing this for an UNKNOWN,UNKNOWN pair.
2354 * Dlines where one of the edges is known, are handled in the
2358 dline_index = dline_index_from_face(g, f, k);
2362 /* minimum YESs in the complement of this dline */
2363 if (mins[k][j] > clue - 2) {
2364 /* Adding 2 YESs would break the clue */
2365 if (set_atmostone(dlines, dline_index))
2366 diff = min(diff, DIFF_NORMAL);
2368 /* maximum YESs in the complement of this dline */
2369 if (maxs[k][j] < clue) {
2370 /* Adding 2 NOs would mean not enough YESs */
2371 if (set_atleastone(dlines, dline_index))
2372 diff = min(diff, DIFF_NORMAL);
2378 if (diff < DIFF_NORMAL)
2381 /* ------ Dot deductions ------ */
2383 for (i = 0; i < g->num_dots; i++) {
2384 grid_dot *d = g->dots + i;
2386 int yes, no, unknown;
2388 if (sstate->dot_solved[i])
2390 yes = sstate->dot_yes_count[i];
2391 no = sstate->dot_no_count[i];
2392 unknown = N - yes - no;
2394 for (j = 0; j < N; j++) {
2397 int line1_index, line2_index;
2398 enum line_state line1, line2;
2401 dline_index = dline_index_from_dot(g, d, j);
2402 line1_index = d->edges[j] - g->edges;
2403 line2_index = d->edges[k] - g->edges;
2404 line1 = state->lines[line1_index];
2405 line2 = state->lines[line2_index];
2407 /* Infer dline state from line state */
2408 if (line1 == LINE_NO || line2 == LINE_NO) {
2409 if (set_atmostone(dlines, dline_index))
2410 diff = min(diff, DIFF_NORMAL);
2412 if (line1 == LINE_YES || line2 == LINE_YES) {
2413 if (set_atleastone(dlines, dline_index))
2414 diff = min(diff, DIFF_NORMAL);
2416 /* Infer line state from dline state */
2417 if (is_atmostone(dlines, dline_index)) {
2418 if (line1 == LINE_YES && line2 == LINE_UNKNOWN) {
2419 solver_set_line(sstate, line2_index, LINE_NO);
2420 diff = min(diff, DIFF_EASY);
2422 if (line2 == LINE_YES && line1 == LINE_UNKNOWN) {
2423 solver_set_line(sstate, line1_index, LINE_NO);
2424 diff = min(diff, DIFF_EASY);
2427 if (is_atleastone(dlines, dline_index)) {
2428 if (line1 == LINE_NO && line2 == LINE_UNKNOWN) {
2429 solver_set_line(sstate, line2_index, LINE_YES);
2430 diff = min(diff, DIFF_EASY);
2432 if (line2 == LINE_NO && line1 == LINE_UNKNOWN) {
2433 solver_set_line(sstate, line1_index, LINE_YES);
2434 diff = min(diff, DIFF_EASY);
2437 /* Deductions that depend on the numbers of lines.
2438 * Only bother if both lines are UNKNOWN, otherwise the
2439 * easy-mode solver (or deductions above) would have taken
2441 if (line1 != LINE_UNKNOWN || line2 != LINE_UNKNOWN)
2444 if (yes == 0 && unknown == 2) {
2445 /* Both these unknowns must be identical. If we know
2446 * atmostone or atleastone, we can make progress. */
2447 if (is_atmostone(dlines, dline_index)) {
2448 solver_set_line(sstate, line1_index, LINE_NO);
2449 solver_set_line(sstate, line2_index, LINE_NO);
2450 diff = min(diff, DIFF_EASY);
2452 if (is_atleastone(dlines, dline_index)) {
2453 solver_set_line(sstate, line1_index, LINE_YES);
2454 solver_set_line(sstate, line2_index, LINE_YES);
2455 diff = min(diff, DIFF_EASY);
2459 if (set_atmostone(dlines, dline_index))
2460 diff = min(diff, DIFF_NORMAL);
2462 if (set_atleastone(dlines, dline_index))
2463 diff = min(diff, DIFF_NORMAL);
2467 /* More advanced deduction that allows propagation along diagonal
2468 * chains of faces connected by dots, for example: 3-2-...-2-3
2469 * in square grids. */
2470 if (sstate->diff >= DIFF_TRICKY) {
2471 /* If we have atleastone set for this dline, infer
2472 * atmostone for each "opposite" dline (that is, each
2473 * dline without edges in common with this one).
2474 * Again, this test is only worth doing if both these
2475 * lines are UNKNOWN. For if one of these lines were YES,
2476 * the (yes == 1) test above would kick in instead. */
2477 if (is_atleastone(dlines, dline_index)) {
2479 for (opp = 0; opp < N; opp++) {
2480 int opp_dline_index;
2481 if (opp == j || opp == j+1 || opp == j-1)
2483 if (j == 0 && opp == N-1)
2485 if (j == N-1 && opp == 0)
2487 opp_dline_index = dline_index_from_dot(g, d, opp);
2488 if (set_atmostone(dlines, opp_dline_index))
2489 diff = min(diff, DIFF_NORMAL);
2491 if (yes == 0 && is_atmostone(dlines, dline_index)) {
2492 /* This dline has *exactly* one YES and there are no
2493 * other YESs. This allows more deductions. */
2495 /* Third unknown must be YES */
2496 for (opp = 0; opp < N; opp++) {
2498 if (opp == j || opp == k)
2500 opp_index = d->edges[opp] - g->edges;
2501 if (state->lines[opp_index] == LINE_UNKNOWN) {
2502 solver_set_line(sstate, opp_index,
2504 diff = min(diff, DIFF_EASY);
2507 } else if (unknown == 4) {
2508 /* Exactly one of opposite UNKNOWNS is YES. We've
2509 * already set atmostone, so set atleastone as
2512 if (dline_set_opp_atleastone(sstate, d, j))
2513 diff = min(diff, DIFF_NORMAL);
2523 static int linedsf_deductions(solver_state *sstate)
2525 game_state *state = sstate->state;
2526 grid *g = state->game_grid;
2527 char *dlines = sstate->dlines;
2529 int diff = DIFF_MAX;
2532 /* ------ Face deductions ------ */
2534 /* A fully-general linedsf deduction seems overly complicated
2535 * (I suspect the problem is NP-complete, though in practice it might just
2536 * be doable because faces are limited in size).
2537 * For simplicity, we only consider *pairs* of LINE_UNKNOWNS that are
2538 * known to be identical. If setting them both to YES (or NO) would break
2539 * the clue, set them to NO (or YES). */
2541 for (i = 0; i < g->num_faces; i++) {
2542 int N, yes, no, unknown;
2545 if (sstate->face_solved[i])
2547 clue = state->clues[i];
2551 N = g->faces[i].order;
2552 yes = sstate->face_yes_count[i];
2553 if (yes + 1 == clue) {
2554 if (face_setall_identical(sstate, i, LINE_NO))
2555 diff = min(diff, DIFF_EASY);
2557 no = sstate->face_no_count[i];
2558 if (no + 1 == N - clue) {
2559 if (face_setall_identical(sstate, i, LINE_YES))
2560 diff = min(diff, DIFF_EASY);
2563 /* Reload YES count, it might have changed */
2564 yes = sstate->face_yes_count[i];
2565 unknown = N - no - yes;
2567 /* Deductions with small number of LINE_UNKNOWNs, based on overall
2568 * parity of lines. */
2569 diff_tmp = parity_deductions(sstate, g->faces[i].edges,
2570 (clue - yes) % 2, unknown);
2571 diff = min(diff, diff_tmp);
2574 /* ------ Dot deductions ------ */
2575 for (i = 0; i < g->num_dots; i++) {
2576 grid_dot *d = g->dots + i;
2579 int yes, no, unknown;
2580 /* Go through dlines, and do any dline<->linedsf deductions wherever
2581 * we find two UNKNOWNS. */
2582 for (j = 0; j < N; j++) {
2583 int dline_index = dline_index_from_dot(g, d, j);
2586 int can1, can2, inv1, inv2;
2588 line1_index = d->edges[j] - g->edges;
2589 if (state->lines[line1_index] != LINE_UNKNOWN)
2592 if (j2 == N) j2 = 0;
2593 line2_index = d->edges[j2] - g->edges;
2594 if (state->lines[line2_index] != LINE_UNKNOWN)
2596 /* Infer dline flags from linedsf */
2597 can1 = edsf_canonify(sstate->linedsf, line1_index, &inv1);
2598 can2 = edsf_canonify(sstate->linedsf, line2_index, &inv2);
2599 if (can1 == can2 && inv1 != inv2) {
2600 /* These are opposites, so set dline atmostone/atleastone */
2601 if (set_atmostone(dlines, dline_index))
2602 diff = min(diff, DIFF_NORMAL);
2603 if (set_atleastone(dlines, dline_index))
2604 diff = min(diff, DIFF_NORMAL);
2607 /* Infer linedsf from dline flags */
2608 if (is_atmostone(dlines, dline_index)
2609 && is_atleastone(dlines, dline_index)) {
2610 if (merge_lines(sstate, line1_index, line2_index, 1))
2611 diff = min(diff, DIFF_HARD);
2615 /* Deductions with small number of LINE_UNKNOWNs, based on overall
2616 * parity of lines. */
2617 yes = sstate->dot_yes_count[i];
2618 no = sstate->dot_no_count[i];
2619 unknown = N - yes - no;
2620 diff_tmp = parity_deductions(sstate, d->edges,
2622 diff = min(diff, diff_tmp);
2625 /* ------ Edge dsf deductions ------ */
2627 /* If the state of a line is known, deduce the state of its canonical line
2628 * too, and vice versa. */
2629 for (i = 0; i < g->num_edges; i++) {
2632 can = edsf_canonify(sstate->linedsf, i, &inv);
2635 s = sstate->state->lines[can];
2636 if (s != LINE_UNKNOWN) {
2637 if (solver_set_line(sstate, i, inv ? OPP(s) : s))
2638 diff = min(diff, DIFF_EASY);
2640 s = sstate->state->lines[i];
2641 if (s != LINE_UNKNOWN) {
2642 if (solver_set_line(sstate, can, inv ? OPP(s) : s))
2643 diff = min(diff, DIFF_EASY);
2651 static int loop_deductions(solver_state *sstate)
2653 int edgecount = 0, clues = 0, satclues = 0, sm1clues = 0;
2654 game_state *state = sstate->state;
2655 grid *g = state->game_grid;
2656 int shortest_chainlen = g->num_dots;
2657 int loop_found = FALSE;
2659 int progress = FALSE;
2663 * Go through the grid and update for all the new edges.
2664 * Since merge_dots() is idempotent, the simplest way to
2665 * do this is just to update for _all_ the edges.
2666 * Also, while we're here, we count the edges.
2668 for (i = 0; i < g->num_edges; i++) {
2669 if (state->lines[i] == LINE_YES) {
2670 loop_found |= merge_dots(sstate, i);
2676 * Count the clues, count the satisfied clues, and count the
2677 * satisfied-minus-one clues.
2679 for (i = 0; i < g->num_faces; i++) {
2680 int c = state->clues[i];
2682 int o = sstate->face_yes_count[i];
2691 for (i = 0; i < g->num_dots; ++i) {
2693 sstate->looplen[dsf_canonify(sstate->dotdsf, i)];
2694 if (dots_connected > 1)
2695 shortest_chainlen = min(shortest_chainlen, dots_connected);
2698 assert(sstate->solver_status == SOLVER_INCOMPLETE);
2700 if (satclues == clues && shortest_chainlen == edgecount) {
2701 sstate->solver_status = SOLVER_SOLVED;
2702 /* This discovery clearly counts as progress, even if we haven't
2703 * just added any lines or anything */
2705 goto finished_loop_deductionsing;
2709 * Now go through looking for LINE_UNKNOWN edges which
2710 * connect two dots that are already in the same
2711 * equivalence class. If we find one, test to see if the
2712 * loop it would create is a solution.
2714 for (i = 0; i < g->num_edges; i++) {
2715 grid_edge *e = g->edges + i;
2716 int d1 = e->dot1 - g->dots;
2717 int d2 = e->dot2 - g->dots;
2719 if (state->lines[i] != LINE_UNKNOWN)
2722 eqclass = dsf_canonify(sstate->dotdsf, d1);
2723 if (eqclass != dsf_canonify(sstate->dotdsf, d2))
2726 val = LINE_NO; /* loop is bad until proven otherwise */
2729 * This edge would form a loop. Next
2730 * question: how long would the loop be?
2731 * Would it equal the total number of edges
2732 * (plus the one we'd be adding if we added
2735 if (sstate->looplen[eqclass] == edgecount + 1) {
2739 * This edge would form a loop which
2740 * took in all the edges in the entire
2741 * grid. So now we need to work out
2742 * whether it would be a valid solution
2743 * to the puzzle, which means we have to
2744 * check if it satisfies all the clues.
2745 * This means that every clue must be
2746 * either satisfied or satisfied-minus-
2747 * 1, and also that the number of
2748 * satisfied-minus-1 clues must be at
2749 * most two and they must lie on either
2750 * side of this edge.
2754 int f = e->face1 - g->faces;
2755 int c = state->clues[f];
2756 if (c >= 0 && sstate->face_yes_count[f] == c - 1)
2760 int f = e->face2 - g->faces;
2761 int c = state->clues[f];
2762 if (c >= 0 && sstate->face_yes_count[f] == c - 1)
2765 if (sm1clues == sm1_nearby &&
2766 sm1clues + satclues == clues) {
2767 val = LINE_YES; /* loop is good! */
2772 * Right. Now we know that adding this edge
2773 * would form a loop, and we know whether
2774 * that loop would be a viable solution or
2777 * If adding this edge produces a solution,
2778 * then we know we've found _a_ solution but
2779 * we don't know that it's _the_ solution -
2780 * if it were provably the solution then
2781 * we'd have deduced this edge some time ago
2782 * without the need to do loop detection. So
2783 * in this state we return SOLVER_AMBIGUOUS,
2784 * which has the effect that hitting Solve
2785 * on a user-provided puzzle will fill in a
2786 * solution but using the solver to
2787 * construct new puzzles won't consider this
2788 * a reasonable deduction for the user to
2791 progress = solver_set_line(sstate, i, val);
2792 assert(progress == TRUE);
2793 if (val == LINE_YES) {
2794 sstate->solver_status = SOLVER_AMBIGUOUS;
2795 goto finished_loop_deductionsing;
2799 finished_loop_deductionsing:
2800 return progress ? DIFF_EASY : DIFF_MAX;
2803 /* This will return a dynamically allocated solver_state containing the (more)
2805 static solver_state *solve_game_rec(const solver_state *sstate_start)
2807 solver_state *sstate;
2809 /* Index of the solver we should call next. */
2812 /* As a speed-optimisation, we avoid re-running solvers that we know
2813 * won't make any progress. This happens when a high-difficulty
2814 * solver makes a deduction that can only help other high-difficulty
2816 * For example: if a new 'dline' flag is set by dline_deductions, the
2817 * trivial_deductions solver cannot do anything with this information.
2818 * If we've already run the trivial_deductions solver (because it's
2819 * earlier in the list), there's no point running it again.
2821 * Therefore: if a solver is earlier in the list than "threshold_index",
2822 * we don't bother running it if it's difficulty level is less than
2825 int threshold_diff = 0;
2826 int threshold_index = 0;
2828 sstate = dup_solver_state(sstate_start);
2830 check_caches(sstate);
2832 while (i < NUM_SOLVERS) {
2833 if (sstate->solver_status == SOLVER_MISTAKE)
2835 if (sstate->solver_status == SOLVER_SOLVED ||
2836 sstate->solver_status == SOLVER_AMBIGUOUS) {
2837 /* solver finished */
2841 if ((solver_diffs[i] >= threshold_diff || i >= threshold_index)
2842 && solver_diffs[i] <= sstate->diff) {
2843 /* current_solver is eligible, so use it */
2844 int next_diff = solver_fns[i](sstate);
2845 if (next_diff != DIFF_MAX) {
2846 /* solver made progress, so use new thresholds and
2847 * start again at top of list. */
2848 threshold_diff = next_diff;
2849 threshold_index = i;
2854 /* current_solver is ineligible, or failed to make progress, so
2855 * go to the next solver in the list */
2859 if (sstate->solver_status == SOLVER_SOLVED ||
2860 sstate->solver_status == SOLVER_AMBIGUOUS) {
2861 /* s/LINE_UNKNOWN/LINE_NO/g */
2862 array_setall(sstate->state->lines, LINE_UNKNOWN, LINE_NO,
2863 sstate->state->game_grid->num_edges);
2870 static char *solve_game(const game_state *state, const game_state *currstate,
2871 const char *aux, char **error)
2874 solver_state *sstate, *new_sstate;
2876 sstate = new_solver_state(state, DIFF_MAX);
2877 new_sstate = solve_game_rec(sstate);
2879 if (new_sstate->solver_status == SOLVER_SOLVED) {
2880 soln = encode_solve_move(new_sstate->state);
2881 } else if (new_sstate->solver_status == SOLVER_AMBIGUOUS) {
2882 soln = encode_solve_move(new_sstate->state);
2883 /**error = "Solver found ambiguous solutions"; */
2885 soln = encode_solve_move(new_sstate->state);
2886 /**error = "Solver failed"; */
2889 free_solver_state(new_sstate);
2890 free_solver_state(sstate);
2895 /* ----------------------------------------------------------------------
2896 * Drawing and mouse-handling
2899 static char *interpret_move(const game_state *state, game_ui *ui,
2900 const game_drawstate *ds,
2901 int x, int y, int button)
2903 grid *g = state->game_grid;
2907 char button_char = ' ';
2908 enum line_state old_state;
2910 button &= ~MOD_MASK;
2912 /* Convert mouse-click (x,y) to grid coordinates */
2913 x -= BORDER(ds->tilesize);
2914 y -= BORDER(ds->tilesize);
2915 x = x * g->tilesize / ds->tilesize;
2916 y = y * g->tilesize / ds->tilesize;
2920 e = grid_nearest_edge(g, x, y);
2926 /* I think it's only possible to play this game with mouse clicks, sorry */
2927 /* Maybe will add mouse drag support some time */
2928 old_state = state->lines[i];
2932 switch (old_state) {
2950 switch (old_state) {
2969 sprintf(buf, "%d%c", i, (int)button_char);
2975 static game_state *execute_move(const game_state *state, const char *move)
2978 game_state *newstate = dup_game(state);
2980 if (move[0] == 'S') {
2982 newstate->cheated = TRUE;
2987 if (i < 0 || i >= newstate->game_grid->num_edges)
2989 move += strspn(move, "1234567890");
2990 switch (*(move++)) {
2992 newstate->lines[i] = LINE_YES;
2995 newstate->lines[i] = LINE_NO;
2998 newstate->lines[i] = LINE_UNKNOWN;
3006 * Check for completion.
3008 if (check_completion(newstate))
3009 newstate->solved = TRUE;
3014 free_game(newstate);
3018 /* ----------------------------------------------------------------------
3022 /* Convert from grid coordinates to screen coordinates */
3023 static void grid_to_screen(const game_drawstate *ds, const grid *g,
3024 int grid_x, int grid_y, int *x, int *y)
3026 *x = grid_x - g->lowest_x;
3027 *y = grid_y - g->lowest_y;
3028 *x = *x * ds->tilesize / g->tilesize;
3029 *y = *y * ds->tilesize / g->tilesize;
3030 *x += BORDER(ds->tilesize);
3031 *y += BORDER(ds->tilesize);
3034 /* Returns (into x,y) position of centre of face for rendering the text clue.
3036 static void face_text_pos(const game_drawstate *ds, const grid *g,
3037 grid_face *f, int *xret, int *yret)
3039 int faceindex = f - g->faces;
3042 * Return the cached position for this face, if we've already
3045 if (ds->textx[faceindex] >= 0) {
3046 *xret = ds->textx[faceindex];
3047 *yret = ds->texty[faceindex];
3052 * Otherwise, use the incentre computed by grid.c and convert it
3053 * to screen coordinates.
3055 grid_find_incentre(f);
3056 grid_to_screen(ds, g, f->ix, f->iy,
3057 &ds->textx[faceindex], &ds->texty[faceindex]);
3059 *xret = ds->textx[faceindex];
3060 *yret = ds->texty[faceindex];
3063 static void face_text_bbox(game_drawstate *ds, grid *g, grid_face *f,
3064 int *x, int *y, int *w, int *h)
3067 face_text_pos(ds, g, f, &xx, &yy);
3069 /* There seems to be a certain amount of trial-and-error involved
3070 * in working out the correct bounding-box for the text. */
3072 *x = xx - ds->tilesize/4 - 1;
3073 *y = yy - ds->tilesize/4 - 3;
3074 *w = ds->tilesize/2 + 2;
3075 *h = ds->tilesize/2 + 5;
3078 static void game_redraw_clue(drawing *dr, game_drawstate *ds,
3079 const game_state *state, int i)
3081 grid *g = state->game_grid;
3082 grid_face *f = g->faces + i;
3086 sprintf(c, "%d", state->clues[i]);
3088 face_text_pos(ds, g, f, &x, &y);
3090 FONT_VARIABLE, ds->tilesize/2,
3091 ALIGN_VCENTRE | ALIGN_HCENTRE,
3092 ds->clue_error[i] ? COL_MISTAKE :
3093 ds->clue_satisfied[i] ? COL_SATISFIED : COL_FOREGROUND, c);
3096 static void edge_bbox(game_drawstate *ds, grid *g, grid_edge *e,
3097 int *x, int *y, int *w, int *h)
3099 int x1 = e->dot1->x;
3100 int y1 = e->dot1->y;
3101 int x2 = e->dot2->x;
3102 int y2 = e->dot2->y;
3103 int xmin, xmax, ymin, ymax;
3105 grid_to_screen(ds, g, x1, y1, &x1, &y1);
3106 grid_to_screen(ds, g, x2, y2, &x2, &y2);
3107 /* Allow extra margin for dots, and thickness of lines */
3108 xmin = min(x1, x2) - 2;
3109 xmax = max(x1, x2) + 2;
3110 ymin = min(y1, y2) - 2;
3111 ymax = max(y1, y2) + 2;
3115 *w = xmax - xmin + 1;
3116 *h = ymax - ymin + 1;
3119 static void dot_bbox(game_drawstate *ds, grid *g, grid_dot *d,
3120 int *x, int *y, int *w, int *h)
3124 grid_to_screen(ds, g, d->x, d->y, &x1, &y1);
3132 static const int loopy_line_redraw_phases[] = {
3133 COL_FAINT, COL_LINEUNKNOWN, COL_FOREGROUND, COL_HIGHLIGHT, COL_MISTAKE
3135 #define NPHASES lenof(loopy_line_redraw_phases)
3137 static void game_redraw_line(drawing *dr, game_drawstate *ds,
3138 const game_state *state, int i, int phase)
3140 grid *g = state->game_grid;
3141 grid_edge *e = g->edges + i;
3145 if (state->line_errors[i])
3146 line_colour = COL_MISTAKE;
3147 else if (state->lines[i] == LINE_UNKNOWN)
3148 line_colour = COL_LINEUNKNOWN;
3149 else if (state->lines[i] == LINE_NO)
3150 line_colour = COL_FAINT;
3151 else if (ds->flashing)
3152 line_colour = COL_HIGHLIGHT;
3154 line_colour = COL_FOREGROUND;
3155 if (line_colour != loopy_line_redraw_phases[phase])
3158 /* Convert from grid to screen coordinates */
3159 grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1);
3160 grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2);
3162 if (line_colour == COL_FAINT) {
3163 static int draw_faint_lines = -1;
3164 if (draw_faint_lines < 0) {
3165 char *env = getenv("LOOPY_FAINT_LINES");
3166 draw_faint_lines = (!env || (env[0] == 'y' ||
3169 if (draw_faint_lines)
3170 draw_line(dr, x1, y1, x2, y2, line_colour);
3172 draw_thick_line(dr, 3.0,
3179 static void game_redraw_dot(drawing *dr, game_drawstate *ds,
3180 const game_state *state, int i)
3182 grid *g = state->game_grid;
3183 grid_dot *d = g->dots + i;
3186 grid_to_screen(ds, g, d->x, d->y, &x, &y);
3187 draw_circle(dr, x, y, 2, COL_FOREGROUND, COL_FOREGROUND);
3190 static int boxes_intersect(int x0, int y0, int w0, int h0,
3191 int x1, int y1, int w1, int h1)
3194 * Two intervals intersect iff neither is wholly on one side of
3195 * the other. Two boxes intersect iff their horizontal and
3196 * vertical intervals both intersect.
3198 return (x0 < x1+w1 && x1 < x0+w0 && y0 < y1+h1 && y1 < y0+h0);
3201 static void game_redraw_in_rect(drawing *dr, game_drawstate *ds,
3202 const game_state *state,
3203 int x, int y, int w, int h)
3205 grid *g = state->game_grid;
3209 clip(dr, x, y, w, h);
3210 draw_rect(dr, x, y, w, h, COL_BACKGROUND);
3212 for (i = 0; i < g->num_faces; i++) {
3213 if (state->clues[i] >= 0) {
3214 face_text_bbox(ds, g, &g->faces[i], &bx, &by, &bw, &bh);
3215 if (boxes_intersect(x, y, w, h, bx, by, bw, bh))
3216 game_redraw_clue(dr, ds, state, i);
3219 for (phase = 0; phase < NPHASES; phase++) {
3220 for (i = 0; i < g->num_edges; i++) {
3221 edge_bbox(ds, g, &g->edges[i], &bx, &by, &bw, &bh);
3222 if (boxes_intersect(x, y, w, h, bx, by, bw, bh))
3223 game_redraw_line(dr, ds, state, i, phase);
3226 for (i = 0; i < g->num_dots; i++) {
3227 dot_bbox(ds, g, &g->dots[i], &bx, &by, &bw, &bh);
3228 if (boxes_intersect(x, y, w, h, bx, by, bw, bh))
3229 game_redraw_dot(dr, ds, state, i);
3233 draw_update(dr, x, y, w, h);
3236 static void game_redraw(drawing *dr, game_drawstate *ds,
3237 const game_state *oldstate, const game_state *state,
3238 int dir, const game_ui *ui,
3239 float animtime, float flashtime)
3241 #define REDRAW_OBJECTS_LIMIT 16 /* Somewhat arbitrary tradeoff */
3243 grid *g = state->game_grid;
3244 int border = BORDER(ds->tilesize);
3247 int redraw_everything = FALSE;
3249 int edges[REDRAW_OBJECTS_LIMIT], nedges = 0;
3250 int faces[REDRAW_OBJECTS_LIMIT], nfaces = 0;
3252 /* Redrawing is somewhat involved.
3254 * An update can theoretically affect an arbitrary number of edges
3255 * (consider, for example, completing or breaking a cycle which doesn't
3256 * satisfy all the clues -- we'll switch many edges between error and
3257 * normal states). On the other hand, redrawing the whole grid takes a
3258 * while, making the game feel sluggish, and many updates are actually
3259 * quite well localized.
3261 * This redraw algorithm attempts to cope with both situations gracefully
3262 * and correctly. For localized changes, we set a clip rectangle, fill
3263 * it with background, and then redraw (a plausible but conservative
3264 * guess at) the objects which intersect the rectangle; if several
3265 * objects need redrawing, we'll do them individually. However, if lots
3266 * of objects are affected, we'll just redraw everything.
3268 * The reason for all of this is that it's just not safe to do the redraw
3269 * piecemeal. If you try to draw an antialiased diagonal line over
3270 * itself, you get a slightly thicker antialiased diagonal line, which
3271 * looks rather ugly after a while.
3273 * So, we take two passes over the grid. The first attempts to work out
3274 * what needs doing, and the second actually does it.
3278 redraw_everything = TRUE;
3280 * But we must still go through the upcoming loops, so that we
3281 * set up stuff in ds correctly for the initial redraw.
3285 /* First, trundle through the faces. */
3286 for (i = 0; i < g->num_faces; i++) {
3287 grid_face *f = g->faces + i;
3288 int sides = f->order;
3289 int yes_order, no_order;
3292 int n = state->clues[i];
3296 yes_order = face_order(state, i, LINE_YES);
3297 if (state->exactly_one_loop) {
3299 * Special case: if the set of LINE_YES edges in the grid
3300 * consists of exactly one loop and nothing else, then we
3301 * switch to treating LINE_UNKNOWN the same as LINE_NO for
3302 * purposes of clue checking.
3304 * This is because some people like to play Loopy without
3305 * using the right-click, i.e. never setting anything to
3306 * LINE_NO. Without this special case, if a person playing
3307 * in that style fills in what they think is a correct
3308 * solution loop but in fact it has an underfilled clue,
3309 * then we will display no victory flash and also no error
3310 * highlight explaining why not. With this special case,
3311 * we light up underfilled clues at the instant the loop
3312 * is closed. (Of course, *overfilled* clues are fine
3315 * (It might still be considered unfortunate that we can't
3316 * warn this style of player any earlier, if they make a
3317 * mistake very near the beginning which doesn't show up
3318 * until they close the last edge of the loop. One other
3319 * thing we _could_ do here is to treat any LINE_UNKNOWN
3320 * as LINE_NO if either of its endpoints has yes-degree 2,
3321 * reflecting the fact that setting that line to YES would
3322 * be an obvious error. But I don't think even that could
3323 * catch _all_ clue errors in a timely manner; I think
3324 * there are some that won't be displayed until the loop
3325 * is filled in, even so, and there's no way to avoid that
3326 * with complete reliability except to switch to being a
3327 * player who sets things to LINE_NO.)
3329 no_order = sides - yes_order;
3331 no_order = face_order(state, i, LINE_NO);
3334 clue_mistake = (yes_order > n || no_order > (sides-n));
3335 clue_satisfied = (yes_order == n && no_order == (sides-n));
3337 if (clue_mistake != ds->clue_error[i] ||
3338 clue_satisfied != ds->clue_satisfied[i]) {
3339 ds->clue_error[i] = clue_mistake;
3340 ds->clue_satisfied[i] = clue_satisfied;
3341 if (nfaces == REDRAW_OBJECTS_LIMIT)
3342 redraw_everything = TRUE;
3344 faces[nfaces++] = i;
3348 /* Work out what the flash state needs to be. */
3349 if (flashtime > 0 &&
3350 (flashtime <= FLASH_TIME/3 ||
3351 flashtime >= FLASH_TIME*2/3)) {
3352 flash_changed = !ds->flashing;
3353 ds->flashing = TRUE;
3355 flash_changed = ds->flashing;
3356 ds->flashing = FALSE;
3359 /* Now, trundle through the edges. */
3360 for (i = 0; i < g->num_edges; i++) {
3362 state->line_errors[i] ? DS_LINE_ERROR : state->lines[i];
3363 if (new_ds != ds->lines[i] ||
3364 (flash_changed && state->lines[i] == LINE_YES)) {
3365 ds->lines[i] = new_ds;
3366 if (nedges == REDRAW_OBJECTS_LIMIT)
3367 redraw_everything = TRUE;
3369 edges[nedges++] = i;
3373 /* Pass one is now done. Now we do the actual drawing. */
3374 if (redraw_everything) {
3375 int grid_width = g->highest_x - g->lowest_x;
3376 int grid_height = g->highest_y - g->lowest_y;
3377 int w = grid_width * ds->tilesize / g->tilesize;
3378 int h = grid_height * ds->tilesize / g->tilesize;
3380 game_redraw_in_rect(dr, ds, state,
3381 0, 0, w + 2*border + 1, h + 2*border + 1);
3384 /* Right. Now we roll up our sleeves. */
3386 for (i = 0; i < nfaces; i++) {
3387 grid_face *f = g->faces + faces[i];
3390 face_text_bbox(ds, g, f, &x, &y, &w, &h);
3391 game_redraw_in_rect(dr, ds, state, x, y, w, h);
3394 for (i = 0; i < nedges; i++) {
3395 grid_edge *e = g->edges + edges[i];
3398 edge_bbox(ds, g, e, &x, &y, &w, &h);
3399 game_redraw_in_rect(dr, ds, state, x, y, w, h);
3406 static float game_flash_length(const game_state *oldstate,
3407 const game_state *newstate, int dir, game_ui *ui)
3409 if (!oldstate->solved && newstate->solved &&
3410 !oldstate->cheated && !newstate->cheated) {
3417 static int game_status(const game_state *state)
3419 return state->solved ? +1 : 0;
3422 static void game_print_size(const game_params *params, float *x, float *y)
3427 * I'll use 7mm "squares" by default.
3429 game_compute_size(params, 700, &pw, &ph);
3434 static void game_print(drawing *dr, const game_state *state, int tilesize)
3436 int ink = print_mono_colour(dr, 0);
3438 game_drawstate ads, *ds = &ads;
3439 grid *g = state->game_grid;
3441 ds->tilesize = tilesize;
3442 ds->textx = snewn(g->num_faces, int);
3443 ds->texty = snewn(g->num_faces, int);
3444 for (i = 0; i < g->num_faces; i++)
3445 ds->textx[i] = ds->texty[i] = -1;
3447 for (i = 0; i < g->num_dots; i++) {
3449 grid_to_screen(ds, g, g->dots[i].x, g->dots[i].y, &x, &y);
3450 draw_circle(dr, x, y, ds->tilesize / 15, ink, ink);
3456 for (i = 0; i < g->num_faces; i++) {
3457 grid_face *f = g->faces + i;
3458 int clue = state->clues[i];
3462 sprintf(c, "%d", state->clues[i]);
3463 face_text_pos(ds, g, f, &x, &y);
3465 FONT_VARIABLE, ds->tilesize / 2,
3466 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, c);
3473 for (i = 0; i < g->num_edges; i++) {
3474 int thickness = (state->lines[i] == LINE_YES) ? 30 : 150;
3475 grid_edge *e = g->edges + i;
3477 grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1);
3478 grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2);
3479 if (state->lines[i] == LINE_YES)
3481 /* (dx, dy) points from (x1, y1) to (x2, y2).
3482 * The line is then "fattened" in a perpendicular
3483 * direction to create a thin rectangle. */
3484 double d = sqrt(SQ((double)x1 - x2) + SQ((double)y1 - y2));
3485 double dx = (x2 - x1) / d;
3486 double dy = (y2 - y1) / d;
3489 dx = (dx * ds->tilesize) / thickness;
3490 dy = (dy * ds->tilesize) / thickness;
3491 points[0] = x1 + (int)dy;
3492 points[1] = y1 - (int)dx;
3493 points[2] = x1 - (int)dy;
3494 points[3] = y1 + (int)dx;
3495 points[4] = x2 - (int)dy;
3496 points[5] = y2 + (int)dx;
3497 points[6] = x2 + (int)dy;
3498 points[7] = y2 - (int)dx;
3499 draw_polygon(dr, points, 4, ink, ink);
3503 /* Draw a dotted line */
3506 for (j = 1; j < divisions; j++) {
3507 /* Weighted average */
3508 int x = (x1 * (divisions -j) + x2 * j) / divisions;
3509 int y = (y1 * (divisions -j) + y2 * j) / divisions;
3510 draw_circle(dr, x, y, ds->tilesize / thickness, ink, ink);
3520 #define thegame loopy
3523 const struct game thegame = {
3524 "Loopy", "games.loopy", "loopy",
3531 TRUE, game_configure, custom_params,
3539 TRUE, game_can_format_as_text_now, game_text_format,
3547 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
3550 game_free_drawstate,
3555 TRUE, FALSE, game_print_size, game_print,
3556 FALSE /* wants_statusbar */,
3557 FALSE, game_timing_state,
3558 0, /* mouse_priorities */
3561 #ifdef STANDALONE_SOLVER
3564 * Half-hearted standalone solver. It can't output the solution to
3565 * anything but a square puzzle, and it can't log the deductions
3566 * it makes either. But it can solve square puzzles, and more
3567 * importantly it can use its solver to grade the difficulty of
3568 * any puzzle you give it.
3573 int main(int argc, char **argv)
3577 char *id = NULL, *desc, *err;
3580 #if 0 /* verbose solver not supported here (yet) */
3581 int really_verbose = FALSE;
3584 while (--argc > 0) {
3586 #if 0 /* verbose solver not supported here (yet) */
3587 if (!strcmp(p, "-v")) {
3588 really_verbose = TRUE;
3591 if (!strcmp(p, "-g")) {
3593 } else if (*p == '-') {
3594 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
3602 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
3606 desc = strchr(id, ':');
3608 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
3613 p = default_params();
3614 decode_params(p, id);
3615 err = validate_desc(p, desc);
3617 fprintf(stderr, "%s: %s\n", argv[0], err);
3620 s = new_game(NULL, p, desc);
3623 * When solving an Easy puzzle, we don't want to bother the
3624 * user with Hard-level deductions. For this reason, we grade
3625 * the puzzle internally before doing anything else.
3627 ret = -1; /* placate optimiser */
3628 for (diff = 0; diff < DIFF_MAX; diff++) {
3629 solver_state *sstate_new;
3630 solver_state *sstate = new_solver_state((game_state *)s, diff);
3632 sstate_new = solve_game_rec(sstate);
3634 if (sstate_new->solver_status == SOLVER_MISTAKE)
3636 else if (sstate_new->solver_status == SOLVER_SOLVED)
3641 free_solver_state(sstate_new);
3642 free_solver_state(sstate);
3648 if (diff == DIFF_MAX) {
3650 printf("Difficulty rating: harder than Hard, or ambiguous\n");
3652 printf("Unable to find a unique solution\n");
3656 printf("Difficulty rating: impossible (no solution exists)\n");
3658 printf("Difficulty rating: %s\n", diffnames[diff]);
3660 solver_state *sstate_new;
3661 solver_state *sstate = new_solver_state((game_state *)s, diff);
3663 /* If we supported a verbose solver, we'd set verbosity here */
3665 sstate_new = solve_game_rec(sstate);
3667 if (sstate_new->solver_status == SOLVER_MISTAKE)
3668 printf("Puzzle is inconsistent\n");
3670 assert(sstate_new->solver_status == SOLVER_SOLVED);
3671 if (s->grid_type == 0) {
3672 fputs(game_text_format(sstate_new->state), stdout);
3674 printf("Unable to output non-square grids\n");
3678 free_solver_state(sstate_new);
3679 free_solver_state(sstate);
3688 /* vim: set shiftwidth=4 tabstop=8: */