2 * solo.c: the number-placing puzzle most popularly known as `Sudoku'.
8 * - can we do anything about nasty centring of text in GTK? It
9 * seems to be taking ascenders/descenders into account when
12 * - implement stronger modes of reasoning in nsolve, thus
13 * enabling harder puzzles
15 * - configurable difficulty levels
17 * - vary the symmetry (rotational or none)?
19 * - try for cleverer ways of reducing the solved grid; they seem
20 * to be coming out a bit full for the most part, and in
21 * particular it's inexcusable to leave a grid with an entire
22 * block (or presumably row or column) filled! I _hope_ we can
23 * do this simply by better prioritising (somehow) the possible
25 * + one simple option might be to work the other way: start
26 * with an empty grid and gradually _add_ numbers until it
27 * becomes solvable? Perhaps there might be some heuristic
28 * which enables us to pinpoint the most critical clues and
29 * thus add as few as possible.
31 * - alternative interface modes
32 * + sudoku.com's Windows program has a palette of possible
33 * entries; you select a palette entry first and then click
34 * on the square you want it to go in, thus enabling
35 * mouse-only play. Useful for PDAs! I don't think it's
36 * actually incompatible with the current highlight-then-type
37 * approach: you _either_ highlight a palette entry and then
38 * click, _or_ you highlight a square and then type. At most
39 * one thing is ever highlighted at a time, so there's no way
41 * + `pencil marks' might be useful for more subtle forms of
42 * deduction, once we implement creation of puzzles that
47 * Solo puzzles need to be square overall (since each row and each
48 * column must contain one of every digit), but they need not be
49 * subdivided the same way internally. I am going to adopt a
50 * convention whereby I _always_ refer to `r' as the number of rows
51 * of _big_ divisions, and `c' as the number of columns of _big_
52 * divisions. Thus, a 2c by 3r puzzle looks something like this:
56 * ------+------ (Of course, you can't subdivide it the other way
57 * 1 4 5 | 6 3 2 or you'll get clashes; observe that the 4 in the
58 * 3 2 6 | 4 1 5 top left would conflict with the 4 in the second
59 * ------+------ box down on the left-hand side.)
63 * The need for a strong naming convention should now be clear:
64 * each small box is two rows of digits by three columns, while the
65 * overall puzzle has three rows of small boxes by two columns. So
66 * I will (hopefully) consistently use `r' to denote the number of
67 * rows _of small boxes_ (here 3), which is also the number of
68 * columns of digits in each small box; and `c' vice versa (here
71 * I'm also going to choose arbitrarily to list c first wherever
72 * possible: the above is a 2x3 puzzle, not a 3x2 one.
85 * To save space, I store digits internally as unsigned char. This
86 * imposes a hard limit of 255 on the order of the puzzle. Since
87 * even a 5x5 takes unacceptably long to generate, I don't see this
88 * as a serious limitation unless something _really_ impressive
89 * happens in computing technology; but here's a typedef anyway for
90 * general good practice.
92 typedef unsigned char digit;
98 #define FLASH_TIME 0.4F
116 unsigned char *immutable; /* marks which digits are clues */
120 static game_params *default_params(void)
122 game_params *ret = snew(game_params);
129 static int game_fetch_preset(int i, char **name, game_params **params)
136 case 0: c = 2, r = 2; break;
137 case 1: c = 2, r = 3; break;
138 case 2: c = 3, r = 3; break;
139 case 3: c = 3, r = 4; break;
140 case 4: c = 4, r = 4; break;
141 default: return FALSE;
144 sprintf(buf, "%dx%d", c, r);
146 *params = ret = snew(game_params);
149 /* FIXME: difficulty presets? */
153 static void free_params(game_params *params)
158 static game_params *dup_params(game_params *params)
160 game_params *ret = snew(game_params);
161 *ret = *params; /* structure copy */
165 static game_params *decode_params(char const *string)
167 game_params *ret = default_params();
169 ret->c = ret->r = atoi(string);
170 while (*string && isdigit((unsigned char)*string)) string++;
171 if (*string == 'x') {
173 ret->r = atoi(string);
174 while (*string && isdigit((unsigned char)*string)) string++;
176 /* FIXME: difficulty levels */
181 static char *encode_params(game_params *params)
185 sprintf(str, "%dx%d", params->c, params->r);
189 static config_item *game_configure(game_params *params)
194 ret = snewn(5, config_item);
196 ret[0].name = "Columns of sub-blocks";
197 ret[0].type = C_STRING;
198 sprintf(buf, "%d", params->c);
199 ret[0].sval = dupstr(buf);
202 ret[1].name = "Rows of sub-blocks";
203 ret[1].type = C_STRING;
204 sprintf(buf, "%d", params->r);
205 ret[1].sval = dupstr(buf);
209 * FIXME: difficulty level.
220 static game_params *custom_params(config_item *cfg)
222 game_params *ret = snew(game_params);
224 ret->c = atof(cfg[0].sval);
225 ret->r = atof(cfg[1].sval);
230 static char *validate_params(game_params *params)
232 if (params->c < 2 || params->r < 2)
233 return "Both dimensions must be at least 2";
234 if (params->c > ORDER_MAX || params->r > ORDER_MAX)
235 return "Dimensions greater than "STR(ORDER_MAX)" are not supported";
239 /* ----------------------------------------------------------------------
240 * Full recursive Solo solver.
242 * The algorithm for this solver is shamelessly copied from a
243 * Python solver written by Andrew Wilkinson (which is GPLed, but
244 * I've reused only ideas and no code). It mostly just does the
245 * obvious recursive thing: pick an empty square, put one of the
246 * possible digits in it, recurse until all squares are filled,
247 * backtrack and change some choices if necessary.
249 * The clever bit is that every time it chooses which square to
250 * fill in next, it does so by counting the number of _possible_
251 * numbers that can go in each square, and it prioritises so that
252 * it picks a square with the _lowest_ number of possibilities. The
253 * idea is that filling in lots of the obvious bits (particularly
254 * any squares with only one possibility) will cut down on the list
255 * of possibilities for other squares and hence reduce the enormous
256 * search space as much as possible as early as possible.
258 * In practice the algorithm appeared to work very well; run on
259 * sample problems from the Times it completed in well under a
260 * second on my G5 even when written in Python, and given an empty
261 * grid (so that in principle it would enumerate _all_ solved
262 * grids!) it found the first valid solution just as quickly. So
263 * with a bit more randomisation I see no reason not to use this as
268 * Internal data structure used in solver to keep track of
271 struct rsolve_coord { int x, y, r; };
272 struct rsolve_usage {
273 int c, r, cr; /* cr == c*r */
274 /* grid is a copy of the input grid, modified as we go along */
276 /* row[y*cr+n-1] TRUE if digit n has been placed in row y */
278 /* col[x*cr+n-1] TRUE if digit n has been placed in row x */
280 /* blk[(y*c+x)*cr+n-1] TRUE if digit n has been placed in block (x,y) */
282 /* This lists all the empty spaces remaining in the grid. */
283 struct rsolve_coord *spaces;
285 /* If we need randomisation in the solve, this is our random state. */
287 /* Number of solutions so far found, and maximum number we care about. */
292 * The real recursive step in the solving function.
294 static void rsolve_real(struct rsolve_usage *usage, digit *grid)
296 int c = usage->c, r = usage->r, cr = usage->cr;
297 int i, j, n, sx, sy, bestm, bestr;
301 * Firstly, check for completion! If there are no spaces left
302 * in the grid, we have a solution.
304 if (usage->nspaces == 0) {
307 * This is our first solution, so fill in the output grid.
309 memcpy(grid, usage->grid, cr * cr);
316 * Otherwise, there must be at least one space. Find the most
317 * constrained space, using the `r' field as a tie-breaker.
319 bestm = cr+1; /* so that any space will beat it */
322 for (j = 0; j < usage->nspaces; j++) {
323 int x = usage->spaces[j].x, y = usage->spaces[j].y;
327 * Find the number of digits that could go in this space.
330 for (n = 0; n < cr; n++)
331 if (!usage->row[y*cr+n] && !usage->col[x*cr+n] &&
332 !usage->blk[((y/c)*c+(x/r))*cr+n])
335 if (m < bestm || (m == bestm && usage->spaces[j].r < bestr)) {
337 bestr = usage->spaces[j].r;
345 * Swap that square into the final place in the spaces array,
346 * so that decrementing nspaces will remove it from the list.
348 if (i != usage->nspaces-1) {
349 struct rsolve_coord t;
350 t = usage->spaces[usage->nspaces-1];
351 usage->spaces[usage->nspaces-1] = usage->spaces[i];
352 usage->spaces[i] = t;
356 * Now we've decided which square to start our recursion at,
357 * simply go through all possible values, shuffling them
358 * randomly first if necessary.
360 digits = snewn(bestm, int);
362 for (n = 0; n < cr; n++)
363 if (!usage->row[sy*cr+n] && !usage->col[sx*cr+n] &&
364 !usage->blk[((sy/c)*c+(sx/r))*cr+n]) {
370 for (i = j; i > 1; i--) {
371 int p = random_upto(usage->rs, i);
374 digits[p] = digits[i-1];
380 /* And finally, go through the digit list and actually recurse. */
381 for (i = 0; i < j; i++) {
384 /* Update the usage structure to reflect the placing of this digit. */
385 usage->row[sy*cr+n-1] = usage->col[sx*cr+n-1] =
386 usage->blk[((sy/c)*c+(sx/r))*cr+n-1] = TRUE;
387 usage->grid[sy*cr+sx] = n;
390 /* Call the solver recursively. */
391 rsolve_real(usage, grid);
394 * If we have seen as many solutions as we need, terminate
395 * all processing immediately.
397 if (usage->solns >= usage->maxsolns)
400 /* Revert the usage structure. */
401 usage->row[sy*cr+n-1] = usage->col[sx*cr+n-1] =
402 usage->blk[((sy/c)*c+(sx/r))*cr+n-1] = FALSE;
403 usage->grid[sy*cr+sx] = 0;
411 * Entry point to solver. You give it dimensions and a starting
412 * grid, which is simply an array of N^4 digits. In that array, 0
413 * means an empty square, and 1..N mean a clue square.
415 * Return value is the number of solutions found; searching will
416 * stop after the provided `max'. (Thus, you can pass max==1 to
417 * indicate that you only care about finding _one_ solution, or
418 * max==2 to indicate that you want to know the difference between
419 * a unique and non-unique solution.) The input parameter `grid' is
420 * also filled in with the _first_ (or only) solution found by the
423 static int rsolve(int c, int r, digit *grid, random_state *rs, int max)
425 struct rsolve_usage *usage;
430 * Create an rsolve_usage structure.
432 usage = snew(struct rsolve_usage);
438 usage->grid = snewn(cr * cr, digit);
439 memcpy(usage->grid, grid, cr * cr);
441 usage->row = snewn(cr * cr, unsigned char);
442 usage->col = snewn(cr * cr, unsigned char);
443 usage->blk = snewn(cr * cr, unsigned char);
444 memset(usage->row, FALSE, cr * cr);
445 memset(usage->col, FALSE, cr * cr);
446 memset(usage->blk, FALSE, cr * cr);
448 usage->spaces = snewn(cr * cr, struct rsolve_coord);
452 usage->maxsolns = max;
457 * Now fill it in with data from the input grid.
459 for (y = 0; y < cr; y++) {
460 for (x = 0; x < cr; x++) {
461 int v = grid[y*cr+x];
463 usage->spaces[usage->nspaces].x = x;
464 usage->spaces[usage->nspaces].y = y;
466 usage->spaces[usage->nspaces].r = random_bits(rs, 31);
468 usage->spaces[usage->nspaces].r = usage->nspaces;
471 usage->row[y*cr+v-1] = TRUE;
472 usage->col[x*cr+v-1] = TRUE;
473 usage->blk[((y/c)*c+(x/r))*cr+v-1] = TRUE;
479 * Run the real recursive solving function.
481 rsolve_real(usage, grid);
485 * Clean up the usage structure now we have our answer.
487 sfree(usage->spaces);
500 /* ----------------------------------------------------------------------
501 * End of recursive solver code.
504 /* ----------------------------------------------------------------------
505 * Less capable non-recursive solver. This one is used to check
506 * solubility of a grid as we gradually remove numbers from it: by
507 * verifying a grid using this solver we can ensure it isn't _too_
508 * hard (e.g. does not actually require guessing and backtracking).
510 * It supports a variety of specific modes of reasoning. By
511 * enabling or disabling subsets of these modes we can arrange a
512 * range of difficulty levels.
516 * Modes of reasoning currently supported:
518 * - Positional elimination: a number must go in a particular
519 * square because all the other empty squares in a given
520 * row/col/blk are ruled out.
522 * - Numeric elimination: a square must have a particular number
523 * in because all the other numbers that could go in it are
526 * More advanced modes of reasoning I'd like to support in future:
528 * - Intersectional elimination: given two domains which overlap
529 * (hence one must be a block, and the other can be a row or
530 * col), if the possible locations for a particular number in
531 * one of the domains can be narrowed down to the overlap, then
532 * that number can be ruled out everywhere but the overlap in
533 * the other domain too.
535 * - Setwise numeric elimination: if there is a subset of the
536 * empty squares within a domain such that the union of the
537 * possible numbers in that subset has the same size as the
538 * subset itself, then those numbers can be ruled out everywhere
539 * else in the domain. (For example, if there are five empty
540 * squares and the possible numbers in each are 12, 23, 13, 134
541 * and 1345, then the first three empty squares form such a
542 * subset: the numbers 1, 2 and 3 _must_ be in those three
543 * squares in some permutation, and hence we can deduce none of
544 * them can be in the fourth or fifth squares.)
547 struct nsolve_usage {
550 * We set up a cubic array, indexed by x, y and digit; each
551 * element of this array is TRUE or FALSE according to whether
552 * or not that digit _could_ in principle go in that position.
554 * The way to index this array is cube[(x*cr+y)*cr+n-1].
558 * This is the grid in which we write down our final
563 * Now we keep track, at a slightly higher level, of what we
564 * have yet to work out, to prevent doing the same deduction
567 /* row[y*cr+n-1] TRUE if digit n has been placed in row y */
569 /* col[x*cr+n-1] TRUE if digit n has been placed in row x */
571 /* blk[(y*c+x)*cr+n-1] TRUE if digit n has been placed in block (x,y) */
574 #define cube(x,y,n) (usage->cube[((x)*usage->cr+(y))*usage->cr+(n)-1])
577 * Function called when we are certain that a particular square has
578 * a particular number in it.
580 static void nsolve_place(struct nsolve_usage *usage, int x, int y, int n)
582 int c = usage->c, r = usage->r, cr = usage->cr;
588 * Rule out all other numbers in this square.
590 for (i = 1; i <= cr; i++)
595 * Rule out this number in all other positions in the row.
597 for (i = 0; i < cr; i++)
602 * Rule out this number in all other positions in the column.
604 for (i = 0; i < cr; i++)
609 * Rule out this number in all other positions in the block.
613 for (i = 0; i < r; i++)
614 for (j = 0; j < c; j++)
615 if (bx+i != x || by+j != y)
616 cube(bx+i,by+j,n) = FALSE;
619 * Enter the number in the result grid.
621 usage->grid[y*cr+x] = n;
624 * Cross out this number from the list of numbers left to place
625 * in its row, its column and its block.
627 usage->row[y*cr+n-1] = usage->col[x*cr+n-1] =
628 usage->blk[((y/c)*c+(x/r))*cr+n-1] = TRUE;
631 static int nsolve_blk_pos_elim(struct nsolve_usage *usage,
634 int c = usage->c, r = usage->r;
641 * Count the possible positions within this block where this
642 * number could appear.
646 for (i = 0; i < r; i++)
647 for (j = 0; j < c; j++)
648 if (cube(x+i,y+j,n)) {
655 assert(fx >= 0 && fy >= 0);
656 nsolve_place(usage, fx, fy, n);
663 static int nsolve_row_pos_elim(struct nsolve_usage *usage,
670 * Count the possible positions within this row where this
671 * number could appear.
675 for (x = 0; x < cr; x++)
683 nsolve_place(usage, fx, y, n);
690 static int nsolve_col_pos_elim(struct nsolve_usage *usage,
697 * Count the possible positions within this column where this
698 * number could appear.
702 for (y = 0; y < cr; y++)
710 nsolve_place(usage, x, fy, n);
717 static int nsolve_num_elim(struct nsolve_usage *usage,
724 * Count the possible numbers that could appear in this square.
728 for (n = 1; n <= cr; n++)
736 nsolve_place(usage, x, y, fn);
743 static int nsolve(int c, int r, digit *grid)
745 struct nsolve_usage *usage;
750 * Set up a usage structure as a clean slate (everything
753 usage = snew(struct nsolve_usage);
757 usage->cube = snewn(cr*cr*cr, unsigned char);
758 usage->grid = grid; /* write straight back to the input */
759 memset(usage->cube, TRUE, cr*cr*cr);
761 usage->row = snewn(cr * cr, unsigned char);
762 usage->col = snewn(cr * cr, unsigned char);
763 usage->blk = snewn(cr * cr, unsigned char);
764 memset(usage->row, FALSE, cr * cr);
765 memset(usage->col, FALSE, cr * cr);
766 memset(usage->blk, FALSE, cr * cr);
769 * Place all the clue numbers we are given.
771 for (x = 0; x < cr; x++)
772 for (y = 0; y < cr; y++)
774 nsolve_place(usage, x, y, grid[y*cr+x]);
777 * Now loop over the grid repeatedly trying all permitted modes
778 * of reasoning. The loop terminates if we complete an
779 * iteration without making any progress; we then return
780 * failure or success depending on whether the grid is full or
785 * Blockwise positional elimination.
787 for (x = 0; x < c; x++)
788 for (y = 0; y < r; y++)
789 for (n = 1; n <= cr; n++)
790 if (!usage->blk[((y/c)*c+(x/r))*cr+n-1] &&
791 nsolve_blk_pos_elim(usage, x, y, n))
795 * Row-wise positional elimination.
797 for (y = 0; y < cr; y++)
798 for (n = 1; n <= cr; n++)
799 if (!usage->row[y*cr+n-1] &&
800 nsolve_row_pos_elim(usage, y, n))
803 * Column-wise positional elimination.
805 for (x = 0; x < cr; x++)
806 for (n = 1; n <= cr; n++)
807 if (!usage->col[x*cr+n-1] &&
808 nsolve_col_pos_elim(usage, x, n))
812 * Numeric elimination.
814 for (x = 0; x < cr; x++)
815 for (y = 0; y < cr; y++)
816 if (!usage->grid[y*cr+x] &&
817 nsolve_num_elim(usage, x, y))
821 * If we reach here, we have made no deductions in this
822 * iteration, so the algorithm terminates.
833 for (x = 0; x < cr; x++)
834 for (y = 0; y < cr; y++)
840 /* ----------------------------------------------------------------------
841 * End of non-recursive solver code.
845 * Check whether a grid contains a valid complete puzzle.
847 static int check_valid(int c, int r, digit *grid)
853 used = snewn(cr, unsigned char);
856 * Check that each row contains precisely one of everything.
858 for (y = 0; y < cr; y++) {
859 memset(used, FALSE, cr);
860 for (x = 0; x < cr; x++)
861 if (grid[y*cr+x] > 0 && grid[y*cr+x] <= cr)
862 used[grid[y*cr+x]-1] = TRUE;
863 for (n = 0; n < cr; n++)
871 * Check that each column contains precisely one of everything.
873 for (x = 0; x < cr; x++) {
874 memset(used, FALSE, cr);
875 for (y = 0; y < cr; y++)
876 if (grid[y*cr+x] > 0 && grid[y*cr+x] <= cr)
877 used[grid[y*cr+x]-1] = TRUE;
878 for (n = 0; n < cr; n++)
886 * Check that each block contains precisely one of everything.
888 for (x = 0; x < cr; x += r) {
889 for (y = 0; y < cr; y += c) {
891 memset(used, FALSE, cr);
892 for (xx = x; xx < x+r; xx++)
893 for (yy = 0; yy < y+c; yy++)
894 if (grid[yy*cr+xx] > 0 && grid[yy*cr+xx] <= cr)
895 used[grid[yy*cr+xx]-1] = TRUE;
896 for (n = 0; n < cr; n++)
908 static char *new_game_seed(game_params *params, random_state *rs)
910 int c = params->c, r = params->r, cr = c*r;
913 struct xy { int x, y; } *locs;
919 * Start the recursive solver with an empty grid to generate a
920 * random solved state.
922 grid = snewn(area, digit);
923 memset(grid, 0, area);
924 ret = rsolve(c, r, grid, rs, 1);
926 assert(check_valid(c, r, grid));
930 "\x0\x1\x0\x0\x6\x0\x0\x0\x0"
931 "\x5\x0\x0\x7\x0\x4\x0\x2\x0"
932 "\x0\x0\x6\x1\x0\x0\x0\x0\x0"
933 "\x8\x9\x7\x0\x0\x0\x0\x0\x0"
934 "\x0\x0\x3\x0\x4\x0\x9\x0\x0"
935 "\x0\x0\x0\x0\x0\x0\x8\x7\x6"
936 "\x0\x0\x0\x0\x0\x9\x1\x0\x0"
937 "\x0\x3\x0\x6\x0\x5\x0\x0\x7"
938 "\x0\x0\x0\x0\x8\x0\x0\x5\x0"
943 for (y = 0; y < cr; y++) {
944 for (x = 0; x < cr; x++) {
945 printf("%2.0d", grid[y*cr+x]);
956 for (y = 0; y < cr; y++) {
957 for (x = 0; x < cr; x++) {
958 printf("%2.0d", grid[y*cr+x]);
967 * Now we have a solved grid, start removing things from it
968 * while preserving solubility.
970 locs = snewn((cr+1)/2 * (cr+1)/2, struct xy);
971 grid2 = snewn(area, digit);
976 * Iterate over the top left corner of the grid and
977 * enumerate all the filled squares we could empty.
981 for (x = 0; 2*x < cr; x++)
982 for (y = 0; 2*y < cr; y++)
990 * Now shuffle that list.
992 for (i = nlocs; i > 1; i--) {
993 int p = random_upto(rs, i);
995 struct xy t = locs[p];
1002 * Now loop over the shuffled list and, for each element,
1003 * see whether removing that element (and its reflections)
1004 * from the grid will still leave the grid soluble by
1007 for (i = 0; i < nlocs; i++) {
1011 memcpy(grid2, grid, area);
1013 grid2[y*cr+cr-1-x] = 0;
1014 grid2[(cr-1-y)*cr+x] = 0;
1015 grid2[(cr-1-y)*cr+cr-1-x] = 0;
1017 if (nsolve(c, r, grid2)) {
1019 grid[y*cr+cr-1-x] = 0;
1020 grid[(cr-1-y)*cr+x] = 0;
1021 grid[(cr-1-y)*cr+cr-1-x] = 0;
1028 * There was nothing we could remove without destroying
1040 for (y = 0; y < cr; y++) {
1041 for (x = 0; x < cr; x++) {
1042 printf("%2.0d", grid[y*cr+x]);
1051 * Now we have the grid as it will be presented to the user.
1052 * Encode it in a game seed.
1058 seed = snewn(5 * area, char);
1061 for (i = 0; i <= area; i++) {
1062 int n = (i < area ? grid[i] : -1);
1069 int c = 'a' - 1 + run;
1073 run -= c - ('a' - 1);
1077 * If there's a number in the very top left or
1078 * bottom right, there's no point putting an
1079 * unnecessary _ before or after it.
1081 if (p > seed && n > 0)
1085 p += sprintf(p, "%d", n);
1089 assert(p - seed < 5 * area);
1091 seed = sresize(seed, p - seed, char);
1099 static char *validate_seed(game_params *params, char *seed)
1101 int area = params->r * params->r * params->c * params->c;
1106 if (n >= 'a' && n <= 'z') {
1107 squares += n - 'a' + 1;
1108 } else if (n == '_') {
1110 } else if (n > '0' && n <= '9') {
1112 while (*seed >= '0' && *seed <= '9')
1115 return "Invalid character in game specification";
1119 return "Not enough data to fill grid";
1122 return "Too much data to fit in grid";
1127 static game_state *new_game(game_params *params, char *seed)
1129 game_state *state = snew(game_state);
1130 int c = params->c, r = params->r, cr = c*r, area = cr * cr;
1133 state->c = params->c;
1134 state->r = params->r;
1136 state->grid = snewn(area, digit);
1137 state->immutable = snewn(area, unsigned char);
1138 memset(state->immutable, FALSE, area);
1140 state->completed = FALSE;
1145 if (n >= 'a' && n <= 'z') {
1146 int run = n - 'a' + 1;
1147 assert(i + run <= area);
1149 state->grid[i++] = 0;
1150 } else if (n == '_') {
1152 } else if (n > '0' && n <= '9') {
1154 state->immutable[i] = TRUE;
1155 state->grid[i++] = atoi(seed-1);
1156 while (*seed >= '0' && *seed <= '9')
1159 assert(!"We can't get here");
1167 static game_state *dup_game(game_state *state)
1169 game_state *ret = snew(game_state);
1170 int c = state->c, r = state->r, cr = c*r, area = cr * cr;
1175 ret->grid = snewn(area, digit);
1176 memcpy(ret->grid, state->grid, area);
1178 ret->immutable = snewn(area, unsigned char);
1179 memcpy(ret->immutable, state->immutable, area);
1181 ret->completed = state->completed;
1186 static void free_game(game_state *state)
1188 sfree(state->immutable);
1195 * These are the coordinates of the currently highlighted
1196 * square on the grid, or -1,-1 if there isn't one. When there
1197 * is, pressing a valid number or letter key or Space will
1198 * enter that number or letter in the grid.
1203 static game_ui *new_ui(game_state *state)
1205 game_ui *ui = snew(game_ui);
1207 ui->hx = ui->hy = -1;
1212 static void free_ui(game_ui *ui)
1217 static game_state *make_move(game_state *from, game_ui *ui, int x, int y,
1220 int c = from->c, r = from->r, cr = c*r;
1224 tx = (x - BORDER) / TILE_SIZE;
1225 ty = (y - BORDER) / TILE_SIZE;
1227 if (tx >= 0 && tx < cr && ty >= 0 && ty < cr && button == LEFT_BUTTON) {
1228 if (tx == ui->hx && ty == ui->hy) {
1229 ui->hx = ui->hy = -1;
1234 return from; /* UI activity occurred */
1237 if (ui->hx != -1 && ui->hy != -1 &&
1238 ((button >= '1' && button <= '9' && button - '0' <= cr) ||
1239 (button >= 'a' && button <= 'z' && button - 'a' + 10 <= cr) ||
1240 (button >= 'A' && button <= 'Z' && button - 'A' + 10 <= cr) ||
1242 int n = button - '0';
1243 if (button >= 'A' && button <= 'Z')
1244 n = button - 'A' + 10;
1245 if (button >= 'a' && button <= 'z')
1246 n = button - 'a' + 10;
1250 if (from->immutable[ui->hy*cr+ui->hx])
1251 return NULL; /* can't overwrite this square */
1253 ret = dup_game(from);
1254 ret->grid[ui->hy*cr+ui->hx] = n;
1255 ui->hx = ui->hy = -1;
1258 * We've made a real change to the grid. Check to see
1259 * if the game has been completed.
1261 if (!ret->completed && check_valid(c, r, ret->grid)) {
1262 ret->completed = TRUE;
1265 return ret; /* made a valid move */
1271 /* ----------------------------------------------------------------------
1275 struct game_drawstate {
1282 #define XSIZE(cr) ((cr) * TILE_SIZE + 2*BORDER + 1)
1283 #define YSIZE(cr) ((cr) * TILE_SIZE + 2*BORDER + 1)
1285 static void game_size(game_params *params, int *x, int *y)
1287 int c = params->c, r = params->r, cr = c*r;
1293 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1295 float *ret = snewn(3 * NCOLOURS, float);
1297 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1299 ret[COL_GRID * 3 + 0] = 0.0F;
1300 ret[COL_GRID * 3 + 1] = 0.0F;
1301 ret[COL_GRID * 3 + 2] = 0.0F;
1303 ret[COL_CLUE * 3 + 0] = 0.0F;
1304 ret[COL_CLUE * 3 + 1] = 0.0F;
1305 ret[COL_CLUE * 3 + 2] = 0.0F;
1307 ret[COL_USER * 3 + 0] = 0.0F;
1308 ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1];
1309 ret[COL_USER * 3 + 2] = 0.0F;
1311 ret[COL_HIGHLIGHT * 3 + 0] = 0.85F * ret[COL_BACKGROUND * 3 + 0];
1312 ret[COL_HIGHLIGHT * 3 + 1] = 0.85F * ret[COL_BACKGROUND * 3 + 1];
1313 ret[COL_HIGHLIGHT * 3 + 2] = 0.85F * ret[COL_BACKGROUND * 3 + 2];
1315 *ncolours = NCOLOURS;
1319 static game_drawstate *game_new_drawstate(game_state *state)
1321 struct game_drawstate *ds = snew(struct game_drawstate);
1322 int c = state->c, r = state->r, cr = c*r;
1324 ds->started = FALSE;
1328 ds->grid = snewn(cr*cr, digit);
1329 memset(ds->grid, 0, cr*cr);
1330 ds->hl = snewn(cr*cr, unsigned char);
1331 memset(ds->hl, 0, cr*cr);
1336 static void game_free_drawstate(game_drawstate *ds)
1343 static void draw_number(frontend *fe, game_drawstate *ds, game_state *state,
1344 int x, int y, int hl)
1346 int c = state->c, r = state->r, cr = c*r;
1351 if (ds->grid[y*cr+x] == state->grid[y*cr+x] && ds->hl[y*cr+x] == hl)
1352 return; /* no change required */
1354 tx = BORDER + x * TILE_SIZE + 2;
1355 ty = BORDER + y * TILE_SIZE + 2;
1371 clip(fe, cx, cy, cw, ch);
1373 /* background needs erasing? */
1374 if (ds->grid[y*cr+x] || ds->hl[y*cr+x] != hl)
1375 draw_rect(fe, cx, cy, cw, ch, hl ? COL_HIGHLIGHT : COL_BACKGROUND);
1377 /* new number needs drawing? */
1378 if (state->grid[y*cr+x]) {
1380 str[0] = state->grid[y*cr+x] + '0';
1382 str[0] += 'a' - ('9'+1);
1383 draw_text(fe, tx + TILE_SIZE/2, ty + TILE_SIZE/2,
1384 FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
1385 state->immutable[y*cr+x] ? COL_CLUE : COL_USER, str);
1390 draw_update(fe, cx, cy, cw, ch);
1392 ds->grid[y*cr+x] = state->grid[y*cr+x];
1393 ds->hl[y*cr+x] = hl;
1396 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1397 game_state *state, int dir, game_ui *ui,
1398 float animtime, float flashtime)
1400 int c = state->c, r = state->r, cr = c*r;
1405 * The initial contents of the window are not guaranteed
1406 * and can vary with front ends. To be on the safe side,
1407 * all games should start by drawing a big
1408 * background-colour rectangle covering the whole window.
1410 draw_rect(fe, 0, 0, XSIZE(cr), YSIZE(cr), COL_BACKGROUND);
1415 for (x = 0; x <= cr; x++) {
1416 int thick = (x % r ? 0 : 1);
1417 draw_rect(fe, BORDER + x*TILE_SIZE - thick, BORDER-1,
1418 1+2*thick, cr*TILE_SIZE+3, COL_GRID);
1420 for (y = 0; y <= cr; y++) {
1421 int thick = (y % c ? 0 : 1);
1422 draw_rect(fe, BORDER-1, BORDER + y*TILE_SIZE - thick,
1423 cr*TILE_SIZE+3, 1+2*thick, COL_GRID);
1428 * Draw any numbers which need redrawing.
1430 for (x = 0; x < cr; x++) {
1431 for (y = 0; y < cr; y++) {
1432 draw_number(fe, ds, state, x, y,
1433 (x == ui->hx && y == ui->hy) ||
1435 (flashtime <= FLASH_TIME/3 ||
1436 flashtime >= FLASH_TIME*2/3)));
1441 * Update the _entire_ grid if necessary.
1444 draw_update(fe, 0, 0, XSIZE(cr), YSIZE(cr));
1449 static float game_anim_length(game_state *oldstate, game_state *newstate,
1455 static float game_flash_length(game_state *oldstate, game_state *newstate,
1458 if (!oldstate->completed && newstate->completed)
1463 static int game_wants_statusbar(void)
1469 #define thegame solo
1472 const struct game thegame = {
1473 "Solo", "games.solo", TRUE,
1494 game_free_drawstate,
1498 game_wants_statusbar,