2 * slant.c: Puzzle from nikoli.co.jp involving drawing a diagonal
3 * line through each square of a grid.
7 * In this puzzle you have a grid of squares, each of which must
8 * contain a diagonal line; you also have clue numbers placed at
9 * _points_ of that grid, which means there's a (w+1) x (h+1) array
10 * of possible clue positions.
12 * I'm therefore going to adopt a rigid convention throughout this
13 * source file of using w and h for the dimensions of the grid of
14 * squares, and W and H for the dimensions of the grid of points.
15 * Thus, W == w+1 and H == h+1 always.
17 * Clue arrays will be W*H `signed char's, and the clue at each
18 * point will be a number from 0 to 4, or -1 if there's no clue.
20 * Solution arrays will be W*H `signed char's, and the number at
21 * each point will be +1 for a forward slash (/), -1 for a
22 * backslash (\), and 0 for unknown.
48 * In standalone solver mode, `verbose' is a variable which can be
49 * set by command-line option; in debugging mode it's simply always
52 #if defined STANDALONE_SOLVER
53 #define SOLVER_DIAGNOSTICS
55 #elif defined SOLVER_DIAGNOSTICS
60 * Difficulty levels. I do some macro ickery here to ensure that my
61 * enum and the various forms of my name list always match up.
66 #define ENUM(upper,title,lower) DIFF_ ## upper,
67 #define TITLE(upper,title,lower) #title,
68 #define ENCODE(upper,title,lower) #lower
69 #define CONFIG(upper,title,lower) ":" #title
70 enum { DIFFLIST(ENUM) DIFFCOUNT };
71 static char const *const slant_diffnames[] = { DIFFLIST(TITLE) };
72 static char const slant_diffchars[] = DIFFLIST(ENCODE);
73 #define DIFFCONFIG DIFFLIST(CONFIG)
79 typedef struct game_clues {
93 unsigned char *errors;
95 int used_solve; /* used to suppress completion flash */
98 static game_params *default_params(void)
100 game_params *ret = snew(game_params);
103 ret->diff = DIFF_EASY;
108 static const struct game_params slant_presets[] = {
117 static int game_fetch_preset(int i, char **name, game_params **params)
122 if (i < 0 || i >= lenof(slant_presets))
125 ret = snew(game_params);
126 *ret = slant_presets[i];
128 sprintf(str, "%dx%d %s", ret->w, ret->h, slant_diffnames[ret->diff]);
135 static void free_params(game_params *params)
140 static game_params *dup_params(const game_params *params)
142 game_params *ret = snew(game_params);
143 *ret = *params; /* structure copy */
147 static void decode_params(game_params *ret, char const *string)
149 ret->w = ret->h = atoi(string);
150 while (*string && isdigit((unsigned char)*string)) string++;
151 if (*string == 'x') {
153 ret->h = atoi(string);
154 while (*string && isdigit((unsigned char)*string)) string++;
156 if (*string == 'd') {
159 for (i = 0; i < DIFFCOUNT; i++)
160 if (*string == slant_diffchars[i])
162 if (*string) string++;
166 static char *encode_params(const game_params *params, int full)
170 sprintf(data, "%dx%d", params->w, params->h);
172 sprintf(data + strlen(data), "d%c", slant_diffchars[params->diff]);
177 static config_item *game_configure(const game_params *params)
182 ret = snewn(4, config_item);
184 ret[0].name = "Width";
185 ret[0].type = C_STRING;
186 sprintf(buf, "%d", params->w);
187 ret[0].sval = dupstr(buf);
190 ret[1].name = "Height";
191 ret[1].type = C_STRING;
192 sprintf(buf, "%d", params->h);
193 ret[1].sval = dupstr(buf);
196 ret[2].name = "Difficulty";
197 ret[2].type = C_CHOICES;
198 ret[2].sval = DIFFCONFIG;
199 ret[2].ival = params->diff;
209 static game_params *custom_params(const config_item *cfg)
211 game_params *ret = snew(game_params);
213 ret->w = atoi(cfg[0].sval);
214 ret->h = atoi(cfg[1].sval);
215 ret->diff = cfg[2].ival;
220 static char *validate_params(const game_params *params, int full)
223 * (At least at the time of writing this comment) The grid
224 * generator is actually capable of handling even zero grid
225 * dimensions without crashing. Puzzles with a zero-area grid
226 * are a bit boring, though, because they're already solved :-)
227 * And puzzles with a dimension of 1 can't be made Hard, which
228 * means the simplest thing is to forbid them altogether.
231 if (params->w < 2 || params->h < 2)
232 return "Width and height must both be at least two";
238 * Scratch space for solver.
240 struct solver_scratch {
242 * Disjoint set forest which tracks the connected sets of
248 * Counts the number of possible exits from each connected set
249 * of points. (That is, the number of possible _simultaneous_
250 * exits: an unconnected point labelled 2 has an exit count of
251 * 2 even if all four possible edges are still under
257 * Tracks whether each connected set of points includes a
260 unsigned char *border;
263 * Another disjoint set forest. This one tracks _squares_ which
264 * are known to slant in the same direction.
269 * Stores slash values which we know for an equivalence class.
270 * When we fill in a square, we set slashval[canonify(x)] to
271 * the same value as soln[x], so that we can then spot other
272 * squares equivalent to it and fill them in immediately via
273 * their known equivalence.
275 signed char *slashval;
278 * Stores possible v-shapes. This array is w by h in size, but
279 * not every bit of every entry is meaningful. The bits mean:
281 * - bit 0 for a square means that that square and the one to
282 * its right might form a v-shape between them
283 * - bit 1 for a square means that that square and the one to
284 * its right might form a ^-shape between them
285 * - bit 2 for a square means that that square and the one
286 * below it might form a >-shape between them
287 * - bit 3 for a square means that that square and the one
288 * below it might form a <-shape between them
290 * Any starting 1 or 3 clue rules out four bits in this array
291 * immediately; a 2 clue propagates any ruled-out bit past it
292 * (if the two squares on one side of a 2 cannot be a v-shape,
293 * then neither can the two on the other side be the same
294 * v-shape); we can rule out further bits during play using
295 * partially filled 2 clues; whenever a pair of squares is
296 * known not to be _either_ kind of v-shape, we can mark them
299 unsigned char *vbitmap;
302 * Useful to have this information automatically passed to
303 * solver subroutines. (This pointer is not dynamically
304 * allocated by new_scratch and free_scratch.)
306 const signed char *clues;
309 static struct solver_scratch *new_scratch(int w, int h)
311 int W = w+1, H = h+1;
312 struct solver_scratch *ret = snew(struct solver_scratch);
313 ret->connected = snewn(W*H, int);
314 ret->exits = snewn(W*H, int);
315 ret->border = snewn(W*H, unsigned char);
316 ret->equiv = snewn(w*h, int);
317 ret->slashval = snewn(w*h, signed char);
318 ret->vbitmap = snewn(w*h, unsigned char);
322 static void free_scratch(struct solver_scratch *sc)
329 sfree(sc->connected);
334 * Wrapper on dsf_merge() which updates the `exits' and `border'
337 static void merge_vertices(int *connected,
338 struct solver_scratch *sc, int i, int j)
340 int exits = -1, border = FALSE; /* initialise to placate optimiser */
343 i = dsf_canonify(connected, i);
344 j = dsf_canonify(connected, j);
347 * We have used one possible exit from each of the two
348 * classes. Thus, the viable exit count of the new class is
349 * the sum of the old exit counts minus two.
351 exits = sc->exits[i] + sc->exits[j] - 2;
353 border = sc->border[i] || sc->border[j];
356 dsf_merge(connected, i, j);
359 i = dsf_canonify(connected, i);
360 sc->exits[i] = exits;
361 sc->border[i] = border;
366 * Called when we have just blocked one way out of a particular
367 * point. If that point is a non-clue point (thus has a variable
368 * number of exits), we have therefore decreased its potential exit
369 * count, so we must decrement the exit count for the group as a
372 static void decr_exits(struct solver_scratch *sc, int i)
374 if (sc->clues[i] < 0) {
375 i = dsf_canonify(sc->connected, i);
380 static void fill_square(int w, int h, int x, int y, int v,
382 int *connected, struct solver_scratch *sc)
384 int W = w+1 /*, H = h+1 */;
386 assert(x >= 0 && x < w && y >= 0 && y < h);
388 if (soln[y*w+x] != 0) {
389 return; /* do nothing */
392 #ifdef SOLVER_DIAGNOSTICS
394 printf(" placing %c in %d,%d\n", v == -1 ? '\\' : '/', x, y);
400 int c = dsf_canonify(sc->equiv, y*w+x);
405 merge_vertices(connected, sc, y*W+x, (y+1)*W+(x+1));
407 decr_exits(sc, y*W+(x+1));
408 decr_exits(sc, (y+1)*W+x);
411 merge_vertices(connected, sc, y*W+(x+1), (y+1)*W+x);
413 decr_exits(sc, y*W+x);
414 decr_exits(sc, (y+1)*W+(x+1));
419 static int vbitmap_clear(int w, int h, struct solver_scratch *sc,
420 int x, int y, int vbits, char *reason, ...)
422 int done_something = FALSE;
425 for (vbit = 1; vbit <= 8; vbit <<= 1)
426 if (vbits & sc->vbitmap[y*w+x] & vbit) {
427 done_something = TRUE;
428 #ifdef SOLVER_DIAGNOSTICS
432 printf("ruling out %c shape at (%d,%d)-(%d,%d) (",
433 "!v^!>!!!<"[vbit], x, y,
434 x+((vbit&0x3)!=0), y+((vbit&0xC)!=0));
436 va_start(ap, reason);
443 sc->vbitmap[y*w+x] &= ~vbit;
446 return done_something;
450 * Solver. Returns 0 for impossibility, 1 for success, 2 for
451 * ambiguity or failure to converge.
453 static int slant_solve(int w, int h, const signed char *clues,
454 signed char *soln, struct solver_scratch *sc,
457 int W = w+1, H = h+1;
464 memset(soln, 0, w*h);
469 * Establish a disjoint set forest for tracking connectedness
470 * between grid points.
472 dsf_init(sc->connected, W*H);
475 * Establish a disjoint set forest for tracking which squares
476 * are known to slant in the same direction.
478 dsf_init(sc->equiv, w*h);
481 * Clear the slashval array.
483 memset(sc->slashval, 0, w*h);
486 * Set up the vbitmap array. Initially all types of v are possible.
488 memset(sc->vbitmap, 0xF, w*h);
491 * Initialise the `exits' and `border' arrays. These are used
492 * to do second-order loop avoidance: the dual of the no loops
493 * constraint is that every point must be somehow connected to
494 * the border of the grid (otherwise there would be a solid
495 * loop around it which prevented this).
497 * I define a `dead end' to be a connected group of points
498 * which contains no border point, and which can form at most
499 * one new connection outside itself. Then I forbid placing an
500 * edge so that it connects together two dead-end groups, since
501 * this would yield a non-border-connected isolated subgraph
502 * with no further scope to extend it.
504 for (y = 0; y < H; y++)
505 for (x = 0; x < W; x++) {
506 if (y == 0 || y == H-1 || x == 0 || x == W-1)
507 sc->border[y*W+x] = TRUE;
509 sc->border[y*W+x] = FALSE;
511 if (clues[y*W+x] < 0)
512 sc->exits[y*W+x] = 4;
514 sc->exits[y*W+x] = clues[y*W+x];
518 * Repeatedly try to deduce something until we can't.
521 done_something = FALSE;
524 * Any clue point with the number of remaining lines equal
525 * to zero or to the number of remaining undecided
526 * neighbouring squares can be filled in completely.
528 for (y = 0; y < H; y++)
529 for (x = 0; x < W; x++) {
534 int nu, nl, c, s, eq, eq2, last, meq, mj1, mj2;
536 if ((c = clues[y*W+x]) < 0)
540 * We have a clue point. Start by listing its
541 * neighbouring squares, in order around the point,
542 * together with the type of slash that would be
543 * required in that square to connect to the point.
546 if (x > 0 && y > 0) {
547 neighbours[nneighbours].pos = (y-1)*w+(x-1);
548 neighbours[nneighbours].slash = -1;
551 if (x > 0 && y < h) {
552 neighbours[nneighbours].pos = y*w+(x-1);
553 neighbours[nneighbours].slash = +1;
556 if (x < w && y < h) {
557 neighbours[nneighbours].pos = y*w+x;
558 neighbours[nneighbours].slash = -1;
561 if (x < w && y > 0) {
562 neighbours[nneighbours].pos = (y-1)*w+x;
563 neighbours[nneighbours].slash = +1;
568 * Count up the number of undecided neighbours, and
569 * also the number of lines already present.
571 * If we're not on DIFF_EASY, then in this loop we
572 * also track whether we've seen two adjacent empty
573 * squares belonging to the same equivalence class
574 * (meaning they have the same type of slash). If
575 * so, we count them jointly as one line.
579 last = neighbours[nneighbours-1].pos;
581 eq = dsf_canonify(sc->equiv, last);
584 meq = mj1 = mj2 = -1;
585 for (i = 0; i < nneighbours; i++) {
586 j = neighbours[i].pos;
587 s = neighbours[i].slash;
589 nu++; /* undecided */
590 if (meq < 0 && difficulty > DIFF_EASY) {
591 eq2 = dsf_canonify(sc->equiv, j);
592 if (eq == eq2 && last != j) {
594 * We've found an equivalent pair.
595 * Mark it. This also inhibits any
596 * further equivalence tracking
597 * around this square, since we can
598 * only handle one pair (and in
599 * particular we want to avoid
600 * being misled by two overlapping
601 * equivalence pairs).
606 nl--; /* count one line */
607 nu -= 2; /* and lose two undecideds */
614 nl--; /* here's a line */
622 if (nl < 0 || nl > nu) {
624 * No consistent value for this at all!
626 #ifdef SOLVER_DIAGNOSTICS
628 printf("need %d / %d lines around clue point at %d,%d!\n",
631 return 0; /* impossible */
634 if (nu > 0 && (nl == 0 || nl == nu)) {
635 #ifdef SOLVER_DIAGNOSTICS
638 printf("partially (since %d,%d == %d,%d) ",
639 mj1%w, mj1/w, mj2%w, mj2/w);
640 printf("%s around clue point at %d,%d\n",
641 nl ? "filling" : "emptying", x, y);
644 for (i = 0; i < nneighbours; i++) {
645 j = neighbours[i].pos;
646 s = neighbours[i].slash;
647 if (soln[j] == 0 && j != mj1 && j != mj2)
648 fill_square(w, h, j%w, j/w, (nl ? s : -s), soln,
652 done_something = TRUE;
653 } else if (nu == 2 && nl == 1 && difficulty > DIFF_EASY) {
655 * If we have precisely two undecided squares
656 * and precisely one line to place between
657 * them, _and_ those squares are adjacent, then
658 * we can mark them as equivalent to one
661 * This even applies if meq >= 0: if we have a
662 * 2 clue point and two of its neighbours are
663 * already marked equivalent, we can indeed
664 * mark the other two as equivalent.
666 * We don't bother with this on DIFF_EASY,
667 * since we wouldn't have used the results
671 for (i = 0; i < nneighbours; i++) {
672 j = neighbours[i].pos;
673 if (soln[j] == 0 && j != mj1 && j != mj2) {
676 else if (last == i-1 || (last == 0 && i == 3))
677 break; /* found a pair */
680 if (i < nneighbours) {
685 * neighbours[last] and neighbours[i] are
686 * the pair. Mark them equivalent.
688 #ifdef SOLVER_DIAGNOSTICS
691 printf("since %d,%d == %d,%d, ",
692 mj1%w, mj1/w, mj2%w, mj2/w);
695 mj1 = neighbours[last].pos;
696 mj2 = neighbours[i].pos;
697 #ifdef SOLVER_DIAGNOSTICS
699 printf("clue point at %d,%d implies %d,%d == %d,"
700 "%d\n", x, y, mj1%w, mj1/w, mj2%w, mj2/w);
702 mj1 = dsf_canonify(sc->equiv, mj1);
703 sv1 = sc->slashval[mj1];
704 mj2 = dsf_canonify(sc->equiv, mj2);
705 sv2 = sc->slashval[mj2];
706 if (sv1 != 0 && sv2 != 0 && sv1 != sv2) {
707 #ifdef SOLVER_DIAGNOSTICS
709 printf("merged two equivalence classes with"
710 " different slash values!\n");
714 sv1 = sv1 ? sv1 : sv2;
715 dsf_merge(sc->equiv, mj1, mj2);
716 mj1 = dsf_canonify(sc->equiv, mj1);
717 sc->slashval[mj1] = sv1;
726 * Failing that, we now apply the second condition, which
727 * is that no square may be filled in such a way as to form
728 * a loop. Also in this loop (since it's over squares
729 * rather than points), we check slashval to see if we've
730 * already filled in another square in the same equivalence
733 * The slashval check is disabled on DIFF_EASY, as is dead
734 * end avoidance. Only _immediate_ loop avoidance remains.
736 for (y = 0; y < h; y++)
737 for (x = 0; x < w; x++) {
740 #ifdef SOLVER_DIAGNOSTICS
741 char *reason = "<internal error>";
745 continue; /* got this one already */
750 if (difficulty > DIFF_EASY)
751 v = sc->slashval[dsf_canonify(sc->equiv, y*w+x)];
756 * Try to rule out connectivity between (x,y) and
757 * (x+1,y+1); if successful, we will deduce that we
758 * must have a forward slash.
760 c1 = dsf_canonify(sc->connected, y*W+x);
761 c2 = dsf_canonify(sc->connected, (y+1)*W+(x+1));
764 #ifdef SOLVER_DIAGNOSTICS
765 reason = "simple loop avoidance";
768 if (difficulty > DIFF_EASY &&
769 !sc->border[c1] && !sc->border[c2] &&
770 sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
772 #ifdef SOLVER_DIAGNOSTICS
773 reason = "dead end avoidance";
778 #ifdef SOLVER_DIAGNOSTICS
779 reason = "equivalence to an already filled square";
784 * Now do the same between (x+1,y) and (x,y+1), to
785 * see if we are required to have a backslash.
787 c1 = dsf_canonify(sc->connected, y*W+(x+1));
788 c2 = dsf_canonify(sc->connected, (y+1)*W+x);
791 #ifdef SOLVER_DIAGNOSTICS
792 reason = "simple loop avoidance";
795 if (difficulty > DIFF_EASY &&
796 !sc->border[c1] && !sc->border[c2] &&
797 sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
799 #ifdef SOLVER_DIAGNOSTICS
800 reason = "dead end avoidance";
805 #ifdef SOLVER_DIAGNOSTICS
806 reason = "equivalence to an already filled square";
812 * No consistent value for this at all!
814 #ifdef SOLVER_DIAGNOSTICS
816 printf("%d,%d has no consistent slash!\n", x, y);
818 return 0; /* impossible */
822 #ifdef SOLVER_DIAGNOSTICS
824 printf("employing %s\n", reason);
826 fill_square(w, h, x, y, +1, soln, sc->connected, sc);
827 done_something = TRUE;
829 #ifdef SOLVER_DIAGNOSTICS
831 printf("employing %s\n", reason);
833 fill_square(w, h, x, y, -1, soln, sc->connected, sc);
834 done_something = TRUE;
842 * Now see what we can do with the vbitmap array. All
843 * vbitmap deductions are disabled at Easy level.
845 if (difficulty <= DIFF_EASY)
848 for (y = 0; y < h; y++)
849 for (x = 0; x < w; x++) {
853 * Any line already placed in a square must rule
854 * out any type of v which contradicts it.
856 if ((s = soln[y*w+x]) != 0) {
859 vbitmap_clear(w, h, sc, x-1, y, (s < 0 ? 0x1 : 0x2),
860 "contradicts known edge at (%d,%d)",x,y);
863 vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x2 : 0x1),
864 "contradicts known edge at (%d,%d)",x,y);
867 vbitmap_clear(w, h, sc, x, y-1, (s < 0 ? 0x4 : 0x8),
868 "contradicts known edge at (%d,%d)",x,y);
871 vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x8 : 0x4),
872 "contradicts known edge at (%d,%d)",x,y);
876 * If both types of v are ruled out for a pair of
877 * adjacent squares, mark them as equivalent.
879 if (x+1 < w && !(sc->vbitmap[y*w+x] & 0x3)) {
880 int n1 = y*w+x, n2 = y*w+(x+1);
881 if (dsf_canonify(sc->equiv, n1) !=
882 dsf_canonify(sc->equiv, n2)) {
883 dsf_merge(sc->equiv, n1, n2);
884 done_something = TRUE;
885 #ifdef SOLVER_DIAGNOSTICS
887 printf("(%d,%d) and (%d,%d) must be equivalent"
888 " because both v-shapes are ruled out\n",
893 if (y+1 < h && !(sc->vbitmap[y*w+x] & 0xC)) {
894 int n1 = y*w+x, n2 = (y+1)*w+x;
895 if (dsf_canonify(sc->equiv, n1) !=
896 dsf_canonify(sc->equiv, n2)) {
897 dsf_merge(sc->equiv, n1, n2);
898 done_something = TRUE;
899 #ifdef SOLVER_DIAGNOSTICS
901 printf("(%d,%d) and (%d,%d) must be equivalent"
902 " because both v-shapes are ruled out\n",
909 * The remaining work in this loop only works
910 * around non-edge clue points.
912 if (y == 0 || x == 0)
914 if ((c = clues[y*W+x]) < 0)
918 * x,y marks a clue point not on the grid edge. See
919 * if this clue point allows us to rule out any v
925 * A 1 clue can never have any v shape pointing
929 vbitmap_clear(w, h, sc, x-1, y-1, 0x5,
930 "points at 1 clue at (%d,%d)", x, y);
932 vbitmap_clear(w, h, sc, x-1, y, 0x2,
933 "points at 1 clue at (%d,%d)", x, y);
935 vbitmap_clear(w, h, sc, x, y-1, 0x8,
936 "points at 1 clue at (%d,%d)", x, y);
939 * A 3 clue can never have any v shape pointing
943 vbitmap_clear(w, h, sc, x-1, y-1, 0xA,
944 "points away from 3 clue at (%d,%d)", x, y);
946 vbitmap_clear(w, h, sc, x-1, y, 0x1,
947 "points away from 3 clue at (%d,%d)", x, y);
949 vbitmap_clear(w, h, sc, x, y-1, 0x4,
950 "points away from 3 clue at (%d,%d)", x, y);
953 * If a 2 clue has any kind of v ruled out on
954 * one side of it, the same v is ruled out on
958 vbitmap_clear(w, h, sc, x-1, y-1,
959 (sc->vbitmap[(y )*w+(x-1)] & 0x3) ^ 0x3,
960 "propagated by 2 clue at (%d,%d)", x, y);
962 vbitmap_clear(w, h, sc, x-1, y-1,
963 (sc->vbitmap[(y-1)*w+(x )] & 0xC) ^ 0xC,
964 "propagated by 2 clue at (%d,%d)", x, y);
966 vbitmap_clear(w, h, sc, x-1, y,
967 (sc->vbitmap[(y-1)*w+(x-1)] & 0x3) ^ 0x3,
968 "propagated by 2 clue at (%d,%d)", x, y);
970 vbitmap_clear(w, h, sc, x, y-1,
971 (sc->vbitmap[(y-1)*w+(x-1)] & 0xC) ^ 0xC,
972 "propagated by 2 clue at (%d,%d)", x, y);
979 } while (done_something);
982 * Solver can make no more progress. See if the grid is full.
984 for (i = 0; i < w*h; i++)
986 return 2; /* failed to converge */
987 return 1; /* success */
991 * Filled-grid generator.
993 static void slant_generate(int w, int h, signed char *soln, random_state *rs)
995 int W = w+1, H = h+1;
997 int *connected, *indices;
1002 memset(soln, 0, w*h);
1005 * Establish a disjoint set forest for tracking connectedness
1006 * between grid points.
1008 connected = snew_dsf(W*H);
1011 * Prepare a list of the squares in the grid, and fill them in
1012 * in a random order.
1014 indices = snewn(w*h, int);
1015 for (i = 0; i < w*h; i++)
1017 shuffle(indices, w*h, sizeof(*indices), rs);
1020 * Fill in each one in turn.
1022 for (i = 0; i < w*h; i++) {
1028 fs = (dsf_canonify(connected, y*W+x) ==
1029 dsf_canonify(connected, (y+1)*W+(x+1)));
1030 bs = (dsf_canonify(connected, (y+1)*W+x) ==
1031 dsf_canonify(connected, y*W+(x+1)));
1034 * It isn't possible to get into a situation where we
1035 * aren't allowed to place _either_ type of slash in a
1036 * square. Thus, filled-grid generation never has to
1039 * Proof (thanks to Gareth Taylor):
1041 * If it were possible, it would have to be because there
1042 * was an existing path (not using this square) between the
1043 * top-left and bottom-right corners of this square, and
1044 * another between the other two. These two paths would
1045 * have to cross at some point.
1047 * Obviously they can't cross in the middle of a square, so
1048 * they must cross by sharing a point in common. But this
1049 * isn't possible either: if you chessboard-colour all the
1050 * points on the grid, you find that any continuous
1051 * diagonal path is entirely composed of points of the same
1052 * colour. And one of our two hypothetical paths is between
1053 * two black points, and the other is between two white
1054 * points - therefore they can have no point in common. []
1056 assert(!(fs && bs));
1058 v = fs ? +1 : bs ? -1 : 2 * random_upto(rs, 2) - 1;
1059 fill_square(w, h, x, y, v, soln, connected, NULL);
1066 static char *new_game_desc(const game_params *params, random_state *rs,
1067 char **aux, int interactive)
1069 int w = params->w, h = params->h, W = w+1, H = h+1;
1070 signed char *soln, *tmpsoln, *clues;
1072 struct solver_scratch *sc;
1076 soln = snewn(w*h, signed char);
1077 tmpsoln = snewn(w*h, signed char);
1078 clues = snewn(W*H, signed char);
1079 clueindices = snewn(W*H, int);
1080 sc = new_scratch(w, h);
1084 * Create the filled grid.
1086 slant_generate(w, h, soln, rs);
1089 * Fill in the complete set of clues.
1091 for (y = 0; y < H; y++)
1092 for (x = 0; x < W; x++) {
1095 if (x > 0 && y > 0 && soln[(y-1)*w+(x-1)] == -1) v++;
1096 if (x > 0 && y < h && soln[y*w+(x-1)] == +1) v++;
1097 if (x < w && y > 0 && soln[(y-1)*w+x] == +1) v++;
1098 if (x < w && y < h && soln[y*w+x] == -1) v++;
1104 * With all clue points filled in, all puzzles are easy: we can
1105 * simply process the clue points in lexicographic order, and
1106 * at each clue point we will always have at most one square
1107 * undecided, which we can then fill in uniquely.
1109 assert(slant_solve(w, h, clues, tmpsoln, sc, DIFF_EASY) == 1);
1112 * Remove as many clues as possible while retaining solubility.
1114 * In DIFF_HARD mode, we prioritise the removal of obvious
1115 * starting points (4s, 0s, border 2s and corner 1s), on
1116 * the grounds that having as few of these as possible
1117 * seems like a good thing. In particular, we can often get
1118 * away without _any_ completely obvious starting points,
1119 * which is even better.
1121 for (i = 0; i < W*H; i++)
1123 shuffle(clueindices, W*H, sizeof(*clueindices), rs);
1124 for (j = 0; j < 2; j++) {
1125 for (i = 0; i < W*H; i++) {
1128 y = clueindices[i] / W;
1129 x = clueindices[i] % W;
1133 * Identify which pass we should process this point
1134 * in. If it's an obvious start point, _or_ we're
1135 * in DIFF_EASY, then it goes in pass 0; otherwise
1138 xb = (x == 0 || x == W-1);
1139 yb = (y == 0 || y == H-1);
1140 if (params->diff == DIFF_EASY || v == 4 || v == 0 ||
1141 (v == 2 && (xb||yb)) || (v == 1 && xb && yb))
1148 if (slant_solve(w, h, clues, tmpsoln, sc,
1150 clues[y*W+x] = v; /* put it back */
1156 * And finally, verify that the grid is of _at least_ the
1157 * requested difficulty, by running the solver one level
1158 * down and verifying that it can't manage it.
1160 } while (params->diff > 0 &&
1161 slant_solve(w, h, clues, tmpsoln, sc, params->diff - 1) <= 1);
1164 * Now we have the clue set as it will be presented to the
1165 * user. Encode it in a game desc.
1171 desc = snewn(W*H+1, char);
1174 for (i = 0; i <= W*H; i++) {
1175 int n = (i < W*H ? clues[i] : -2);
1182 int c = 'a' - 1 + run;
1186 run -= c - ('a' - 1);
1194 assert(p - desc <= W*H);
1196 desc = sresize(desc, p - desc, char);
1200 * Encode the solution as an aux_info.
1204 *aux = auxbuf = snewn(w*h+1, char);
1205 for (i = 0; i < w*h; i++)
1206 auxbuf[i] = soln[i] < 0 ? '\\' : '/';
1219 static char *validate_desc(const game_params *params, const char *desc)
1221 int w = params->w, h = params->h, W = w+1, H = h+1;
1227 if (n >= 'a' && n <= 'z') {
1228 squares += n - 'a' + 1;
1229 } else if (n >= '0' && n <= '4') {
1232 return "Invalid character in game description";
1236 return "Not enough data to fill grid";
1239 return "Too much data to fit in grid";
1244 static game_state *new_game(midend *me, const game_params *params,
1247 int w = params->w, h = params->h, W = w+1, H = h+1;
1248 game_state *state = snew(game_state);
1253 state->soln = snewn(w*h, signed char);
1254 memset(state->soln, 0, w*h);
1255 state->completed = state->used_solve = FALSE;
1256 state->errors = snewn(W*H, unsigned char);
1257 memset(state->errors, 0, W*H);
1259 state->clues = snew(game_clues);
1260 state->clues->w = w;
1261 state->clues->h = h;
1262 state->clues->clues = snewn(W*H, signed char);
1263 state->clues->refcount = 1;
1264 state->clues->tmpdsf = snewn(W*H*2+W+H, int);
1265 memset(state->clues->clues, -1, W*H);
1268 if (n >= 'a' && n <= 'z') {
1269 squares += n - 'a' + 1;
1270 } else if (n >= '0' && n <= '4') {
1271 state->clues->clues[squares++] = n - '0';
1273 assert(!"can't get here");
1275 assert(squares == area);
1280 static game_state *dup_game(const game_state *state)
1282 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1283 game_state *ret = snew(game_state);
1286 ret->clues = state->clues;
1287 ret->clues->refcount++;
1288 ret->completed = state->completed;
1289 ret->used_solve = state->used_solve;
1291 ret->soln = snewn(w*h, signed char);
1292 memcpy(ret->soln, state->soln, w*h);
1294 ret->errors = snewn(W*H, unsigned char);
1295 memcpy(ret->errors, state->errors, W*H);
1300 static void free_game(game_state *state)
1302 sfree(state->errors);
1304 assert(state->clues);
1305 if (--state->clues->refcount <= 0) {
1306 sfree(state->clues->clues);
1307 sfree(state->clues->tmpdsf);
1308 sfree(state->clues);
1314 * Utility function to return the current degree of a vertex. If
1315 * `anti' is set, it returns the number of filled-in edges
1316 * surrounding the point which _don't_ connect to it; thus 4 minus
1317 * its anti-degree is the maximum degree it could have if all the
1318 * empty spaces around it were filled in.
1320 * (Yes, _4_ minus its anti-degree even if it's a border vertex.)
1322 * If ret > 0, *sx and *sy are set to the coordinates of one of the
1323 * squares that contributed to it.
1325 static int vertex_degree(int w, int h, signed char *soln, int x, int y,
1326 int anti, int *sx, int *sy)
1330 assert(x >= 0 && x <= w && y >= 0 && y <= h);
1331 if (x > 0 && y > 0 && soln[(y-1)*w+(x-1)] - anti < 0) {
1336 if (x > 0 && y < h && soln[y*w+(x-1)] + anti > 0) {
1341 if (x < w && y > 0 && soln[(y-1)*w+x] + anti > 0) {
1346 if (x < w && y < h && soln[y*w+x] - anti < 0) {
1352 return anti ? 4 - ret : ret;
1355 static int check_completion(game_state *state)
1357 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1358 int x, y, err = FALSE;
1361 memset(state->errors, 0, W*H);
1364 * To detect loops in the grid, we iterate through each edge
1365 * building up a dsf of connected components of the space
1366 * around the edges; if there's more than one such component,
1367 * we have a loop, and in particular we can then easily
1368 * identify and highlight every edge forming part of a loop
1369 * because it separates two nonequivalent regions.
1371 * We use the `tmpdsf' scratch space in the shared clues
1372 * structure, to avoid mallocing too often.
1374 * For these purposes, the grid is considered to be divided
1375 * into diamond-shaped regions surrounding an orthogonal edge.
1376 * This means we have W*h vertical edges and w*H horizontal
1377 * ones; so our vertical edges are indexed in the dsf as
1378 * (y*W+x) (0<=y<h, 0<=x<W), and the horizontal ones as (W*h +
1379 * y*w+x) (0<=y<H, 0<=x<w), where (x,y) is the topmost or
1380 * leftmost point on the edge.
1382 dsf = state->clues->tmpdsf;
1383 dsf_init(dsf, W*h + w*H);
1384 /* Start by identifying all the outer edges with each other. */
1385 for (y = 0; y < h; y++) {
1386 dsf_merge(dsf, 0, y*W+0);
1387 dsf_merge(dsf, 0, y*W+w);
1389 for (x = 0; x < w; x++) {
1390 dsf_merge(dsf, 0, W*h + 0*w+x);
1391 dsf_merge(dsf, 0, W*h + h*w+x);
1393 /* Now go through the actual grid. */
1394 for (y = 0; y < h; y++)
1395 for (x = 0; x < w; x++) {
1396 if (state->soln[y*w+x] >= 0) {
1398 * There isn't a \ in this square, so we can unify
1399 * the top edge with the left, and the bottom with
1402 dsf_merge(dsf, y*W+x, W*h + y*w+x);
1403 dsf_merge(dsf, y*W+(x+1), W*h + (y+1)*w+x);
1405 if (state->soln[y*w+x] <= 0) {
1407 * There isn't a / in this square, so we can unify
1408 * the top edge with the right, and the bottom
1411 dsf_merge(dsf, y*W+x, W*h + (y+1)*w+x);
1412 dsf_merge(dsf, y*W+(x+1), W*h + y*w+x);
1415 /* Now go through again and mark the appropriate edges as erroneous. */
1416 for (y = 0; y < h; y++)
1417 for (x = 0; x < w; x++) {
1419 if (state->soln[y*w+x] > 0) {
1421 * A / separates the top and left edges (which
1422 * must already have been identified with each
1423 * other) from the bottom and right (likewise).
1424 * Hence it is erroneous if and only if the top
1425 * and right edges are nonequivalent.
1427 erroneous = (dsf_canonify(dsf, y*W+(x+1)) !=
1428 dsf_canonify(dsf, W*h + y*w+x));
1429 } else if (state->soln[y*w+x] < 0) {
1431 * A \ separates the top and right edges (which
1432 * must already have been identified with each
1433 * other) from the bottom and left (likewise).
1434 * Hence it is erroneous if and only if the top
1435 * and left edges are nonequivalent.
1437 erroneous = (dsf_canonify(dsf, y*W+x) !=
1438 dsf_canonify(dsf, W*h + y*w+x));
1441 state->errors[y*W+x] |= ERR_SQUARE;
1447 * Now go through and check the degree of each clue vertex, and
1448 * mark it with ERR_VERTEX if it cannot be fulfilled.
1450 for (y = 0; y < H; y++)
1451 for (x = 0; x < W; x++) {
1454 if ((c = state->clues->clues[y*W+x]) < 0)
1458 * Check to see if there are too many connections to
1459 * this vertex _or_ too many non-connections. Either is
1460 * grounds for marking the vertex as erroneous.
1462 if (vertex_degree(w, h, state->soln, x, y,
1463 FALSE, NULL, NULL) > c ||
1464 vertex_degree(w, h, state->soln, x, y,
1465 TRUE, NULL, NULL) > 4-c) {
1466 state->errors[y*W+x] |= ERR_VERTEX;
1472 * Now our actual victory condition is that (a) none of the
1473 * above code marked anything as erroneous, and (b) every
1474 * square has an edge in it.
1480 for (y = 0; y < h; y++)
1481 for (x = 0; x < w; x++)
1482 if (state->soln[y*w+x] == 0)
1488 static char *solve_game(const game_state *state, const game_state *currstate,
1489 const char *aux, char **error)
1491 int w = state->p.w, h = state->p.h;
1494 int free_soln = FALSE;
1495 char *move, buf[80];
1496 int movelen, movesize;
1501 * If we already have the solution, save ourselves some
1504 soln = (signed char *)aux;
1505 bs = (signed char)'\\';
1508 struct solver_scratch *sc = new_scratch(w, h);
1509 soln = snewn(w*h, signed char);
1511 ret = slant_solve(w, h, state->clues->clues, soln, sc, DIFF_HARD);
1516 *error = "This puzzle is not self-consistent";
1518 *error = "Unable to find a unique solution for this puzzle";
1525 * Construct a move string which turns the current state into
1529 move = snewn(movesize, char);
1531 move[movelen++] = 'S';
1532 move[movelen] = '\0';
1533 for (y = 0; y < h; y++)
1534 for (x = 0; x < w; x++) {
1535 int v = (soln[y*w+x] == bs ? -1 : +1);
1536 if (state->soln[y*w+x] != v) {
1537 int len = sprintf(buf, ";%c%d,%d", (int)(v < 0 ? '\\' : '/'), x, y);
1538 if (movelen + len >= movesize) {
1539 movesize = movelen + len + 256;
1540 move = sresize(move, movesize, char);
1542 strcpy(move + movelen, buf);
1553 static int game_can_format_as_text_now(const game_params *params)
1558 static char *game_text_format(const game_state *state)
1560 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1565 * There are h+H rows of w+W columns.
1567 len = (h+H) * (w+W+1) + 1;
1568 ret = snewn(len, char);
1571 for (y = 0; y < H; y++) {
1572 for (x = 0; x < W; x++) {
1573 if (state->clues->clues[y*W+x] >= 0)
1574 *p++ = state->clues->clues[y*W+x] + '0';
1582 for (x = 0; x < W; x++) {
1585 if (state->soln[y*w+x] != 0)
1586 *p++ = (state->soln[y*w+x] < 0 ? '\\' : '/');
1596 assert(p - ret == len);
1601 int cur_x, cur_y, cur_visible;
1604 static game_ui *new_ui(const game_state *state)
1606 game_ui *ui = snew(game_ui);
1607 ui->cur_x = ui->cur_y = ui->cur_visible = 0;
1611 static void free_ui(game_ui *ui)
1616 static char *encode_ui(const game_ui *ui)
1621 static void decode_ui(game_ui *ui, const char *encoding)
1625 static void game_changed_state(game_ui *ui, const game_state *oldstate,
1626 const game_state *newstate)
1630 #define PREFERRED_TILESIZE 32
1631 #define TILESIZE (ds->tilesize)
1632 #define BORDER TILESIZE
1633 #define CLUE_RADIUS (TILESIZE / 3)
1634 #define CLUE_TEXTSIZE (TILESIZE / 2)
1635 #define COORD(x) ( (x) * TILESIZE + BORDER )
1636 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1638 #define FLASH_TIME 0.30F
1641 * Bit fields in the `grid' and `todraw' elements of the drawstate.
1643 #define BACKSLASH 0x00000001L
1644 #define FORWSLASH 0x00000002L
1645 #define L_T 0x00000004L
1646 #define ERR_L_T 0x00000008L
1647 #define L_B 0x00000010L
1648 #define ERR_L_B 0x00000020L
1649 #define T_L 0x00000040L
1650 #define ERR_T_L 0x00000080L
1651 #define T_R 0x00000100L
1652 #define ERR_T_R 0x00000200L
1653 #define C_TL 0x00000400L
1654 #define ERR_C_TL 0x00000800L
1655 #define FLASH 0x00001000L
1656 #define ERRSLASH 0x00002000L
1657 #define ERR_TL 0x00004000L
1658 #define ERR_TR 0x00008000L
1659 #define ERR_BL 0x00010000L
1660 #define ERR_BR 0x00020000L
1661 #define CURSOR 0x00040000L
1663 struct game_drawstate {
1670 static char *interpret_move(const game_state *state, game_ui *ui,
1671 const game_drawstate *ds,
1672 int x, int y, int button)
1674 int w = state->p.w, h = state->p.h;
1677 enum { CLOCKWISE, ANTICLOCKWISE, NONE } action = NONE;
1679 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
1681 * This is an utterly awful hack which I should really sort out
1682 * by means of a proper configuration mechanism. One Slant
1683 * player has observed that they prefer the mouse buttons to
1684 * function exactly the opposite way round, so here's a
1685 * mechanism for environment-based configuration. I cache the
1686 * result in a global variable - yuck! - to avoid repeated
1690 static int swap_buttons = -1;
1691 if (swap_buttons < 0) {
1692 char *env = getenv("SLANT_SWAP_BUTTONS");
1693 swap_buttons = (env && (env[0] == 'y' || env[0] == 'Y'));
1696 if (button == LEFT_BUTTON)
1697 button = RIGHT_BUTTON;
1699 button = LEFT_BUTTON;
1702 action = (button == LEFT_BUTTON) ? CLOCKWISE : ANTICLOCKWISE;
1706 if (x < 0 || y < 0 || x >= w || y >= h)
1708 ui->cur_visible = 0;
1709 } else if (IS_CURSOR_SELECT(button)) {
1710 if (!ui->cur_visible) {
1711 ui->cur_visible = 1;
1717 action = (button == CURSOR_SELECT2) ? ANTICLOCKWISE : CLOCKWISE;
1718 } else if (IS_CURSOR_MOVE(button)) {
1719 move_cursor(button, &ui->cur_x, &ui->cur_y, w, h, 0);
1720 ui->cur_visible = 1;
1722 } else if (button == '\\' || button == '\b' || button == '/') {
1723 int x = ui->cur_x, y = ui->cur_y;
1724 if (button == ("\\" "\b" "/")[state->soln[y*w + x] + 1]) return NULL;
1725 sprintf(buf, "%c%d,%d", button == '\b' ? 'C' : button, x, y);
1729 if (action != NONE) {
1730 if (action == CLOCKWISE) {
1732 * Left-clicking cycles blank -> \ -> / -> blank.
1734 v = state->soln[y*w+x] - 1;
1739 * Right-clicking cycles blank -> / -> \ -> blank.
1741 v = state->soln[y*w+x] + 1;
1746 sprintf(buf, "%c%d,%d", (int)(v==-1 ? '\\' : v==+1 ? '/' : 'C'), x, y);
1753 static game_state *execute_move(const game_state *state, const char *move)
1755 int w = state->p.w, h = state->p.h;
1758 game_state *ret = dup_game(state);
1763 ret->used_solve = TRUE;
1765 } else if (c == '\\' || c == '/' || c == 'C') {
1767 if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
1768 x < 0 || y < 0 || x >= w || y >= h) {
1772 ret->soln[y*w+x] = (c == '\\' ? -1 : c == '/' ? +1 : 0);
1787 * We never clear the `completed' flag, but we must always
1788 * re-run the completion check because it also highlights
1789 * errors in the grid.
1791 ret->completed = check_completion(ret) || ret->completed;
1796 /* ----------------------------------------------------------------------
1800 static void game_compute_size(const game_params *params, int tilesize,
1803 /* fool the macros */
1804 struct dummy { int tilesize; } dummy, *ds = &dummy;
1805 dummy.tilesize = tilesize;
1807 *x = 2 * BORDER + params->w * TILESIZE + 1;
1808 *y = 2 * BORDER + params->h * TILESIZE + 1;
1811 static void game_set_size(drawing *dr, game_drawstate *ds,
1812 const game_params *params, int tilesize)
1814 ds->tilesize = tilesize;
1817 static float *game_colours(frontend *fe, int *ncolours)
1819 float *ret = snewn(3 * NCOLOURS, float);
1821 /* CURSOR colour is a background highlight. */
1822 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_CURSOR, -1);
1824 ret[COL_FILLEDSQUARE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0];
1825 ret[COL_FILLEDSQUARE * 3 + 1] = ret[COL_BACKGROUND * 3 + 1];
1826 ret[COL_FILLEDSQUARE * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
1828 ret[COL_GRID * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.7F;
1829 ret[COL_GRID * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.7F;
1830 ret[COL_GRID * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.7F;
1832 ret[COL_INK * 3 + 0] = 0.0F;
1833 ret[COL_INK * 3 + 1] = 0.0F;
1834 ret[COL_INK * 3 + 2] = 0.0F;
1836 ret[COL_SLANT1 * 3 + 0] = 0.0F;
1837 ret[COL_SLANT1 * 3 + 1] = 0.0F;
1838 ret[COL_SLANT1 * 3 + 2] = 0.0F;
1840 ret[COL_SLANT2 * 3 + 0] = 0.0F;
1841 ret[COL_SLANT2 * 3 + 1] = 0.0F;
1842 ret[COL_SLANT2 * 3 + 2] = 0.0F;
1844 ret[COL_ERROR * 3 + 0] = 1.0F;
1845 ret[COL_ERROR * 3 + 1] = 0.0F;
1846 ret[COL_ERROR * 3 + 2] = 0.0F;
1848 *ncolours = NCOLOURS;
1852 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1854 int w = state->p.w, h = state->p.h;
1856 struct game_drawstate *ds = snew(struct game_drawstate);
1859 ds->started = FALSE;
1860 ds->grid = snewn((w+2)*(h+2), long);
1861 ds->todraw = snewn((w+2)*(h+2), long);
1862 for (i = 0; i < (w+2)*(h+2); i++)
1863 ds->grid[i] = ds->todraw[i] = -1;
1868 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1875 static void draw_clue(drawing *dr, game_drawstate *ds,
1876 int x, int y, long v, long err, int bg, int colour)
1879 int ccol = colour >= 0 ? colour : ((x ^ y) & 1) ? COL_SLANT1 : COL_SLANT2;
1880 int tcol = colour >= 0 ? colour : err ? COL_ERROR : COL_INK;
1885 p[0] = (char)v + '0';
1887 draw_circle(dr, COORD(x), COORD(y), CLUE_RADIUS,
1888 bg >= 0 ? bg : COL_BACKGROUND, ccol);
1889 draw_text(dr, COORD(x), COORD(y), FONT_VARIABLE,
1890 CLUE_TEXTSIZE, ALIGN_VCENTRE|ALIGN_HCENTRE, tcol, p);
1893 static void draw_tile(drawing *dr, game_drawstate *ds, game_clues *clues,
1894 int x, int y, long v)
1896 int w = clues->w, h = clues->h, W = w+1 /*, H = h+1 */;
1897 int chesscolour = (x ^ y) & 1;
1898 int fscol = chesscolour ? COL_SLANT2 : COL_SLANT1;
1899 int bscol = chesscolour ? COL_SLANT1 : COL_SLANT2;
1901 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
1903 draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
1904 (v & FLASH) ? COL_GRID :
1905 (v & CURSOR) ? COL_CURSOR :
1906 (v & (BACKSLASH | FORWSLASH)) ? COL_FILLEDSQUARE :
1910 * Draw the grid lines.
1912 if (x >= 0 && x < w && y >= 0)
1913 draw_rect(dr, COORD(x), COORD(y), TILESIZE+1, 1, COL_GRID);
1914 if (x >= 0 && x < w && y < h)
1915 draw_rect(dr, COORD(x), COORD(y+1), TILESIZE+1, 1, COL_GRID);
1916 if (y >= 0 && y < h && x >= 0)
1917 draw_rect(dr, COORD(x), COORD(y), 1, TILESIZE+1, COL_GRID);
1918 if (y >= 0 && y < h && x < w)
1919 draw_rect(dr, COORD(x+1), COORD(y), 1, TILESIZE+1, COL_GRID);
1920 if (x == -1 && y == -1)
1921 draw_rect(dr, COORD(x+1), COORD(y+1), 1, 1, COL_GRID);
1922 if (x == -1 && y == h)
1923 draw_rect(dr, COORD(x+1), COORD(y), 1, 1, COL_GRID);
1924 if (x == w && y == -1)
1925 draw_rect(dr, COORD(x), COORD(y+1), 1, 1, COL_GRID);
1926 if (x == w && y == h)
1927 draw_rect(dr, COORD(x), COORD(y), 1, 1, COL_GRID);
1932 if (v & BACKSLASH) {
1933 int scol = (v & ERRSLASH) ? COL_ERROR : bscol;
1934 draw_line(dr, COORD(x), COORD(y), COORD(x+1), COORD(y+1), scol);
1935 draw_line(dr, COORD(x)+1, COORD(y), COORD(x+1), COORD(y+1)-1,
1937 draw_line(dr, COORD(x), COORD(y)+1, COORD(x+1)-1, COORD(y+1),
1939 } else if (v & FORWSLASH) {
1940 int scol = (v & ERRSLASH) ? COL_ERROR : fscol;
1941 draw_line(dr, COORD(x+1), COORD(y), COORD(x), COORD(y+1), scol);
1942 draw_line(dr, COORD(x+1)-1, COORD(y), COORD(x), COORD(y+1)-1,
1944 draw_line(dr, COORD(x+1), COORD(y)+1, COORD(x)+1, COORD(y+1),
1949 * Draw dots on the grid corners that appear if a slash is in a
1950 * neighbouring cell.
1952 if (v & (L_T | BACKSLASH))
1953 draw_rect(dr, COORD(x), COORD(y)+1, 1, 1,
1954 (v & ERR_L_T ? COL_ERROR : bscol));
1955 if (v & (L_B | FORWSLASH))
1956 draw_rect(dr, COORD(x), COORD(y+1)-1, 1, 1,
1957 (v & ERR_L_B ? COL_ERROR : fscol));
1958 if (v & (T_L | BACKSLASH))
1959 draw_rect(dr, COORD(x)+1, COORD(y), 1, 1,
1960 (v & ERR_T_L ? COL_ERROR : bscol));
1961 if (v & (T_R | FORWSLASH))
1962 draw_rect(dr, COORD(x+1)-1, COORD(y), 1, 1,
1963 (v & ERR_T_R ? COL_ERROR : fscol));
1964 if (v & (C_TL | BACKSLASH))
1965 draw_rect(dr, COORD(x), COORD(y), 1, 1,
1966 (v & ERR_C_TL ? COL_ERROR : bscol));
1969 * And finally the clues at the corners.
1971 if (x >= 0 && y >= 0)
1972 draw_clue(dr, ds, x, y, clues->clues[y*W+x], v & ERR_TL, -1, -1);
1973 if (x < w && y >= 0)
1974 draw_clue(dr, ds, x+1, y, clues->clues[y*W+(x+1)], v & ERR_TR, -1, -1);
1975 if (x >= 0 && y < h)
1976 draw_clue(dr, ds, x, y+1, clues->clues[(y+1)*W+x], v & ERR_BL, -1, -1);
1978 draw_clue(dr, ds, x+1, y+1, clues->clues[(y+1)*W+(x+1)], v & ERR_BR,
1982 draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
1985 static void game_redraw(drawing *dr, game_drawstate *ds,
1986 const game_state *oldstate, const game_state *state,
1987 int dir, const game_ui *ui,
1988 float animtime, float flashtime)
1990 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1995 flashing = (int)(flashtime * 3 / FLASH_TIME) != 1;
2001 game_compute_size(&state->p, TILESIZE, &ww, &wh);
2002 draw_rect(dr, 0, 0, ww, wh, COL_BACKGROUND);
2003 draw_update(dr, 0, 0, ww, wh);
2008 * Loop over the grid and work out where all the slashes are.
2009 * We need to do this because a slash in one square affects the
2010 * drawing of the next one along.
2012 for (y = -1; y <= h; y++)
2013 for (x = -1; x <= w; x++) {
2014 if (x >= 0 && x < w && y >= 0 && y < h)
2015 ds->todraw[(y+1)*(w+2)+(x+1)] = flashing ? FLASH : 0;
2017 ds->todraw[(y+1)*(w+2)+(x+1)] = 0;
2020 for (y = 0; y < h; y++) {
2021 for (x = 0; x < w; x++) {
2022 int err = state->errors[y*W+x] & ERR_SQUARE;
2024 if (state->soln[y*w+x] < 0) {
2025 ds->todraw[(y+1)*(w+2)+(x+1)] |= BACKSLASH;
2026 ds->todraw[(y+2)*(w+2)+(x+1)] |= T_R;
2027 ds->todraw[(y+1)*(w+2)+(x+2)] |= L_B;
2028 ds->todraw[(y+2)*(w+2)+(x+2)] |= C_TL;
2030 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERRSLASH |
2031 ERR_T_L | ERR_L_T | ERR_C_TL;
2032 ds->todraw[(y+2)*(w+2)+(x+1)] |= ERR_T_R;
2033 ds->todraw[(y+1)*(w+2)+(x+2)] |= ERR_L_B;
2034 ds->todraw[(y+2)*(w+2)+(x+2)] |= ERR_C_TL;
2036 } else if (state->soln[y*w+x] > 0) {
2037 ds->todraw[(y+1)*(w+2)+(x+1)] |= FORWSLASH;
2038 ds->todraw[(y+1)*(w+2)+(x+2)] |= L_T | C_TL;
2039 ds->todraw[(y+2)*(w+2)+(x+1)] |= T_L | C_TL;
2041 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERRSLASH |
2043 ds->todraw[(y+1)*(w+2)+(x+2)] |= ERR_L_T | ERR_C_TL;
2044 ds->todraw[(y+2)*(w+2)+(x+1)] |= ERR_T_L | ERR_C_TL;
2047 if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
2048 ds->todraw[(y+1)*(w+2)+(x+1)] |= CURSOR;
2052 for (y = 0; y < H; y++)
2053 for (x = 0; x < W; x++)
2054 if (state->errors[y*W+x] & ERR_VERTEX) {
2055 ds->todraw[y*(w+2)+x] |= ERR_BR;
2056 ds->todraw[y*(w+2)+(x+1)] |= ERR_BL;
2057 ds->todraw[(y+1)*(w+2)+x] |= ERR_TR;
2058 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERR_TL;
2062 * Now go through and draw the grid squares.
2064 for (y = -1; y <= h; y++) {
2065 for (x = -1; x <= w; x++) {
2066 if (ds->todraw[(y+1)*(w+2)+(x+1)] != ds->grid[(y+1)*(w+2)+(x+1)]) {
2067 draw_tile(dr, ds, state->clues, x, y,
2068 ds->todraw[(y+1)*(w+2)+(x+1)]);
2069 ds->grid[(y+1)*(w+2)+(x+1)] = ds->todraw[(y+1)*(w+2)+(x+1)];
2075 static float game_anim_length(const game_state *oldstate,
2076 const game_state *newstate, int dir, game_ui *ui)
2081 static float game_flash_length(const game_state *oldstate,
2082 const game_state *newstate, int dir, game_ui *ui)
2084 if (!oldstate->completed && newstate->completed &&
2085 !oldstate->used_solve && !newstate->used_solve)
2091 static int game_status(const game_state *state)
2093 return state->completed ? +1 : 0;
2096 static int game_timing_state(const game_state *state, game_ui *ui)
2101 static void game_print_size(const game_params *params, float *x, float *y)
2106 * I'll use 6mm squares by default.
2108 game_compute_size(params, 600, &pw, &ph);
2113 static void game_print(drawing *dr, const game_state *state, int tilesize)
2115 int w = state->p.w, h = state->p.h, W = w+1;
2116 int ink = print_mono_colour(dr, 0);
2117 int paper = print_mono_colour(dr, 1);
2120 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2121 game_drawstate ads, *ds = &ads;
2122 game_set_size(dr, ds, NULL, tilesize);
2127 print_line_width(dr, TILESIZE / 16);
2128 draw_rect_outline(dr, COORD(0), COORD(0), w*TILESIZE, h*TILESIZE, ink);
2133 print_line_width(dr, TILESIZE / 24);
2134 for (x = 1; x < w; x++)
2135 draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), ink);
2136 for (y = 1; y < h; y++)
2137 draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), ink);
2142 print_line_width(dr, TILESIZE / 12);
2143 for (y = 0; y < h; y++)
2144 for (x = 0; x < w; x++)
2145 if (state->soln[y*w+x]) {
2148 * To prevent nasty line-ending artefacts at
2149 * corners, I'll do something slightly cunning
2152 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2153 if (state->soln[y*w+x] < 0)
2157 draw_line(dr, COORD(x-1), COORD(ly), COORD(x+2), COORD(ry),
2165 print_line_width(dr, TILESIZE / 24);
2166 for (y = 0; y <= h; y++)
2167 for (x = 0; x <= w; x++)
2168 draw_clue(dr, ds, x, y, state->clues->clues[y*W+x],
2173 #define thegame slant
2176 const struct game thegame = {
2177 "Slant", "games.slant", "slant",
2184 TRUE, game_configure, custom_params,
2192 TRUE, game_can_format_as_text_now, game_text_format,
2200 PREFERRED_TILESIZE, game_compute_size, game_set_size,
2203 game_free_drawstate,
2208 TRUE, FALSE, game_print_size, game_print,
2209 FALSE, /* wants_statusbar */
2210 FALSE, game_timing_state,
2214 #ifdef STANDALONE_SOLVER
2218 int main(int argc, char **argv)
2222 char *id = NULL, *desc, *err;
2224 int ret, diff, really_verbose = FALSE;
2225 struct solver_scratch *sc;
2227 while (--argc > 0) {
2229 if (!strcmp(p, "-v")) {
2230 really_verbose = TRUE;
2231 } else if (!strcmp(p, "-g")) {
2233 } else if (*p == '-') {
2234 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2242 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
2246 desc = strchr(id, ':');
2248 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2253 p = default_params();
2254 decode_params(p, id);
2255 err = validate_desc(p, desc);
2257 fprintf(stderr, "%s: %s\n", argv[0], err);
2260 s = new_game(NULL, p, desc);
2262 sc = new_scratch(p->w, p->h);
2265 * When solving an Easy puzzle, we don't want to bother the
2266 * user with Hard-level deductions. For this reason, we grade
2267 * the puzzle internally before doing anything else.
2269 ret = -1; /* placate optimiser */
2270 for (diff = 0; diff < DIFFCOUNT; diff++) {
2271 ret = slant_solve(p->w, p->h, s->clues->clues,
2277 if (diff == DIFFCOUNT) {
2279 printf("Difficulty rating: harder than Hard, or ambiguous\n");
2281 printf("Unable to find a unique solution\n");
2285 printf("Difficulty rating: impossible (no solution exists)\n");
2287 printf("Difficulty rating: %s\n", slant_diffnames[diff]);
2289 verbose = really_verbose;
2290 ret = slant_solve(p->w, p->h, s->clues->clues,
2293 printf("Puzzle is inconsistent\n");
2295 fputs(game_text_format(s), stdout);
2304 /* vim: set shiftwidth=4 tabstop=8: */