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.
46 * In standalone solver mode, `verbose' is a variable which can be
47 * set by command-line option; in debugging mode it's simply always
50 #if defined STANDALONE_SOLVER
51 #define SOLVER_DIAGNOSTICS
53 #elif defined SOLVER_DIAGNOSTICS
58 * Difficulty levels. I do some macro ickery here to ensure that my
59 * enum and the various forms of my name list always match up.
64 #define ENUM(upper,title,lower) DIFF_ ## upper,
65 #define TITLE(upper,title,lower) #title,
66 #define ENCODE(upper,title,lower) #lower
67 #define CONFIG(upper,title,lower) ":" #title
68 enum { DIFFLIST(ENUM) DIFFCOUNT };
69 static char const *const slant_diffnames[] = { DIFFLIST(TITLE) };
70 static char const slant_diffchars[] = DIFFLIST(ENCODE);
71 #define DIFFCONFIG DIFFLIST(CONFIG)
77 typedef struct game_clues {
86 #define ERR_SQUARE_TMP 4
92 unsigned char *errors;
94 int used_solve; /* used to suppress completion flash */
97 static game_params *default_params(void)
99 game_params *ret = snew(game_params);
102 ret->diff = DIFF_EASY;
107 static const struct game_params slant_presets[] = {
116 static int game_fetch_preset(int i, char **name, game_params **params)
121 if (i < 0 || i >= lenof(slant_presets))
124 ret = snew(game_params);
125 *ret = slant_presets[i];
127 sprintf(str, "%dx%d %s", ret->w, ret->h, slant_diffnames[ret->diff]);
134 static void free_params(game_params *params)
139 static game_params *dup_params(game_params *params)
141 game_params *ret = snew(game_params);
142 *ret = *params; /* structure copy */
146 static void decode_params(game_params *ret, char const *string)
148 ret->w = ret->h = atoi(string);
149 while (*string && isdigit((unsigned char)*string)) string++;
150 if (*string == 'x') {
152 ret->h = atoi(string);
153 while (*string && isdigit((unsigned char)*string)) string++;
155 if (*string == 'd') {
158 for (i = 0; i < DIFFCOUNT; i++)
159 if (*string == slant_diffchars[i])
161 if (*string) string++;
165 static char *encode_params(game_params *params, int full)
169 sprintf(data, "%dx%d", params->w, params->h);
171 sprintf(data + strlen(data), "d%c", slant_diffchars[params->diff]);
176 static config_item *game_configure(game_params *params)
181 ret = snewn(4, config_item);
183 ret[0].name = "Width";
184 ret[0].type = C_STRING;
185 sprintf(buf, "%d", params->w);
186 ret[0].sval = dupstr(buf);
189 ret[1].name = "Height";
190 ret[1].type = C_STRING;
191 sprintf(buf, "%d", params->h);
192 ret[1].sval = dupstr(buf);
195 ret[2].name = "Difficulty";
196 ret[2].type = C_CHOICES;
197 ret[2].sval = DIFFCONFIG;
198 ret[2].ival = params->diff;
208 static game_params *custom_params(config_item *cfg)
210 game_params *ret = snew(game_params);
212 ret->w = atoi(cfg[0].sval);
213 ret->h = atoi(cfg[1].sval);
214 ret->diff = cfg[2].ival;
219 static char *validate_params(game_params *params, int full)
222 * (At least at the time of writing this comment) The grid
223 * generator is actually capable of handling even zero grid
224 * dimensions without crashing. Puzzles with a zero-area grid
225 * are a bit boring, though, because they're already solved :-)
226 * And puzzles with a dimension of 1 can't be made Hard, which
227 * means the simplest thing is to forbid them altogether.
230 if (params->w < 2 || params->h < 2)
231 return "Width and height must both be at least two";
237 * Scratch space for solver.
239 struct solver_scratch {
241 * Disjoint set forest which tracks the connected sets of
247 * Counts the number of possible exits from each connected set
248 * of points. (That is, the number of possible _simultaneous_
249 * exits: an unconnected point labelled 2 has an exit count of
250 * 2 even if all four possible edges are still under
256 * Tracks whether each connected set of points includes a
259 unsigned char *border;
262 * Another disjoint set forest. This one tracks _squares_ which
263 * are known to slant in the same direction.
268 * Stores slash values which we know for an equivalence class.
269 * When we fill in a square, we set slashval[canonify(x)] to
270 * the same value as soln[x], so that we can then spot other
271 * squares equivalent to it and fill them in immediately via
272 * their known equivalence.
274 signed char *slashval;
277 * Stores possible v-shapes. This array is w by h in size, but
278 * not every bit of every entry is meaningful. The bits mean:
280 * - bit 0 for a square means that that square and the one to
281 * its right might form a v-shape between them
282 * - bit 1 for a square means that that square and the one to
283 * its right might form a ^-shape between them
284 * - bit 2 for a square means that that square and the one
285 * below it might form a >-shape between them
286 * - bit 3 for a square means that that square and the one
287 * below it might form a <-shape between them
289 * Any starting 1 or 3 clue rules out four bits in this array
290 * immediately; we can rule out further bits during play using
291 * partially filled 2 clues; whenever a pair of squares is
292 * known not to be _either_ kind of v-shape, we can mark them
295 unsigned char *vbitmap;
298 * Useful to have this information automatically passed to
299 * solver subroutines. (This pointer is not dynamically
300 * allocated by new_scratch and free_scratch.)
302 const signed char *clues;
305 static struct solver_scratch *new_scratch(int w, int h)
307 int W = w+1, H = h+1;
308 struct solver_scratch *ret = snew(struct solver_scratch);
309 ret->connected = snewn(W*H, int);
310 ret->exits = snewn(W*H, int);
311 ret->border = snewn(W*H, unsigned char);
312 ret->equiv = snewn(w*h, int);
313 ret->slashval = snewn(w*h, signed char);
314 ret->vbitmap = snewn(w*h, unsigned char);
318 static void free_scratch(struct solver_scratch *sc)
325 sfree(sc->connected);
330 * Wrapper on dsf_merge() which updates the `exits' and `border'
333 static void merge_vertices(int *connected,
334 struct solver_scratch *sc, int i, int j)
336 int exits = -1, border = FALSE; /* initialise to placate optimiser */
339 i = dsf_canonify(connected, i);
340 j = dsf_canonify(connected, j);
343 * We have used one possible exit from each of the two
344 * classes. Thus, the viable exit count of the new class is
345 * the sum of the old exit counts minus two.
347 exits = sc->exits[i] + sc->exits[j] - 2;
349 border = sc->border[i] || sc->border[j];
352 dsf_merge(connected, i, j);
355 i = dsf_canonify(connected, i);
356 sc->exits[i] = exits;
357 sc->border[i] = border;
362 * Called when we have just blocked one way out of a particular
363 * point. If that point is a non-clue point (thus has a variable
364 * number of exits), we have therefore decreased its potential exit
365 * count, so we must decrement the exit count for the group as a
368 static void decr_exits(struct solver_scratch *sc, int i)
370 if (sc->clues[i] < 0) {
371 i = dsf_canonify(sc->connected, i);
376 static void fill_square(int w, int h, int x, int y, int v,
378 int *connected, struct solver_scratch *sc)
380 int W = w+1 /*, H = h+1 */;
382 assert(x >= 0 && x < w && y >= 0 && y < h);
384 if (soln[y*w+x] != 0) {
385 return; /* do nothing */
388 #ifdef SOLVER_DIAGNOSTICS
390 printf(" placing %c in %d,%d\n", v == -1 ? '\\' : '/', x, y);
396 int c = dsf_canonify(sc->equiv, y*w+x);
401 merge_vertices(connected, sc, y*W+x, (y+1)*W+(x+1));
403 decr_exits(sc, y*W+(x+1));
404 decr_exits(sc, (y+1)*W+x);
407 merge_vertices(connected, sc, y*W+(x+1), (y+1)*W+x);
409 decr_exits(sc, y*W+x);
410 decr_exits(sc, (y+1)*W+(x+1));
415 static int vbitmap_clear(int w, int h, struct solver_scratch *sc,
416 int x, int y, int vbits, char *reason, ...)
418 int done_something = FALSE;
421 for (vbit = 1; vbit <= 8; vbit <<= 1)
422 if (vbits & sc->vbitmap[y*w+x] & vbit) {
423 done_something = TRUE;
424 #ifdef SOLVER_DIAGNOSTICS
428 printf("ruling out %c shape at (%d,%d)-(%d,%d) (",
429 "!v^!>!!!<"[vbit], x, y,
430 x+((vbit&0x3)!=0), y+((vbit&0xC)!=0));
432 va_start(ap, reason);
439 sc->vbitmap[y*w+x] &= ~vbit;
442 return done_something;
446 * Solver. Returns 0 for impossibility, 1 for success, 2 for
447 * ambiguity or failure to converge.
449 static int slant_solve(int w, int h, const signed char *clues,
450 signed char *soln, struct solver_scratch *sc,
453 int W = w+1, H = h+1;
460 memset(soln, 0, w*h);
465 * Establish a disjoint set forest for tracking connectedness
466 * between grid points.
468 for (i = 0; i < W*H; i++)
469 sc->connected[i] = i; /* initially all distinct */
472 * Establish a disjoint set forest for tracking which squares
473 * are known to slant in the same direction.
475 for (i = 0; i < w*h; i++)
476 sc->equiv[i] = i; /* initially all distinct */
479 * Clear the slashval array.
481 memset(sc->slashval, 0, w*h);
484 * Set up the vbitmap array. Initially all types of v are possible.
486 memset(sc->vbitmap, 0xF, w*h);
489 * Initialise the `exits' and `border' arrays. Theses is used
490 * to do second-order loop avoidance: the dual of the no loops
491 * constraint is that every point must be somehow connected to
492 * the border of the grid (otherwise there would be a solid
493 * loop around it which prevented this).
495 * I define a `dead end' to be a connected group of points
496 * which contains no border point, and which can form at most
497 * one new connection outside itself. Then I forbid placing an
498 * edge so that it connects together two dead-end groups, since
499 * this would yield a non-border-connected isolated subgraph
500 * with no further scope to extend it.
502 for (y = 0; y < H; y++)
503 for (x = 0; x < W; x++) {
504 if (y == 0 || y == H-1 || x == 0 || x == W-1)
505 sc->border[y*W+x] = TRUE;
507 sc->border[y*W+x] = FALSE;
509 if (clues[y*W+x] < 0)
510 sc->exits[y*W+x] = 4;
512 sc->exits[y*W+x] = clues[y*W+x];
516 * Repeatedly try to deduce something until we can't.
519 done_something = FALSE;
522 * Any clue point with the number of remaining lines equal
523 * to zero or to the number of remaining undecided
524 * neighbouring squares can be filled in completely.
526 for (y = 0; y < H; y++)
527 for (x = 0; x < W; x++) {
532 int nu, nl, c, s, eq, eq2, last, meq, mj1, mj2;
534 if ((c = clues[y*W+x]) < 0)
538 * We have a clue point. Start by listing its
539 * neighbouring squares, in order around the point,
540 * together with the type of slash that would be
541 * required in that square to connect to the point.
544 if (x > 0 && y > 0) {
545 neighbours[nneighbours].pos = (y-1)*w+(x-1);
546 neighbours[nneighbours].slash = -1;
549 if (x > 0 && y < h) {
550 neighbours[nneighbours].pos = y*w+(x-1);
551 neighbours[nneighbours].slash = +1;
554 if (x < w && y < h) {
555 neighbours[nneighbours].pos = y*w+x;
556 neighbours[nneighbours].slash = -1;
559 if (x < w && y > 0) {
560 neighbours[nneighbours].pos = (y-1)*w+x;
561 neighbours[nneighbours].slash = +1;
566 * Count up the number of undecided neighbours, and
567 * also the number of lines already present.
569 * If we're not on DIFF_EASY, then in this loop we
570 * also track whether we've seen two adjacent empty
571 * squares belonging to the same equivalence class
572 * (meaning they have the same type of slash). If
573 * so, we count them jointly as one line.
577 last = neighbours[nneighbours-1].pos;
579 eq = dsf_canonify(sc->equiv, last);
582 meq = mj1 = mj2 = -1;
583 for (i = 0; i < nneighbours; i++) {
584 j = neighbours[i].pos;
585 s = neighbours[i].slash;
587 nu++; /* undecided */
588 if (meq < 0 && difficulty > DIFF_EASY) {
589 eq2 = dsf_canonify(sc->equiv, j);
590 if (eq == eq2 && last != j) {
592 * We've found an equivalent pair.
593 * Mark it. This also inhibits any
594 * further equivalence tracking
595 * around this square, since we can
596 * only handle one pair (and in
597 * particular we want to avoid
598 * being misled by two overlapping
599 * equivalence pairs).
604 nl--; /* count one line */
605 nu -= 2; /* and lose two undecideds */
612 nl--; /* here's a line */
620 if (nl < 0 || nl > nu) {
622 * No consistent value for this at all!
624 #ifdef SOLVER_DIAGNOSTICS
626 printf("need %d / %d lines around clue point at %d,%d!\n",
629 return 0; /* impossible */
632 if (nu > 0 && (nl == 0 || nl == nu)) {
633 #ifdef SOLVER_DIAGNOSTICS
636 printf("partially (since %d,%d == %d,%d) ",
637 mj1%w, mj1/w, mj2%w, mj2/w);
638 printf("%s around clue point at %d,%d\n",
639 nl ? "filling" : "emptying", x, y);
642 for (i = 0; i < nneighbours; i++) {
643 j = neighbours[i].pos;
644 s = neighbours[i].slash;
645 if (soln[j] == 0 && j != mj1 && j != mj2)
646 fill_square(w, h, j%w, j/w, (nl ? s : -s), soln,
650 done_something = TRUE;
651 } else if (nu == 2 && nl == 1 && difficulty > DIFF_EASY) {
653 * If we have precisely two undecided squares
654 * and precisely one line to place between
655 * them, _and_ those squares are adjacent, then
656 * we can mark them as equivalent to one
659 * This even applies if meq >= 0: if we have a
660 * 2 clue point and two of its neighbours are
661 * already marked equivalent, we can indeed
662 * mark the other two as equivalent.
664 * We don't bother with this on DIFF_EASY,
665 * since we wouldn't have used the results
669 for (i = 0; i < nneighbours; i++) {
670 j = neighbours[i].pos;
671 if (soln[j] == 0 && j != mj1 && j != mj2) {
674 else if (last == i-1 || (last == 0 && i == 3))
675 break; /* found a pair */
678 if (i < nneighbours) {
683 * neighbours[last] and neighbours[i] are
684 * the pair. Mark them equivalent.
686 #ifdef SOLVER_DIAGNOSTICS
689 printf("since %d,%d == %d,%d, ",
690 mj1%w, mj1/w, mj2%w, mj2/w);
693 mj1 = neighbours[last].pos;
694 mj2 = neighbours[i].pos;
695 #ifdef SOLVER_DIAGNOSTICS
697 printf("clue point at %d,%d implies %d,%d == %d,"
698 "%d\n", x, y, mj1%w, mj1/w, mj2%w, mj2/w);
700 mj1 = dsf_canonify(sc->equiv, mj1);
701 sv1 = sc->slashval[mj1];
702 mj2 = dsf_canonify(sc->equiv, mj2);
703 sv2 = sc->slashval[mj2];
704 if (sv1 != 0 && sv2 != 0 && sv1 != sv2) {
705 #ifdef SOLVER_DIAGNOSTICS
707 printf("merged two equivalence classes with"
708 " different slash values!\n");
712 sv1 = sv1 ? sv1 : sv2;
713 dsf_merge(sc->equiv, mj1, mj2);
714 mj1 = dsf_canonify(sc->equiv, mj1);
715 sc->slashval[mj1] = sv1;
724 * Failing that, we now apply the second condition, which
725 * is that no square may be filled in such a way as to form
726 * a loop. Also in this loop (since it's over squares
727 * rather than points), we check slashval to see if we've
728 * already filled in another square in the same equivalence
731 * The slashval check is disabled on DIFF_EASY, as is dead
732 * end avoidance. Only _immediate_ loop avoidance remains.
734 for (y = 0; y < h; y++)
735 for (x = 0; x < w; x++) {
738 #ifdef SOLVER_DIAGNOSTICS
739 char *reason = "<internal error>";
743 continue; /* got this one already */
748 if (difficulty > DIFF_EASY)
749 v = sc->slashval[dsf_canonify(sc->equiv, y*w+x)];
754 * Try to rule out connectivity between (x,y) and
755 * (x+1,y+1); if successful, we will deduce that we
756 * must have a forward slash.
758 c1 = dsf_canonify(sc->connected, y*W+x);
759 c2 = dsf_canonify(sc->connected, (y+1)*W+(x+1));
762 #ifdef SOLVER_DIAGNOSTICS
763 reason = "simple loop avoidance";
766 if (difficulty > DIFF_EASY &&
767 !sc->border[c1] && !sc->border[c2] &&
768 sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
770 #ifdef SOLVER_DIAGNOSTICS
771 reason = "dead end avoidance";
776 #ifdef SOLVER_DIAGNOSTICS
777 reason = "equivalence to an already filled square";
782 * Now do the same between (x+1,y) and (x,y+1), to
783 * see if we are required to have a backslash.
785 c1 = dsf_canonify(sc->connected, y*W+(x+1));
786 c2 = dsf_canonify(sc->connected, (y+1)*W+x);
789 #ifdef SOLVER_DIAGNOSTICS
790 reason = "simple loop avoidance";
793 if (difficulty > DIFF_EASY &&
794 !sc->border[c1] && !sc->border[c2] &&
795 sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
797 #ifdef SOLVER_DIAGNOSTICS
798 reason = "dead end avoidance";
803 #ifdef SOLVER_DIAGNOSTICS
804 reason = "equivalence to an already filled square";
810 * No consistent value for this at all!
812 #ifdef SOLVER_DIAGNOSTICS
814 printf("%d,%d has no consistent slash!\n", x, y);
816 return 0; /* impossible */
820 #ifdef SOLVER_DIAGNOSTICS
822 printf("employing %s\n", reason);
824 fill_square(w, h, x, y, +1, soln, sc->connected, sc);
825 done_something = TRUE;
827 #ifdef SOLVER_DIAGNOSTICS
829 printf("employing %s\n", reason);
831 fill_square(w, h, x, y, -1, soln, sc->connected, sc);
832 done_something = TRUE;
840 * Now see what we can do with the vbitmap array. All
841 * vbitmap deductions are disabled at Easy level.
843 if (difficulty <= DIFF_EASY)
846 for (y = 0; y < h; y++)
847 for (x = 0; x < w; x++) {
851 * Any line already placed in a square must rule
852 * out any type of v which contradicts it.
854 if ((s = soln[y*w+x]) != 0) {
857 vbitmap_clear(w, h, sc, x-1, y, (s < 0 ? 0x1 : 0x2),
858 "contradicts known edge at (%d,%d)",x,y);
861 vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x2 : 0x1),
862 "contradicts known edge at (%d,%d)",x,y);
865 vbitmap_clear(w, h, sc, x, y-1, (s < 0 ? 0x4 : 0x8),
866 "contradicts known edge at (%d,%d)",x,y);
869 vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x8 : 0x4),
870 "contradicts known edge at (%d,%d)",x,y);
874 * If both types of v are ruled out for a pair of
875 * adjacent squares, mark them as equivalent.
877 if (x+1 < w && !(sc->vbitmap[y*w+x] & 0x3)) {
878 int n1 = y*w+x, n2 = y*w+(x+1);
879 if (dsf_canonify(sc->equiv, n1) !=
880 dsf_canonify(sc->equiv, n2)) {
881 dsf_merge(sc->equiv, n1, n2);
882 done_something = TRUE;
883 #ifdef SOLVER_DIAGNOSTICS
885 printf("(%d,%d) and (%d,%d) must be equivalent"
886 " because both v-shapes are ruled out\n",
891 if (y+1 < h && !(sc->vbitmap[y*w+x] & 0xC)) {
892 int n1 = y*w+x, n2 = (y+1)*w+x;
893 if (dsf_canonify(sc->equiv, n1) !=
894 dsf_canonify(sc->equiv, n2)) {
895 dsf_merge(sc->equiv, n1, n2);
896 done_something = TRUE;
897 #ifdef SOLVER_DIAGNOSTICS
899 printf("(%d,%d) and (%d,%d) must be equivalent"
900 " because both v-shapes are ruled out\n",
907 * The remaining work in this loop only works
908 * around non-edge clue points.
910 if (y == 0 || x == 0)
912 if ((c = clues[y*W+x]) < 0)
916 * x,y marks a clue point not on the grid edge. See
917 * if this clue point allows us to rule out any v
923 * A 1 clue can never have any v shape pointing
927 vbitmap_clear(w, h, sc, x-1, y-1, 0x5,
928 "points at 1 clue at (%d,%d)", x, y);
930 vbitmap_clear(w, h, sc, x-1, y, 0x2,
931 "points at 1 clue at (%d,%d)", x, y);
933 vbitmap_clear(w, h, sc, x, y-1, 0x8,
934 "points at 1 clue at (%d,%d)", x, y);
937 * A 3 clue can never have any v shape pointing
941 vbitmap_clear(w, h, sc, x-1, y-1, 0xA,
942 "points away from 3 clue at (%d,%d)", x, y);
944 vbitmap_clear(w, h, sc, x-1, y, 0x1,
945 "points away from 3 clue at (%d,%d)", x, y);
947 vbitmap_clear(w, h, sc, x, y-1, 0x4,
948 "points away from 3 clue at (%d,%d)", x, y);
951 * If a 2 clue has any kind of v ruled out on
952 * one side of it, the same v is ruled out on
956 vbitmap_clear(w, h, sc, x-1, y-1,
957 (sc->vbitmap[(y )*w+(x-1)] & 0x3) ^ 0x3,
958 "propagated by 2 clue at (%d,%d)", x, y);
960 vbitmap_clear(w, h, sc, x-1, y-1,
961 (sc->vbitmap[(y-1)*w+(x )] & 0xC) ^ 0xC,
962 "propagated by 2 clue at (%d,%d)", x, y);
964 vbitmap_clear(w, h, sc, x-1, y,
965 (sc->vbitmap[(y-1)*w+(x-1)] & 0x3) ^ 0x3,
966 "propagated by 2 clue at (%d,%d)", x, y);
968 vbitmap_clear(w, h, sc, x, y-1,
969 (sc->vbitmap[(y-1)*w+(x-1)] & 0xC) ^ 0xC,
970 "propagated by 2 clue at (%d,%d)", x, y);
977 } while (done_something);
980 * Solver can make no more progress. See if the grid is full.
982 for (i = 0; i < w*h; i++)
984 return 2; /* failed to converge */
985 return 1; /* success */
989 * Filled-grid generator.
991 static void slant_generate(int w, int h, signed char *soln, random_state *rs)
993 int W = w+1, H = h+1;
995 int *connected, *indices;
1000 memset(soln, 0, w*h);
1003 * Establish a disjoint set forest for tracking connectedness
1004 * between grid points.
1006 connected = snewn(W*H, int);
1007 for (i = 0; i < W*H; i++)
1008 connected[i] = i; /* initially all distinct */
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(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(game_params *params, 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, game_params *params, char *desc)
1246 int w = params->w, h = params->h, W = w+1, H = h+1;
1247 game_state *state = snew(game_state);
1252 state->soln = snewn(w*h, signed char);
1253 memset(state->soln, 0, w*h);
1254 state->completed = state->used_solve = FALSE;
1255 state->errors = snewn(W*H, unsigned char);
1256 memset(state->errors, 0, W*H);
1258 state->clues = snew(game_clues);
1259 state->clues->w = w;
1260 state->clues->h = h;
1261 state->clues->clues = snewn(W*H, signed char);
1262 state->clues->refcount = 1;
1263 state->clues->tmpdsf = snewn(W*H, int);
1264 memset(state->clues->clues, -1, W*H);
1267 if (n >= 'a' && n <= 'z') {
1268 squares += n - 'a' + 1;
1269 } else if (n >= '0' && n <= '4') {
1270 state->clues->clues[squares++] = n - '0';
1272 assert(!"can't get here");
1274 assert(squares == area);
1279 static game_state *dup_game(game_state *state)
1281 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1282 game_state *ret = snew(game_state);
1285 ret->clues = state->clues;
1286 ret->clues->refcount++;
1287 ret->completed = state->completed;
1288 ret->used_solve = state->used_solve;
1290 ret->soln = snewn(w*h, signed char);
1291 memcpy(ret->soln, state->soln, w*h);
1293 ret->errors = snewn(W*H, unsigned char);
1294 memcpy(ret->errors, state->errors, W*H);
1299 static void free_game(game_state *state)
1301 sfree(state->errors);
1303 assert(state->clues);
1304 if (--state->clues->refcount <= 0) {
1305 sfree(state->clues->clues);
1306 sfree(state->clues->tmpdsf);
1307 sfree(state->clues);
1313 * Utility function to return the current degree of a vertex. If
1314 * `anti' is set, it returns the number of filled-in edges
1315 * surrounding the point which _don't_ connect to it; thus 4 minus
1316 * its anti-degree is the maximum degree it could have if all the
1317 * empty spaces around it were filled in.
1319 * (Yes, _4_ minus its anti-degree even if it's a border vertex.)
1321 * If ret > 0, *sx and *sy are set to the coordinates of one of the
1322 * squares that contributed to it.
1324 static int vertex_degree(int w, int h, signed char *soln, int x, int y,
1325 int anti, int *sx, int *sy)
1329 assert(x >= 0 && x <= w && y >= 0 && y <= h);
1330 if (x > 0 && y > 0 && soln[(y-1)*w+(x-1)] - anti < 0) {
1335 if (x > 0 && y < h && soln[y*w+(x-1)] + anti > 0) {
1340 if (x < w && y > 0 && soln[(y-1)*w+x] + anti > 0) {
1345 if (x < w && y < h && soln[y*w+x] - anti < 0) {
1351 return anti ? 4 - ret : ret;
1354 static int check_completion(game_state *state)
1356 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1357 int i, x, y, err = FALSE;
1360 memset(state->errors, 0, W*H);
1363 * To detect loops in the grid, we iterate through each edge
1364 * building up a dsf of connected components, and raise the
1365 * alarm whenever we find an edge that connects two
1366 * already-connected vertices.
1368 * We use the `tmpdsf' scratch space in the shared clues
1369 * structure, to avoid mallocing too often.
1371 * When we find such an edge, we then search around the grid to
1372 * find the loop it is a part of, so that we can highlight it
1373 * as an error for the user. We do this by the hand-on-one-wall
1374 * technique: the search will follow branches off the inside of
1375 * the loop, discover they're dead ends, and unhighlight them
1376 * again when returning to the actual loop.
1378 * This technique guarantees that every loop it tracks will
1379 * surround a disjoint area of the grid (since if an existing
1380 * loop appears on the boundary of a new one, so that there are
1381 * multiple possible paths that would come back to the starting
1382 * point, it will pick the one that allows it to turn right
1383 * most sharply and hence the one that does not re-surround the
1384 * area of the previous one). Thus, the total time taken in
1385 * searching round loops is linear in the grid area since every
1386 * edge is visited at most twice.
1388 dsf = state->clues->tmpdsf;
1389 for (i = 0; i < W*H; i++)
1390 dsf[i] = i; /* initially all distinct */
1391 for (y = 0; y < h; y++)
1392 for (x = 0; x < w; x++) {
1395 if (state->soln[y*w+x] == 0)
1397 if (state->soln[y*w+x] < 0) {
1406 * Our edge connects i1 with i2. If they're already
1407 * connected, flag an error. Otherwise, link them.
1409 if (dsf_canonify(dsf, i1) == dsf_canonify(dsf, i2)) {
1410 int x1, y1, x2, y2, dx, dy, dt, pass;
1415 * Now search around the boundary of the loop to
1418 * We have to do this in two passes. The first
1419 * time, we toggle ERR_SQUARE_TMP on each edge;
1420 * this pass terminates with ERR_SQUARE_TMP set on
1421 * exactly the loop edges. In the second pass, we
1422 * trace round that loop again and turn
1423 * ERR_SQUARE_TMP into ERR_SQUARE. We have to do
1424 * this because otherwise we might cancel part of a
1425 * loop highlighted in a previous iteration of the
1429 for (pass = 0; pass < 2; pass++) {
1437 /* Mark this edge. */
1439 state->errors[min(y1,y2)*W+min(x1,x2)] ^=
1442 state->errors[min(y1,y2)*W+min(x1,x2)] |=
1444 state->errors[min(y1,y2)*W+min(x1,x2)] &=
1449 * Progress to the next edge by turning as
1450 * sharply right as possible. In fact we do
1451 * this by facing back along the edge and
1452 * turning _left_ until we see an edge we
1458 for (i = 0; i < 4; i++) {
1460 * Rotate (dx,dy) to the left.
1462 dt = dx; dx = dy; dy = -dt;
1465 * See if (x2,y2) has an edge in direction
1468 if (x2+dx < 0 || x2+dx >= W ||
1469 y2+dy < 0 || y2+dy >= H)
1470 continue; /* off the side of the grid */
1471 /* In the second pass, ignore unmarked edges. */
1473 !(state->errors[(y2-(dy<0))*W+x2-(dx<0)] &
1476 if (state->soln[(y2-(dy<0))*w+x2-(dx<0)] ==
1482 * In pass 0, we expect to have found
1483 * _some_ edge we can follow, even if it
1484 * was found by rotating all the way round
1485 * and going back the way we came.
1487 * In pass 1, because we're removing the
1488 * mark on each edge that allows us to
1489 * follow it, we expect to find _no_ edge
1490 * we can follow when we've come all the
1491 * way round the loop.
1493 if (pass == 1 && i == 4)
1498 * Set x1,y1 to x2,y2, and x2,y2 to be the
1499 * other end of the new edge.
1505 } while (y2*W+x2 != i2);
1510 dsf_merge(dsf, i1, i2);
1514 * Now go through and check the degree of each clue vertex, and
1515 * mark it with ERR_VERTEX if it cannot be fulfilled.
1517 for (y = 0; y < H; y++)
1518 for (x = 0; x < W; x++) {
1521 if ((c = state->clues->clues[y*W+x]) < 0)
1525 * Check to see if there are too many connections to
1526 * this vertex _or_ too many non-connections. Either is
1527 * grounds for marking the vertex as erroneous.
1529 if (vertex_degree(w, h, state->soln, x, y,
1530 FALSE, NULL, NULL) > c ||
1531 vertex_degree(w, h, state->soln, x, y,
1532 TRUE, NULL, NULL) > 4-c) {
1533 state->errors[y*W+x] |= ERR_VERTEX;
1539 * Now our actual victory condition is that (a) none of the
1540 * above code marked anything as erroneous, and (b) every
1541 * square has an edge in it.
1547 for (y = 0; y < h; y++)
1548 for (x = 0; x < w; x++)
1549 if (state->soln[y*w+x] == 0)
1555 static char *solve_game(game_state *state, game_state *currstate,
1556 char *aux, char **error)
1558 int w = state->p.w, h = state->p.h;
1561 int free_soln = FALSE;
1562 char *move, buf[80];
1563 int movelen, movesize;
1568 * If we already have the solution, save ourselves some
1571 soln = (signed char *)aux;
1572 bs = (signed char)'\\';
1575 struct solver_scratch *sc = new_scratch(w, h);
1576 soln = snewn(w*h, signed char);
1578 ret = slant_solve(w, h, state->clues->clues, soln, sc, DIFF_HARD);
1583 *error = "This puzzle is not self-consistent";
1585 *error = "Unable to find a unique solution for this puzzle";
1592 * Construct a move string which turns the current state into
1596 move = snewn(movesize, char);
1598 move[movelen++] = 'S';
1599 move[movelen] = '\0';
1600 for (y = 0; y < h; y++)
1601 for (x = 0; x < w; x++) {
1602 int v = (soln[y*w+x] == bs ? -1 : +1);
1603 if (state->soln[y*w+x] != v) {
1604 int len = sprintf(buf, ";%c%d,%d", (int)(v < 0 ? '\\' : '/'), x, y);
1605 if (movelen + len >= movesize) {
1606 movesize = movelen + len + 256;
1607 move = sresize(move, movesize, char);
1609 strcpy(move + movelen, buf);
1620 static char *game_text_format(game_state *state)
1622 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1627 * There are h+H rows of w+W columns.
1629 len = (h+H) * (w+W+1) + 1;
1630 ret = snewn(len, char);
1633 for (y = 0; y < H; y++) {
1634 for (x = 0; x < W; x++) {
1635 if (state->clues->clues[y*W+x] >= 0)
1636 *p++ = state->clues->clues[y*W+x] + '0';
1644 for (x = 0; x < W; x++) {
1647 if (state->soln[y*w+x] != 0)
1648 *p++ = (state->soln[y*w+x] < 0 ? '\\' : '/');
1658 assert(p - ret == len);
1662 static game_ui *new_ui(game_state *state)
1667 static void free_ui(game_ui *ui)
1671 static char *encode_ui(game_ui *ui)
1676 static void decode_ui(game_ui *ui, char *encoding)
1680 static void game_changed_state(game_ui *ui, game_state *oldstate,
1681 game_state *newstate)
1685 #define PREFERRED_TILESIZE 32
1686 #define TILESIZE (ds->tilesize)
1687 #define BORDER TILESIZE
1688 #define CLUE_RADIUS (TILESIZE / 3)
1689 #define CLUE_TEXTSIZE (TILESIZE / 2)
1690 #define COORD(x) ( (x) * TILESIZE + BORDER )
1691 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1693 #define FLASH_TIME 0.30F
1696 * Bit fields in the `grid' and `todraw' elements of the drawstate.
1698 #define BACKSLASH 0x00000001L
1699 #define FORWSLASH 0x00000002L
1700 #define L_T 0x00000004L
1701 #define ERR_L_T 0x00000008L
1702 #define L_B 0x00000010L
1703 #define ERR_L_B 0x00000020L
1704 #define T_L 0x00000040L
1705 #define ERR_T_L 0x00000080L
1706 #define T_R 0x00000100L
1707 #define ERR_T_R 0x00000200L
1708 #define C_TL 0x00000400L
1709 #define ERR_C_TL 0x00000800L
1710 #define FLASH 0x00001000L
1711 #define ERRSLASH 0x00002000L
1712 #define ERR_TL 0x00004000L
1713 #define ERR_TR 0x00008000L
1714 #define ERR_BL 0x00010000L
1715 #define ERR_BR 0x00020000L
1717 struct game_drawstate {
1724 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
1725 int x, int y, int button)
1727 int w = state->p.w, h = state->p.h;
1729 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
1734 * This is an utterly awful hack which I should really sort out
1735 * by means of a proper configuration mechanism. One Slant
1736 * player has observed that they prefer the mouse buttons to
1737 * function exactly the opposite way round, so here's a
1738 * mechanism for environment-based configuration. I cache the
1739 * result in a global variable - yuck! - to avoid repeated
1743 static int swap_buttons = -1;
1744 if (swap_buttons < 0) {
1745 char *env = getenv("SLANT_SWAP_BUTTONS");
1746 swap_buttons = (env && (env[0] == 'y' || env[0] == 'Y'));
1749 if (button == LEFT_BUTTON)
1750 button = RIGHT_BUTTON;
1752 button = LEFT_BUTTON;
1758 if (x < 0 || y < 0 || x >= w || y >= h)
1761 if (button == LEFT_BUTTON) {
1763 * Left-clicking cycles blank -> \ -> / -> blank.
1765 v = state->soln[y*w+x] - 1;
1770 * Right-clicking cycles blank -> / -> \ -> blank.
1772 v = state->soln[y*w+x] + 1;
1777 sprintf(buf, "%c%d,%d", (int)(v==-1 ? '\\' : v==+1 ? '/' : 'C'), x, y);
1784 static game_state *execute_move(game_state *state, char *move)
1786 int w = state->p.w, h = state->p.h;
1789 game_state *ret = dup_game(state);
1794 ret->used_solve = TRUE;
1796 } else if (c == '\\' || c == '/' || c == 'C') {
1798 if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
1799 x < 0 || y < 0 || x >= w || y >= h) {
1803 ret->soln[y*w+x] = (c == '\\' ? -1 : c == '/' ? +1 : 0);
1818 * We never clear the `completed' flag, but we must always
1819 * re-run the completion check because it also highlights
1820 * errors in the grid.
1822 ret->completed = check_completion(ret) || ret->completed;
1827 /* ----------------------------------------------------------------------
1831 static void game_compute_size(game_params *params, int tilesize,
1834 /* fool the macros */
1835 struct dummy { int tilesize; } dummy = { tilesize }, *ds = &dummy;
1837 *x = 2 * BORDER + params->w * TILESIZE + 1;
1838 *y = 2 * BORDER + params->h * TILESIZE + 1;
1841 static void game_set_size(drawing *dr, game_drawstate *ds,
1842 game_params *params, int tilesize)
1844 ds->tilesize = tilesize;
1847 static float *game_colours(frontend *fe, int *ncolours)
1849 float *ret = snewn(3 * NCOLOURS, float);
1851 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1853 ret[COL_GRID * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.7F;
1854 ret[COL_GRID * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.7F;
1855 ret[COL_GRID * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.7F;
1857 ret[COL_INK * 3 + 0] = 0.0F;
1858 ret[COL_INK * 3 + 1] = 0.0F;
1859 ret[COL_INK * 3 + 2] = 0.0F;
1861 ret[COL_SLANT1 * 3 + 0] = 0.0F;
1862 ret[COL_SLANT1 * 3 + 1] = 0.0F;
1863 ret[COL_SLANT1 * 3 + 2] = 0.0F;
1865 ret[COL_SLANT2 * 3 + 0] = 0.0F;
1866 ret[COL_SLANT2 * 3 + 1] = 0.0F;
1867 ret[COL_SLANT2 * 3 + 2] = 0.0F;
1869 ret[COL_ERROR * 3 + 0] = 1.0F;
1870 ret[COL_ERROR * 3 + 1] = 0.0F;
1871 ret[COL_ERROR * 3 + 2] = 0.0F;
1873 *ncolours = NCOLOURS;
1877 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1879 int w = state->p.w, h = state->p.h;
1881 struct game_drawstate *ds = snew(struct game_drawstate);
1884 ds->started = FALSE;
1885 ds->grid = snewn((w+2)*(h+2), long);
1886 ds->todraw = snewn((w+2)*(h+2), long);
1887 for (i = 0; i < (w+2)*(h+2); i++)
1888 ds->grid[i] = ds->todraw[i] = -1;
1893 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1900 static void draw_clue(drawing *dr, game_drawstate *ds,
1901 int x, int y, long v, long err, int bg, int colour)
1904 int ccol = colour >= 0 ? colour : ((x ^ y) & 1) ? COL_SLANT1 : COL_SLANT2;
1905 int tcol = colour >= 0 ? colour : err ? COL_ERROR : COL_INK;
1912 draw_circle(dr, COORD(x), COORD(y), CLUE_RADIUS,
1913 bg >= 0 ? bg : COL_BACKGROUND, ccol);
1914 draw_text(dr, COORD(x), COORD(y), FONT_VARIABLE,
1915 CLUE_TEXTSIZE, ALIGN_VCENTRE|ALIGN_HCENTRE, tcol, p);
1918 static void draw_tile(drawing *dr, game_drawstate *ds, game_clues *clues,
1919 int x, int y, long v)
1921 int w = clues->w, h = clues->h, W = w+1 /*, H = h+1 */;
1922 int chesscolour = (x ^ y) & 1;
1923 int fscol = chesscolour ? COL_SLANT2 : COL_SLANT1;
1924 int bscol = chesscolour ? COL_SLANT1 : COL_SLANT2;
1926 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
1928 draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
1929 (v & FLASH) ? COL_GRID : COL_BACKGROUND);
1932 * Draw the grid lines.
1934 if (x >= 0 && x < w && y >= 0)
1935 draw_rect(dr, COORD(x), COORD(y), TILESIZE+1, 1, COL_GRID);
1936 if (x >= 0 && x < w && y < h)
1937 draw_rect(dr, COORD(x), COORD(y+1), TILESIZE+1, 1, COL_GRID);
1938 if (y >= 0 && y < h && x >= 0)
1939 draw_rect(dr, COORD(x), COORD(y), 1, TILESIZE+1, COL_GRID);
1940 if (y >= 0 && y < h && x < w)
1941 draw_rect(dr, COORD(x+1), COORD(y), 1, TILESIZE+1, COL_GRID);
1942 if (x == -1 && y == -1)
1943 draw_rect(dr, COORD(x+1), COORD(y+1), 1, 1, COL_GRID);
1944 if (x == -1 && y == h)
1945 draw_rect(dr, COORD(x+1), COORD(y), 1, 1, COL_GRID);
1946 if (x == w && y == -1)
1947 draw_rect(dr, COORD(x), COORD(y+1), 1, 1, COL_GRID);
1948 if (x == w && y == h)
1949 draw_rect(dr, COORD(x), COORD(y), 1, 1, COL_GRID);
1954 if (v & BACKSLASH) {
1955 int scol = (v & ERRSLASH) ? COL_ERROR : bscol;
1956 draw_line(dr, COORD(x), COORD(y), COORD(x+1), COORD(y+1), scol);
1957 draw_line(dr, COORD(x)+1, COORD(y), COORD(x+1), COORD(y+1)-1,
1959 draw_line(dr, COORD(x), COORD(y)+1, COORD(x+1)-1, COORD(y+1),
1961 } else if (v & FORWSLASH) {
1962 int scol = (v & ERRSLASH) ? COL_ERROR : fscol;
1963 draw_line(dr, COORD(x+1), COORD(y), COORD(x), COORD(y+1), scol);
1964 draw_line(dr, COORD(x+1)-1, COORD(y), COORD(x), COORD(y+1)-1,
1966 draw_line(dr, COORD(x+1), COORD(y)+1, COORD(x)+1, COORD(y+1),
1971 * Draw dots on the grid corners that appear if a slash is in a
1972 * neighbouring cell.
1974 if (v & (L_T | BACKSLASH))
1975 draw_rect(dr, COORD(x), COORD(y)+1, 1, 1,
1976 (v & ERR_L_T ? COL_ERROR : bscol));
1977 if (v & (L_B | FORWSLASH))
1978 draw_rect(dr, COORD(x), COORD(y+1)-1, 1, 1,
1979 (v & ERR_L_B ? COL_ERROR : fscol));
1980 if (v & (T_L | BACKSLASH))
1981 draw_rect(dr, COORD(x)+1, COORD(y), 1, 1,
1982 (v & ERR_T_L ? COL_ERROR : bscol));
1983 if (v & (T_R | FORWSLASH))
1984 draw_rect(dr, COORD(x+1)-1, COORD(y), 1, 1,
1985 (v & ERR_T_R ? COL_ERROR : fscol));
1986 if (v & (C_TL | BACKSLASH))
1987 draw_rect(dr, COORD(x), COORD(y), 1, 1,
1988 (v & ERR_C_TL ? COL_ERROR : bscol));
1991 * And finally the clues at the corners.
1993 if (x >= 0 && y >= 0)
1994 draw_clue(dr, ds, x, y, clues->clues[y*W+x], v & ERR_TL, -1, -1);
1995 if (x < w && y >= 0)
1996 draw_clue(dr, ds, x+1, y, clues->clues[y*W+(x+1)], v & ERR_TR, -1, -1);
1997 if (x >= 0 && y < h)
1998 draw_clue(dr, ds, x, y+1, clues->clues[(y+1)*W+x], v & ERR_BL, -1, -1);
2000 draw_clue(dr, ds, x+1, y+1, clues->clues[(y+1)*W+(x+1)], v & ERR_BR,
2004 draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2007 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2008 game_state *state, int dir, game_ui *ui,
2009 float animtime, float flashtime)
2011 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
2016 flashing = (int)(flashtime * 3 / FLASH_TIME) != 1;
2022 game_compute_size(&state->p, TILESIZE, &ww, &wh);
2023 draw_rect(dr, 0, 0, ww, wh, COL_BACKGROUND);
2024 draw_update(dr, 0, 0, ww, wh);
2029 * Loop over the grid and work out where all the slashes are.
2030 * We need to do this because a slash in one square affects the
2031 * drawing of the next one along.
2033 for (y = -1; y <= h; y++)
2034 for (x = -1; x <= w; x++) {
2035 if (x >= 0 && x < w && y >= 0 && y < h)
2036 ds->todraw[(y+1)*(w+2)+(x+1)] = flashing ? FLASH : 0;
2038 ds->todraw[(y+1)*(w+2)+(x+1)] = 0;
2041 for (y = 0; y < h; y++) {
2042 for (x = 0; x < w; x++) {
2043 int err = state->errors[y*W+x] & ERR_SQUARE;
2045 if (state->soln[y*w+x] < 0) {
2046 ds->todraw[(y+1)*(w+2)+(x+1)] |= BACKSLASH;
2047 ds->todraw[(y+2)*(w+2)+(x+1)] |= T_R;
2048 ds->todraw[(y+1)*(w+2)+(x+2)] |= L_B;
2049 ds->todraw[(y+2)*(w+2)+(x+2)] |= C_TL;
2051 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERRSLASH |
2052 ERR_T_L | ERR_L_T | ERR_C_TL;
2053 ds->todraw[(y+2)*(w+2)+(x+1)] |= ERR_T_R;
2054 ds->todraw[(y+1)*(w+2)+(x+2)] |= ERR_L_B;
2055 ds->todraw[(y+2)*(w+2)+(x+2)] |= ERR_C_TL;
2057 } else if (state->soln[y*w+x] > 0) {
2058 ds->todraw[(y+1)*(w+2)+(x+1)] |= FORWSLASH;
2059 ds->todraw[(y+1)*(w+2)+(x+2)] |= L_T | C_TL;
2060 ds->todraw[(y+2)*(w+2)+(x+1)] |= T_L | C_TL;
2062 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERRSLASH |
2064 ds->todraw[(y+1)*(w+2)+(x+2)] |= ERR_L_T | ERR_C_TL;
2065 ds->todraw[(y+2)*(w+2)+(x+1)] |= ERR_T_L | ERR_C_TL;
2071 for (y = 0; y < H; y++)
2072 for (x = 0; x < W; x++)
2073 if (state->errors[y*W+x] & ERR_VERTEX) {
2074 ds->todraw[y*(w+2)+x] |= ERR_BR;
2075 ds->todraw[y*(w+2)+(x+1)] |= ERR_BL;
2076 ds->todraw[(y+1)*(w+2)+x] |= ERR_TR;
2077 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERR_TL;
2081 * Now go through and draw the grid squares.
2083 for (y = -1; y <= h; y++) {
2084 for (x = -1; x <= w; x++) {
2085 if (ds->todraw[(y+1)*(w+2)+(x+1)] != ds->grid[(y+1)*(w+2)+(x+1)]) {
2086 draw_tile(dr, ds, state->clues, x, y,
2087 ds->todraw[(y+1)*(w+2)+(x+1)]);
2088 ds->grid[(y+1)*(w+2)+(x+1)] = ds->todraw[(y+1)*(w+2)+(x+1)];
2094 static float game_anim_length(game_state *oldstate, game_state *newstate,
2095 int dir, game_ui *ui)
2100 static float game_flash_length(game_state *oldstate, game_state *newstate,
2101 int dir, game_ui *ui)
2103 if (!oldstate->completed && newstate->completed &&
2104 !oldstate->used_solve && !newstate->used_solve)
2110 static int game_timing_state(game_state *state, game_ui *ui)
2115 static void game_print_size(game_params *params, float *x, float *y)
2120 * I'll use 6mm squares by default.
2122 game_compute_size(params, 600, &pw, &ph);
2127 static void game_print(drawing *dr, game_state *state, int tilesize)
2129 int w = state->p.w, h = state->p.h, W = w+1;
2130 int ink = print_mono_colour(dr, 0);
2131 int paper = print_mono_colour(dr, 1);
2134 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2135 game_drawstate ads, *ds = &ads;
2136 game_set_size(dr, ds, NULL, tilesize);
2141 print_line_width(dr, TILESIZE / 16);
2142 draw_rect_outline(dr, COORD(0), COORD(0), w*TILESIZE, h*TILESIZE, ink);
2147 print_line_width(dr, TILESIZE / 24);
2148 for (x = 1; x < w; x++)
2149 draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), ink);
2150 for (y = 1; y < h; y++)
2151 draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), ink);
2156 print_line_width(dr, TILESIZE / 12);
2157 for (y = 0; y < h; y++)
2158 for (x = 0; x < w; x++)
2159 if (state->soln[y*w+x]) {
2162 * To prevent nasty line-ending artefacts at
2163 * corners, I'll do something slightly cunning
2166 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2167 if (state->soln[y*w+x] < 0)
2171 draw_line(dr, COORD(x-1), COORD(ly), COORD(x+2), COORD(ry),
2179 print_line_width(dr, TILESIZE / 24);
2180 for (y = 0; y <= h; y++)
2181 for (x = 0; x <= w; x++)
2182 draw_clue(dr, ds, x, y, state->clues->clues[y*W+x],
2187 #define thegame slant
2190 const struct game thegame = {
2191 "Slant", "games.slant",
2198 TRUE, game_configure, custom_params,
2206 TRUE, game_text_format,
2214 PREFERRED_TILESIZE, game_compute_size, game_set_size,
2217 game_free_drawstate,
2221 TRUE, FALSE, game_print_size, game_print,
2222 FALSE, /* wants_statusbar */
2223 FALSE, game_timing_state,
2227 #ifdef STANDALONE_SOLVER
2231 int main(int argc, char **argv)
2235 char *id = NULL, *desc, *err;
2237 int ret, diff, really_verbose = FALSE;
2238 struct solver_scratch *sc;
2240 while (--argc > 0) {
2242 if (!strcmp(p, "-v")) {
2243 really_verbose = TRUE;
2244 } else if (!strcmp(p, "-g")) {
2246 } else if (*p == '-') {
2247 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2255 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
2259 desc = strchr(id, ':');
2261 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2266 p = default_params();
2267 decode_params(p, id);
2268 err = validate_desc(p, desc);
2270 fprintf(stderr, "%s: %s\n", argv[0], err);
2273 s = new_game(NULL, p, desc);
2275 sc = new_scratch(p->w, p->h);
2278 * When solving an Easy puzzle, we don't want to bother the
2279 * user with Hard-level deductions. For this reason, we grade
2280 * the puzzle internally before doing anything else.
2282 ret = -1; /* placate optimiser */
2283 for (diff = 0; diff < DIFFCOUNT; diff++) {
2284 ret = slant_solve(p->w, p->h, s->clues->clues,
2290 if (diff == DIFFCOUNT) {
2292 printf("Difficulty rating: harder than Hard, or ambiguous\n");
2294 printf("Unable to find a unique solution\n");
2298 printf("Difficulty rating: impossible (no solution exists)\n");
2300 printf("Difficulty rating: %s\n", slant_diffnames[diff]);
2302 verbose = really_verbose;
2303 ret = slant_solve(p->w, p->h, s->clues->clues,
2306 printf("Puzzle is inconsistent\n");
2308 fputs(game_text_format(s), stdout);