Introduce a new deductive mode in Slant's Hard level, which is the
[sgt/puzzles] / slant.c
1 /*
2 * slant.c: Puzzle from nikoli.co.jp involving drawing a diagonal
3 * line through each square of a grid.
4 */
5
6 /*
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.
11 *
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.
16 *
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.
19 *
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.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <math.h>
32
33 #include "puzzles.h"
34
35 enum {
36 COL_BACKGROUND,
37 COL_GRID,
38 COL_INK,
39 COL_SLANT1,
40 COL_SLANT2,
41 COL_ERROR,
42 NCOLOURS
43 };
44
45 /*
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
48 * true.
49 */
50 #if defined STANDALONE_SOLVER
51 #define SOLVER_DIAGNOSTICS
52 int verbose = FALSE;
53 #elif defined SOLVER_DIAGNOSTICS
54 #define verbose TRUE
55 #endif
56
57 /*
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.
60 */
61 #define DIFFLIST(A) \
62 A(EASY,Easy,e) \
63 A(HARD,Hard,h)
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)
72
73 struct game_params {
74 int w, h, diff;
75 };
76
77 typedef struct game_clues {
78 int w, h;
79 signed char *clues;
80 int *tmpdsf;
81 int refcount;
82 } game_clues;
83
84 #define ERR_VERTEX 1
85 #define ERR_SQUARE 2
86 #define ERR_SQUARE_TMP 4
87
88 struct game_state {
89 struct game_params p;
90 game_clues *clues;
91 signed char *soln;
92 unsigned char *errors;
93 int completed;
94 int used_solve; /* used to suppress completion flash */
95 };
96
97 static game_params *default_params(void)
98 {
99 game_params *ret = snew(game_params);
100
101 ret->w = ret->h = 8;
102 ret->diff = DIFF_EASY;
103
104 return ret;
105 }
106
107 static const struct game_params slant_presets[] = {
108 {5, 5, DIFF_EASY},
109 {5, 5, DIFF_HARD},
110 {8, 8, DIFF_EASY},
111 {8, 8, DIFF_HARD},
112 {12, 10, DIFF_EASY},
113 {12, 10, DIFF_HARD},
114 };
115
116 static int game_fetch_preset(int i, char **name, game_params **params)
117 {
118 game_params *ret;
119 char str[80];
120
121 if (i < 0 || i >= lenof(slant_presets))
122 return FALSE;
123
124 ret = snew(game_params);
125 *ret = slant_presets[i];
126
127 sprintf(str, "%dx%d %s", ret->w, ret->h, slant_diffnames[ret->diff]);
128
129 *name = dupstr(str);
130 *params = ret;
131 return TRUE;
132 }
133
134 static void free_params(game_params *params)
135 {
136 sfree(params);
137 }
138
139 static game_params *dup_params(game_params *params)
140 {
141 game_params *ret = snew(game_params);
142 *ret = *params; /* structure copy */
143 return ret;
144 }
145
146 static void decode_params(game_params *ret, char const *string)
147 {
148 ret->w = ret->h = atoi(string);
149 while (*string && isdigit((unsigned char)*string)) string++;
150 if (*string == 'x') {
151 string++;
152 ret->h = atoi(string);
153 while (*string && isdigit((unsigned char)*string)) string++;
154 }
155 if (*string == 'd') {
156 int i;
157 string++;
158 for (i = 0; i < DIFFCOUNT; i++)
159 if (*string == slant_diffchars[i])
160 ret->diff = i;
161 if (*string) string++;
162 }
163 }
164
165 static char *encode_params(game_params *params, int full)
166 {
167 char data[256];
168
169 sprintf(data, "%dx%d", params->w, params->h);
170 if (full)
171 sprintf(data + strlen(data), "d%c", slant_diffchars[params->diff]);
172
173 return dupstr(data);
174 }
175
176 static config_item *game_configure(game_params *params)
177 {
178 config_item *ret;
179 char buf[80];
180
181 ret = snewn(4, config_item);
182
183 ret[0].name = "Width";
184 ret[0].type = C_STRING;
185 sprintf(buf, "%d", params->w);
186 ret[0].sval = dupstr(buf);
187 ret[0].ival = 0;
188
189 ret[1].name = "Height";
190 ret[1].type = C_STRING;
191 sprintf(buf, "%d", params->h);
192 ret[1].sval = dupstr(buf);
193 ret[1].ival = 0;
194
195 ret[2].name = "Difficulty";
196 ret[2].type = C_CHOICES;
197 ret[2].sval = DIFFCONFIG;
198 ret[2].ival = params->diff;
199
200 ret[3].name = NULL;
201 ret[3].type = C_END;
202 ret[3].sval = NULL;
203 ret[3].ival = 0;
204
205 return ret;
206 }
207
208 static game_params *custom_params(config_item *cfg)
209 {
210 game_params *ret = snew(game_params);
211
212 ret->w = atoi(cfg[0].sval);
213 ret->h = atoi(cfg[1].sval);
214 ret->diff = cfg[2].ival;
215
216 return ret;
217 }
218
219 static char *validate_params(game_params *params, int full)
220 {
221 /*
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.
228 */
229
230 if (params->w < 2 || params->h < 2)
231 return "Width and height must both be at least two";
232
233 return NULL;
234 }
235
236 /*
237 * Scratch space for solver.
238 */
239 struct solver_scratch {
240 /*
241 * Disjoint set forest which tracks the connected sets of
242 * points.
243 */
244 int *connected;
245
246 /*
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
251 * consideration.)
252 */
253 int *exits;
254
255 /*
256 * Tracks whether each connected set of points includes a
257 * border point.
258 */
259 unsigned char *border;
260
261 /*
262 * Another disjoint set forest. This one tracks _squares_ which
263 * are known to slant in the same direction.
264 */
265 int *equiv;
266
267 /*
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.
273 */
274 signed char *slashval;
275
276 /*
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:
279 *
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
288 *
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
293 * as equivalent.
294 */
295 unsigned char *vbitmap;
296
297 /*
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.)
301 */
302 const signed char *clues;
303 };
304
305 static struct solver_scratch *new_scratch(int w, int h)
306 {
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);
315 return ret;
316 }
317
318 static void free_scratch(struct solver_scratch *sc)
319 {
320 sfree(sc->vbitmap);
321 sfree(sc->slashval);
322 sfree(sc->equiv);
323 sfree(sc->border);
324 sfree(sc->exits);
325 sfree(sc->connected);
326 sfree(sc);
327 }
328
329 /*
330 * Wrapper on dsf_merge() which updates the `exits' and `border'
331 * arrays.
332 */
333 static void merge_vertices(int *connected,
334 struct solver_scratch *sc, int i, int j)
335 {
336 int exits = -1, border = FALSE; /* initialise to placate optimiser */
337
338 if (sc) {
339 i = dsf_canonify(connected, i);
340 j = dsf_canonify(connected, j);
341
342 /*
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.
346 */
347 exits = sc->exits[i] + sc->exits[j] - 2;
348
349 border = sc->border[i] || sc->border[j];
350 }
351
352 dsf_merge(connected, i, j);
353
354 if (sc) {
355 i = dsf_canonify(connected, i);
356 sc->exits[i] = exits;
357 sc->border[i] = border;
358 }
359 }
360
361 /*
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
366 * whole.
367 */
368 static void decr_exits(struct solver_scratch *sc, int i)
369 {
370 if (sc->clues[i] < 0) {
371 i = dsf_canonify(sc->connected, i);
372 sc->exits[i]--;
373 }
374 }
375
376 static void fill_square(int w, int h, int x, int y, int v,
377 signed char *soln,
378 int *connected, struct solver_scratch *sc)
379 {
380 int W = w+1 /*, H = h+1 */;
381
382 assert(x >= 0 && x < w && y >= 0 && y < h);
383
384 if (soln[y*w+x] != 0) {
385 return; /* do nothing */
386 }
387
388 #ifdef SOLVER_DIAGNOSTICS
389 if (verbose)
390 printf(" placing %c in %d,%d\n", v == -1 ? '\\' : '/', x, y);
391 #endif
392
393 soln[y*w+x] = v;
394
395 if (sc) {
396 int c = dsf_canonify(sc->equiv, y*w+x);
397 sc->slashval[c] = v;
398 }
399
400 if (v < 0) {
401 merge_vertices(connected, sc, y*W+x, (y+1)*W+(x+1));
402 if (sc) {
403 decr_exits(sc, y*W+(x+1));
404 decr_exits(sc, (y+1)*W+x);
405 }
406 } else {
407 merge_vertices(connected, sc, y*W+(x+1), (y+1)*W+x);
408 if (sc) {
409 decr_exits(sc, y*W+x);
410 decr_exits(sc, (y+1)*W+(x+1));
411 }
412 }
413 }
414
415 static int vbitmap_clear(int w, int h, struct solver_scratch *sc,
416 int x, int y, int vbits, char *reason, ...)
417 {
418 int done_something = FALSE;
419 int vbit;
420
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
425 if (verbose) {
426 va_list ap;
427
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));
431
432 va_start(ap, reason);
433 vprintf(reason, ap);
434 va_end(ap);
435
436 printf(")\n");
437 }
438 #endif
439 sc->vbitmap[y*w+x] &= ~vbit;
440 }
441
442 return done_something;
443 }
444
445 /*
446 * Solver. Returns 0 for impossibility, 1 for success, 2 for
447 * ambiguity or failure to converge.
448 */
449 static int slant_solve(int w, int h, const signed char *clues,
450 signed char *soln, struct solver_scratch *sc,
451 int difficulty)
452 {
453 int W = w+1, H = h+1;
454 int x, y, i, j;
455 int done_something;
456
457 /*
458 * Clear the output.
459 */
460 memset(soln, 0, w*h);
461
462 sc->clues = clues;
463
464 /*
465 * Establish a disjoint set forest for tracking connectedness
466 * between grid points.
467 */
468 for (i = 0; i < W*H; i++)
469 sc->connected[i] = i; /* initially all distinct */
470
471 /*
472 * Establish a disjoint set forest for tracking which squares
473 * are known to slant in the same direction.
474 */
475 for (i = 0; i < w*h; i++)
476 sc->equiv[i] = i; /* initially all distinct */
477
478 /*
479 * Clear the slashval array.
480 */
481 memset(sc->slashval, 0, w*h);
482
483 /*
484 * Set up the vbitmap array. Initially all types of v are possible.
485 */
486 memset(sc->vbitmap, 0xF, w*h);
487
488 /*
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).
494 *
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.
501 */
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;
506 else
507 sc->border[y*W+x] = FALSE;
508
509 if (clues[y*W+x] < 0)
510 sc->exits[y*W+x] = 4;
511 else
512 sc->exits[y*W+x] = clues[y*W+x];
513 }
514
515 /*
516 * Repeatedly try to deduce something until we can't.
517 */
518 do {
519 done_something = FALSE;
520
521 /*
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.
525 */
526 for (y = 0; y < H; y++)
527 for (x = 0; x < W; x++) {
528 struct {
529 int pos, slash;
530 } neighbours[4];
531 int nneighbours;
532 int nu, nl, c, s, eq, eq2, last, meq, mj1, mj2;
533
534 if ((c = clues[y*W+x]) < 0)
535 continue;
536
537 /*
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.
542 */
543 nneighbours = 0;
544 if (x > 0 && y > 0) {
545 neighbours[nneighbours].pos = (y-1)*w+(x-1);
546 neighbours[nneighbours].slash = -1;
547 nneighbours++;
548 }
549 if (x > 0 && y < h) {
550 neighbours[nneighbours].pos = y*w+(x-1);
551 neighbours[nneighbours].slash = +1;
552 nneighbours++;
553 }
554 if (x < w && y < h) {
555 neighbours[nneighbours].pos = y*w+x;
556 neighbours[nneighbours].slash = -1;
557 nneighbours++;
558 }
559 if (x < w && y > 0) {
560 neighbours[nneighbours].pos = (y-1)*w+x;
561 neighbours[nneighbours].slash = +1;
562 nneighbours++;
563 }
564
565 /*
566 * Count up the number of undecided neighbours, and
567 * also the number of lines already present.
568 *
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.
574 */
575 nu = 0;
576 nl = c;
577 last = neighbours[nneighbours-1].pos;
578 if (soln[last] == 0)
579 eq = dsf_canonify(sc->equiv, last);
580 else
581 eq = -1;
582 meq = mj1 = mj2 = -1;
583 for (i = 0; i < nneighbours; i++) {
584 j = neighbours[i].pos;
585 s = neighbours[i].slash;
586 if (soln[j] == 0) {
587 nu++; /* undecided */
588 if (meq < 0 && difficulty > DIFF_EASY) {
589 eq2 = dsf_canonify(sc->equiv, j);
590 if (eq == eq2 && last != j) {
591 /*
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).
600 */
601 meq = eq;
602 mj1 = last;
603 mj2 = j;
604 nl--; /* count one line */
605 nu -= 2; /* and lose two undecideds */
606 } else
607 eq = eq2;
608 }
609 } else {
610 eq = -1;
611 if (soln[j] == s)
612 nl--; /* here's a line */
613 }
614 last = j;
615 }
616
617 /*
618 * Check the counts.
619 */
620 if (nl < 0 || nl > nu) {
621 /*
622 * No consistent value for this at all!
623 */
624 #ifdef SOLVER_DIAGNOSTICS
625 if (verbose)
626 printf("need %d / %d lines around clue point at %d,%d!\n",
627 nl, nu, x, y);
628 #endif
629 return 0; /* impossible */
630 }
631
632 if (nu > 0 && (nl == 0 || nl == nu)) {
633 #ifdef SOLVER_DIAGNOSTICS
634 if (verbose) {
635 if (meq >= 0)
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);
640 }
641 #endif
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,
647 sc->connected, sc);
648 }
649
650 done_something = TRUE;
651 } else if (nu == 2 && nl == 1 && difficulty > DIFF_EASY) {
652 /*
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
657 * another.
658 *
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.
663 *
664 * We don't bother with this on DIFF_EASY,
665 * since we wouldn't have used the results
666 * anyway.
667 */
668 last = -1;
669 for (i = 0; i < nneighbours; i++) {
670 j = neighbours[i].pos;
671 if (soln[j] == 0 && j != mj1 && j != mj2) {
672 if (last < 0)
673 last = i;
674 else if (last == i-1 || (last == 0 && i == 3))
675 break; /* found a pair */
676 }
677 }
678 if (i < nneighbours) {
679 int sv1, sv2;
680
681 assert(last >= 0);
682 /*
683 * neighbours[last] and neighbours[i] are
684 * the pair. Mark them equivalent.
685 */
686 #ifdef SOLVER_DIAGNOSTICS
687 if (verbose) {
688 if (meq >= 0)
689 printf("since %d,%d == %d,%d, ",
690 mj1%w, mj1/w, mj2%w, mj2/w);
691 }
692 #endif
693 mj1 = neighbours[last].pos;
694 mj2 = neighbours[i].pos;
695 #ifdef SOLVER_DIAGNOSTICS
696 if (verbose)
697 printf("clue point at %d,%d implies %d,%d == %d,"
698 "%d\n", x, y, mj1%w, mj1/w, mj2%w, mj2/w);
699 #endif
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
706 if (verbose)
707 printf("merged two equivalence classes with"
708 " different slash values!\n");
709 #endif
710 return 0;
711 }
712 sv1 = sv1 ? sv1 : sv2;
713 dsf_merge(sc->equiv, mj1, mj2);
714 mj1 = dsf_canonify(sc->equiv, mj1);
715 sc->slashval[mj1] = sv1;
716 }
717 }
718 }
719
720 if (done_something)
721 continue;
722
723 /*
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
729 * class.
730 *
731 * The slashval check is disabled on DIFF_EASY, as is dead
732 * end avoidance. Only _immediate_ loop avoidance remains.
733 */
734 for (y = 0; y < h; y++)
735 for (x = 0; x < w; x++) {
736 int fs, bs, v;
737 int c1, c2;
738 #ifdef SOLVER_DIAGNOSTICS
739 char *reason = "<internal error>";
740 #endif
741
742 if (soln[y*w+x])
743 continue; /* got this one already */
744
745 fs = FALSE;
746 bs = FALSE;
747
748 if (difficulty > DIFF_EASY)
749 v = sc->slashval[dsf_canonify(sc->equiv, y*w+x)];
750 else
751 v = 0;
752
753 /*
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.
757 */
758 c1 = dsf_canonify(sc->connected, y*W+x);
759 c2 = dsf_canonify(sc->connected, (y+1)*W+(x+1));
760 if (c1 == c2) {
761 fs = TRUE;
762 #ifdef SOLVER_DIAGNOSTICS
763 reason = "simple loop avoidance";
764 #endif
765 }
766 if (difficulty > DIFF_EASY &&
767 !sc->border[c1] && !sc->border[c2] &&
768 sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
769 fs = TRUE;
770 #ifdef SOLVER_DIAGNOSTICS
771 reason = "dead end avoidance";
772 #endif
773 }
774 if (v == +1) {
775 fs = TRUE;
776 #ifdef SOLVER_DIAGNOSTICS
777 reason = "equivalence to an already filled square";
778 #endif
779 }
780
781 /*
782 * Now do the same between (x+1,y) and (x,y+1), to
783 * see if we are required to have a backslash.
784 */
785 c1 = dsf_canonify(sc->connected, y*W+(x+1));
786 c2 = dsf_canonify(sc->connected, (y+1)*W+x);
787 if (c1 == c2) {
788 bs = TRUE;
789 #ifdef SOLVER_DIAGNOSTICS
790 reason = "simple loop avoidance";
791 #endif
792 }
793 if (difficulty > DIFF_EASY &&
794 !sc->border[c1] && !sc->border[c2] &&
795 sc->exits[c1] <= 1 && sc->exits[c2] <= 1) {
796 bs = TRUE;
797 #ifdef SOLVER_DIAGNOSTICS
798 reason = "dead end avoidance";
799 #endif
800 }
801 if (v == -1) {
802 bs = TRUE;
803 #ifdef SOLVER_DIAGNOSTICS
804 reason = "equivalence to an already filled square";
805 #endif
806 }
807
808 if (fs && bs) {
809 /*
810 * No consistent value for this at all!
811 */
812 #ifdef SOLVER_DIAGNOSTICS
813 if (verbose)
814 printf("%d,%d has no consistent slash!\n", x, y);
815 #endif
816 return 0; /* impossible */
817 }
818
819 if (fs) {
820 #ifdef SOLVER_DIAGNOSTICS
821 if (verbose)
822 printf("employing %s\n", reason);
823 #endif
824 fill_square(w, h, x, y, +1, soln, sc->connected, sc);
825 done_something = TRUE;
826 } else if (bs) {
827 #ifdef SOLVER_DIAGNOSTICS
828 if (verbose)
829 printf("employing %s\n", reason);
830 #endif
831 fill_square(w, h, x, y, -1, soln, sc->connected, sc);
832 done_something = TRUE;
833 }
834 }
835
836 if (done_something)
837 continue;
838
839 /*
840 * Now see what we can do with the vbitmap array. All
841 * vbitmap deductions are disabled at Easy level.
842 */
843 if (difficulty <= DIFF_EASY)
844 continue;
845
846 for (y = 0; y < h; y++)
847 for (x = 0; x < w; x++) {
848 int s, c;
849
850 /*
851 * Any line already placed in a square must rule
852 * out any type of v which contradicts it.
853 */
854 if ((s = soln[y*w+x]) != 0) {
855 if (x > 0)
856 done_something |=
857 vbitmap_clear(w, h, sc, x-1, y, (s < 0 ? 0x1 : 0x2),
858 "contradicts known edge at (%d,%d)",x,y);
859 if (x+1 < w)
860 done_something |=
861 vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x2 : 0x1),
862 "contradicts known edge at (%d,%d)",x,y);
863 if (y > 0)
864 done_something |=
865 vbitmap_clear(w, h, sc, x, y-1, (s < 0 ? 0x4 : 0x8),
866 "contradicts known edge at (%d,%d)",x,y);
867 if (y+1 < h)
868 done_something |=
869 vbitmap_clear(w, h, sc, x, y, (s < 0 ? 0x8 : 0x4),
870 "contradicts known edge at (%d,%d)",x,y);
871 }
872
873 /*
874 * If both types of v are ruled out for a pair of
875 * adjacent squares, mark them as equivalent.
876 */
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
884 if (verbose)
885 printf("(%d,%d) and (%d,%d) must be equivalent"
886 " because both v-shapes are ruled out\n",
887 x, y, x+1, y);
888 #endif
889 }
890 }
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
898 if (verbose)
899 printf("(%d,%d) and (%d,%d) must be equivalent"
900 " because both v-shapes are ruled out\n",
901 x, y, x, y+1);
902 #endif
903 }
904 }
905
906 /*
907 * The remaining work in this loop only works
908 * around non-edge clue points.
909 */
910 if (y == 0 || x == 0)
911 continue;
912 if ((c = clues[y*W+x]) < 0)
913 continue;
914
915 /*
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
918 * shapes.
919 */
920
921 if (c == 1) {
922 /*
923 * A 1 clue can never have any v shape pointing
924 * at it.
925 */
926 done_something |=
927 vbitmap_clear(w, h, sc, x-1, y-1, 0x5,
928 "points at 1 clue at (%d,%d)", x, y);
929 done_something |=
930 vbitmap_clear(w, h, sc, x-1, y, 0x2,
931 "points at 1 clue at (%d,%d)", x, y);
932 done_something |=
933 vbitmap_clear(w, h, sc, x, y-1, 0x8,
934 "points at 1 clue at (%d,%d)", x, y);
935 } else if (c == 3) {
936 /*
937 * A 3 clue can never have any v shape pointing
938 * away from it.
939 */
940 done_something |=
941 vbitmap_clear(w, h, sc, x-1, y-1, 0xA,
942 "points away from 3 clue at (%d,%d)", x, y);
943 done_something |=
944 vbitmap_clear(w, h, sc, x-1, y, 0x1,
945 "points away from 3 clue at (%d,%d)", x, y);
946 done_something |=
947 vbitmap_clear(w, h, sc, x, y-1, 0x4,
948 "points away from 3 clue at (%d,%d)", x, y);
949 } else if (c == 2) {
950 /*
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
953 * the other side.
954 */
955 done_something |=
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);
959 done_something |=
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);
963 done_something |=
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);
967 done_something |=
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);
971 }
972
973 #undef CLEARBITS
974
975 }
976
977 } while (done_something);
978
979 /*
980 * Solver can make no more progress. See if the grid is full.
981 */
982 for (i = 0; i < w*h; i++)
983 if (!soln[i])
984 return 2; /* failed to converge */
985 return 1; /* success */
986 }
987
988 /*
989 * Filled-grid generator.
990 */
991 static void slant_generate(int w, int h, signed char *soln, random_state *rs)
992 {
993 int W = w+1, H = h+1;
994 int x, y, i;
995 int *connected, *indices;
996
997 /*
998 * Clear the output.
999 */
1000 memset(soln, 0, w*h);
1001
1002 /*
1003 * Establish a disjoint set forest for tracking connectedness
1004 * between grid points.
1005 */
1006 connected = snewn(W*H, int);
1007 for (i = 0; i < W*H; i++)
1008 connected[i] = i; /* initially all distinct */
1009
1010 /*
1011 * Prepare a list of the squares in the grid, and fill them in
1012 * in a random order.
1013 */
1014 indices = snewn(w*h, int);
1015 for (i = 0; i < w*h; i++)
1016 indices[i] = i;
1017 shuffle(indices, w*h, sizeof(*indices), rs);
1018
1019 /*
1020 * Fill in each one in turn.
1021 */
1022 for (i = 0; i < w*h; i++) {
1023 int fs, bs, v;
1024
1025 y = indices[i] / w;
1026 x = indices[i] % w;
1027
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)));
1032
1033 /*
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
1037 * backtrack.
1038 *
1039 * Proof (thanks to Gareth Taylor):
1040 *
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.
1046 *
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. []
1055 */
1056 assert(!(fs && bs));
1057
1058 v = fs ? +1 : bs ? -1 : 2 * random_upto(rs, 2) - 1;
1059 fill_square(w, h, x, y, v, soln, connected, NULL);
1060 }
1061
1062 sfree(indices);
1063 sfree(connected);
1064 }
1065
1066 static char *new_game_desc(game_params *params, random_state *rs,
1067 char **aux, int interactive)
1068 {
1069 int w = params->w, h = params->h, W = w+1, H = h+1;
1070 signed char *soln, *tmpsoln, *clues;
1071 int *clueindices;
1072 struct solver_scratch *sc;
1073 int x, y, v, i, j;
1074 char *desc;
1075
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);
1081
1082 do {
1083 /*
1084 * Create the filled grid.
1085 */
1086 slant_generate(w, h, soln, rs);
1087
1088 /*
1089 * Fill in the complete set of clues.
1090 */
1091 for (y = 0; y < H; y++)
1092 for (x = 0; x < W; x++) {
1093 v = 0;
1094
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++;
1099
1100 clues[y*W+x] = v;
1101 }
1102
1103 /*
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.
1108 */
1109 assert(slant_solve(w, h, clues, tmpsoln, sc, DIFF_EASY) == 1);
1110
1111 /*
1112 * Remove as many clues as possible while retaining solubility.
1113 *
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.
1120 */
1121 for (i = 0; i < W*H; i++)
1122 clueindices[i] = i;
1123 shuffle(clueindices, W*H, sizeof(*clueindices), rs);
1124 for (j = 0; j < 2; j++) {
1125 for (i = 0; i < W*H; i++) {
1126 int pass, yb, xb;
1127
1128 y = clueindices[i] / W;
1129 x = clueindices[i] % W;
1130 v = clues[y*W+x];
1131
1132 /*
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
1136 * pass 1.
1137 */
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))
1142 pass = 0;
1143 else
1144 pass = 1;
1145
1146 if (pass == j) {
1147 clues[y*W+x] = -1;
1148 if (slant_solve(w, h, clues, tmpsoln, sc,
1149 params->diff) != 1)
1150 clues[y*W+x] = v; /* put it back */
1151 }
1152 }
1153 }
1154
1155 /*
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.
1159 */
1160 } while (params->diff > 0 &&
1161 slant_solve(w, h, clues, tmpsoln, sc, params->diff - 1) <= 1);
1162
1163 /*
1164 * Now we have the clue set as it will be presented to the
1165 * user. Encode it in a game desc.
1166 */
1167 {
1168 char *p;
1169 int run, i;
1170
1171 desc = snewn(W*H+1, char);
1172 p = desc;
1173 run = 0;
1174 for (i = 0; i <= W*H; i++) {
1175 int n = (i < W*H ? clues[i] : -2);
1176
1177 if (n == -1)
1178 run++;
1179 else {
1180 if (run) {
1181 while (run > 0) {
1182 int c = 'a' - 1 + run;
1183 if (run > 26)
1184 c = 'z';
1185 *p++ = c;
1186 run -= c - ('a' - 1);
1187 }
1188 }
1189 if (n >= 0)
1190 *p++ = '0' + n;
1191 run = 0;
1192 }
1193 }
1194 assert(p - desc <= W*H);
1195 *p++ = '\0';
1196 desc = sresize(desc, p - desc, char);
1197 }
1198
1199 /*
1200 * Encode the solution as an aux_info.
1201 */
1202 {
1203 char *auxbuf;
1204 *aux = auxbuf = snewn(w*h+1, char);
1205 for (i = 0; i < w*h; i++)
1206 auxbuf[i] = soln[i] < 0 ? '\\' : '/';
1207 auxbuf[w*h] = '\0';
1208 }
1209
1210 free_scratch(sc);
1211 sfree(clueindices);
1212 sfree(clues);
1213 sfree(tmpsoln);
1214 sfree(soln);
1215
1216 return desc;
1217 }
1218
1219 static char *validate_desc(game_params *params, char *desc)
1220 {
1221 int w = params->w, h = params->h, W = w+1, H = h+1;
1222 int area = W*H;
1223 int squares = 0;
1224
1225 while (*desc) {
1226 int n = *desc++;
1227 if (n >= 'a' && n <= 'z') {
1228 squares += n - 'a' + 1;
1229 } else if (n >= '0' && n <= '4') {
1230 squares++;
1231 } else
1232 return "Invalid character in game description";
1233 }
1234
1235 if (squares < area)
1236 return "Not enough data to fill grid";
1237
1238 if (squares > area)
1239 return "Too much data to fit in grid";
1240
1241 return NULL;
1242 }
1243
1244 static game_state *new_game(midend *me, game_params *params, char *desc)
1245 {
1246 int w = params->w, h = params->h, W = w+1, H = h+1;
1247 game_state *state = snew(game_state);
1248 int area = W*H;
1249 int squares = 0;
1250
1251 state->p = *params;
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);
1257
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);
1265 while (*desc) {
1266 int n = *desc++;
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';
1271 } else
1272 assert(!"can't get here");
1273 }
1274 assert(squares == area);
1275
1276 return state;
1277 }
1278
1279 static game_state *dup_game(game_state *state)
1280 {
1281 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1282 game_state *ret = snew(game_state);
1283
1284 ret->p = state->p;
1285 ret->clues = state->clues;
1286 ret->clues->refcount++;
1287 ret->completed = state->completed;
1288 ret->used_solve = state->used_solve;
1289
1290 ret->soln = snewn(w*h, signed char);
1291 memcpy(ret->soln, state->soln, w*h);
1292
1293 ret->errors = snewn(W*H, unsigned char);
1294 memcpy(ret->errors, state->errors, W*H);
1295
1296 return ret;
1297 }
1298
1299 static void free_game(game_state *state)
1300 {
1301 sfree(state->errors);
1302 sfree(state->soln);
1303 assert(state->clues);
1304 if (--state->clues->refcount <= 0) {
1305 sfree(state->clues->clues);
1306 sfree(state->clues->tmpdsf);
1307 sfree(state->clues);
1308 }
1309 sfree(state);
1310 }
1311
1312 /*
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.
1318 *
1319 * (Yes, _4_ minus its anti-degree even if it's a border vertex.)
1320 *
1321 * If ret > 0, *sx and *sy are set to the coordinates of one of the
1322 * squares that contributed to it.
1323 */
1324 static int vertex_degree(int w, int h, signed char *soln, int x, int y,
1325 int anti, int *sx, int *sy)
1326 {
1327 int ret = 0;
1328
1329 assert(x >= 0 && x <= w && y >= 0 && y <= h);
1330 if (x > 0 && y > 0 && soln[(y-1)*w+(x-1)] - anti < 0) {
1331 if (sx) *sx = x-1;
1332 if (sy) *sy = y-1;
1333 ret++;
1334 }
1335 if (x > 0 && y < h && soln[y*w+(x-1)] + anti > 0) {
1336 if (sx) *sx = x-1;
1337 if (sy) *sy = y;
1338 ret++;
1339 }
1340 if (x < w && y > 0 && soln[(y-1)*w+x] + anti > 0) {
1341 if (sx) *sx = x;
1342 if (sy) *sy = y-1;
1343 ret++;
1344 }
1345 if (x < w && y < h && soln[y*w+x] - anti < 0) {
1346 if (sx) *sx = x;
1347 if (sy) *sy = y;
1348 ret++;
1349 }
1350
1351 return anti ? 4 - ret : ret;
1352 }
1353
1354 static int check_completion(game_state *state)
1355 {
1356 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1357 int i, x, y, err = FALSE;
1358 int *dsf;
1359
1360 memset(state->errors, 0, W*H);
1361
1362 /*
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.
1367 *
1368 * We use the `tmpdsf' scratch space in the shared clues
1369 * structure, to avoid mallocing too often.
1370 *
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.
1377 *
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.
1387 */
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++) {
1393 int i1, i2;
1394
1395 if (state->soln[y*w+x] == 0)
1396 continue;
1397 if (state->soln[y*w+x] < 0) {
1398 i1 = y*W+x;
1399 i2 = (y+1)*W+(x+1);
1400 } else {
1401 i1 = y*W+(x+1);
1402 i2 = (y+1)*W+x;
1403 }
1404
1405 /*
1406 * Our edge connects i1 with i2. If they're already
1407 * connected, flag an error. Otherwise, link them.
1408 */
1409 if (dsf_canonify(dsf, i1) == dsf_canonify(dsf, i2)) {
1410 int x1, y1, x2, y2, dx, dy, dt, pass;
1411
1412 err = TRUE;
1413
1414 /*
1415 * Now search around the boundary of the loop to
1416 * highlight it.
1417 *
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
1426 * outer loop.
1427 */
1428
1429 for (pass = 0; pass < 2; pass++) {
1430
1431 x1 = i1 % W;
1432 y1 = i1 / W;
1433 x2 = i2 % W;
1434 y2 = i2 / W;
1435
1436 do {
1437 /* Mark this edge. */
1438 if (pass == 0) {
1439 state->errors[min(y1,y2)*W+min(x1,x2)] ^=
1440 ERR_SQUARE_TMP;
1441 } else {
1442 state->errors[min(y1,y2)*W+min(x1,x2)] |=
1443 ERR_SQUARE;
1444 state->errors[min(y1,y2)*W+min(x1,x2)] &=
1445 ~ERR_SQUARE_TMP;
1446 }
1447
1448 /*
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
1453 * can follow.
1454 */
1455 dx = x1 - x2;
1456 dy = y1 - y2;
1457
1458 for (i = 0; i < 4; i++) {
1459 /*
1460 * Rotate (dx,dy) to the left.
1461 */
1462 dt = dx; dx = dy; dy = -dt;
1463
1464 /*
1465 * See if (x2,y2) has an edge in direction
1466 * (dx,dy).
1467 */
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. */
1472 if (pass == 1 &&
1473 !(state->errors[(y2-(dy<0))*W+x2-(dx<0)] &
1474 ERR_SQUARE_TMP))
1475 continue;
1476 if (state->soln[(y2-(dy<0))*w+x2-(dx<0)] ==
1477 (dx==dy ? -1 : +1))
1478 break;
1479 }
1480
1481 /*
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.
1486 *
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.
1492 */
1493 if (pass == 1 && i == 4)
1494 break;
1495 assert(i < 4);
1496
1497 /*
1498 * Set x1,y1 to x2,y2, and x2,y2 to be the
1499 * other end of the new edge.
1500 */
1501 x1 = x2;
1502 y1 = y2;
1503 x2 += dx;
1504 y2 += dy;
1505 } while (y2*W+x2 != i2);
1506
1507 }
1508
1509 } else
1510 dsf_merge(dsf, i1, i2);
1511 }
1512
1513 /*
1514 * Now go through and check the degree of each clue vertex, and
1515 * mark it with ERR_VERTEX if it cannot be fulfilled.
1516 */
1517 for (y = 0; y < H; y++)
1518 for (x = 0; x < W; x++) {
1519 int c;
1520
1521 if ((c = state->clues->clues[y*W+x]) < 0)
1522 continue;
1523
1524 /*
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.
1528 */
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;
1534 err = TRUE;
1535 }
1536 }
1537
1538 /*
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.
1542 */
1543
1544 if (err)
1545 return FALSE;
1546
1547 for (y = 0; y < h; y++)
1548 for (x = 0; x < w; x++)
1549 if (state->soln[y*w+x] == 0)
1550 return FALSE;
1551
1552 return TRUE;
1553 }
1554
1555 static char *solve_game(game_state *state, game_state *currstate,
1556 char *aux, char **error)
1557 {
1558 int w = state->p.w, h = state->p.h;
1559 signed char *soln;
1560 int bs, ret;
1561 int free_soln = FALSE;
1562 char *move, buf[80];
1563 int movelen, movesize;
1564 int x, y;
1565
1566 if (aux) {
1567 /*
1568 * If we already have the solution, save ourselves some
1569 * time.
1570 */
1571 soln = (signed char *)aux;
1572 bs = (signed char)'\\';
1573 free_soln = FALSE;
1574 } else {
1575 struct solver_scratch *sc = new_scratch(w, h);
1576 soln = snewn(w*h, signed char);
1577 bs = -1;
1578 ret = slant_solve(w, h, state->clues->clues, soln, sc, DIFF_HARD);
1579 free_scratch(sc);
1580 if (ret != 1) {
1581 sfree(soln);
1582 if (ret == 0)
1583 *error = "This puzzle is not self-consistent";
1584 else
1585 *error = "Unable to find a unique solution for this puzzle";
1586 return NULL;
1587 }
1588 free_soln = TRUE;
1589 }
1590
1591 /*
1592 * Construct a move string which turns the current state into
1593 * the solved state.
1594 */
1595 movesize = 256;
1596 move = snewn(movesize, char);
1597 movelen = 0;
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);
1608 }
1609 strcpy(move + movelen, buf);
1610 movelen += len;
1611 }
1612 }
1613
1614 if (free_soln)
1615 sfree(soln);
1616
1617 return move;
1618 }
1619
1620 static char *game_text_format(game_state *state)
1621 {
1622 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1623 int x, y, len;
1624 char *ret, *p;
1625
1626 /*
1627 * There are h+H rows of w+W columns.
1628 */
1629 len = (h+H) * (w+W+1) + 1;
1630 ret = snewn(len, char);
1631 p = ret;
1632
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';
1637 else
1638 *p++ = '+';
1639 if (x < w)
1640 *p++ = '-';
1641 }
1642 *p++ = '\n';
1643 if (y < h) {
1644 for (x = 0; x < W; x++) {
1645 *p++ = '|';
1646 if (x < w) {
1647 if (state->soln[y*w+x] != 0)
1648 *p++ = (state->soln[y*w+x] < 0 ? '\\' : '/');
1649 else
1650 *p++ = ' ';
1651 }
1652 }
1653 *p++ = '\n';
1654 }
1655 }
1656 *p++ = '\0';
1657
1658 assert(p - ret == len);
1659 return ret;
1660 }
1661
1662 static game_ui *new_ui(game_state *state)
1663 {
1664 return NULL;
1665 }
1666
1667 static void free_ui(game_ui *ui)
1668 {
1669 }
1670
1671 static char *encode_ui(game_ui *ui)
1672 {
1673 return NULL;
1674 }
1675
1676 static void decode_ui(game_ui *ui, char *encoding)
1677 {
1678 }
1679
1680 static void game_changed_state(game_ui *ui, game_state *oldstate,
1681 game_state *newstate)
1682 {
1683 }
1684
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 )
1692
1693 #define FLASH_TIME 0.30F
1694
1695 /*
1696 * Bit fields in the `grid' and `todraw' elements of the drawstate.
1697 */
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
1716
1717 struct game_drawstate {
1718 int tilesize;
1719 int started;
1720 long *grid;
1721 long *todraw;
1722 };
1723
1724 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
1725 int x, int y, int button)
1726 {
1727 int w = state->p.w, h = state->p.h;
1728
1729 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
1730 int v;
1731 char buf[80];
1732
1733 /*
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
1740 * lookups.
1741 */
1742 {
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'));
1747 }
1748 if (swap_buttons) {
1749 if (button == LEFT_BUTTON)
1750 button = RIGHT_BUTTON;
1751 else
1752 button = LEFT_BUTTON;
1753 }
1754 }
1755
1756 x = FROMCOORD(x);
1757 y = FROMCOORD(y);
1758 if (x < 0 || y < 0 || x >= w || y >= h)
1759 return NULL;
1760
1761 if (button == LEFT_BUTTON) {
1762 /*
1763 * Left-clicking cycles blank -> \ -> / -> blank.
1764 */
1765 v = state->soln[y*w+x] - 1;
1766 if (v == -2)
1767 v = +1;
1768 } else {
1769 /*
1770 * Right-clicking cycles blank -> / -> \ -> blank.
1771 */
1772 v = state->soln[y*w+x] + 1;
1773 if (v == +2)
1774 v = -1;
1775 }
1776
1777 sprintf(buf, "%c%d,%d", (int)(v==-1 ? '\\' : v==+1 ? '/' : 'C'), x, y);
1778 return dupstr(buf);
1779 }
1780
1781 return NULL;
1782 }
1783
1784 static game_state *execute_move(game_state *state, char *move)
1785 {
1786 int w = state->p.w, h = state->p.h;
1787 char c;
1788 int x, y, n;
1789 game_state *ret = dup_game(state);
1790
1791 while (*move) {
1792 c = *move;
1793 if (c == 'S') {
1794 ret->used_solve = TRUE;
1795 move++;
1796 } else if (c == '\\' || c == '/' || c == 'C') {
1797 move++;
1798 if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
1799 x < 0 || y < 0 || x >= w || y >= h) {
1800 free_game(ret);
1801 return NULL;
1802 }
1803 ret->soln[y*w+x] = (c == '\\' ? -1 : c == '/' ? +1 : 0);
1804 move += n;
1805 } else {
1806 free_game(ret);
1807 return NULL;
1808 }
1809 if (*move == ';')
1810 move++;
1811 else if (*move) {
1812 free_game(ret);
1813 return NULL;
1814 }
1815 }
1816
1817 /*
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.
1821 */
1822 ret->completed = check_completion(ret) || ret->completed;
1823
1824 return ret;
1825 }
1826
1827 /* ----------------------------------------------------------------------
1828 * Drawing routines.
1829 */
1830
1831 static void game_compute_size(game_params *params, int tilesize,
1832 int *x, int *y)
1833 {
1834 /* fool the macros */
1835 struct dummy { int tilesize; } dummy = { tilesize }, *ds = &dummy;
1836
1837 *x = 2 * BORDER + params->w * TILESIZE + 1;
1838 *y = 2 * BORDER + params->h * TILESIZE + 1;
1839 }
1840
1841 static void game_set_size(drawing *dr, game_drawstate *ds,
1842 game_params *params, int tilesize)
1843 {
1844 ds->tilesize = tilesize;
1845 }
1846
1847 static float *game_colours(frontend *fe, int *ncolours)
1848 {
1849 float *ret = snewn(3 * NCOLOURS, float);
1850
1851 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1852
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;
1856
1857 ret[COL_INK * 3 + 0] = 0.0F;
1858 ret[COL_INK * 3 + 1] = 0.0F;
1859 ret[COL_INK * 3 + 2] = 0.0F;
1860
1861 ret[COL_SLANT1 * 3 + 0] = 0.0F;
1862 ret[COL_SLANT1 * 3 + 1] = 0.0F;
1863 ret[COL_SLANT1 * 3 + 2] = 0.0F;
1864
1865 ret[COL_SLANT2 * 3 + 0] = 0.0F;
1866 ret[COL_SLANT2 * 3 + 1] = 0.0F;
1867 ret[COL_SLANT2 * 3 + 2] = 0.0F;
1868
1869 ret[COL_ERROR * 3 + 0] = 1.0F;
1870 ret[COL_ERROR * 3 + 1] = 0.0F;
1871 ret[COL_ERROR * 3 + 2] = 0.0F;
1872
1873 *ncolours = NCOLOURS;
1874 return ret;
1875 }
1876
1877 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1878 {
1879 int w = state->p.w, h = state->p.h;
1880 int i;
1881 struct game_drawstate *ds = snew(struct game_drawstate);
1882
1883 ds->tilesize = 0;
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;
1889
1890 return ds;
1891 }
1892
1893 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1894 {
1895 sfree(ds->todraw);
1896 sfree(ds->grid);
1897 sfree(ds);
1898 }
1899
1900 static void draw_clue(drawing *dr, game_drawstate *ds,
1901 int x, int y, long v, long err, int bg, int colour)
1902 {
1903 char p[2];
1904 int ccol = colour >= 0 ? colour : ((x ^ y) & 1) ? COL_SLANT1 : COL_SLANT2;
1905 int tcol = colour >= 0 ? colour : err ? COL_ERROR : COL_INK;
1906
1907 if (v < 0)
1908 return;
1909
1910 p[0] = v + '0';
1911 p[1] = '\0';
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);
1916 }
1917
1918 static void draw_tile(drawing *dr, game_drawstate *ds, game_clues *clues,
1919 int x, int y, long v)
1920 {
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;
1925
1926 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
1927
1928 draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
1929 (v & FLASH) ? COL_GRID : COL_BACKGROUND);
1930
1931 /*
1932 * Draw the grid lines.
1933 */
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);
1950
1951 /*
1952 * Draw the slash.
1953 */
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,
1958 scol);
1959 draw_line(dr, COORD(x), COORD(y)+1, COORD(x+1)-1, COORD(y+1),
1960 scol);
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,
1965 scol);
1966 draw_line(dr, COORD(x+1), COORD(y)+1, COORD(x)+1, COORD(y+1),
1967 scol);
1968 }
1969
1970 /*
1971 * Draw dots on the grid corners that appear if a slash is in a
1972 * neighbouring cell.
1973 */
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));
1989
1990 /*
1991 * And finally the clues at the corners.
1992 */
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);
1999 if (x < w && y < h)
2000 draw_clue(dr, ds, x+1, y+1, clues->clues[(y+1)*W+(x+1)], v & ERR_BR,
2001 -1, -1);
2002
2003 unclip(dr);
2004 draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2005 }
2006
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)
2010 {
2011 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
2012 int x, y;
2013 int flashing;
2014
2015 if (flashtime > 0)
2016 flashing = (int)(flashtime * 3 / FLASH_TIME) != 1;
2017 else
2018 flashing = FALSE;
2019
2020 if (!ds->started) {
2021 int ww, wh;
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);
2025 ds->started = TRUE;
2026 }
2027
2028 /*
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.
2032 */
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;
2037 else
2038 ds->todraw[(y+1)*(w+2)+(x+1)] = 0;
2039 }
2040
2041 for (y = 0; y < h; y++) {
2042 for (x = 0; x < w; x++) {
2043 int err = state->errors[y*W+x] & ERR_SQUARE;
2044
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;
2050 if (err) {
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;
2056 }
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;
2061 if (err) {
2062 ds->todraw[(y+1)*(w+2)+(x+1)] |= ERRSLASH |
2063 ERR_L_B | ERR_T_R;
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;
2066 }
2067 }
2068 }
2069 }
2070
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;
2078 }
2079
2080 /*
2081 * Now go through and draw the grid squares.
2082 */
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)];
2089 }
2090 }
2091 }
2092 }
2093
2094 static float game_anim_length(game_state *oldstate, game_state *newstate,
2095 int dir, game_ui *ui)
2096 {
2097 return 0.0F;
2098 }
2099
2100 static float game_flash_length(game_state *oldstate, game_state *newstate,
2101 int dir, game_ui *ui)
2102 {
2103 if (!oldstate->completed && newstate->completed &&
2104 !oldstate->used_solve && !newstate->used_solve)
2105 return FLASH_TIME;
2106
2107 return 0.0F;
2108 }
2109
2110 static int game_timing_state(game_state *state, game_ui *ui)
2111 {
2112 return TRUE;
2113 }
2114
2115 static void game_print_size(game_params *params, float *x, float *y)
2116 {
2117 int pw, ph;
2118
2119 /*
2120 * I'll use 6mm squares by default.
2121 */
2122 game_compute_size(params, 600, &pw, &ph);
2123 *x = pw / 100.0;
2124 *y = ph / 100.0;
2125 }
2126
2127 static void game_print(drawing *dr, game_state *state, int tilesize)
2128 {
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);
2132 int x, y;
2133
2134 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2135 game_drawstate ads, *ds = &ads;
2136 game_set_size(dr, ds, NULL, tilesize);
2137
2138 /*
2139 * Border.
2140 */
2141 print_line_width(dr, TILESIZE / 16);
2142 draw_rect_outline(dr, COORD(0), COORD(0), w*TILESIZE, h*TILESIZE, ink);
2143
2144 /*
2145 * Grid.
2146 */
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);
2152
2153 /*
2154 * Solution.
2155 */
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]) {
2160 int ly, ry;
2161 /*
2162 * To prevent nasty line-ending artefacts at
2163 * corners, I'll do something slightly cunning
2164 * here.
2165 */
2166 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2167 if (state->soln[y*w+x] < 0)
2168 ly = y-1, ry = y+2;
2169 else
2170 ry = y-1, ly = y+2;
2171 draw_line(dr, COORD(x-1), COORD(ly), COORD(x+2), COORD(ry),
2172 ink);
2173 unclip(dr);
2174 }
2175
2176 /*
2177 * Clues.
2178 */
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],
2183 FALSE, paper, ink);
2184 }
2185
2186 #ifdef COMBINED
2187 #define thegame slant
2188 #endif
2189
2190 const struct game thegame = {
2191 "Slant", "games.slant",
2192 default_params,
2193 game_fetch_preset,
2194 decode_params,
2195 encode_params,
2196 free_params,
2197 dup_params,
2198 TRUE, game_configure, custom_params,
2199 validate_params,
2200 new_game_desc,
2201 validate_desc,
2202 new_game,
2203 dup_game,
2204 free_game,
2205 TRUE, solve_game,
2206 TRUE, game_text_format,
2207 new_ui,
2208 free_ui,
2209 encode_ui,
2210 decode_ui,
2211 game_changed_state,
2212 interpret_move,
2213 execute_move,
2214 PREFERRED_TILESIZE, game_compute_size, game_set_size,
2215 game_colours,
2216 game_new_drawstate,
2217 game_free_drawstate,
2218 game_redraw,
2219 game_anim_length,
2220 game_flash_length,
2221 TRUE, FALSE, game_print_size, game_print,
2222 FALSE, /* wants_statusbar */
2223 FALSE, game_timing_state,
2224 0, /* flags */
2225 };
2226
2227 #ifdef STANDALONE_SOLVER
2228
2229 #include <stdarg.h>
2230
2231 int main(int argc, char **argv)
2232 {
2233 game_params *p;
2234 game_state *s;
2235 char *id = NULL, *desc, *err;
2236 int grade = FALSE;
2237 int ret, diff, really_verbose = FALSE;
2238 struct solver_scratch *sc;
2239
2240 while (--argc > 0) {
2241 char *p = *++argv;
2242 if (!strcmp(p, "-v")) {
2243 really_verbose = TRUE;
2244 } else if (!strcmp(p, "-g")) {
2245 grade = TRUE;
2246 } else if (*p == '-') {
2247 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2248 return 1;
2249 } else {
2250 id = p;
2251 }
2252 }
2253
2254 if (!id) {
2255 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
2256 return 1;
2257 }
2258
2259 desc = strchr(id, ':');
2260 if (!desc) {
2261 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2262 return 1;
2263 }
2264 *desc++ = '\0';
2265
2266 p = default_params();
2267 decode_params(p, id);
2268 err = validate_desc(p, desc);
2269 if (err) {
2270 fprintf(stderr, "%s: %s\n", argv[0], err);
2271 return 1;
2272 }
2273 s = new_game(NULL, p, desc);
2274
2275 sc = new_scratch(p->w, p->h);
2276
2277 /*
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.
2281 */
2282 ret = -1; /* placate optimiser */
2283 for (diff = 0; diff < DIFFCOUNT; diff++) {
2284 ret = slant_solve(p->w, p->h, s->clues->clues,
2285 s->soln, sc, diff);
2286 if (ret < 2)
2287 break;
2288 }
2289
2290 if (diff == DIFFCOUNT) {
2291 if (grade)
2292 printf("Difficulty rating: harder than Hard, or ambiguous\n");
2293 else
2294 printf("Unable to find a unique solution\n");
2295 } else {
2296 if (grade) {
2297 if (ret == 0)
2298 printf("Difficulty rating: impossible (no solution exists)\n");
2299 else if (ret == 1)
2300 printf("Difficulty rating: %s\n", slant_diffnames[diff]);
2301 } else {
2302 verbose = really_verbose;
2303 ret = slant_solve(p->w, p->h, s->clues->clues,
2304 s->soln, sc, diff);
2305 if (ret == 0)
2306 printf("Puzzle is inconsistent\n");
2307 else
2308 fputs(game_text_format(s), stdout);
2309 }
2310 }
2311
2312 return 0;
2313 }
2314
2315 #endif