2 * solo.c: the number-placing puzzle most popularly known as `Sudoku'.
6 * - reports from users are that `Trivial'-mode puzzles are still
7 * rather hard compared to newspapers' easy ones, so some better
8 * low-end difficulty grading would be nice
9 * + it's possible that really easy puzzles always have
10 * _several_ things you can do, so don't make you hunt too
11 * hard for the one deduction you can currently make
12 * + it's also possible that easy puzzles require fewer
13 * cross-eliminations: perhaps there's a higher incidence of
14 * things you can deduce by looking only at (say) rows,
15 * rather than things you have to check both rows and columns
17 * + but really, what I need to do is find some really easy
18 * puzzles and _play_ them, to see what's actually easy about
20 * + while I'm revamping this area, filling in the _last_
21 * number in a nearly-full row or column should certainly be
22 * permitted even at the lowest difficulty level.
23 * + also Owen noticed that `Basic' grids requiring numeric
24 * elimination are actually very hard, so I wonder if a
25 * difficulty gradation between that and positional-
26 * elimination-only might be in order
27 * + but it's not good to have _too_ many difficulty levels, or
28 * it'll take too long to randomly generate a given level.
30 * - it might still be nice to do some prioritisation on the
31 * removal of numbers from the grid
32 * + one possibility is to try to minimise the maximum number
33 * of filled squares in any block, which in particular ought
34 * to enforce never leaving a completely filled block in the
35 * puzzle as presented.
37 * - alternative interface modes
38 * + sudoku.com's Windows program has a palette of possible
39 * entries; you select a palette entry first and then click
40 * on the square you want it to go in, thus enabling
41 * mouse-only play. Useful for PDAs! I don't think it's
42 * actually incompatible with the current highlight-then-type
43 * approach: you _either_ highlight a palette entry and then
44 * click, _or_ you highlight a square and then type. At most
45 * one thing is ever highlighted at a time, so there's no way
47 * + then again, I don't actually like sudoku.com's interface;
48 * it's too much like a paint package whereas I prefer to
49 * think of Solo as a text editor.
50 * + another PDA-friendly possibility is a drag interface:
51 * _drag_ numbers from the palette into the grid squares.
52 * Thought experiments suggest I'd prefer that to the
53 * sudoku.com approach, but I haven't actually tried it.
57 * Solo puzzles need to be square overall (since each row and each
58 * column must contain one of every digit), but they need not be
59 * subdivided the same way internally. I am going to adopt a
60 * convention whereby I _always_ refer to `r' as the number of rows
61 * of _big_ divisions, and `c' as the number of columns of _big_
62 * divisions. Thus, a 2c by 3r puzzle looks something like this:
66 * ------+------ (Of course, you can't subdivide it the other way
67 * 1 4 5 | 6 3 2 or you'll get clashes; observe that the 4 in the
68 * 3 2 6 | 4 1 5 top left would conflict with the 4 in the second
69 * ------+------ box down on the left-hand side.)
73 * The need for a strong naming convention should now be clear:
74 * each small box is two rows of digits by three columns, while the
75 * overall puzzle has three rows of small boxes by two columns. So
76 * I will (hopefully) consistently use `r' to denote the number of
77 * rows _of small boxes_ (here 3), which is also the number of
78 * columns of digits in each small box; and `c' vice versa (here
81 * I'm also going to choose arbitrarily to list c first wherever
82 * possible: the above is a 2x3 puzzle, not a 3x2 one.
92 #ifdef STANDALONE_SOLVER
94 int solver_show_working
, solver_recurse_depth
;
100 * To save space, I store digits internally as unsigned char. This
101 * imposes a hard limit of 255 on the order of the puzzle. Since
102 * even a 5x5 takes unacceptably long to generate, I don't see this
103 * as a serious limitation unless something _really_ impressive
104 * happens in computing technology; but here's a typedef anyway for
105 * general good practice.
107 typedef unsigned char digit
;
108 #define ORDER_MAX 255
110 #define PREFERRED_TILE_SIZE 32
111 #define TILE_SIZE (ds->tilesize)
112 #define BORDER (TILE_SIZE / 2)
114 #define FLASH_TIME 0.4F
116 enum { SYMM_NONE
, SYMM_ROT2
, SYMM_ROT4
, SYMM_REF2
, SYMM_REF2D
, SYMM_REF4
,
117 SYMM_REF4D
, SYMM_REF8
};
119 enum { DIFF_BLOCK
, DIFF_SIMPLE
, DIFF_INTERSECT
,
120 DIFF_SET
, DIFF_RECURSIVE
, DIFF_AMBIGUOUS
, DIFF_IMPOSSIBLE
};
134 int c
, r
, symm
, diff
;
140 unsigned char *pencil
; /* c*r*c*r elements */
141 unsigned char *immutable
; /* marks which digits are clues */
142 int completed
, cheated
;
145 static game_params
*default_params(void)
147 game_params
*ret
= snew(game_params
);
150 ret
->symm
= SYMM_ROT2
; /* a plausible default */
151 ret
->diff
= DIFF_BLOCK
; /* so is this */
156 static void free_params(game_params
*params
)
161 static game_params
*dup_params(game_params
*params
)
163 game_params
*ret
= snew(game_params
);
164 *ret
= *params
; /* structure copy */
168 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
174 { "2x2 Trivial", { 2, 2, SYMM_ROT2
, DIFF_BLOCK
} },
175 { "2x3 Basic", { 2, 3, SYMM_ROT2
, DIFF_SIMPLE
} },
176 { "3x3 Trivial", { 3, 3, SYMM_ROT2
, DIFF_BLOCK
} },
177 { "3x3 Basic", { 3, 3, SYMM_ROT2
, DIFF_SIMPLE
} },
178 { "3x3 Intermediate", { 3, 3, SYMM_ROT2
, DIFF_INTERSECT
} },
179 { "3x3 Advanced", { 3, 3, SYMM_ROT2
, DIFF_SET
} },
180 { "3x3 Unreasonable", { 3, 3, SYMM_ROT2
, DIFF_RECURSIVE
} },
182 { "3x4 Basic", { 3, 4, SYMM_ROT2
, DIFF_SIMPLE
} },
183 { "4x4 Basic", { 4, 4, SYMM_ROT2
, DIFF_SIMPLE
} },
187 if (i
< 0 || i
>= lenof(presets
))
190 *name
= dupstr(presets
[i
].title
);
191 *params
= dup_params(&presets
[i
].params
);
196 static void decode_params(game_params
*ret
, char const *string
)
198 ret
->c
= ret
->r
= atoi(string
);
199 while (*string
&& isdigit((unsigned char)*string
)) string
++;
200 if (*string
== 'x') {
202 ret
->r
= atoi(string
);
203 while (*string
&& isdigit((unsigned char)*string
)) string
++;
206 if (*string
== 'r' || *string
== 'm' || *string
== 'a') {
209 if (*string
== 'd') {
216 while (*string
&& isdigit((unsigned char)*string
)) string
++;
217 if (sc
== 'm' && sn
== 8)
218 ret
->symm
= SYMM_REF8
;
219 if (sc
== 'm' && sn
== 4)
220 ret
->symm
= sd ? SYMM_REF4D
: SYMM_REF4
;
221 if (sc
== 'm' && sn
== 2)
222 ret
->symm
= sd ? SYMM_REF2D
: SYMM_REF2
;
223 if (sc
== 'r' && sn
== 4)
224 ret
->symm
= SYMM_ROT4
;
225 if (sc
== 'r' && sn
== 2)
226 ret
->symm
= SYMM_ROT2
;
228 ret
->symm
= SYMM_NONE
;
229 } else if (*string
== 'd') {
231 if (*string
== 't') /* trivial */
232 string
++, ret
->diff
= DIFF_BLOCK
;
233 else if (*string
== 'b') /* basic */
234 string
++, ret
->diff
= DIFF_SIMPLE
;
235 else if (*string
== 'i') /* intermediate */
236 string
++, ret
->diff
= DIFF_INTERSECT
;
237 else if (*string
== 'a') /* advanced */
238 string
++, ret
->diff
= DIFF_SET
;
239 else if (*string
== 'u') /* unreasonable */
240 string
++, ret
->diff
= DIFF_RECURSIVE
;
242 string
++; /* eat unknown character */
246 static char *encode_params(game_params
*params
, int full
)
250 sprintf(str
, "%dx%d", params
->c
, params
->r
);
252 switch (params
->symm
) {
253 case SYMM_REF8
: strcat(str
, "m8"); break;
254 case SYMM_REF4
: strcat(str
, "m4"); break;
255 case SYMM_REF4D
: strcat(str
, "md4"); break;
256 case SYMM_REF2
: strcat(str
, "m2"); break;
257 case SYMM_REF2D
: strcat(str
, "md2"); break;
258 case SYMM_ROT4
: strcat(str
, "r4"); break;
259 /* case SYMM_ROT2: strcat(str, "r2"); break; [default] */
260 case SYMM_NONE
: strcat(str
, "a"); break;
262 switch (params
->diff
) {
263 /* case DIFF_BLOCK: strcat(str, "dt"); break; [default] */
264 case DIFF_SIMPLE
: strcat(str
, "db"); break;
265 case DIFF_INTERSECT
: strcat(str
, "di"); break;
266 case DIFF_SET
: strcat(str
, "da"); break;
267 case DIFF_RECURSIVE
: strcat(str
, "du"); break;
273 static config_item
*game_configure(game_params
*params
)
278 ret
= snewn(5, config_item
);
280 ret
[0].name
= "Columns of sub-blocks";
281 ret
[0].type
= C_STRING
;
282 sprintf(buf
, "%d", params
->c
);
283 ret
[0].sval
= dupstr(buf
);
286 ret
[1].name
= "Rows of sub-blocks";
287 ret
[1].type
= C_STRING
;
288 sprintf(buf
, "%d", params
->r
);
289 ret
[1].sval
= dupstr(buf
);
292 ret
[2].name
= "Symmetry";
293 ret
[2].type
= C_CHOICES
;
294 ret
[2].sval
= ":None:2-way rotation:4-way rotation:2-way mirror:"
295 "2-way diagonal mirror:4-way mirror:4-way diagonal mirror:"
297 ret
[2].ival
= params
->symm
;
299 ret
[3].name
= "Difficulty";
300 ret
[3].type
= C_CHOICES
;
301 ret
[3].sval
= ":Trivial:Basic:Intermediate:Advanced:Unreasonable";
302 ret
[3].ival
= params
->diff
;
312 static game_params
*custom_params(config_item
*cfg
)
314 game_params
*ret
= snew(game_params
);
316 ret
->c
= atoi(cfg
[0].sval
);
317 ret
->r
= atoi(cfg
[1].sval
);
318 ret
->symm
= cfg
[2].ival
;
319 ret
->diff
= cfg
[3].ival
;
324 static char *validate_params(game_params
*params
, int full
)
326 if (params
->c
< 2 || params
->r
< 2)
327 return "Both dimensions must be at least 2";
328 if (params
->c
> ORDER_MAX
|| params
->r
> ORDER_MAX
)
329 return "Dimensions greater than "STR(ORDER_MAX
)" are not supported";
333 /* ----------------------------------------------------------------------
336 * This solver is used for several purposes:
337 * + to generate filled grids as the basis for new puzzles (by
338 * supplying no clue squares at all)
339 * + to check solubility of a grid as we gradually remove numbers
341 * + to solve an externally generated puzzle when the user selects
344 * It supports a variety of specific modes of reasoning. By
345 * enabling or disabling subsets of these modes we can arrange a
346 * range of difficulty levels.
350 * Modes of reasoning currently supported:
352 * - Positional elimination: a number must go in a particular
353 * square because all the other empty squares in a given
354 * row/col/blk are ruled out.
356 * - Numeric elimination: a square must have a particular number
357 * in because all the other numbers that could go in it are
360 * - Intersectional analysis: given two domains which overlap
361 * (hence one must be a block, and the other can be a row or
362 * col), if the possible locations for a particular number in
363 * one of the domains can be narrowed down to the overlap, then
364 * that number can be ruled out everywhere but the overlap in
365 * the other domain too.
367 * - Set elimination: if there is a subset of the empty squares
368 * within a domain such that the union of the possible numbers
369 * in that subset has the same size as the subset itself, then
370 * those numbers can be ruled out everywhere else in the domain.
371 * (For example, if there are five empty squares and the
372 * possible numbers in each are 12, 23, 13, 134 and 1345, then
373 * the first three empty squares form such a subset: the numbers
374 * 1, 2 and 3 _must_ be in those three squares in some
375 * permutation, and hence we can deduce none of them can be in
376 * the fourth or fifth squares.)
377 * + You can also see this the other way round, concentrating
378 * on numbers rather than squares: if there is a subset of
379 * the unplaced numbers within a domain such that the union
380 * of all their possible positions has the same size as the
381 * subset itself, then all other numbers can be ruled out for
382 * those positions. However, it turns out that this is
383 * exactly equivalent to the first formulation at all times:
384 * there is a 1-1 correspondence between suitable subsets of
385 * the unplaced numbers and suitable subsets of the unfilled
386 * places, found by taking the _complement_ of the union of
387 * the numbers' possible positions (or the spaces' possible
390 * - Recursion. If all else fails, we pick one of the currently
391 * most constrained empty squares and take a random guess at its
392 * contents, then continue solving on that basis and see if we
397 * Within this solver, I'm going to transform all y-coordinates by
398 * inverting the significance of the block number and the position
399 * within the block. That is, we will start with the top row of
400 * each block in order, then the second row of each block in order,
403 * This transformation has the enormous advantage that it means
404 * every row, column _and_ block is described by an arithmetic
405 * progression of coordinates within the cubic array, so that I can
406 * use the same very simple function to do blockwise, row-wise and
407 * column-wise elimination.
409 #define YTRANS(y) (((y)%c)*r+(y)/c)
410 #define YUNTRANS(y) (((y)%r)*c+(y)/r)
412 struct solver_usage
{
415 * We set up a cubic array, indexed by x, y and digit; each
416 * element of this array is TRUE or FALSE according to whether
417 * or not that digit _could_ in principle go in that position.
419 * The way to index this array is cube[(x*cr+y)*cr+n-1].
420 * y-coordinates in here are transformed.
424 * This is the grid in which we write down our final
425 * deductions. y-coordinates in here are _not_ transformed.
429 * Now we keep track, at a slightly higher level, of what we
430 * have yet to work out, to prevent doing the same deduction
433 /* row[y*cr+n-1] TRUE if digit n has been placed in row y */
435 /* col[x*cr+n-1] TRUE if digit n has been placed in row x */
437 /* blk[(y*c+x)*cr+n-1] TRUE if digit n has been placed in block (x,y) */
440 #define cubepos(x,y,n) (((x)*usage->cr+(y))*usage->cr+(n)-1)
441 #define cube(x,y,n) (usage->cube[cubepos(x,y,n)])
444 * Function called when we are certain that a particular square has
445 * a particular number in it. The y-coordinate passed in here is
448 static void solver_place(struct solver_usage
*usage
, int x
, int y
, int n
)
450 int c
= usage
->c
, r
= usage
->r
, cr
= usage
->cr
;
456 * Rule out all other numbers in this square.
458 for (i
= 1; i
<= cr
; i
++)
463 * Rule out this number in all other positions in the row.
465 for (i
= 0; i
< cr
; i
++)
470 * Rule out this number in all other positions in the column.
472 for (i
= 0; i
< cr
; i
++)
477 * Rule out this number in all other positions in the block.
481 for (i
= 0; i
< r
; i
++)
482 for (j
= 0; j
< c
; j
++)
483 if (bx
+i
!= x
|| by
+j
*r
!= y
)
484 cube(bx
+i
,by
+j
*r
,n
) = FALSE
;
487 * Enter the number in the result grid.
489 usage
->grid
[YUNTRANS(y
)*cr
+x
] = n
;
492 * Cross out this number from the list of numbers left to place
493 * in its row, its column and its block.
495 usage
->row
[y
*cr
+n
-1] = usage
->col
[x
*cr
+n
-1] =
496 usage
->blk
[((y
%r
)*c
+(x
/r
))*cr
+n
-1] = TRUE
;
499 static int solver_elim(struct solver_usage
*usage
, int start
, int step
500 #ifdef STANDALONE_SOLVER
505 int c
= usage
->c
, r
= usage
->r
, cr
= c
*r
;
509 * Count the number of set bits within this section of the
514 for (i
= 0; i
< cr
; i
++)
515 if (usage
->cube
[start
+i
*step
]) {
529 if (!usage
->grid
[YUNTRANS(y
)*cr
+x
]) {
530 #ifdef STANDALONE_SOLVER
531 if (solver_show_working
) {
533 printf("%*s", solver_recurse_depth
*4, "");
537 printf(":\n%*s placing %d at (%d,%d)\n",
538 solver_recurse_depth
*4, "", n
, 1+x
, 1+YUNTRANS(y
));
541 solver_place(usage
, x
, y
, n
);
545 #ifdef STANDALONE_SOLVER
546 if (solver_show_working
) {
548 printf("%*s", solver_recurse_depth
*4, "");
552 printf(":\n%*s no possibilities available\n",
553 solver_recurse_depth
*4, "");
562 static int solver_intersect(struct solver_usage
*usage
,
563 int start1
, int step1
, int start2
, int step2
564 #ifdef STANDALONE_SOLVER
569 int c
= usage
->c
, r
= usage
->r
, cr
= c
*r
;
573 * Loop over the first domain and see if there's any set bit
574 * not also in the second.
576 for (i
= 0; i
< cr
; i
++) {
577 int p
= start1
+i
*step1
;
578 if (usage
->cube
[p
] &&
579 !(p
>= start2
&& p
< start2
+cr
*step2
&&
580 (p
- start2
) % step2
== 0))
581 return 0; /* there is, so we can't deduce */
585 * We have determined that all set bits in the first domain are
586 * within its overlap with the second. So loop over the second
587 * domain and remove all set bits that aren't also in that
588 * overlap; return +1 iff we actually _did_ anything.
591 for (i
= 0; i
< cr
; i
++) {
592 int p
= start2
+i
*step2
;
593 if (usage
->cube
[p
] &&
594 !(p
>= start1
&& p
< start1
+cr
*step1
&& (p
- start1
) % step1
== 0))
596 #ifdef STANDALONE_SOLVER
597 if (solver_show_working
) {
602 printf("%*s", solver_recurse_depth
*4, "");
614 printf("%*s ruling out %d at (%d,%d)\n",
615 solver_recurse_depth
*4, "", pn
, 1+px
, 1+YUNTRANS(py
));
618 ret
= +1; /* we did something */
626 struct solver_scratch
{
627 unsigned char *grid
, *rowidx
, *colidx
, *set
;
630 static int solver_set(struct solver_usage
*usage
,
631 struct solver_scratch
*scratch
,
632 int start
, int step1
, int step2
633 #ifdef STANDALONE_SOLVER
638 int c
= usage
->c
, r
= usage
->r
, cr
= c
*r
;
640 unsigned char *grid
= scratch
->grid
;
641 unsigned char *rowidx
= scratch
->rowidx
;
642 unsigned char *colidx
= scratch
->colidx
;
643 unsigned char *set
= scratch
->set
;
646 * We are passed a cr-by-cr matrix of booleans. Our first job
647 * is to winnow it by finding any definite placements - i.e.
648 * any row with a solitary 1 - and discarding that row and the
649 * column containing the 1.
651 memset(rowidx
, TRUE
, cr
);
652 memset(colidx
, TRUE
, cr
);
653 for (i
= 0; i
< cr
; i
++) {
654 int count
= 0, first
= -1;
655 for (j
= 0; j
< cr
; j
++)
656 if (usage
->cube
[start
+i
*step1
+j
*step2
])
660 * If count == 0, then there's a row with no 1s at all and
661 * the puzzle is internally inconsistent. However, we ought
662 * to have caught this already during the simpler reasoning
663 * methods, so we can safely fail an assertion if we reach
668 rowidx
[i
] = colidx
[first
] = FALSE
;
672 * Convert each of rowidx/colidx from a list of 0s and 1s to a
673 * list of the indices of the 1s.
675 for (i
= j
= 0; i
< cr
; i
++)
679 for (i
= j
= 0; i
< cr
; i
++)
685 * And create the smaller matrix.
687 for (i
= 0; i
< n
; i
++)
688 for (j
= 0; j
< n
; j
++)
689 grid
[i
*cr
+j
] = usage
->cube
[start
+rowidx
[i
]*step1
+colidx
[j
]*step2
];
692 * Having done that, we now have a matrix in which every row
693 * has at least two 1s in. Now we search to see if we can find
694 * a rectangle of zeroes (in the set-theoretic sense of
695 * `rectangle', i.e. a subset of rows crossed with a subset of
696 * columns) whose width and height add up to n.
703 * We have a candidate set. If its size is <=1 or >=n-1
704 * then we move on immediately.
706 if (count
> 1 && count
< n
-1) {
708 * The number of rows we need is n-count. See if we can
709 * find that many rows which each have a zero in all
710 * the positions listed in `set'.
713 for (i
= 0; i
< n
; i
++) {
715 for (j
= 0; j
< n
; j
++)
716 if (set
[j
] && grid
[i
*cr
+j
]) {
725 * We expect never to be able to get _more_ than
726 * n-count suitable rows: this would imply that (for
727 * example) there are four numbers which between them
728 * have at most three possible positions, and hence it
729 * indicates a faulty deduction before this point or
732 if (rows
> n
- count
) {
733 #ifdef STANDALONE_SOLVER
734 if (solver_show_working
) {
736 printf("%*s", solver_recurse_depth
*4,
741 printf(":\n%*s contradiction reached\n",
742 solver_recurse_depth
*4, "");
748 if (rows
>= n
- count
) {
749 int progress
= FALSE
;
752 * We've got one! Now, for each row which _doesn't_
753 * satisfy the criterion, eliminate all its set
754 * bits in the positions _not_ listed in `set'.
755 * Return +1 (meaning progress has been made) if we
756 * successfully eliminated anything at all.
758 * This involves referring back through
759 * rowidx/colidx in order to work out which actual
760 * positions in the cube to meddle with.
762 for (i
= 0; i
< n
; i
++) {
764 for (j
= 0; j
< n
; j
++)
765 if (set
[j
] && grid
[i
*cr
+j
]) {
770 for (j
= 0; j
< n
; j
++)
771 if (!set
[j
] && grid
[i
*cr
+j
]) {
772 int fpos
= (start
+rowidx
[i
]*step1
+
774 #ifdef STANDALONE_SOLVER
775 if (solver_show_working
) {
780 printf("%*s", solver_recurse_depth
*4,
793 printf("%*s ruling out %d at (%d,%d)\n",
794 solver_recurse_depth
*4, "",
795 pn
, 1+px
, 1+YUNTRANS(py
));
799 usage
->cube
[fpos
] = FALSE
;
811 * Binary increment: change the rightmost 0 to a 1, and
812 * change all 1s to the right of it to 0s.
815 while (i
> 0 && set
[i
-1])
816 set
[--i
] = 0, count
--;
818 set
[--i
] = 1, count
++;
826 static struct solver_scratch
*solver_new_scratch(struct solver_usage
*usage
)
828 struct solver_scratch
*scratch
= snew(struct solver_scratch
);
830 scratch
->grid
= snewn(cr
*cr
, unsigned char);
831 scratch
->rowidx
= snewn(cr
, unsigned char);
832 scratch
->colidx
= snewn(cr
, unsigned char);
833 scratch
->set
= snewn(cr
, unsigned char);
837 static void solver_free_scratch(struct solver_scratch
*scratch
)
840 sfree(scratch
->colidx
);
841 sfree(scratch
->rowidx
);
842 sfree(scratch
->grid
);
846 static int solver(int c
, int r
, digit
*grid
, int maxdiff
)
848 struct solver_usage
*usage
;
849 struct solver_scratch
*scratch
;
852 int diff
= DIFF_BLOCK
;
855 * Set up a usage structure as a clean slate (everything
858 usage
= snew(struct solver_usage
);
862 usage
->cube
= snewn(cr
*cr
*cr
, unsigned char);
863 usage
->grid
= grid
; /* write straight back to the input */
864 memset(usage
->cube
, TRUE
, cr
*cr
*cr
);
866 usage
->row
= snewn(cr
* cr
, unsigned char);
867 usage
->col
= snewn(cr
* cr
, unsigned char);
868 usage
->blk
= snewn(cr
* cr
, unsigned char);
869 memset(usage
->row
, FALSE
, cr
* cr
);
870 memset(usage
->col
, FALSE
, cr
* cr
);
871 memset(usage
->blk
, FALSE
, cr
* cr
);
873 scratch
= solver_new_scratch(usage
);
876 * Place all the clue numbers we are given.
878 for (x
= 0; x
< cr
; x
++)
879 for (y
= 0; y
< cr
; y
++)
881 solver_place(usage
, x
, YTRANS(y
), grid
[y
*cr
+x
]);
884 * Now loop over the grid repeatedly trying all permitted modes
885 * of reasoning. The loop terminates if we complete an
886 * iteration without making any progress; we then return
887 * failure or success depending on whether the grid is full or
892 * I'd like to write `continue;' inside each of the
893 * following loops, so that the solver returns here after
894 * making some progress. However, I can't specify that I
895 * want to continue an outer loop rather than the innermost
896 * one, so I'm apologetically resorting to a goto.
901 * Blockwise positional elimination.
903 for (x
= 0; x
< cr
; x
+= r
)
904 for (y
= 0; y
< r
; y
++)
905 for (n
= 1; n
<= cr
; n
++)
906 if (!usage
->blk
[(y
*c
+(x
/r
))*cr
+n
-1]) {
907 ret
= solver_elim(usage
, cubepos(x
,y
,n
), r
*cr
908 #ifdef STANDALONE_SOLVER
909 , "positional elimination,"
910 " %d in block (%d,%d)", n
, 1+x
/r
, 1+y
914 diff
= DIFF_IMPOSSIBLE
;
916 } else if (ret
> 0) {
917 diff
= max(diff
, DIFF_BLOCK
);
922 if (maxdiff
<= DIFF_BLOCK
)
926 * Row-wise positional elimination.
928 for (y
= 0; y
< cr
; y
++)
929 for (n
= 1; n
<= cr
; n
++)
930 if (!usage
->row
[y
*cr
+n
-1]) {
931 ret
= solver_elim(usage
, cubepos(0,y
,n
), cr
*cr
932 #ifdef STANDALONE_SOLVER
933 , "positional elimination,"
934 " %d in row %d", n
, 1+YUNTRANS(y
)
938 diff
= DIFF_IMPOSSIBLE
;
940 } else if (ret
> 0) {
941 diff
= max(diff
, DIFF_SIMPLE
);
946 * Column-wise positional elimination.
948 for (x
= 0; x
< cr
; x
++)
949 for (n
= 1; n
<= cr
; n
++)
950 if (!usage
->col
[x
*cr
+n
-1]) {
951 ret
= solver_elim(usage
, cubepos(x
,0,n
), cr
952 #ifdef STANDALONE_SOLVER
953 , "positional elimination,"
954 " %d in column %d", n
, 1+x
958 diff
= DIFF_IMPOSSIBLE
;
960 } else if (ret
> 0) {
961 diff
= max(diff
, DIFF_SIMPLE
);
967 * Numeric elimination.
969 for (x
= 0; x
< cr
; x
++)
970 for (y
= 0; y
< cr
; y
++)
971 if (!usage
->grid
[YUNTRANS(y
)*cr
+x
]) {
972 ret
= solver_elim(usage
, cubepos(x
,y
,1), 1
973 #ifdef STANDALONE_SOLVER
974 , "numeric elimination at (%d,%d)", 1+x
,
979 diff
= DIFF_IMPOSSIBLE
;
981 } else if (ret
> 0) {
982 diff
= max(diff
, DIFF_SIMPLE
);
987 if (maxdiff
<= DIFF_SIMPLE
)
991 * Intersectional analysis, rows vs blocks.
993 for (y
= 0; y
< cr
; y
++)
994 for (x
= 0; x
< cr
; x
+= r
)
995 for (n
= 1; n
<= cr
; n
++)
997 * solver_intersect() never returns -1.
999 if (!usage
->row
[y
*cr
+n
-1] &&
1000 !usage
->blk
[((y
%r
)*c
+(x
/r
))*cr
+n
-1] &&
1001 (solver_intersect(usage
, cubepos(0,y
,n
), cr
*cr
,
1002 cubepos(x
,y
%r
,n
), r
*cr
1003 #ifdef STANDALONE_SOLVER
1004 , "intersectional analysis,"
1005 " %d in row %d vs block (%d,%d)",
1006 n
, 1+YUNTRANS(y
), 1+x
/r
, 1+y
%r
1009 solver_intersect(usage
, cubepos(x
,y
%r
,n
), r
*cr
,
1010 cubepos(0,y
,n
), cr
*cr
1011 #ifdef STANDALONE_SOLVER
1012 , "intersectional analysis,"
1013 " %d in block (%d,%d) vs row %d",
1014 n
, 1+x
/r
, 1+y
%r
, 1+YUNTRANS(y
)
1017 diff
= max(diff
, DIFF_INTERSECT
);
1022 * Intersectional analysis, columns vs blocks.
1024 for (x
= 0; x
< cr
; x
++)
1025 for (y
= 0; y
< r
; y
++)
1026 for (n
= 1; n
<= cr
; n
++)
1027 if (!usage
->col
[x
*cr
+n
-1] &&
1028 !usage
->blk
[(y
*c
+(x
/r
))*cr
+n
-1] &&
1029 (solver_intersect(usage
, cubepos(x
,0,n
), cr
,
1030 cubepos((x
/r
)*r
,y
,n
), r
*cr
1031 #ifdef STANDALONE_SOLVER
1032 , "intersectional analysis,"
1033 " %d in column %d vs block (%d,%d)",
1037 solver_intersect(usage
, cubepos((x
/r
)*r
,y
,n
), r
*cr
,
1039 #ifdef STANDALONE_SOLVER
1040 , "intersectional analysis,"
1041 " %d in block (%d,%d) vs column %d",
1045 diff
= max(diff
, DIFF_INTERSECT
);
1049 if (maxdiff
<= DIFF_INTERSECT
)
1053 * Blockwise set elimination.
1055 for (x
= 0; x
< cr
; x
+= r
)
1056 for (y
= 0; y
< r
; y
++) {
1057 ret
= solver_set(usage
, scratch
, cubepos(x
,y
,1), r
*cr
, 1
1058 #ifdef STANDALONE_SOLVER
1059 , "set elimination, block (%d,%d)", 1+x
/r
, 1+y
1063 diff
= DIFF_IMPOSSIBLE
;
1065 } else if (ret
> 0) {
1066 diff
= max(diff
, DIFF_SET
);
1072 * Row-wise set elimination.
1074 for (y
= 0; y
< cr
; y
++) {
1075 ret
= solver_set(usage
, scratch
, cubepos(0,y
,1), cr
*cr
, 1
1076 #ifdef STANDALONE_SOLVER
1077 , "set elimination, row %d", 1+YUNTRANS(y
)
1081 diff
= DIFF_IMPOSSIBLE
;
1083 } else if (ret
> 0) {
1084 diff
= max(diff
, DIFF_SET
);
1090 * Column-wise set elimination.
1092 for (x
= 0; x
< cr
; x
++) {
1093 ret
= solver_set(usage
, scratch
, cubepos(x
,0,1), cr
, 1
1094 #ifdef STANDALONE_SOLVER
1095 , "set elimination, column %d", 1+x
1099 diff
= DIFF_IMPOSSIBLE
;
1101 } else if (ret
> 0) {
1102 diff
= max(diff
, DIFF_SET
);
1108 * If we reach here, we have made no deductions in this
1109 * iteration, so the algorithm terminates.
1115 * Last chance: if we haven't fully solved the puzzle yet, try
1116 * recursing based on guesses for a particular square. We pick
1117 * one of the most constrained empty squares we can find, which
1118 * has the effect of pruning the search tree as much as
1121 if (maxdiff
>= DIFF_RECURSIVE
) {
1122 int best
, bestcount
;
1127 for (y
= 0; y
< cr
; y
++)
1128 for (x
= 0; x
< cr
; x
++)
1129 if (!grid
[y
*cr
+x
]) {
1133 * An unfilled square. Count the number of
1134 * possible digits in it.
1137 for (n
= 1; n
<= cr
; n
++)
1138 if (cube(x
,YTRANS(y
),n
))
1142 * We should have found any impossibilities
1143 * already, so this can safely be an assert.
1147 if (count
< bestcount
) {
1155 digit
*list
, *ingrid
, *outgrid
;
1157 diff
= DIFF_IMPOSSIBLE
; /* no solution found yet */
1160 * Attempt recursion.
1165 list
= snewn(cr
, digit
);
1166 ingrid
= snewn(cr
* cr
, digit
);
1167 outgrid
= snewn(cr
* cr
, digit
);
1168 memcpy(ingrid
, grid
, cr
* cr
);
1170 /* Make a list of the possible digits. */
1171 for (j
= 0, n
= 1; n
<= cr
; n
++)
1172 if (cube(x
,YTRANS(y
),n
))
1175 #ifdef STANDALONE_SOLVER
1176 if (solver_show_working
) {
1178 printf("%*srecursing on (%d,%d) [",
1179 solver_recurse_depth
*4, "", x
, y
);
1180 for (i
= 0; i
< j
; i
++) {
1181 printf("%s%d", sep
, list
[i
]);
1189 * And step along the list, recursing back into the
1190 * main solver at every stage.
1192 for (i
= 0; i
< j
; i
++) {
1195 memcpy(outgrid
, ingrid
, cr
* cr
);
1196 outgrid
[y
*cr
+x
] = list
[i
];
1198 #ifdef STANDALONE_SOLVER
1199 if (solver_show_working
)
1200 printf("%*sguessing %d at (%d,%d)\n",
1201 solver_recurse_depth
*4, "", list
[i
], x
, y
);
1202 solver_recurse_depth
++;
1205 ret
= solver(c
, r
, outgrid
, maxdiff
);
1207 #ifdef STANDALONE_SOLVER
1208 solver_recurse_depth
--;
1209 if (solver_show_working
) {
1210 printf("%*sretracting %d at (%d,%d)\n",
1211 solver_recurse_depth
*4, "", list
[i
], x
, y
);
1216 * If we have our first solution, copy it into the
1217 * grid we will return.
1219 if (diff
== DIFF_IMPOSSIBLE
&& ret
!= DIFF_IMPOSSIBLE
)
1220 memcpy(grid
, outgrid
, cr
*cr
);
1222 if (ret
== DIFF_AMBIGUOUS
)
1223 diff
= DIFF_AMBIGUOUS
;
1224 else if (ret
== DIFF_IMPOSSIBLE
)
1225 /* do not change our return value */;
1227 /* the recursion turned up exactly one solution */
1228 if (diff
== DIFF_IMPOSSIBLE
)
1229 diff
= DIFF_RECURSIVE
;
1231 diff
= DIFF_AMBIGUOUS
;
1235 * As soon as we've found more than one solution,
1236 * give up immediately.
1238 if (diff
== DIFF_AMBIGUOUS
)
1249 * We're forbidden to use recursion, so we just see whether
1250 * our grid is fully solved, and return DIFF_IMPOSSIBLE
1253 for (y
= 0; y
< cr
; y
++)
1254 for (x
= 0; x
< cr
; x
++)
1256 diff
= DIFF_IMPOSSIBLE
;
1261 #ifdef STANDALONE_SOLVER
1262 if (solver_show_working
)
1263 printf("%*s%s found\n",
1264 solver_recurse_depth
*4, "",
1265 diff
== DIFF_IMPOSSIBLE ?
"no solution" :
1266 diff
== DIFF_AMBIGUOUS ?
"multiple solutions" :
1276 solver_free_scratch(scratch
);
1281 /* ----------------------------------------------------------------------
1282 * End of solver code.
1285 /* ----------------------------------------------------------------------
1286 * Solo filled-grid generator.
1288 * This grid generator works by essentially trying to solve a grid
1289 * starting from no clues, and not worrying that there's more than
1290 * one possible solution. Unfortunately, it isn't computationally
1291 * feasible to do this by calling the above solver with an empty
1292 * grid, because that one needs to allocate a lot of scratch space
1293 * at every recursion level. Instead, I have a much simpler
1294 * algorithm which I shamelessly copied from a Python solver
1295 * written by Andrew Wilkinson (which is GPLed, but I've reused
1296 * only ideas and no code). It mostly just does the obvious
1297 * recursive thing: pick an empty square, put one of the possible
1298 * digits in it, recurse until all squares are filled, backtrack
1299 * and change some choices if necessary.
1301 * The clever bit is that every time it chooses which square to
1302 * fill in next, it does so by counting the number of _possible_
1303 * numbers that can go in each square, and it prioritises so that
1304 * it picks a square with the _lowest_ number of possibilities. The
1305 * idea is that filling in lots of the obvious bits (particularly
1306 * any squares with only one possibility) will cut down on the list
1307 * of possibilities for other squares and hence reduce the enormous
1308 * search space as much as possible as early as possible.
1312 * Internal data structure used in gridgen to keep track of
1315 struct gridgen_coord
{ int x
, y
, r
; };
1316 struct gridgen_usage
{
1317 int c
, r
, cr
; /* cr == c*r */
1318 /* grid is a copy of the input grid, modified as we go along */
1320 /* row[y*cr+n-1] TRUE if digit n has been placed in row y */
1322 /* col[x*cr+n-1] TRUE if digit n has been placed in row x */
1324 /* blk[(y*c+x)*cr+n-1] TRUE if digit n has been placed in block (x,y) */
1326 /* This lists all the empty spaces remaining in the grid. */
1327 struct gridgen_coord
*spaces
;
1329 /* If we need randomisation in the solve, this is our random state. */
1334 * The real recursive step in the generating function.
1336 static int gridgen_real(struct gridgen_usage
*usage
, digit
*grid
)
1338 int c
= usage
->c
, r
= usage
->r
, cr
= usage
->cr
;
1339 int i
, j
, n
, sx
, sy
, bestm
, bestr
, ret
;
1343 * Firstly, check for completion! If there are no spaces left
1344 * in the grid, we have a solution.
1346 if (usage
->nspaces
== 0) {
1347 memcpy(grid
, usage
->grid
, cr
* cr
);
1352 * Otherwise, there must be at least one space. Find the most
1353 * constrained space, using the `r' field as a tie-breaker.
1355 bestm
= cr
+1; /* so that any space will beat it */
1358 for (j
= 0; j
< usage
->nspaces
; j
++) {
1359 int x
= usage
->spaces
[j
].x
, y
= usage
->spaces
[j
].y
;
1363 * Find the number of digits that could go in this space.
1366 for (n
= 0; n
< cr
; n
++)
1367 if (!usage
->row
[y
*cr
+n
] && !usage
->col
[x
*cr
+n
] &&
1368 !usage
->blk
[((y
/c
)*c
+(x
/r
))*cr
+n
])
1371 if (m
< bestm
|| (m
== bestm
&& usage
->spaces
[j
].r
< bestr
)) {
1373 bestr
= usage
->spaces
[j
].r
;
1381 * Swap that square into the final place in the spaces array,
1382 * so that decrementing nspaces will remove it from the list.
1384 if (i
!= usage
->nspaces
-1) {
1385 struct gridgen_coord t
;
1386 t
= usage
->spaces
[usage
->nspaces
-1];
1387 usage
->spaces
[usage
->nspaces
-1] = usage
->spaces
[i
];
1388 usage
->spaces
[i
] = t
;
1392 * Now we've decided which square to start our recursion at,
1393 * simply go through all possible values, shuffling them
1394 * randomly first if necessary.
1396 digits
= snewn(bestm
, int);
1398 for (n
= 0; n
< cr
; n
++)
1399 if (!usage
->row
[sy
*cr
+n
] && !usage
->col
[sx
*cr
+n
] &&
1400 !usage
->blk
[((sy
/c
)*c
+(sx
/r
))*cr
+n
]) {
1405 shuffle(digits
, j
, sizeof(*digits
), usage
->rs
);
1407 /* And finally, go through the digit list and actually recurse. */
1409 for (i
= 0; i
< j
; i
++) {
1412 /* Update the usage structure to reflect the placing of this digit. */
1413 usage
->row
[sy
*cr
+n
-1] = usage
->col
[sx
*cr
+n
-1] =
1414 usage
->blk
[((sy
/c
)*c
+(sx
/r
))*cr
+n
-1] = TRUE
;
1415 usage
->grid
[sy
*cr
+sx
] = n
;
1418 /* Call the solver recursively. Stop when we find a solution. */
1419 if (gridgen_real(usage
, grid
))
1422 /* Revert the usage structure. */
1423 usage
->row
[sy
*cr
+n
-1] = usage
->col
[sx
*cr
+n
-1] =
1424 usage
->blk
[((sy
/c
)*c
+(sx
/r
))*cr
+n
-1] = FALSE
;
1425 usage
->grid
[sy
*cr
+sx
] = 0;
1437 * Entry point to generator. You give it dimensions and a starting
1438 * grid, which is simply an array of cr*cr digits.
1440 static void gridgen(int c
, int r
, digit
*grid
, random_state
*rs
)
1442 struct gridgen_usage
*usage
;
1446 * Clear the grid to start with.
1448 memset(grid
, 0, cr
*cr
);
1451 * Create a gridgen_usage structure.
1453 usage
= snew(struct gridgen_usage
);
1459 usage
->grid
= snewn(cr
* cr
, digit
);
1460 memcpy(usage
->grid
, grid
, cr
* cr
);
1462 usage
->row
= snewn(cr
* cr
, unsigned char);
1463 usage
->col
= snewn(cr
* cr
, unsigned char);
1464 usage
->blk
= snewn(cr
* cr
, unsigned char);
1465 memset(usage
->row
, FALSE
, cr
* cr
);
1466 memset(usage
->col
, FALSE
, cr
* cr
);
1467 memset(usage
->blk
, FALSE
, cr
* cr
);
1469 usage
->spaces
= snewn(cr
* cr
, struct gridgen_coord
);
1475 * Initialise the list of grid spaces.
1477 for (y
= 0; y
< cr
; y
++) {
1478 for (x
= 0; x
< cr
; x
++) {
1479 usage
->spaces
[usage
->nspaces
].x
= x
;
1480 usage
->spaces
[usage
->nspaces
].y
= y
;
1481 usage
->spaces
[usage
->nspaces
].r
= random_bits(rs
, 31);
1487 * Run the real generator function.
1489 gridgen_real(usage
, grid
);
1492 * Clean up the usage structure now we have our answer.
1494 sfree(usage
->spaces
);
1502 /* ----------------------------------------------------------------------
1503 * End of grid generator code.
1507 * Check whether a grid contains a valid complete puzzle.
1509 static int check_valid(int c
, int r
, digit
*grid
)
1512 unsigned char *used
;
1515 used
= snewn(cr
, unsigned char);
1518 * Check that each row contains precisely one of everything.
1520 for (y
= 0; y
< cr
; y
++) {
1521 memset(used
, FALSE
, cr
);
1522 for (x
= 0; x
< cr
; x
++)
1523 if (grid
[y
*cr
+x
] > 0 && grid
[y
*cr
+x
] <= cr
)
1524 used
[grid
[y
*cr
+x
]-1] = TRUE
;
1525 for (n
= 0; n
< cr
; n
++)
1533 * Check that each column contains precisely one of everything.
1535 for (x
= 0; x
< cr
; x
++) {
1536 memset(used
, FALSE
, cr
);
1537 for (y
= 0; y
< cr
; y
++)
1538 if (grid
[y
*cr
+x
] > 0 && grid
[y
*cr
+x
] <= cr
)
1539 used
[grid
[y
*cr
+x
]-1] = TRUE
;
1540 for (n
= 0; n
< cr
; n
++)
1548 * Check that each block contains precisely one of everything.
1550 for (x
= 0; x
< cr
; x
+= r
) {
1551 for (y
= 0; y
< cr
; y
+= c
) {
1553 memset(used
, FALSE
, cr
);
1554 for (xx
= x
; xx
< x
+r
; xx
++)
1555 for (yy
= 0; yy
< y
+c
; yy
++)
1556 if (grid
[yy
*cr
+xx
] > 0 && grid
[yy
*cr
+xx
] <= cr
)
1557 used
[grid
[yy
*cr
+xx
]-1] = TRUE
;
1558 for (n
= 0; n
< cr
; n
++)
1570 static int symmetries(game_params
*params
, int x
, int y
, int *output
, int s
)
1572 int c
= params
->c
, r
= params
->r
, cr
= c
*r
;
1575 #define ADD(x,y) (*output++ = (x), *output++ = (y), i++)
1581 break; /* just x,y is all we need */
1583 ADD(cr
- 1 - x
, cr
- 1 - y
);
1588 ADD(cr
- 1 - x
, cr
- 1 - y
);
1599 ADD(cr
- 1 - x
, cr
- 1 - y
);
1603 ADD(cr
- 1 - x
, cr
- 1 - y
);
1604 ADD(cr
- 1 - y
, cr
- 1 - x
);
1609 ADD(cr
- 1 - x
, cr
- 1 - y
);
1613 ADD(cr
- 1 - y
, cr
- 1 - x
);
1622 static char *encode_solve_move(int cr
, digit
*grid
)
1625 char *ret
, *p
, *sep
;
1628 * It's surprisingly easy to work out _exactly_ how long this
1629 * string needs to be. To decimal-encode all the numbers from 1
1632 * - every number has a units digit; total is n.
1633 * - all numbers above 9 have a tens digit; total is max(n-9,0).
1634 * - all numbers above 99 have a hundreds digit; total is max(n-99,0).
1638 for (i
= 1; i
<= cr
; i
*= 10)
1639 len
+= max(cr
- i
+ 1, 0);
1640 len
+= cr
; /* don't forget the commas */
1641 len
*= cr
; /* there are cr rows of these */
1644 * Now len is one bigger than the total size of the
1645 * comma-separated numbers (because we counted an
1646 * additional leading comma). We need to have a leading S
1647 * and a trailing NUL, so we're off by one in total.
1651 ret
= snewn(len
, char);
1655 for (i
= 0; i
< cr
*cr
; i
++) {
1656 p
+= sprintf(p
, "%s%d", sep
, grid
[i
]);
1660 assert(p
- ret
== len
);
1665 static char *new_game_desc(game_params
*params
, random_state
*rs
,
1666 char **aux
, int interactive
)
1668 int c
= params
->c
, r
= params
->r
, cr
= c
*r
;
1670 digit
*grid
, *grid2
;
1671 struct xy
{ int x
, y
; } *locs
;
1674 int coords
[16], ncoords
;
1675 int *symmclasses
, nsymmclasses
;
1676 int maxdiff
, recursing
;
1679 * Adjust the maximum difficulty level to be consistent with
1680 * the puzzle size: all 2x2 puzzles appear to be Trivial
1681 * (DIFF_BLOCK) so we cannot hold out for even a Basic
1682 * (DIFF_SIMPLE) one.
1684 maxdiff
= params
->diff
;
1685 if (c
== 2 && r
== 2)
1686 maxdiff
= DIFF_BLOCK
;
1688 grid
= snewn(area
, digit
);
1689 locs
= snewn(area
, struct xy
);
1690 grid2
= snewn(area
, digit
);
1693 * Find the set of equivalence classes of squares permitted
1694 * by the selected symmetry. We do this by enumerating all
1695 * the grid squares which have no symmetric companion
1696 * sorting lower than themselves.
1699 symmclasses
= snewn(cr
* cr
, int);
1703 for (y
= 0; y
< cr
; y
++)
1704 for (x
= 0; x
< cr
; x
++) {
1708 ncoords
= symmetries(params
, x
, y
, coords
, params
->symm
);
1709 for (j
= 0; j
< ncoords
; j
++)
1710 if (coords
[2*j
+1]*cr
+coords
[2*j
] < i
)
1713 symmclasses
[nsymmclasses
++] = i
;
1718 * Loop until we get a grid of the required difficulty. This is
1719 * nasty, but it seems to be unpleasantly hard to generate
1720 * difficult grids otherwise.
1724 * Generate a random solved state.
1726 gridgen(c
, r
, grid
, rs
);
1727 assert(check_valid(c
, r
, grid
));
1730 * Save the solved grid in aux.
1734 * We might already have written *aux the last time we
1735 * went round this loop, in which case we should free
1736 * the old aux before overwriting it with the new one.
1742 *aux
= encode_solve_move(cr
, grid
);
1746 * Now we have a solved grid, start removing things from it
1747 * while preserving solubility.
1754 * Iterate over the grid and enumerate all the filled
1755 * squares we could empty.
1759 for (i
= 0; i
< nsymmclasses
; i
++) {
1760 x
= symmclasses
[i
] % cr
;
1761 y
= symmclasses
[i
] / cr
;
1770 * Now shuffle that list.
1772 shuffle(locs
, nlocs
, sizeof(*locs
), rs
);
1775 * Now loop over the shuffled list and, for each element,
1776 * see whether removing that element (and its reflections)
1777 * from the grid will still leave the grid soluble by
1780 for (i
= 0; i
< nlocs
; i
++) {
1786 memcpy(grid2
, grid
, area
);
1787 ncoords
= symmetries(params
, x
, y
, coords
, params
->symm
);
1788 for (j
= 0; j
< ncoords
; j
++)
1789 grid2
[coords
[2*j
+1]*cr
+coords
[2*j
]] = 0;
1791 ret
= solver(c
, r
, grid2
, maxdiff
);
1792 if (ret
!= DIFF_IMPOSSIBLE
&& ret
!= DIFF_AMBIGUOUS
) {
1793 for (j
= 0; j
< ncoords
; j
++)
1794 grid
[coords
[2*j
+1]*cr
+coords
[2*j
]] = 0;
1801 * There was nothing we could remove without
1802 * destroying solvability. Give up.
1808 memcpy(grid2
, grid
, area
);
1809 } while (solver(c
, r
, grid2
, maxdiff
) < maxdiff
);
1817 * Now we have the grid as it will be presented to the user.
1818 * Encode it in a game desc.
1824 desc
= snewn(5 * area
, char);
1827 for (i
= 0; i
<= area
; i
++) {
1828 int n
= (i
< area ? grid
[i
] : -1);
1835 int c
= 'a' - 1 + run
;
1839 run
-= c
- ('a' - 1);
1843 * If there's a number in the very top left or
1844 * bottom right, there's no point putting an
1845 * unnecessary _ before or after it.
1847 if (p
> desc
&& n
> 0)
1851 p
+= sprintf(p
, "%d", n
);
1855 assert(p
- desc
< 5 * area
);
1857 desc
= sresize(desc
, p
- desc
, char);
1865 static char *validate_desc(game_params
*params
, char *desc
)
1867 int area
= params
->r
* params
->r
* params
->c
* params
->c
;
1872 if (n
>= 'a' && n
<= 'z') {
1873 squares
+= n
- 'a' + 1;
1874 } else if (n
== '_') {
1876 } else if (n
> '0' && n
<= '9') {
1878 while (*desc
>= '0' && *desc
<= '9')
1881 return "Invalid character in game description";
1885 return "Not enough data to fill grid";
1888 return "Too much data to fit in grid";
1893 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
1895 game_state
*state
= snew(game_state
);
1896 int c
= params
->c
, r
= params
->r
, cr
= c
*r
, area
= cr
* cr
;
1899 state
->c
= params
->c
;
1900 state
->r
= params
->r
;
1902 state
->grid
= snewn(area
, digit
);
1903 state
->pencil
= snewn(area
* cr
, unsigned char);
1904 memset(state
->pencil
, 0, area
* cr
);
1905 state
->immutable
= snewn(area
, unsigned char);
1906 memset(state
->immutable
, FALSE
, area
);
1908 state
->completed
= state
->cheated
= FALSE
;
1913 if (n
>= 'a' && n
<= 'z') {
1914 int run
= n
- 'a' + 1;
1915 assert(i
+ run
<= area
);
1917 state
->grid
[i
++] = 0;
1918 } else if (n
== '_') {
1920 } else if (n
> '0' && n
<= '9') {
1922 state
->immutable
[i
] = TRUE
;
1923 state
->grid
[i
++] = atoi(desc
-1);
1924 while (*desc
>= '0' && *desc
<= '9')
1927 assert(!"We can't get here");
1935 static game_state
*dup_game(game_state
*state
)
1937 game_state
*ret
= snew(game_state
);
1938 int c
= state
->c
, r
= state
->r
, cr
= c
*r
, area
= cr
* cr
;
1943 ret
->grid
= snewn(area
, digit
);
1944 memcpy(ret
->grid
, state
->grid
, area
);
1946 ret
->pencil
= snewn(area
* cr
, unsigned char);
1947 memcpy(ret
->pencil
, state
->pencil
, area
* cr
);
1949 ret
->immutable
= snewn(area
, unsigned char);
1950 memcpy(ret
->immutable
, state
->immutable
, area
);
1952 ret
->completed
= state
->completed
;
1953 ret
->cheated
= state
->cheated
;
1958 static void free_game(game_state
*state
)
1960 sfree(state
->immutable
);
1961 sfree(state
->pencil
);
1966 static char *solve_game(game_state
*state
, game_state
*currstate
,
1967 char *ai
, char **error
)
1969 int c
= state
->c
, r
= state
->r
, cr
= c
*r
;
1975 * If we already have the solution in ai, save ourselves some
1981 grid
= snewn(cr
*cr
, digit
);
1982 memcpy(grid
, state
->grid
, cr
*cr
);
1983 solve_ret
= solver(c
, r
, grid
, DIFF_RECURSIVE
);
1987 if (solve_ret
== DIFF_IMPOSSIBLE
)
1988 *error
= "No solution exists for this puzzle";
1989 else if (solve_ret
== DIFF_AMBIGUOUS
)
1990 *error
= "Multiple solutions exist for this puzzle";
1997 ret
= encode_solve_move(cr
, grid
);
2004 static char *grid_text_format(int c
, int r
, digit
*grid
)
2012 * There are cr lines of digits, plus r-1 lines of block
2013 * separators. Each line contains cr digits, cr-1 separating
2014 * spaces, and c-1 two-character block separators. Thus, the
2015 * total length of a line is 2*cr+2*c-3 (not counting the
2016 * newline), and there are cr+r-1 of them.
2018 maxlen
= (cr
+r
-1) * (2*cr
+2*c
-2);
2019 ret
= snewn(maxlen
+1, char);
2022 for (y
= 0; y
< cr
; y
++) {
2023 for (x
= 0; x
< cr
; x
++) {
2024 int ch
= grid
[y
* cr
+ x
];
2034 if ((x
+1) % r
== 0) {
2041 if (y
+1 < cr
&& (y
+1) % c
== 0) {
2042 for (x
= 0; x
< cr
; x
++) {
2046 if ((x
+1) % r
== 0) {
2056 assert(p
- ret
== maxlen
);
2061 static char *game_text_format(game_state
*state
)
2063 return grid_text_format(state
->c
, state
->r
, state
->grid
);
2068 * These are the coordinates of the currently highlighted
2069 * square on the grid, or -1,-1 if there isn't one. When there
2070 * is, pressing a valid number or letter key or Space will
2071 * enter that number or letter in the grid.
2075 * This indicates whether the current highlight is a
2076 * pencil-mark one or a real one.
2081 static game_ui
*new_ui(game_state
*state
)
2083 game_ui
*ui
= snew(game_ui
);
2085 ui
->hx
= ui
->hy
= -1;
2091 static void free_ui(game_ui
*ui
)
2096 static char *encode_ui(game_ui
*ui
)
2101 static void decode_ui(game_ui
*ui
, char *encoding
)
2105 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2106 game_state
*newstate
)
2108 int c
= newstate
->c
, r
= newstate
->r
, cr
= c
*r
;
2110 * We prevent pencil-mode highlighting of a filled square. So
2111 * if the user has just filled in a square which we had a
2112 * pencil-mode highlight in (by Undo, or by Redo, or by Solve),
2113 * then we cancel the highlight.
2115 if (ui
->hx
>= 0 && ui
->hy
>= 0 && ui
->hpencil
&&
2116 newstate
->grid
[ui
->hy
* cr
+ ui
->hx
] != 0) {
2117 ui
->hx
= ui
->hy
= -1;
2121 struct game_drawstate
{
2126 unsigned char *pencil
;
2128 /* This is scratch space used within a single call to game_redraw. */
2132 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2133 int x
, int y
, int button
)
2135 int c
= state
->c
, r
= state
->r
, cr
= c
*r
;
2139 button
&= ~MOD_MASK
;
2141 tx
= (x
+ TILE_SIZE
- BORDER
) / TILE_SIZE
- 1;
2142 ty
= (y
+ TILE_SIZE
- BORDER
) / TILE_SIZE
- 1;
2144 if (tx
>= 0 && tx
< cr
&& ty
>= 0 && ty
< cr
) {
2145 if (button
== LEFT_BUTTON
) {
2146 if (state
->immutable
[ty
*cr
+tx
]) {
2147 ui
->hx
= ui
->hy
= -1;
2148 } else if (tx
== ui
->hx
&& ty
== ui
->hy
&& ui
->hpencil
== 0) {
2149 ui
->hx
= ui
->hy
= -1;
2155 return ""; /* UI activity occurred */
2157 if (button
== RIGHT_BUTTON
) {
2159 * Pencil-mode highlighting for non filled squares.
2161 if (state
->grid
[ty
*cr
+tx
] == 0) {
2162 if (tx
== ui
->hx
&& ty
== ui
->hy
&& ui
->hpencil
) {
2163 ui
->hx
= ui
->hy
= -1;
2170 ui
->hx
= ui
->hy
= -1;
2172 return ""; /* UI activity occurred */
2176 if (ui
->hx
!= -1 && ui
->hy
!= -1 &&
2177 ((button
>= '1' && button
<= '9' && button
- '0' <= cr
) ||
2178 (button
>= 'a' && button
<= 'z' && button
- 'a' + 10 <= cr
) ||
2179 (button
>= 'A' && button
<= 'Z' && button
- 'A' + 10 <= cr
) ||
2181 int n
= button
- '0';
2182 if (button
>= 'A' && button
<= 'Z')
2183 n
= button
- 'A' + 10;
2184 if (button
>= 'a' && button
<= 'z')
2185 n
= button
- 'a' + 10;
2190 * Can't overwrite this square. In principle this shouldn't
2191 * happen anyway because we should never have even been
2192 * able to highlight the square, but it never hurts to be
2195 if (state
->immutable
[ui
->hy
*cr
+ui
->hx
])
2199 * Can't make pencil marks in a filled square. In principle
2200 * this shouldn't happen anyway because we should never
2201 * have even been able to pencil-highlight the square, but
2202 * it never hurts to be careful.
2204 if (ui
->hpencil
&& state
->grid
[ui
->hy
*cr
+ui
->hx
])
2207 sprintf(buf
, "%c%d,%d,%d",
2208 (char)(ui
->hpencil
&& n
> 0 ?
'P' : 'R'), ui
->hx
, ui
->hy
, n
);
2210 ui
->hx
= ui
->hy
= -1;
2218 static game_state
*execute_move(game_state
*from
, char *move
)
2220 int c
= from
->c
, r
= from
->r
, cr
= c
*r
;
2224 if (move
[0] == 'S') {
2227 ret
= dup_game(from
);
2228 ret
->completed
= ret
->cheated
= TRUE
;
2231 for (n
= 0; n
< cr
*cr
; n
++) {
2232 ret
->grid
[n
] = atoi(p
);
2234 if (!*p
|| ret
->grid
[n
] < 1 || ret
->grid
[n
] > cr
) {
2239 while (*p
&& isdigit((unsigned char)*p
)) p
++;
2244 } else if ((move
[0] == 'P' || move
[0] == 'R') &&
2245 sscanf(move
+1, "%d,%d,%d", &x
, &y
, &n
) == 3 &&
2246 x
>= 0 && x
< cr
&& y
>= 0 && y
< cr
&& n
>= 0 && n
<= cr
) {
2248 ret
= dup_game(from
);
2249 if (move
[0] == 'P' && n
> 0) {
2250 int index
= (y
*cr
+x
) * cr
+ (n
-1);
2251 ret
->pencil
[index
] = !ret
->pencil
[index
];
2253 ret
->grid
[y
*cr
+x
] = n
;
2254 memset(ret
->pencil
+ (y
*cr
+x
)*cr
, 0, cr
);
2257 * We've made a real change to the grid. Check to see
2258 * if the game has been completed.
2260 if (!ret
->completed
&& check_valid(c
, r
, ret
->grid
)) {
2261 ret
->completed
= TRUE
;
2266 return NULL
; /* couldn't parse move string */
2269 /* ----------------------------------------------------------------------
2273 #define SIZE(cr) ((cr) * TILE_SIZE + 2*BORDER + 1)
2274 #define GETTILESIZE(cr, w) ( (double)(w-1) / (double)(cr+1) )
2276 static void game_compute_size(game_params
*params
, int tilesize
,
2279 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2280 struct { int tilesize
; } ads
, *ds
= &ads
;
2281 ads
.tilesize
= tilesize
;
2283 *x
= SIZE(params
->c
* params
->r
);
2284 *y
= SIZE(params
->c
* params
->r
);
2287 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
2290 ds
->tilesize
= tilesize
;
2293 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2295 float *ret
= snewn(3 * NCOLOURS
, float);
2297 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2299 ret
[COL_GRID
* 3 + 0] = 0.0F
;
2300 ret
[COL_GRID
* 3 + 1] = 0.0F
;
2301 ret
[COL_GRID
* 3 + 2] = 0.0F
;
2303 ret
[COL_CLUE
* 3 + 0] = 0.0F
;
2304 ret
[COL_CLUE
* 3 + 1] = 0.0F
;
2305 ret
[COL_CLUE
* 3 + 2] = 0.0F
;
2307 ret
[COL_USER
* 3 + 0] = 0.0F
;
2308 ret
[COL_USER
* 3 + 1] = 0.6F
* ret
[COL_BACKGROUND
* 3 + 1];
2309 ret
[COL_USER
* 3 + 2] = 0.0F
;
2311 ret
[COL_HIGHLIGHT
* 3 + 0] = 0.85F
* ret
[COL_BACKGROUND
* 3 + 0];
2312 ret
[COL_HIGHLIGHT
* 3 + 1] = 0.85F
* ret
[COL_BACKGROUND
* 3 + 1];
2313 ret
[COL_HIGHLIGHT
* 3 + 2] = 0.85F
* ret
[COL_BACKGROUND
* 3 + 2];
2315 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
2316 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
2317 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
2319 ret
[COL_PENCIL
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
2320 ret
[COL_PENCIL
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
2321 ret
[COL_PENCIL
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
2323 *ncolours
= NCOLOURS
;
2327 static game_drawstate
*game_new_drawstate(game_state
*state
)
2329 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2330 int c
= state
->c
, r
= state
->r
, cr
= c
*r
;
2332 ds
->started
= FALSE
;
2336 ds
->grid
= snewn(cr
*cr
, digit
);
2337 memset(ds
->grid
, 0, cr
*cr
);
2338 ds
->pencil
= snewn(cr
*cr
*cr
, digit
);
2339 memset(ds
->pencil
, 0, cr
*cr
*cr
);
2340 ds
->hl
= snewn(cr
*cr
, unsigned char);
2341 memset(ds
->hl
, 0, cr
*cr
);
2342 ds
->entered_items
= snewn(cr
*cr
, int);
2343 ds
->tilesize
= 0; /* not decided yet */
2347 static void game_free_drawstate(game_drawstate
*ds
)
2352 sfree(ds
->entered_items
);
2356 static void draw_number(frontend
*fe
, game_drawstate
*ds
, game_state
*state
,
2357 int x
, int y
, int hl
)
2359 int c
= state
->c
, r
= state
->r
, cr
= c
*r
;
2364 if (ds
->grid
[y
*cr
+x
] == state
->grid
[y
*cr
+x
] &&
2365 ds
->hl
[y
*cr
+x
] == hl
&&
2366 !memcmp(ds
->pencil
+(y
*cr
+x
)*cr
, state
->pencil
+(y
*cr
+x
)*cr
, cr
))
2367 return; /* no change required */
2369 tx
= BORDER
+ x
* TILE_SIZE
+ 2;
2370 ty
= BORDER
+ y
* TILE_SIZE
+ 2;
2386 clip(fe
, cx
, cy
, cw
, ch
);
2388 /* background needs erasing */
2389 draw_rect(fe
, cx
, cy
, cw
, ch
, (hl
& 15) == 1 ? COL_HIGHLIGHT
: COL_BACKGROUND
);
2391 /* pencil-mode highlight */
2392 if ((hl
& 15) == 2) {
2396 coords
[2] = cx
+cw
/2;
2399 coords
[5] = cy
+ch
/2;
2400 draw_polygon(fe
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
2403 /* new number needs drawing? */
2404 if (state
->grid
[y
*cr
+x
]) {
2406 str
[0] = state
->grid
[y
*cr
+x
] + '0';
2408 str
[0] += 'a' - ('9'+1);
2409 draw_text(fe
, tx
+ TILE_SIZE
/2, ty
+ TILE_SIZE
/2,
2410 FONT_VARIABLE
, TILE_SIZE
/2, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2411 state
->immutable
[y
*cr
+x
] ? COL_CLUE
: (hl
& 16) ? COL_ERROR
: COL_USER
, str
);
2414 int pw
, ph
, pmax
, fontsize
;
2416 /* count the pencil marks required */
2417 for (i
= npencil
= 0; i
< cr
; i
++)
2418 if (state
->pencil
[(y
*cr
+x
)*cr
+i
])
2422 * It's not sensible to arrange pencil marks in the same
2423 * layout as the squares within a block, because this leads
2424 * to the font being too small. Instead, we arrange pencil
2425 * marks in the nearest thing we can to a square layout,
2426 * and we adjust the square layout depending on the number
2427 * of pencil marks in the square.
2429 for (pw
= 1; pw
* pw
< npencil
; pw
++);
2430 if (pw
< 3) pw
= 3; /* otherwise it just looks _silly_ */
2431 ph
= (npencil
+ pw
- 1) / pw
;
2432 if (ph
< 2) ph
= 2; /* likewise */
2434 fontsize
= TILE_SIZE
/(pmax
*(11-pmax
)/8);
2436 for (i
= j
= 0; i
< cr
; i
++)
2437 if (state
->pencil
[(y
*cr
+x
)*cr
+i
]) {
2438 int dx
= j
% pw
, dy
= j
/ pw
;
2443 str
[0] += 'a' - ('9'+1);
2444 draw_text(fe
, tx
+ (4*dx
+3) * TILE_SIZE
/ (4*pw
+2),
2445 ty
+ (4*dy
+3) * TILE_SIZE
/ (4*ph
+2),
2446 FONT_VARIABLE
, fontsize
,
2447 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_PENCIL
, str
);
2454 draw_update(fe
, cx
, cy
, cw
, ch
);
2456 ds
->grid
[y
*cr
+x
] = state
->grid
[y
*cr
+x
];
2457 memcpy(ds
->pencil
+(y
*cr
+x
)*cr
, state
->pencil
+(y
*cr
+x
)*cr
, cr
);
2458 ds
->hl
[y
*cr
+x
] = hl
;
2461 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
2462 game_state
*state
, int dir
, game_ui
*ui
,
2463 float animtime
, float flashtime
)
2465 int c
= state
->c
, r
= state
->r
, cr
= c
*r
;
2470 * The initial contents of the window are not guaranteed
2471 * and can vary with front ends. To be on the safe side,
2472 * all games should start by drawing a big
2473 * background-colour rectangle covering the whole window.
2475 draw_rect(fe
, 0, 0, SIZE(cr
), SIZE(cr
), COL_BACKGROUND
);
2480 for (x
= 0; x
<= cr
; x
++) {
2481 int thick
= (x
% r ?
0 : 1);
2482 draw_rect(fe
, BORDER
+ x
*TILE_SIZE
- thick
, BORDER
-1,
2483 1+2*thick
, cr
*TILE_SIZE
+3, COL_GRID
);
2485 for (y
= 0; y
<= cr
; y
++) {
2486 int thick
= (y
% c ?
0 : 1);
2487 draw_rect(fe
, BORDER
-1, BORDER
+ y
*TILE_SIZE
- thick
,
2488 cr
*TILE_SIZE
+3, 1+2*thick
, COL_GRID
);
2493 * This array is used to keep track of rows, columns and boxes
2494 * which contain a number more than once.
2496 for (x
= 0; x
< cr
* cr
; x
++)
2497 ds
->entered_items
[x
] = 0;
2498 for (x
= 0; x
< cr
; x
++)
2499 for (y
= 0; y
< cr
; y
++) {
2500 digit d
= state
->grid
[y
*cr
+x
];
2502 int box
= (x
/r
)+(y
/c
)*c
;
2503 ds
->entered_items
[x
*cr
+d
-1] |= ((ds
->entered_items
[x
*cr
+d
-1] & 1) << 1) | 1;
2504 ds
->entered_items
[y
*cr
+d
-1] |= ((ds
->entered_items
[y
*cr
+d
-1] & 4) << 1) | 4;
2505 ds
->entered_items
[box
*cr
+d
-1] |= ((ds
->entered_items
[box
*cr
+d
-1] & 16) << 1) | 16;
2510 * Draw any numbers which need redrawing.
2512 for (x
= 0; x
< cr
; x
++) {
2513 for (y
= 0; y
< cr
; y
++) {
2515 digit d
= state
->grid
[y
*cr
+x
];
2517 if (flashtime
> 0 &&
2518 (flashtime
<= FLASH_TIME
/3 ||
2519 flashtime
>= FLASH_TIME
*2/3))
2522 /* Highlight active input areas. */
2523 if (x
== ui
->hx
&& y
== ui
->hy
)
2524 highlight
= ui
->hpencil ?
2 : 1;
2526 /* Mark obvious errors (ie, numbers which occur more than once
2527 * in a single row, column, or box). */
2528 if (d
&& ((ds
->entered_items
[x
*cr
+d
-1] & 2) ||
2529 (ds
->entered_items
[y
*cr
+d
-1] & 8) ||
2530 (ds
->entered_items
[((x
/r
)+(y
/c
)*c
)*cr
+d
-1] & 32)))
2533 draw_number(fe
, ds
, state
, x
, y
, highlight
);
2538 * Update the _entire_ grid if necessary.
2541 draw_update(fe
, 0, 0, SIZE(cr
), SIZE(cr
));
2546 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2547 int dir
, game_ui
*ui
)
2552 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2553 int dir
, game_ui
*ui
)
2555 if (!oldstate
->completed
&& newstate
->completed
&&
2556 !oldstate
->cheated
&& !newstate
->cheated
)
2561 static int game_wants_statusbar(void)
2566 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2572 #define thegame solo
2575 const struct game thegame
= {
2576 "Solo", "games.solo",
2583 TRUE
, game_configure
, custom_params
,
2591 TRUE
, game_text_format
,
2599 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2602 game_free_drawstate
,
2606 game_wants_statusbar
,
2607 FALSE
, game_timing_state
,
2608 0, /* mouse_priorities */
2611 #ifdef STANDALONE_SOLVER
2614 * gcc -DSTANDALONE_SOLVER -o solosolver solo.c malloc.c
2617 void frontend_default_colour(frontend
*fe
, float *output
) {}
2618 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
2619 int align
, int colour
, char *text
) {}
2620 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
) {}
2621 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
) {}
2622 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
2623 int fillcolour
, int outlinecolour
) {}
2624 void clip(frontend
*fe
, int x
, int y
, int w
, int h
) {}
2625 void unclip(frontend
*fe
) {}
2626 void start_draw(frontend
*fe
) {}
2627 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
) {}
2628 void end_draw(frontend
*fe
) {}
2629 unsigned long random_bits(random_state
*state
, int bits
)
2630 { assert(!"Shouldn't get randomness"); return 0; }
2631 unsigned long random_upto(random_state
*state
, unsigned long limit
)
2632 { assert(!"Shouldn't get randomness"); return 0; }
2633 void shuffle(void *array
, int nelts
, int eltsize
, random_state
*rs
)
2634 { assert(!"Shouldn't get randomness"); }
2636 void fatal(char *fmt
, ...)
2640 fprintf(stderr
, "fatal error: ");
2643 vfprintf(stderr
, fmt
, ap
);
2646 fprintf(stderr
, "\n");
2650 int main(int argc
, char **argv
)
2654 char *id
= NULL
, *desc
, *err
;
2658 while (--argc
> 0) {
2660 if (!strcmp(p
, "-v")) {
2661 solver_show_working
= TRUE
;
2662 } else if (!strcmp(p
, "-g")) {
2664 } else if (*p
== '-') {
2665 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
2673 fprintf(stderr
, "usage: %s [-g | -v] <game_id>\n", argv
[0]);
2677 desc
= strchr(id
, ':');
2679 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
2684 p
= default_params();
2685 decode_params(p
, id
);
2686 err
= validate_desc(p
, desc
);
2688 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
2691 s
= new_game(NULL
, p
, desc
);
2693 ret
= solver(p
->c
, p
->r
, s
->grid
, DIFF_RECURSIVE
);
2695 printf("Difficulty rating: %s\n",
2696 ret
==DIFF_BLOCK ?
"Trivial (blockwise positional elimination only)":
2697 ret
==DIFF_SIMPLE ?
"Basic (row/column/number elimination required)":
2698 ret
==DIFF_INTERSECT ?
"Intermediate (intersectional analysis required)":
2699 ret
==DIFF_SET ?
"Advanced (set elimination required)":
2700 ret
==DIFF_RECURSIVE ?
"Unreasonable (guesswork and backtracking required)":
2701 ret
==DIFF_AMBIGUOUS ?
"Ambiguous (multiple solutions exist)":
2702 ret
==DIFF_IMPOSSIBLE ?
"Impossible (no solution exists)":
2703 "INTERNAL ERROR: unrecognised difficulty code");
2705 printf("%s\n", grid_text_format(p
->c
, p
->r
, s
->grid
));