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.
42 COL_CURSOR
, COL_LOWLIGHT
, /* LOWLIGHT currently not used. */
47 * In standalone solver mode, `verbose' is a variable which can be
48 * set by command-line option; in debugging mode it's simply always
51 #if defined STANDALONE_SOLVER
52 #define SOLVER_DIAGNOSTICS
54 #elif defined SOLVER_DIAGNOSTICS
59 * Difficulty levels. I do some macro ickery here to ensure that my
60 * enum and the various forms of my name list always match up.
65 #define ENUM(upper,title,lower) DIFF_ ## upper,
66 #define TITLE(upper,title,lower) #title,
67 #define ENCODE(upper,title,lower) #lower
68 #define CONFIG(upper,title,lower) ":" #title
69 enum { DIFFLIST(ENUM
) DIFFCOUNT
};
70 static char const *const slant_diffnames
[] = { DIFFLIST(TITLE
) };
71 static char const slant_diffchars
[] = DIFFLIST(ENCODE
);
72 #define DIFFCONFIG DIFFLIST(CONFIG)
78 typedef struct game_clues
{
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; a 2 clue propagates any ruled-out bit past it
291 * (if the two squares on one side of a 2 cannot be a v-shape,
292 * then neither can the two on the other side be the same
293 * v-shape); we can rule out further bits during play using
294 * partially filled 2 clues; whenever a pair of squares is
295 * known not to be _either_ kind of v-shape, we can mark them
298 unsigned char *vbitmap
;
301 * Useful to have this information automatically passed to
302 * solver subroutines. (This pointer is not dynamically
303 * allocated by new_scratch and free_scratch.)
305 const signed char *clues
;
308 static struct solver_scratch
*new_scratch(int w
, int h
)
310 int W
= w
+1, H
= h
+1;
311 struct solver_scratch
*ret
= snew(struct solver_scratch
);
312 ret
->connected
= snewn(W
*H
, int);
313 ret
->exits
= snewn(W
*H
, int);
314 ret
->border
= snewn(W
*H
, unsigned char);
315 ret
->equiv
= snewn(w
*h
, int);
316 ret
->slashval
= snewn(w
*h
, signed char);
317 ret
->vbitmap
= snewn(w
*h
, unsigned char);
321 static void free_scratch(struct solver_scratch
*sc
)
328 sfree(sc
->connected
);
333 * Wrapper on dsf_merge() which updates the `exits' and `border'
336 static void merge_vertices(int *connected
,
337 struct solver_scratch
*sc
, int i
, int j
)
339 int exits
= -1, border
= FALSE
; /* initialise to placate optimiser */
342 i
= dsf_canonify(connected
, i
);
343 j
= dsf_canonify(connected
, j
);
346 * We have used one possible exit from each of the two
347 * classes. Thus, the viable exit count of the new class is
348 * the sum of the old exit counts minus two.
350 exits
= sc
->exits
[i
] + sc
->exits
[j
] - 2;
352 border
= sc
->border
[i
] || sc
->border
[j
];
355 dsf_merge(connected
, i
, j
);
358 i
= dsf_canonify(connected
, i
);
359 sc
->exits
[i
] = exits
;
360 sc
->border
[i
] = border
;
365 * Called when we have just blocked one way out of a particular
366 * point. If that point is a non-clue point (thus has a variable
367 * number of exits), we have therefore decreased its potential exit
368 * count, so we must decrement the exit count for the group as a
371 static void decr_exits(struct solver_scratch
*sc
, int i
)
373 if (sc
->clues
[i
] < 0) {
374 i
= dsf_canonify(sc
->connected
, i
);
379 static void fill_square(int w
, int h
, int x
, int y
, int v
,
381 int *connected
, struct solver_scratch
*sc
)
383 int W
= w
+1 /*, H = h+1 */;
385 assert(x
>= 0 && x
< w
&& y
>= 0 && y
< h
);
387 if (soln
[y
*w
+x
] != 0) {
388 return; /* do nothing */
391 #ifdef SOLVER_DIAGNOSTICS
393 printf(" placing %c in %d,%d\n", v
== -1 ? '\\' : '/', x
, y
);
399 int c
= dsf_canonify(sc
->equiv
, y
*w
+x
);
404 merge_vertices(connected
, sc
, y
*W
+x
, (y
+1)*W
+(x
+1));
406 decr_exits(sc
, y
*W
+(x
+1));
407 decr_exits(sc
, (y
+1)*W
+x
);
410 merge_vertices(connected
, sc
, y
*W
+(x
+1), (y
+1)*W
+x
);
412 decr_exits(sc
, y
*W
+x
);
413 decr_exits(sc
, (y
+1)*W
+(x
+1));
418 static int vbitmap_clear(int w
, int h
, struct solver_scratch
*sc
,
419 int x
, int y
, int vbits
, char *reason
, ...)
421 int done_something
= FALSE
;
424 for (vbit
= 1; vbit
<= 8; vbit
<<= 1)
425 if (vbits
& sc
->vbitmap
[y
*w
+x
] & vbit
) {
426 done_something
= TRUE
;
427 #ifdef SOLVER_DIAGNOSTICS
431 printf("ruling out %c shape at (%d,%d)-(%d,%d) (",
432 "!v^!>!!!<"[vbit
], x
, y
,
433 x
+((vbit
&0x3)!=0), y
+((vbit
&0xC)!=0));
435 va_start(ap
, reason
);
442 sc
->vbitmap
[y
*w
+x
] &= ~vbit
;
445 return done_something
;
449 * Solver. Returns 0 for impossibility, 1 for success, 2 for
450 * ambiguity or failure to converge.
452 static int slant_solve(int w
, int h
, const signed char *clues
,
453 signed char *soln
, struct solver_scratch
*sc
,
456 int W
= w
+1, H
= h
+1;
463 memset(soln
, 0, w
*h
);
468 * Establish a disjoint set forest for tracking connectedness
469 * between grid points.
471 dsf_init(sc
->connected
, W
*H
);
474 * Establish a disjoint set forest for tracking which squares
475 * are known to slant in the same direction.
477 dsf_init(sc
->equiv
, w
*h
);
480 * Clear the slashval array.
482 memset(sc
->slashval
, 0, w
*h
);
485 * Set up the vbitmap array. Initially all types of v are possible.
487 memset(sc
->vbitmap
, 0xF, w
*h
);
490 * Initialise the `exits' and `border' arrays. These are used
491 * to do second-order loop avoidance: the dual of the no loops
492 * constraint is that every point must be somehow connected to
493 * the border of the grid (otherwise there would be a solid
494 * loop around it which prevented this).
496 * I define a `dead end' to be a connected group of points
497 * which contains no border point, and which can form at most
498 * one new connection outside itself. Then I forbid placing an
499 * edge so that it connects together two dead-end groups, since
500 * this would yield a non-border-connected isolated subgraph
501 * with no further scope to extend it.
503 for (y
= 0; y
< H
; y
++)
504 for (x
= 0; x
< W
; x
++) {
505 if (y
== 0 || y
== H
-1 || x
== 0 || x
== W
-1)
506 sc
->border
[y
*W
+x
] = TRUE
;
508 sc
->border
[y
*W
+x
] = FALSE
;
510 if (clues
[y
*W
+x
] < 0)
511 sc
->exits
[y
*W
+x
] = 4;
513 sc
->exits
[y
*W
+x
] = clues
[y
*W
+x
];
517 * Repeatedly try to deduce something until we can't.
520 done_something
= FALSE
;
523 * Any clue point with the number of remaining lines equal
524 * to zero or to the number of remaining undecided
525 * neighbouring squares can be filled in completely.
527 for (y
= 0; y
< H
; y
++)
528 for (x
= 0; x
< W
; x
++) {
533 int nu
, nl
, c
, s
, eq
, eq2
, last
, meq
, mj1
, mj2
;
535 if ((c
= clues
[y
*W
+x
]) < 0)
539 * We have a clue point. Start by listing its
540 * neighbouring squares, in order around the point,
541 * together with the type of slash that would be
542 * required in that square to connect to the point.
545 if (x
> 0 && y
> 0) {
546 neighbours
[nneighbours
].pos
= (y
-1)*w
+(x
-1);
547 neighbours
[nneighbours
].slash
= -1;
550 if (x
> 0 && y
< h
) {
551 neighbours
[nneighbours
].pos
= y
*w
+(x
-1);
552 neighbours
[nneighbours
].slash
= +1;
555 if (x
< w
&& y
< h
) {
556 neighbours
[nneighbours
].pos
= y
*w
+x
;
557 neighbours
[nneighbours
].slash
= -1;
560 if (x
< w
&& y
> 0) {
561 neighbours
[nneighbours
].pos
= (y
-1)*w
+x
;
562 neighbours
[nneighbours
].slash
= +1;
567 * Count up the number of undecided neighbours, and
568 * also the number of lines already present.
570 * If we're not on DIFF_EASY, then in this loop we
571 * also track whether we've seen two adjacent empty
572 * squares belonging to the same equivalence class
573 * (meaning they have the same type of slash). If
574 * so, we count them jointly as one line.
578 last
= neighbours
[nneighbours
-1].pos
;
580 eq
= dsf_canonify(sc
->equiv
, last
);
583 meq
= mj1
= mj2
= -1;
584 for (i
= 0; i
< nneighbours
; i
++) {
585 j
= neighbours
[i
].pos
;
586 s
= neighbours
[i
].slash
;
588 nu
++; /* undecided */
589 if (meq
< 0 && difficulty
> DIFF_EASY
) {
590 eq2
= dsf_canonify(sc
->equiv
, j
);
591 if (eq
== eq2
&& last
!= j
) {
593 * We've found an equivalent pair.
594 * Mark it. This also inhibits any
595 * further equivalence tracking
596 * around this square, since we can
597 * only handle one pair (and in
598 * particular we want to avoid
599 * being misled by two overlapping
600 * equivalence pairs).
605 nl
--; /* count one line */
606 nu
-= 2; /* and lose two undecideds */
613 nl
--; /* here's a line */
621 if (nl
< 0 || nl
> nu
) {
623 * No consistent value for this at all!
625 #ifdef SOLVER_DIAGNOSTICS
627 printf("need %d / %d lines around clue point at %d,%d!\n",
630 return 0; /* impossible */
633 if (nu
> 0 && (nl
== 0 || nl
== nu
)) {
634 #ifdef SOLVER_DIAGNOSTICS
637 printf("partially (since %d,%d == %d,%d) ",
638 mj1
%w
, mj1
/w
, mj2
%w
, mj2
/w
);
639 printf("%s around clue point at %d,%d\n",
640 nl
? "filling" : "emptying", x
, y
);
643 for (i
= 0; i
< nneighbours
; i
++) {
644 j
= neighbours
[i
].pos
;
645 s
= neighbours
[i
].slash
;
646 if (soln
[j
] == 0 && j
!= mj1
&& j
!= mj2
)
647 fill_square(w
, h
, j
%w
, j
/w
, (nl
? s
: -s
), soln
,
651 done_something
= TRUE
;
652 } else if (nu
== 2 && nl
== 1 && difficulty
> DIFF_EASY
) {
654 * If we have precisely two undecided squares
655 * and precisely one line to place between
656 * them, _and_ those squares are adjacent, then
657 * we can mark them as equivalent to one
660 * This even applies if meq >= 0: if we have a
661 * 2 clue point and two of its neighbours are
662 * already marked equivalent, we can indeed
663 * mark the other two as equivalent.
665 * We don't bother with this on DIFF_EASY,
666 * since we wouldn't have used the results
670 for (i
= 0; i
< nneighbours
; i
++) {
671 j
= neighbours
[i
].pos
;
672 if (soln
[j
] == 0 && j
!= mj1
&& j
!= mj2
) {
675 else if (last
== i
-1 || (last
== 0 && i
== 3))
676 break; /* found a pair */
679 if (i
< nneighbours
) {
684 * neighbours[last] and neighbours[i] are
685 * the pair. Mark them equivalent.
687 #ifdef SOLVER_DIAGNOSTICS
690 printf("since %d,%d == %d,%d, ",
691 mj1
%w
, mj1
/w
, mj2
%w
, mj2
/w
);
694 mj1
= neighbours
[last
].pos
;
695 mj2
= neighbours
[i
].pos
;
696 #ifdef SOLVER_DIAGNOSTICS
698 printf("clue point at %d,%d implies %d,%d == %d,"
699 "%d\n", x
, y
, mj1
%w
, mj1
/w
, mj2
%w
, mj2
/w
);
701 mj1
= dsf_canonify(sc
->equiv
, mj1
);
702 sv1
= sc
->slashval
[mj1
];
703 mj2
= dsf_canonify(sc
->equiv
, mj2
);
704 sv2
= sc
->slashval
[mj2
];
705 if (sv1
!= 0 && sv2
!= 0 && sv1
!= sv2
) {
706 #ifdef SOLVER_DIAGNOSTICS
708 printf("merged two equivalence classes with"
709 " different slash values!\n");
713 sv1
= sv1
? sv1
: sv2
;
714 dsf_merge(sc
->equiv
, mj1
, mj2
);
715 mj1
= dsf_canonify(sc
->equiv
, mj1
);
716 sc
->slashval
[mj1
] = sv1
;
725 * Failing that, we now apply the second condition, which
726 * is that no square may be filled in such a way as to form
727 * a loop. Also in this loop (since it's over squares
728 * rather than points), we check slashval to see if we've
729 * already filled in another square in the same equivalence
732 * The slashval check is disabled on DIFF_EASY, as is dead
733 * end avoidance. Only _immediate_ loop avoidance remains.
735 for (y
= 0; y
< h
; y
++)
736 for (x
= 0; x
< w
; x
++) {
739 #ifdef SOLVER_DIAGNOSTICS
740 char *reason
= "<internal error>";
744 continue; /* got this one already */
749 if (difficulty
> DIFF_EASY
)
750 v
= sc
->slashval
[dsf_canonify(sc
->equiv
, y
*w
+x
)];
755 * Try to rule out connectivity between (x,y) and
756 * (x+1,y+1); if successful, we will deduce that we
757 * must have a forward slash.
759 c1
= dsf_canonify(sc
->connected
, y
*W
+x
);
760 c2
= dsf_canonify(sc
->connected
, (y
+1)*W
+(x
+1));
763 #ifdef SOLVER_DIAGNOSTICS
764 reason
= "simple loop avoidance";
767 if (difficulty
> DIFF_EASY
&&
768 !sc
->border
[c1
] && !sc
->border
[c2
] &&
769 sc
->exits
[c1
] <= 1 && sc
->exits
[c2
] <= 1) {
771 #ifdef SOLVER_DIAGNOSTICS
772 reason
= "dead end avoidance";
777 #ifdef SOLVER_DIAGNOSTICS
778 reason
= "equivalence to an already filled square";
783 * Now do the same between (x+1,y) and (x,y+1), to
784 * see if we are required to have a backslash.
786 c1
= dsf_canonify(sc
->connected
, y
*W
+(x
+1));
787 c2
= dsf_canonify(sc
->connected
, (y
+1)*W
+x
);
790 #ifdef SOLVER_DIAGNOSTICS
791 reason
= "simple loop avoidance";
794 if (difficulty
> DIFF_EASY
&&
795 !sc
->border
[c1
] && !sc
->border
[c2
] &&
796 sc
->exits
[c1
] <= 1 && sc
->exits
[c2
] <= 1) {
798 #ifdef SOLVER_DIAGNOSTICS
799 reason
= "dead end avoidance";
804 #ifdef SOLVER_DIAGNOSTICS
805 reason
= "equivalence to an already filled square";
811 * No consistent value for this at all!
813 #ifdef SOLVER_DIAGNOSTICS
815 printf("%d,%d has no consistent slash!\n", x
, y
);
817 return 0; /* impossible */
821 #ifdef SOLVER_DIAGNOSTICS
823 printf("employing %s\n", reason
);
825 fill_square(w
, h
, x
, y
, +1, soln
, sc
->connected
, sc
);
826 done_something
= TRUE
;
828 #ifdef SOLVER_DIAGNOSTICS
830 printf("employing %s\n", reason
);
832 fill_square(w
, h
, x
, y
, -1, soln
, sc
->connected
, sc
);
833 done_something
= TRUE
;
841 * Now see what we can do with the vbitmap array. All
842 * vbitmap deductions are disabled at Easy level.
844 if (difficulty
<= DIFF_EASY
)
847 for (y
= 0; y
< h
; y
++)
848 for (x
= 0; x
< w
; x
++) {
852 * Any line already placed in a square must rule
853 * out any type of v which contradicts it.
855 if ((s
= soln
[y
*w
+x
]) != 0) {
858 vbitmap_clear(w
, h
, sc
, x
-1, y
, (s
< 0 ? 0x1 : 0x2),
859 "contradicts known edge at (%d,%d)",x
,y
);
862 vbitmap_clear(w
, h
, sc
, x
, y
, (s
< 0 ? 0x2 : 0x1),
863 "contradicts known edge at (%d,%d)",x
,y
);
866 vbitmap_clear(w
, h
, sc
, x
, y
-1, (s
< 0 ? 0x4 : 0x8),
867 "contradicts known edge at (%d,%d)",x
,y
);
870 vbitmap_clear(w
, h
, sc
, x
, y
, (s
< 0 ? 0x8 : 0x4),
871 "contradicts known edge at (%d,%d)",x
,y
);
875 * If both types of v are ruled out for a pair of
876 * adjacent squares, mark them as equivalent.
878 if (x
+1 < w
&& !(sc
->vbitmap
[y
*w
+x
] & 0x3)) {
879 int n1
= y
*w
+x
, n2
= y
*w
+(x
+1);
880 if (dsf_canonify(sc
->equiv
, n1
) !=
881 dsf_canonify(sc
->equiv
, n2
)) {
882 dsf_merge(sc
->equiv
, n1
, n2
);
883 done_something
= TRUE
;
884 #ifdef SOLVER_DIAGNOSTICS
886 printf("(%d,%d) and (%d,%d) must be equivalent"
887 " because both v-shapes are ruled out\n",
892 if (y
+1 < h
&& !(sc
->vbitmap
[y
*w
+x
] & 0xC)) {
893 int n1
= y
*w
+x
, n2
= (y
+1)*w
+x
;
894 if (dsf_canonify(sc
->equiv
, n1
) !=
895 dsf_canonify(sc
->equiv
, n2
)) {
896 dsf_merge(sc
->equiv
, n1
, n2
);
897 done_something
= TRUE
;
898 #ifdef SOLVER_DIAGNOSTICS
900 printf("(%d,%d) and (%d,%d) must be equivalent"
901 " because both v-shapes are ruled out\n",
908 * The remaining work in this loop only works
909 * around non-edge clue points.
911 if (y
== 0 || x
== 0)
913 if ((c
= clues
[y
*W
+x
]) < 0)
917 * x,y marks a clue point not on the grid edge. See
918 * if this clue point allows us to rule out any v
924 * A 1 clue can never have any v shape pointing
928 vbitmap_clear(w
, h
, sc
, x
-1, y
-1, 0x5,
929 "points at 1 clue at (%d,%d)", x
, y
);
931 vbitmap_clear(w
, h
, sc
, x
-1, y
, 0x2,
932 "points at 1 clue at (%d,%d)", x
, y
);
934 vbitmap_clear(w
, h
, sc
, x
, y
-1, 0x8,
935 "points at 1 clue at (%d,%d)", x
, y
);
938 * A 3 clue can never have any v shape pointing
942 vbitmap_clear(w
, h
, sc
, x
-1, y
-1, 0xA,
943 "points away from 3 clue at (%d,%d)", x
, y
);
945 vbitmap_clear(w
, h
, sc
, x
-1, y
, 0x1,
946 "points away from 3 clue at (%d,%d)", x
, y
);
948 vbitmap_clear(w
, h
, sc
, x
, y
-1, 0x4,
949 "points away from 3 clue at (%d,%d)", x
, y
);
952 * If a 2 clue has any kind of v ruled out on
953 * one side of it, the same v is ruled out on
957 vbitmap_clear(w
, h
, sc
, x
-1, y
-1,
958 (sc
->vbitmap
[(y
)*w
+(x
-1)] & 0x3) ^ 0x3,
959 "propagated by 2 clue at (%d,%d)", x
, y
);
961 vbitmap_clear(w
, h
, sc
, x
-1, y
-1,
962 (sc
->vbitmap
[(y
-1)*w
+(x
)] & 0xC) ^ 0xC,
963 "propagated by 2 clue at (%d,%d)", x
, y
);
965 vbitmap_clear(w
, h
, sc
, x
-1, y
,
966 (sc
->vbitmap
[(y
-1)*w
+(x
-1)] & 0x3) ^ 0x3,
967 "propagated by 2 clue at (%d,%d)", x
, y
);
969 vbitmap_clear(w
, h
, sc
, x
, y
-1,
970 (sc
->vbitmap
[(y
-1)*w
+(x
-1)] & 0xC) ^ 0xC,
971 "propagated by 2 clue at (%d,%d)", x
, y
);
978 } while (done_something
);
981 * Solver can make no more progress. See if the grid is full.
983 for (i
= 0; i
< w
*h
; i
++)
985 return 2; /* failed to converge */
986 return 1; /* success */
990 * Filled-grid generator.
992 static void slant_generate(int w
, int h
, signed char *soln
, random_state
*rs
)
994 int W
= w
+1, H
= h
+1;
996 int *connected
, *indices
;
1001 memset(soln
, 0, w
*h
);
1004 * Establish a disjoint set forest for tracking connectedness
1005 * between grid points.
1007 connected
= snew_dsf(W
*H
);
1010 * Prepare a list of the squares in the grid, and fill them in
1011 * in a random order.
1013 indices
= snewn(w
*h
, int);
1014 for (i
= 0; i
< w
*h
; i
++)
1016 shuffle(indices
, w
*h
, sizeof(*indices
), rs
);
1019 * Fill in each one in turn.
1021 for (i
= 0; i
< w
*h
; i
++) {
1027 fs
= (dsf_canonify(connected
, y
*W
+x
) ==
1028 dsf_canonify(connected
, (y
+1)*W
+(x
+1)));
1029 bs
= (dsf_canonify(connected
, (y
+1)*W
+x
) ==
1030 dsf_canonify(connected
, y
*W
+(x
+1)));
1033 * It isn't possible to get into a situation where we
1034 * aren't allowed to place _either_ type of slash in a
1035 * square. Thus, filled-grid generation never has to
1038 * Proof (thanks to Gareth Taylor):
1040 * If it were possible, it would have to be because there
1041 * was an existing path (not using this square) between the
1042 * top-left and bottom-right corners of this square, and
1043 * another between the other two. These two paths would
1044 * have to cross at some point.
1046 * Obviously they can't cross in the middle of a square, so
1047 * they must cross by sharing a point in common. But this
1048 * isn't possible either: if you chessboard-colour all the
1049 * points on the grid, you find that any continuous
1050 * diagonal path is entirely composed of points of the same
1051 * colour. And one of our two hypothetical paths is between
1052 * two black points, and the other is between two white
1053 * points - therefore they can have no point in common. []
1055 assert(!(fs
&& bs
));
1057 v
= fs
? +1 : bs
? -1 : 2 * random_upto(rs
, 2) - 1;
1058 fill_square(w
, h
, x
, y
, v
, soln
, connected
, NULL
);
1065 static char *new_game_desc(game_params
*params
, random_state
*rs
,
1066 char **aux
, int interactive
)
1068 int w
= params
->w
, h
= params
->h
, W
= w
+1, H
= h
+1;
1069 signed char *soln
, *tmpsoln
, *clues
;
1071 struct solver_scratch
*sc
;
1075 soln
= snewn(w
*h
, signed char);
1076 tmpsoln
= snewn(w
*h
, signed char);
1077 clues
= snewn(W
*H
, signed char);
1078 clueindices
= snewn(W
*H
, int);
1079 sc
= new_scratch(w
, h
);
1083 * Create the filled grid.
1085 slant_generate(w
, h
, soln
, rs
);
1088 * Fill in the complete set of clues.
1090 for (y
= 0; y
< H
; y
++)
1091 for (x
= 0; x
< W
; x
++) {
1094 if (x
> 0 && y
> 0 && soln
[(y
-1)*w
+(x
-1)] == -1) v
++;
1095 if (x
> 0 && y
< h
&& soln
[y
*w
+(x
-1)] == +1) v
++;
1096 if (x
< w
&& y
> 0 && soln
[(y
-1)*w
+x
] == +1) v
++;
1097 if (x
< w
&& y
< h
&& soln
[y
*w
+x
] == -1) v
++;
1103 * With all clue points filled in, all puzzles are easy: we can
1104 * simply process the clue points in lexicographic order, and
1105 * at each clue point we will always have at most one square
1106 * undecided, which we can then fill in uniquely.
1108 assert(slant_solve(w
, h
, clues
, tmpsoln
, sc
, DIFF_EASY
) == 1);
1111 * Remove as many clues as possible while retaining solubility.
1113 * In DIFF_HARD mode, we prioritise the removal of obvious
1114 * starting points (4s, 0s, border 2s and corner 1s), on
1115 * the grounds that having as few of these as possible
1116 * seems like a good thing. In particular, we can often get
1117 * away without _any_ completely obvious starting points,
1118 * which is even better.
1120 for (i
= 0; i
< W
*H
; i
++)
1122 shuffle(clueindices
, W
*H
, sizeof(*clueindices
), rs
);
1123 for (j
= 0; j
< 2; j
++) {
1124 for (i
= 0; i
< W
*H
; i
++) {
1127 y
= clueindices
[i
] / W
;
1128 x
= clueindices
[i
] % W
;
1132 * Identify which pass we should process this point
1133 * in. If it's an obvious start point, _or_ we're
1134 * in DIFF_EASY, then it goes in pass 0; otherwise
1137 xb
= (x
== 0 || x
== W
-1);
1138 yb
= (y
== 0 || y
== H
-1);
1139 if (params
->diff
== DIFF_EASY
|| v
== 4 || v
== 0 ||
1140 (v
== 2 && (xb
||yb
)) || (v
== 1 && xb
&& yb
))
1147 if (slant_solve(w
, h
, clues
, tmpsoln
, sc
,
1149 clues
[y
*W
+x
] = v
; /* put it back */
1155 * And finally, verify that the grid is of _at least_ the
1156 * requested difficulty, by running the solver one level
1157 * down and verifying that it can't manage it.
1159 } while (params
->diff
> 0 &&
1160 slant_solve(w
, h
, clues
, tmpsoln
, sc
, params
->diff
- 1) <= 1);
1163 * Now we have the clue set as it will be presented to the
1164 * user. Encode it in a game desc.
1170 desc
= snewn(W
*H
+1, char);
1173 for (i
= 0; i
<= W
*H
; i
++) {
1174 int n
= (i
< W
*H
? clues
[i
] : -2);
1181 int c
= 'a' - 1 + run
;
1185 run
-= c
- ('a' - 1);
1193 assert(p
- desc
<= W
*H
);
1195 desc
= sresize(desc
, p
- desc
, char);
1199 * Encode the solution as an aux_info.
1203 *aux
= auxbuf
= snewn(w
*h
+1, char);
1204 for (i
= 0; i
< w
*h
; i
++)
1205 auxbuf
[i
] = soln
[i
] < 0 ? '\\' : '/';
1218 static char *validate_desc(game_params
*params
, char *desc
)
1220 int w
= params
->w
, h
= params
->h
, W
= w
+1, H
= h
+1;
1226 if (n
>= 'a' && n
<= 'z') {
1227 squares
+= n
- 'a' + 1;
1228 } else if (n
>= '0' && n
<= '4') {
1231 return "Invalid character in game description";
1235 return "Not enough data to fill grid";
1238 return "Too much data to fit in grid";
1243 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1245 int w
= params
->w
, h
= params
->h
, W
= w
+1, H
= h
+1;
1246 game_state
*state
= snew(game_state
);
1251 state
->soln
= snewn(w
*h
, signed char);
1252 memset(state
->soln
, 0, w
*h
);
1253 state
->completed
= state
->used_solve
= FALSE
;
1254 state
->errors
= snewn(W
*H
, unsigned char);
1255 memset(state
->errors
, 0, W
*H
);
1257 state
->clues
= snew(game_clues
);
1258 state
->clues
->w
= w
;
1259 state
->clues
->h
= h
;
1260 state
->clues
->clues
= snewn(W
*H
, signed char);
1261 state
->clues
->refcount
= 1;
1262 state
->clues
->tmpdsf
= snewn(W
*H
*2+W
+H
, int);
1263 memset(state
->clues
->clues
, -1, W
*H
);
1266 if (n
>= 'a' && n
<= 'z') {
1267 squares
+= n
- 'a' + 1;
1268 } else if (n
>= '0' && n
<= '4') {
1269 state
->clues
->clues
[squares
++] = n
- '0';
1271 assert(!"can't get here");
1273 assert(squares
== area
);
1278 static game_state
*dup_game(game_state
*state
)
1280 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
1281 game_state
*ret
= snew(game_state
);
1284 ret
->clues
= state
->clues
;
1285 ret
->clues
->refcount
++;
1286 ret
->completed
= state
->completed
;
1287 ret
->used_solve
= state
->used_solve
;
1289 ret
->soln
= snewn(w
*h
, signed char);
1290 memcpy(ret
->soln
, state
->soln
, w
*h
);
1292 ret
->errors
= snewn(W
*H
, unsigned char);
1293 memcpy(ret
->errors
, state
->errors
, W
*H
);
1298 static void free_game(game_state
*state
)
1300 sfree(state
->errors
);
1302 assert(state
->clues
);
1303 if (--state
->clues
->refcount
<= 0) {
1304 sfree(state
->clues
->clues
);
1305 sfree(state
->clues
->tmpdsf
);
1306 sfree(state
->clues
);
1312 * Utility function to return the current degree of a vertex. If
1313 * `anti' is set, it returns the number of filled-in edges
1314 * surrounding the point which _don't_ connect to it; thus 4 minus
1315 * its anti-degree is the maximum degree it could have if all the
1316 * empty spaces around it were filled in.
1318 * (Yes, _4_ minus its anti-degree even if it's a border vertex.)
1320 * If ret > 0, *sx and *sy are set to the coordinates of one of the
1321 * squares that contributed to it.
1323 static int vertex_degree(int w
, int h
, signed char *soln
, int x
, int y
,
1324 int anti
, int *sx
, int *sy
)
1328 assert(x
>= 0 && x
<= w
&& y
>= 0 && y
<= h
);
1329 if (x
> 0 && y
> 0 && soln
[(y
-1)*w
+(x
-1)] - anti
< 0) {
1334 if (x
> 0 && y
< h
&& soln
[y
*w
+(x
-1)] + anti
> 0) {
1339 if (x
< w
&& y
> 0 && soln
[(y
-1)*w
+x
] + anti
> 0) {
1344 if (x
< w
&& y
< h
&& soln
[y
*w
+x
] - anti
< 0) {
1350 return anti
? 4 - ret
: ret
;
1353 static int check_completion(game_state
*state
)
1355 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
1356 int x
, y
, err
= FALSE
;
1359 memset(state
->errors
, 0, W
*H
);
1362 * To detect loops in the grid, we iterate through each edge
1363 * building up a dsf of connected components of the space
1364 * around the edges; if there's more than one such component,
1365 * we have a loop, and in particular we can then easily
1366 * identify and highlight every edge forming part of a loop
1367 * because it separates two nonequivalent regions.
1369 * We use the `tmpdsf' scratch space in the shared clues
1370 * structure, to avoid mallocing too often.
1372 * For these purposes, the grid is considered to be divided
1373 * into diamond-shaped regions surrounding an orthogonal edge.
1374 * This means we have W*h vertical edges and w*H horizontal
1375 * ones; so our vertical edges are indexed in the dsf as
1376 * (y*W+x) (0<=y<h, 0<=x<W), and the horizontal ones as (W*h +
1377 * y*w+x) (0<=y<H, 0<=x<w), where (x,y) is the topmost or
1378 * leftmost point on the edge.
1380 dsf
= state
->clues
->tmpdsf
;
1381 dsf_init(dsf
, W
*h
+ w
*H
);
1382 /* Start by identifying all the outer edges with each other. */
1383 for (y
= 0; y
< h
; y
++) {
1384 dsf_merge(dsf
, 0, y
*W
+0);
1385 dsf_merge(dsf
, 0, y
*W
+w
);
1387 for (x
= 0; x
< w
; x
++) {
1388 dsf_merge(dsf
, 0, W
*h
+ 0*w
+x
);
1389 dsf_merge(dsf
, 0, W
*h
+ h
*w
+x
);
1391 /* Now go through the actual grid. */
1392 for (y
= 0; y
< h
; y
++)
1393 for (x
= 0; x
< w
; x
++) {
1394 if (state
->soln
[y
*w
+x
] >= 0) {
1396 * There isn't a \ in this square, so we can unify
1397 * the top edge with the left, and the bottom with
1400 dsf_merge(dsf
, y
*W
+x
, W
*h
+ y
*w
+x
);
1401 dsf_merge(dsf
, y
*W
+(x
+1), W
*h
+ (y
+1)*w
+x
);
1403 if (state
->soln
[y
*w
+x
] <= 0) {
1405 * There isn't a / in this square, so we can unify
1406 * the top edge with the right, and the bottom
1409 dsf_merge(dsf
, y
*W
+x
, W
*h
+ (y
+1)*w
+x
);
1410 dsf_merge(dsf
, y
*W
+(x
+1), W
*h
+ y
*w
+x
);
1413 /* Now go through again and mark the appropriate edges as erroneous. */
1414 for (y
= 0; y
< h
; y
++)
1415 for (x
= 0; x
< w
; x
++) {
1417 if (state
->soln
[y
*w
+x
] > 0) {
1419 * A / separates the top and left edges (which
1420 * must already have been identified with each
1421 * other) from the bottom and right (likewise).
1422 * Hence it is erroneous if and only if the top
1423 * and right edges are nonequivalent.
1425 erroneous
= (dsf_canonify(dsf
, y
*W
+(x
+1)) !=
1426 dsf_canonify(dsf
, W
*h
+ y
*w
+x
));
1427 } else if (state
->soln
[y
*w
+x
] < 0) {
1429 * A \ separates the top and right edges (which
1430 * must already have been identified with each
1431 * other) from the bottom and left (likewise).
1432 * Hence it is erroneous if and only if the top
1433 * and left edges are nonequivalent.
1435 erroneous
= (dsf_canonify(dsf
, y
*W
+x
) !=
1436 dsf_canonify(dsf
, W
*h
+ y
*w
+x
));
1439 state
->errors
[y
*W
+x
] |= ERR_SQUARE
;
1445 * Now go through and check the degree of each clue vertex, and
1446 * mark it with ERR_VERTEX if it cannot be fulfilled.
1448 for (y
= 0; y
< H
; y
++)
1449 for (x
= 0; x
< W
; x
++) {
1452 if ((c
= state
->clues
->clues
[y
*W
+x
]) < 0)
1456 * Check to see if there are too many connections to
1457 * this vertex _or_ too many non-connections. Either is
1458 * grounds for marking the vertex as erroneous.
1460 if (vertex_degree(w
, h
, state
->soln
, x
, y
,
1461 FALSE
, NULL
, NULL
) > c
||
1462 vertex_degree(w
, h
, state
->soln
, x
, y
,
1463 TRUE
, NULL
, NULL
) > 4-c
) {
1464 state
->errors
[y
*W
+x
] |= ERR_VERTEX
;
1470 * Now our actual victory condition is that (a) none of the
1471 * above code marked anything as erroneous, and (b) every
1472 * square has an edge in it.
1478 for (y
= 0; y
< h
; y
++)
1479 for (x
= 0; x
< w
; x
++)
1480 if (state
->soln
[y
*w
+x
] == 0)
1486 static char *solve_game(game_state
*state
, game_state
*currstate
,
1487 char *aux
, char **error
)
1489 int w
= state
->p
.w
, h
= state
->p
.h
;
1492 int free_soln
= FALSE
;
1493 char *move
, buf
[80];
1494 int movelen
, movesize
;
1499 * If we already have the solution, save ourselves some
1502 soln
= (signed char *)aux
;
1503 bs
= (signed char)'\\';
1506 struct solver_scratch
*sc
= new_scratch(w
, h
);
1507 soln
= snewn(w
*h
, signed char);
1509 ret
= slant_solve(w
, h
, state
->clues
->clues
, soln
, sc
, DIFF_HARD
);
1514 *error
= "This puzzle is not self-consistent";
1516 *error
= "Unable to find a unique solution for this puzzle";
1523 * Construct a move string which turns the current state into
1527 move
= snewn(movesize
, char);
1529 move
[movelen
++] = 'S';
1530 move
[movelen
] = '\0';
1531 for (y
= 0; y
< h
; y
++)
1532 for (x
= 0; x
< w
; x
++) {
1533 int v
= (soln
[y
*w
+x
] == bs
? -1 : +1);
1534 if (state
->soln
[y
*w
+x
] != v
) {
1535 int len
= sprintf(buf
, ";%c%d,%d", (int)(v
< 0 ? '\\' : '/'), x
, y
);
1536 if (movelen
+ len
>= movesize
) {
1537 movesize
= movelen
+ len
+ 256;
1538 move
= sresize(move
, movesize
, char);
1540 strcpy(move
+ movelen
, buf
);
1551 static int game_can_format_as_text_now(game_params
*params
)
1556 static char *game_text_format(game_state
*state
)
1558 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
1563 * There are h+H rows of w+W columns.
1565 len
= (h
+H
) * (w
+W
+1) + 1;
1566 ret
= snewn(len
, char);
1569 for (y
= 0; y
< H
; y
++) {
1570 for (x
= 0; x
< W
; x
++) {
1571 if (state
->clues
->clues
[y
*W
+x
] >= 0)
1572 *p
++ = state
->clues
->clues
[y
*W
+x
] + '0';
1580 for (x
= 0; x
< W
; x
++) {
1583 if (state
->soln
[y
*w
+x
] != 0)
1584 *p
++ = (state
->soln
[y
*w
+x
] < 0 ? '\\' : '/');
1594 assert(p
- ret
== len
);
1599 int cur_x
, cur_y
, cur_visible
;
1602 static game_ui
*new_ui(game_state
*state
)
1604 game_ui
*ui
= snew(game_ui
);
1605 ui
->cur_x
= ui
->cur_y
= ui
->cur_visible
= 0;
1609 static void free_ui(game_ui
*ui
)
1614 static char *encode_ui(game_ui
*ui
)
1619 static void decode_ui(game_ui
*ui
, char *encoding
)
1623 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1624 game_state
*newstate
)
1628 #define PREFERRED_TILESIZE 32
1629 #define TILESIZE (ds->tilesize)
1630 #define BORDER TILESIZE
1631 #define CLUE_RADIUS (TILESIZE / 3)
1632 #define CLUE_TEXTSIZE (TILESIZE / 2)
1633 #define COORD(x) ( (x) * TILESIZE + BORDER )
1634 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1636 #define FLASH_TIME 0.30F
1639 * Bit fields in the `grid' and `todraw' elements of the drawstate.
1641 #define BACKSLASH 0x00000001L
1642 #define FORWSLASH 0x00000002L
1643 #define L_T 0x00000004L
1644 #define ERR_L_T 0x00000008L
1645 #define L_B 0x00000010L
1646 #define ERR_L_B 0x00000020L
1647 #define T_L 0x00000040L
1648 #define ERR_T_L 0x00000080L
1649 #define T_R 0x00000100L
1650 #define ERR_T_R 0x00000200L
1651 #define C_TL 0x00000400L
1652 #define ERR_C_TL 0x00000800L
1653 #define FLASH 0x00001000L
1654 #define ERRSLASH 0x00002000L
1655 #define ERR_TL 0x00004000L
1656 #define ERR_TR 0x00008000L
1657 #define ERR_BL 0x00010000L
1658 #define ERR_BR 0x00020000L
1659 #define CURSOR 0x00040000L
1661 struct game_drawstate
{
1668 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1669 int x
, int y
, int button
)
1671 int w
= state
->p
.w
, h
= state
->p
.h
;
1674 enum { CLOCKWISE
, ANTICLOCKWISE
, NONE
} action
= NONE
;
1676 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
1678 * This is an utterly awful hack which I should really sort out
1679 * by means of a proper configuration mechanism. One Slant
1680 * player has observed that they prefer the mouse buttons to
1681 * function exactly the opposite way round, so here's a
1682 * mechanism for environment-based configuration. I cache the
1683 * result in a global variable - yuck! - to avoid repeated
1687 static int swap_buttons
= -1;
1688 if (swap_buttons
< 0) {
1689 char *env
= getenv("SLANT_SWAP_BUTTONS");
1690 swap_buttons
= (env
&& (env
[0] == 'y' || env
[0] == 'Y'));
1693 if (button
== LEFT_BUTTON
)
1694 button
= RIGHT_BUTTON
;
1696 button
= LEFT_BUTTON
;
1699 action
= (button
== LEFT_BUTTON
) ? CLOCKWISE
: ANTICLOCKWISE
;
1703 if (x
< 0 || y
< 0 || x
>= w
|| y
>= h
)
1705 } else if (IS_CURSOR_SELECT(button
)) {
1706 if (!ui
->cur_visible
) {
1707 ui
->cur_visible
= 1;
1713 action
= (button
== CURSOR_SELECT2
) ? ANTICLOCKWISE
: CLOCKWISE
;
1714 } else if (IS_CURSOR_MOVE(button
)) {
1715 move_cursor(button
, &ui
->cur_x
, &ui
->cur_y
, w
, h
, 0);
1716 ui
->cur_visible
= 1;
1720 if (action
!= NONE
) {
1721 if (action
== CLOCKWISE
) {
1723 * Left-clicking cycles blank -> \ -> / -> blank.
1725 v
= state
->soln
[y
*w
+x
] - 1;
1730 * Right-clicking cycles blank -> / -> \ -> blank.
1732 v
= state
->soln
[y
*w
+x
] + 1;
1737 sprintf(buf
, "%c%d,%d", (int)(v
==-1 ? '\\' : v
==+1 ? '/' : 'C'), x
, y
);
1744 static game_state
*execute_move(game_state
*state
, char *move
)
1746 int w
= state
->p
.w
, h
= state
->p
.h
;
1749 game_state
*ret
= dup_game(state
);
1754 ret
->used_solve
= TRUE
;
1756 } else if (c
== '\\' || c
== '/' || c
== 'C') {
1758 if (sscanf(move
, "%d,%d%n", &x
, &y
, &n
) != 2 ||
1759 x
< 0 || y
< 0 || x
>= w
|| y
>= h
) {
1763 ret
->soln
[y
*w
+x
] = (c
== '\\' ? -1 : c
== '/' ? +1 : 0);
1778 * We never clear the `completed' flag, but we must always
1779 * re-run the completion check because it also highlights
1780 * errors in the grid.
1782 ret
->completed
= check_completion(ret
) || ret
->completed
;
1787 /* ----------------------------------------------------------------------
1791 static void game_compute_size(game_params
*params
, int tilesize
,
1794 /* fool the macros */
1795 struct dummy
{ int tilesize
; } dummy
, *ds
= &dummy
;
1796 dummy
.tilesize
= tilesize
;
1798 *x
= 2 * BORDER
+ params
->w
* TILESIZE
+ 1;
1799 *y
= 2 * BORDER
+ params
->h
* TILESIZE
+ 1;
1802 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1803 game_params
*params
, int tilesize
)
1805 ds
->tilesize
= tilesize
;
1808 static float *game_colours(frontend
*fe
, int *ncolours
)
1810 float *ret
= snewn(3 * NCOLOURS
, float);
1812 /* CURSOR colour is a background highlight. LOWLIGHT is unused. */
1813 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_CURSOR
, COL_LOWLIGHT
);
1815 ret
[COL_GRID
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 0.7F
;
1816 ret
[COL_GRID
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 0.7F
;
1817 ret
[COL_GRID
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 0.7F
;
1819 ret
[COL_INK
* 3 + 0] = 0.0F
;
1820 ret
[COL_INK
* 3 + 1] = 0.0F
;
1821 ret
[COL_INK
* 3 + 2] = 0.0F
;
1823 ret
[COL_SLANT1
* 3 + 0] = 0.0F
;
1824 ret
[COL_SLANT1
* 3 + 1] = 0.0F
;
1825 ret
[COL_SLANT1
* 3 + 2] = 0.0F
;
1827 ret
[COL_SLANT2
* 3 + 0] = 0.0F
;
1828 ret
[COL_SLANT2
* 3 + 1] = 0.0F
;
1829 ret
[COL_SLANT2
* 3 + 2] = 0.0F
;
1831 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1832 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1833 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1835 *ncolours
= NCOLOURS
;
1839 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1841 int w
= state
->p
.w
, h
= state
->p
.h
;
1843 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1846 ds
->started
= FALSE
;
1847 ds
->grid
= snewn((w
+2)*(h
+2), long);
1848 ds
->todraw
= snewn((w
+2)*(h
+2), long);
1849 for (i
= 0; i
< (w
+2)*(h
+2); i
++)
1850 ds
->grid
[i
] = ds
->todraw
[i
] = -1;
1855 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1862 static void draw_clue(drawing
*dr
, game_drawstate
*ds
,
1863 int x
, int y
, long v
, long err
, int bg
, int colour
)
1866 int ccol
= colour
>= 0 ? colour
: ((x
^ y
) & 1) ? COL_SLANT1
: COL_SLANT2
;
1867 int tcol
= colour
>= 0 ? colour
: err
? COL_ERROR
: COL_INK
;
1872 p
[0] = (char)v
+ '0';
1874 draw_circle(dr
, COORD(x
), COORD(y
), CLUE_RADIUS
,
1875 bg
>= 0 ? bg
: COL_BACKGROUND
, ccol
);
1876 draw_text(dr
, COORD(x
), COORD(y
), FONT_VARIABLE
,
1877 CLUE_TEXTSIZE
, ALIGN_VCENTRE
|ALIGN_HCENTRE
, tcol
, p
);
1880 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, game_clues
*clues
,
1881 int x
, int y
, long v
)
1883 int w
= clues
->w
, h
= clues
->h
, W
= w
+1 /*, H = h+1 */;
1884 int chesscolour
= (x
^ y
) & 1;
1885 int fscol
= chesscolour
? COL_SLANT2
: COL_SLANT1
;
1886 int bscol
= chesscolour
? COL_SLANT1
: COL_SLANT2
;
1888 clip(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
);
1890 draw_rect(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
,
1891 (v
& FLASH
) ? COL_GRID
:
1892 (v
& CURSOR
) ? COL_CURSOR
: COL_BACKGROUND
);
1895 * Draw the grid lines.
1897 if (x
>= 0 && x
< w
&& y
>= 0)
1898 draw_rect(dr
, COORD(x
), COORD(y
), TILESIZE
+1, 1, COL_GRID
);
1899 if (x
>= 0 && x
< w
&& y
< h
)
1900 draw_rect(dr
, COORD(x
), COORD(y
+1), TILESIZE
+1, 1, COL_GRID
);
1901 if (y
>= 0 && y
< h
&& x
>= 0)
1902 draw_rect(dr
, COORD(x
), COORD(y
), 1, TILESIZE
+1, COL_GRID
);
1903 if (y
>= 0 && y
< h
&& x
< w
)
1904 draw_rect(dr
, COORD(x
+1), COORD(y
), 1, TILESIZE
+1, COL_GRID
);
1905 if (x
== -1 && y
== -1)
1906 draw_rect(dr
, COORD(x
+1), COORD(y
+1), 1, 1, COL_GRID
);
1907 if (x
== -1 && y
== h
)
1908 draw_rect(dr
, COORD(x
+1), COORD(y
), 1, 1, COL_GRID
);
1909 if (x
== w
&& y
== -1)
1910 draw_rect(dr
, COORD(x
), COORD(y
+1), 1, 1, COL_GRID
);
1911 if (x
== w
&& y
== h
)
1912 draw_rect(dr
, COORD(x
), COORD(y
), 1, 1, COL_GRID
);
1917 if (v
& BACKSLASH
) {
1918 int scol
= (v
& ERRSLASH
) ? COL_ERROR
: bscol
;
1919 draw_line(dr
, COORD(x
), COORD(y
), COORD(x
+1), COORD(y
+1), scol
);
1920 draw_line(dr
, COORD(x
)+1, COORD(y
), COORD(x
+1), COORD(y
+1)-1,
1922 draw_line(dr
, COORD(x
), COORD(y
)+1, COORD(x
+1)-1, COORD(y
+1),
1924 } else if (v
& FORWSLASH
) {
1925 int scol
= (v
& ERRSLASH
) ? COL_ERROR
: fscol
;
1926 draw_line(dr
, COORD(x
+1), COORD(y
), COORD(x
), COORD(y
+1), scol
);
1927 draw_line(dr
, COORD(x
+1)-1, COORD(y
), COORD(x
), COORD(y
+1)-1,
1929 draw_line(dr
, COORD(x
+1), COORD(y
)+1, COORD(x
)+1, COORD(y
+1),
1934 * Draw dots on the grid corners that appear if a slash is in a
1935 * neighbouring cell.
1937 if (v
& (L_T
| BACKSLASH
))
1938 draw_rect(dr
, COORD(x
), COORD(y
)+1, 1, 1,
1939 (v
& ERR_L_T
? COL_ERROR
: bscol
));
1940 if (v
& (L_B
| FORWSLASH
))
1941 draw_rect(dr
, COORD(x
), COORD(y
+1)-1, 1, 1,
1942 (v
& ERR_L_B
? COL_ERROR
: fscol
));
1943 if (v
& (T_L
| BACKSLASH
))
1944 draw_rect(dr
, COORD(x
)+1, COORD(y
), 1, 1,
1945 (v
& ERR_T_L
? COL_ERROR
: bscol
));
1946 if (v
& (T_R
| FORWSLASH
))
1947 draw_rect(dr
, COORD(x
+1)-1, COORD(y
), 1, 1,
1948 (v
& ERR_T_R
? COL_ERROR
: fscol
));
1949 if (v
& (C_TL
| BACKSLASH
))
1950 draw_rect(dr
, COORD(x
), COORD(y
), 1, 1,
1951 (v
& ERR_C_TL
? COL_ERROR
: bscol
));
1954 * And finally the clues at the corners.
1956 if (x
>= 0 && y
>= 0)
1957 draw_clue(dr
, ds
, x
, y
, clues
->clues
[y
*W
+x
], v
& ERR_TL
, -1, -1);
1958 if (x
< w
&& y
>= 0)
1959 draw_clue(dr
, ds
, x
+1, y
, clues
->clues
[y
*W
+(x
+1)], v
& ERR_TR
, -1, -1);
1960 if (x
>= 0 && y
< h
)
1961 draw_clue(dr
, ds
, x
, y
+1, clues
->clues
[(y
+1)*W
+x
], v
& ERR_BL
, -1, -1);
1963 draw_clue(dr
, ds
, x
+1, y
+1, clues
->clues
[(y
+1)*W
+(x
+1)], v
& ERR_BR
,
1967 draw_update(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
);
1970 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1971 game_state
*state
, int dir
, game_ui
*ui
,
1972 float animtime
, float flashtime
)
1974 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
1979 flashing
= (int)(flashtime
* 3 / FLASH_TIME
) != 1;
1985 game_compute_size(&state
->p
, TILESIZE
, &ww
, &wh
);
1986 draw_rect(dr
, 0, 0, ww
, wh
, COL_BACKGROUND
);
1987 draw_update(dr
, 0, 0, ww
, wh
);
1992 * Loop over the grid and work out where all the slashes are.
1993 * We need to do this because a slash in one square affects the
1994 * drawing of the next one along.
1996 for (y
= -1; y
<= h
; y
++)
1997 for (x
= -1; x
<= w
; x
++) {
1998 if (x
>= 0 && x
< w
&& y
>= 0 && y
< h
)
1999 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] = flashing
? FLASH
: 0;
2001 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] = 0;
2004 for (y
= 0; y
< h
; y
++) {
2005 for (x
= 0; x
< w
; x
++) {
2006 int err
= state
->errors
[y
*W
+x
] & ERR_SQUARE
;
2008 if (state
->soln
[y
*w
+x
] < 0) {
2009 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] |= BACKSLASH
;
2010 ds
->todraw
[(y
+2)*(w
+2)+(x
+1)] |= T_R
;
2011 ds
->todraw
[(y
+1)*(w
+2)+(x
+2)] |= L_B
;
2012 ds
->todraw
[(y
+2)*(w
+2)+(x
+2)] |= C_TL
;
2014 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] |= ERRSLASH
|
2015 ERR_T_L
| ERR_L_T
| ERR_C_TL
;
2016 ds
->todraw
[(y
+2)*(w
+2)+(x
+1)] |= ERR_T_R
;
2017 ds
->todraw
[(y
+1)*(w
+2)+(x
+2)] |= ERR_L_B
;
2018 ds
->todraw
[(y
+2)*(w
+2)+(x
+2)] |= ERR_C_TL
;
2020 } else if (state
->soln
[y
*w
+x
] > 0) {
2021 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] |= FORWSLASH
;
2022 ds
->todraw
[(y
+1)*(w
+2)+(x
+2)] |= L_T
| C_TL
;
2023 ds
->todraw
[(y
+2)*(w
+2)+(x
+1)] |= T_L
| C_TL
;
2025 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] |= ERRSLASH
|
2027 ds
->todraw
[(y
+1)*(w
+2)+(x
+2)] |= ERR_L_T
| ERR_C_TL
;
2028 ds
->todraw
[(y
+2)*(w
+2)+(x
+1)] |= ERR_T_L
| ERR_C_TL
;
2031 if (ui
->cur_visible
&& ui
->cur_x
== x
&& ui
->cur_y
== y
)
2032 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] |= CURSOR
;
2036 for (y
= 0; y
< H
; y
++)
2037 for (x
= 0; x
< W
; x
++)
2038 if (state
->errors
[y
*W
+x
] & ERR_VERTEX
) {
2039 ds
->todraw
[y
*(w
+2)+x
] |= ERR_BR
;
2040 ds
->todraw
[y
*(w
+2)+(x
+1)] |= ERR_BL
;
2041 ds
->todraw
[(y
+1)*(w
+2)+x
] |= ERR_TR
;
2042 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] |= ERR_TL
;
2046 * Now go through and draw the grid squares.
2048 for (y
= -1; y
<= h
; y
++) {
2049 for (x
= -1; x
<= w
; x
++) {
2050 if (ds
->todraw
[(y
+1)*(w
+2)+(x
+1)] != ds
->grid
[(y
+1)*(w
+2)+(x
+1)]) {
2051 draw_tile(dr
, ds
, state
->clues
, x
, y
,
2052 ds
->todraw
[(y
+1)*(w
+2)+(x
+1)]);
2053 ds
->grid
[(y
+1)*(w
+2)+(x
+1)] = ds
->todraw
[(y
+1)*(w
+2)+(x
+1)];
2059 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2060 int dir
, game_ui
*ui
)
2065 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2066 int dir
, game_ui
*ui
)
2068 if (!oldstate
->completed
&& newstate
->completed
&&
2069 !oldstate
->used_solve
&& !newstate
->used_solve
)
2075 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2080 static void game_print_size(game_params
*params
, float *x
, float *y
)
2085 * I'll use 6mm squares by default.
2087 game_compute_size(params
, 600, &pw
, &ph
);
2092 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2094 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1;
2095 int ink
= print_mono_colour(dr
, 0);
2096 int paper
= print_mono_colour(dr
, 1);
2099 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2100 game_drawstate ads
, *ds
= &ads
;
2101 game_set_size(dr
, ds
, NULL
, tilesize
);
2106 print_line_width(dr
, TILESIZE
/ 16);
2107 draw_rect_outline(dr
, COORD(0), COORD(0), w
*TILESIZE
, h
*TILESIZE
, ink
);
2112 print_line_width(dr
, TILESIZE
/ 24);
2113 for (x
= 1; x
< w
; x
++)
2114 draw_line(dr
, COORD(x
), COORD(0), COORD(x
), COORD(h
), ink
);
2115 for (y
= 1; y
< h
; y
++)
2116 draw_line(dr
, COORD(0), COORD(y
), COORD(w
), COORD(y
), ink
);
2121 print_line_width(dr
, TILESIZE
/ 12);
2122 for (y
= 0; y
< h
; y
++)
2123 for (x
= 0; x
< w
; x
++)
2124 if (state
->soln
[y
*w
+x
]) {
2127 * To prevent nasty line-ending artefacts at
2128 * corners, I'll do something slightly cunning
2131 clip(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
);
2132 if (state
->soln
[y
*w
+x
] < 0)
2136 draw_line(dr
, COORD(x
-1), COORD(ly
), COORD(x
+2), COORD(ry
),
2144 print_line_width(dr
, TILESIZE
/ 24);
2145 for (y
= 0; y
<= h
; y
++)
2146 for (x
= 0; x
<= w
; x
++)
2147 draw_clue(dr
, ds
, x
, y
, state
->clues
->clues
[y
*W
+x
],
2152 #define thegame slant
2155 const struct game thegame
= {
2156 "Slant", "games.slant", "slant",
2163 TRUE
, game_configure
, custom_params
,
2171 TRUE
, game_can_format_as_text_now
, game_text_format
,
2179 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
2182 game_free_drawstate
,
2186 TRUE
, FALSE
, game_print_size
, game_print
,
2187 FALSE
, /* wants_statusbar */
2188 FALSE
, game_timing_state
,
2192 #ifdef STANDALONE_SOLVER
2196 int main(int argc
, char **argv
)
2200 char *id
= NULL
, *desc
, *err
;
2202 int ret
, diff
, really_verbose
= FALSE
;
2203 struct solver_scratch
*sc
;
2205 while (--argc
> 0) {
2207 if (!strcmp(p
, "-v")) {
2208 really_verbose
= TRUE
;
2209 } else if (!strcmp(p
, "-g")) {
2211 } else if (*p
== '-') {
2212 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
2220 fprintf(stderr
, "usage: %s [-g | -v] <game_id>\n", argv
[0]);
2224 desc
= strchr(id
, ':');
2226 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
2231 p
= default_params();
2232 decode_params(p
, id
);
2233 err
= validate_desc(p
, desc
);
2235 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
2238 s
= new_game(NULL
, p
, desc
);
2240 sc
= new_scratch(p
->w
, p
->h
);
2243 * When solving an Easy puzzle, we don't want to bother the
2244 * user with Hard-level deductions. For this reason, we grade
2245 * the puzzle internally before doing anything else.
2247 ret
= -1; /* placate optimiser */
2248 for (diff
= 0; diff
< DIFFCOUNT
; diff
++) {
2249 ret
= slant_solve(p
->w
, p
->h
, s
->clues
->clues
,
2255 if (diff
== DIFFCOUNT
) {
2257 printf("Difficulty rating: harder than Hard, or ambiguous\n");
2259 printf("Unable to find a unique solution\n");
2263 printf("Difficulty rating: impossible (no solution exists)\n");
2265 printf("Difficulty rating: %s\n", slant_diffnames
[diff
]);
2267 verbose
= really_verbose
;
2268 ret
= slant_solve(p
->w
, p
->h
, s
->clues
->clues
,
2271 printf("Puzzle is inconsistent\n");
2273 fputs(game_text_format(s
), stdout
);
2282 /* vim: set shiftwidth=4 tabstop=8: */