2 * dominosa.c: Domino jigsaw puzzle. Aim to place one of every
3 * possible domino within a rectangle in such a way that the number
4 * on each square matches the provided clue.
10 * - improve solver so as to use more interesting forms of
13 * * rule out a domino placement if it would divide an unfilled
14 * region such that at least one resulting region had an odd
16 * + use b.f.s. to determine the area of an unfilled region
17 * + a square is unfilled iff it has at least two possible
18 * placements, and two adjacent unfilled squares are part
19 * of the same region iff the domino placement joining
22 * * perhaps set analysis
23 * + look at all unclaimed squares containing a given number
24 * + for each one, find the set of possible numbers that it
25 * can connect to (i.e. each neighbouring tile such that
26 * the placement between it and that neighbour has not yet
28 * + now proceed similarly to Solo set analysis: try to find
29 * a subset of the squares such that the union of their
30 * possible numbers is the same size as the subset. If so,
31 * rule out those possible numbers for all other squares.
32 * * important wrinkle: the double dominoes complicate
33 * matters. Connecting a number to itself uses up _two_
34 * of the unclaimed squares containing a number. Thus,
35 * when finding the initial subset we must never
36 * include two adjacent squares; and also, when ruling
37 * things out after finding the subset, we must be
38 * careful that we don't rule out precisely the domino
39 * placement that was _included_ in our set!
51 /* nth triangular number */
52 #define TRI(n) ( (n) * ((n) + 1) / 2 )
53 /* number of dominoes for value n */
54 #define DCOUNT(n) TRI((n)+1)
55 /* map a pair of numbers to a unique domino index from 0 upwards. */
56 #define DINDEX(n1,n2) ( TRI(max(n1,n2)) + min(n1,n2) )
58 #define FLASH_TIME 0.13F
79 int *numbers; /* h x w */
90 struct game_numbers *numbers;
92 unsigned short *edges; /* h x w */
93 int completed, cheated;
96 static game_params *default_params(void)
98 game_params *ret = snew(game_params);
106 static int game_fetch_preset(int i, char **name, game_params **params)
113 case 0: n = 3; break;
114 case 1: n = 4; break;
115 case 2: n = 5; break;
116 case 3: n = 6; break;
117 case 4: n = 7; break;
118 case 5: n = 8; break;
119 case 6: n = 9; break;
120 default: return FALSE;
123 sprintf(buf, "Up to double-%d", n);
126 *params = ret = snew(game_params);
133 static void free_params(game_params *params)
138 static game_params *dup_params(const game_params *params)
140 game_params *ret = snew(game_params);
141 *ret = *params; /* structure copy */
145 static void decode_params(game_params *params, char const *string)
147 params->n = atoi(string);
148 while (*string && isdigit((unsigned char)*string)) string++;
150 params->unique = FALSE;
153 static char *encode_params(const game_params *params, int full)
156 sprintf(buf, "%d", params->n);
157 if (full && !params->unique)
162 static config_item *game_configure(const game_params *params)
167 ret = snewn(3, config_item);
169 ret[0].name = "Maximum number on dominoes";
170 ret[0].type = C_STRING;
171 sprintf(buf, "%d", params->n);
172 ret[0].sval = dupstr(buf);
175 ret[1].name = "Ensure unique solution";
176 ret[1].type = C_BOOLEAN;
178 ret[1].ival = params->unique;
188 static game_params *custom_params(const config_item *cfg)
190 game_params *ret = snew(game_params);
192 ret->n = atoi(cfg[0].sval);
193 ret->unique = cfg[1].ival;
198 static char *validate_params(const game_params *params, int full)
201 return "Maximum face number must be at least one";
205 /* ----------------------------------------------------------------------
209 static int find_overlaps(int w, int h, int placement, int *set)
213 n = 0; /* number of returned placements */
221 * Horizontal domino, indexed by its left end.
224 set[n++] = placement-2; /* horizontal domino to the left */
226 set[n++] = placement-2*w-1;/* vertical domino above left side */
228 set[n++] = placement-1; /* vertical domino below left side */
230 set[n++] = placement+2; /* horizontal domino to the right */
232 set[n++] = placement-2*w+2-1;/* vertical domino above right side */
234 set[n++] = placement+2-1; /* vertical domino below right side */
237 * Vertical domino, indexed by its top end.
240 set[n++] = placement-2*w; /* vertical domino above */
242 set[n++] = placement-2+1; /* horizontal domino left of top */
244 set[n++] = placement+1; /* horizontal domino right of top */
246 set[n++] = placement+2*w; /* vertical domino below */
248 set[n++] = placement-2+2*w+1;/* horizontal domino left of bottom */
250 set[n++] = placement+2*w+1;/* horizontal domino right of bottom */
257 * Returns 0, 1 or 2 for number of solutions. 2 means `any number
258 * more than one', or more accurately `we were unable to prove
259 * there was only one'.
261 * Outputs in a `placements' array, indexed the same way as the one
262 * within this function (see below); entries in there are <0 for a
263 * placement ruled out, 0 for an uncertain placement, and 1 for a
266 static int solver(int w, int h, int n, int *grid, int *output)
268 int wh = w*h, dc = DCOUNT(n);
269 int *placements, *heads;
273 * This array has one entry for every possible domino
274 * placement. Vertical placements are indexed by their top
275 * half, at (y*w+x)*2; horizontal placements are indexed by
276 * their left half at (y*w+x)*2+1.
278 * This array is used to link domino placements together into
279 * linked lists, so that we can track all the possible
280 * placements of each different domino. It's also used as a
281 * quick means of looking up an individual placement to see
282 * whether we still think it's possible. Actual values stored
283 * in this array are -2 (placement not possible at all), -1
284 * (end of list), or the array index of the next item.
286 * Oh, and -3 for `not even valid', used for array indices
287 * which don't even represent a plausible placement.
289 placements = snewn(2*wh, int);
290 for (i = 0; i < 2*wh; i++)
291 placements[i] = -3; /* not even valid */
294 * This array has one entry for every domino, and it is an
295 * index into `placements' denoting the head of the placement
296 * list for that domino.
298 heads = snewn(dc, int);
299 for (i = 0; i < dc; i++)
303 * Set up the initial possibility lists by scanning the grid.
305 for (y = 0; y < h-1; y++)
306 for (x = 0; x < w; x++) {
307 int di = DINDEX(grid[y*w+x], grid[(y+1)*w+x]);
308 placements[(y*w+x)*2] = heads[di];
309 heads[di] = (y*w+x)*2;
311 for (y = 0; y < h; y++)
312 for (x = 0; x < w-1; x++) {
313 int di = DINDEX(grid[y*w+x], grid[y*w+(x+1)]);
314 placements[(y*w+x)*2+1] = heads[di];
315 heads[di] = (y*w+x)*2+1;
318 #ifdef SOLVER_DIAGNOSTICS
319 printf("before solver:\n");
320 for (i = 0; i <= n; i++)
321 for (j = 0; j <= i; j++) {
324 printf("%2d [%d %d]:", DINDEX(i, j), i, j);
325 for (k = heads[DINDEX(i,j)]; k >= 0; k = placements[k])
326 printf(" %3d [%d,%d,%c]", k, k/2%w, k/2/w, k%2?'h':'v');
332 int done_something = FALSE;
335 * For each domino, look at its possible placements, and
336 * for each placement consider the placements (of any
337 * domino) it overlaps. Any placement overlapped by all
338 * placements of this domino can be ruled out.
340 * Each domino placement overlaps only six others, so we
341 * need not do serious set theory to work this out.
343 for (i = 0; i < dc; i++) {
344 int permset[6], permlen = 0, p;
347 if (heads[i] == -1) { /* no placement for this domino */
348 ret = 0; /* therefore puzzle is impossible */
351 for (j = heads[i]; j >= 0; j = placements[j]) {
352 assert(placements[j] != -2);
355 permlen = find_overlaps(w, h, j, permset);
357 int tempset[6], templen, m, n, k;
359 templen = find_overlaps(w, h, j, tempset);
362 * Pathetically primitive set intersection
363 * algorithm, which I'm only getting away with
364 * because I know my sets are bounded by a very
367 for (m = n = 0; m < permlen; m++) {
368 for (k = 0; k < templen; k++)
369 if (tempset[k] == permset[m])
372 permset[n++] = permset[m];
377 for (p = 0; p < permlen; p++) {
379 if (placements[j] != -2) {
382 done_something = TRUE;
385 * Rule out this placement. First find what
389 p2 = (j & 1) ? p1 + 1 : p1 + w;
390 di = DINDEX(grid[p1], grid[p2]);
391 #ifdef SOLVER_DIAGNOSTICS
392 printf("considering domino %d: ruling out placement %d"
393 " for %d\n", i, j, di);
397 * ... then walk that domino's placement list,
398 * removing this placement when we find it.
401 heads[di] = placements[j];
404 while (placements[k] != -1 && placements[k] != j)
406 assert(placements[k] == j);
407 placements[k] = placements[j];
415 * For each square, look at the available placements
416 * involving that square. If all of them are for the same
417 * domino, then rule out any placements for that domino
418 * _not_ involving this square.
420 for (i = 0; i < wh; i++) {
421 int list[4], k, n, adi;
428 list[j++] = 2*(i-1)+1;
436 for (n = k = 0; k < j; k++)
437 if (placements[list[k]] >= -1)
442 for (j = 0; j < n; j++) {
447 p2 = (k & 1) ? p1 + 1 : p1 + w;
448 di = DINDEX(grid[p1], grid[p2]);
461 * We've found something. All viable placements
462 * involving this square are for domino `adi'. If
463 * the current placement list for that domino is
464 * longer than n, reduce it to precisely this
465 * placement list and we've done something.
468 for (k = heads[adi]; k >= 0; k = placements[k])
471 done_something = TRUE;
472 #ifdef SOLVER_DIAGNOSTICS
473 printf("considering square %d,%d: reducing placements "
474 "of domino %d\n", x, y, adi);
477 * Set all other placements on the list to
482 int tmp = placements[k];
487 * Set up the new list.
489 heads[adi] = list[0];
490 for (k = 0; k < n; k++)
491 placements[list[k]] = (k+1 == n ? -1 : list[k+1]);
500 #ifdef SOLVER_DIAGNOSTICS
501 printf("after solver:\n");
502 for (i = 0; i <= n; i++)
503 for (j = 0; j <= i; j++) {
506 printf("%2d [%d %d]:", DINDEX(i, j), i, j);
507 for (k = heads[DINDEX(i,j)]; k >= 0; k = placements[k])
508 printf(" %3d [%d,%d,%c]", k, k/2%w, k/2/w, k%2?'h':'v');
514 for (i = 0; i < wh*2; i++) {
515 if (placements[i] == -2) {
517 output[i] = -1; /* ruled out */
518 } else if (placements[i] != -3) {
522 p2 = (i & 1) ? p1 + 1 : p1 + w;
523 di = DINDEX(grid[p1], grid[p2]);
525 if (i == heads[di] && placements[i] == -1) {
527 output[i] = 1; /* certain */
530 output[i] = 0; /* uncertain */
546 /* ----------------------------------------------------------------------
547 * End of solver code.
550 static char *new_game_desc(const game_params *params, random_state *rs,
551 char **aux, int interactive)
553 int n = params->n, w = n+2, h = n+1, wh = w*h;
554 int *grid, *grid2, *list;
559 * Allocate space in which to lay the grid out.
561 grid = snewn(wh, int);
562 grid2 = snewn(wh, int);
563 list = snewn(2*wh, int);
566 * I haven't been able to think of any particularly clever
567 * techniques for generating instances of Dominosa with a
568 * unique solution. Many of the deductions used in this puzzle
569 * are based on information involving half the grid at a time
570 * (`of all the 6s, exactly one is next to a 3'), so a strategy
571 * of partially solving the grid and then perturbing the place
572 * where the solver got stuck seems particularly likely to
573 * accidentally destroy the information which the solver had
574 * used in getting that far. (Contrast with, say, Mines, in
575 * which most deductions are local so this is an excellent
578 * Therefore I resort to the basest of brute force methods:
579 * generate a random grid, see if it's solvable, throw it away
580 * and try again if not. My only concession to sophistication
581 * and cleverness is to at least _try_ not to generate obvious
582 * 2x2 ambiguous sections (see comment below in the domino-
585 * During tests performed on 2005-07-15, I found that the brute
586 * force approach without that tweak had to throw away about 87
587 * grids on average (at the default n=6) before finding a
588 * unique one, or a staggering 379 at n=9; good job the
589 * generator and solver are fast! When I added the
590 * ambiguous-section avoidance, those numbers came down to 19
591 * and 26 respectively, which is a lot more sensible.
595 domino_layout_prealloc(w, h, rs, grid, grid2, list);
598 * Now we have a complete layout covering the whole
599 * rectangle with dominoes. So shuffle the actual domino
600 * values and fill the rectangle with numbers.
603 for (i = 0; i <= params->n; i++)
604 for (j = 0; j <= i; j++) {
608 shuffle(list, k/2, 2*sizeof(*list), rs);
610 for (i = 0; i < wh; i++)
612 /* Optionally flip the domino round. */
615 if (params->unique) {
618 * If we're after a unique solution, we can do
619 * something here to improve the chances. If
620 * we're placing a domino so that it forms a
621 * 2x2 rectangle with one we've already placed,
622 * and if that domino and this one share a
623 * number, we can try not to put them so that
624 * the identical numbers are diagonally
625 * separated, because that automatically causes
636 if (t2 == t1 + w) { /* this domino is vertical */
637 if (t1 % w > 0 &&/* and not on the left hand edge */
638 grid[t1-1] == t2-1 &&/* alongside one to left */
639 (grid2[t1-1] == list[j] || /* and has a number */
640 grid2[t1-1] == list[j+1] || /* in common */
641 grid2[t2-1] == list[j] ||
642 grid2[t2-1] == list[j+1])) {
643 if (grid2[t1-1] == list[j] ||
644 grid2[t2-1] == list[j+1])
649 } else { /* this domino is horizontal */
650 if (t1 / w > 0 &&/* and not on the top edge */
651 grid[t1-w] == t2-w &&/* alongside one above */
652 (grid2[t1-w] == list[j] || /* and has a number */
653 grid2[t1-w] == list[j+1] || /* in common */
654 grid2[t2-w] == list[j] ||
655 grid2[t2-w] == list[j+1])) {
656 if (grid2[t1-w] == list[j] ||
657 grid2[t2-w] == list[j+1])
666 flip = random_upto(rs, 2);
668 grid2[i] = list[j + flip];
669 grid2[grid[i]] = list[j + 1 - flip];
673 } while (params->unique && solver(w, h, n, grid2, NULL) > 1);
675 #ifdef GENERATION_DIAGNOSTICS
676 for (j = 0; j < h; j++) {
677 for (i = 0; i < w; i++) {
678 putchar('0' + grid2[j*w+i]);
686 * Encode the resulting game state.
688 * Our encoding is a string of digits. Any number greater than
689 * 9 is represented by a decimal integer within square
690 * brackets. We know there are n+2 of every number (it's paired
691 * with each number from 0 to n inclusive, and one of those is
692 * itself so that adds another occurrence), so we can work out
693 * the string length in advance.
697 * To work out the total length of the decimal encodings of all
698 * the numbers from 0 to n inclusive:
699 * - every number has a units digit; total is n+1.
700 * - all numbers above 9 have a tens digit; total is max(n+1-10,0).
701 * - all numbers above 99 have a hundreds digit; total is max(n+1-100,0).
705 for (i = 10; i <= n; i *= 10)
706 len += max(n + 1 - i, 0);
707 /* Now add two square brackets for each number above 9. */
708 len += 2 * max(n + 1 - 10, 0);
709 /* And multiply by n+2 for the repeated occurrences of each number. */
713 * Now actually encode the string.
715 ret = snewn(len+1, char);
717 for (i = 0; i < wh; i++) {
722 j += sprintf(ret+j, "[%d]", k);
729 * Encode the solved state as an aux_info.
732 char *auxinfo = snewn(wh+1, char);
734 for (i = 0; i < wh; i++) {
736 auxinfo[i] = (v == i+1 ? 'L' : v == i-1 ? 'R' :
737 v == i+w ? 'T' : v == i-w ? 'B' : '.');
751 static char *validate_desc(const game_params *params, const char *desc)
753 int n = params->n, w = n+2, h = n+1, wh = w*h;
759 occurrences = snewn(n+1, int);
760 for (i = 0; i <= n; i++)
763 for (i = 0; i < wh; i++) {
765 ret = ret ? ret : "Game description is too short";
767 if (*desc >= '0' && *desc <= '9')
769 else if (*desc == '[') {
772 while (*desc && isdigit((unsigned char)*desc)) desc++;
774 ret = ret ? ret : "Missing ']' in game description";
779 ret = ret ? ret : "Invalid syntax in game description";
782 ret = ret ? ret : "Number out of range in game description";
789 ret = ret ? ret : "Game description is too long";
792 for (i = 0; i <= n; i++)
793 if (occurrences[i] != n+2)
794 ret = "Incorrect number balance in game description";
802 static game_state *new_game(midend *me, const game_params *params,
805 int n = params->n, w = n+2, h = n+1, wh = w*h;
806 game_state *state = snew(game_state);
809 state->params = *params;
813 state->grid = snewn(wh, int);
814 for (i = 0; i < wh; i++)
817 state->edges = snewn(wh, unsigned short);
818 for (i = 0; i < wh; i++)
821 state->numbers = snew(struct game_numbers);
822 state->numbers->refcount = 1;
823 state->numbers->numbers = snewn(wh, int);
825 for (i = 0; i < wh; i++) {
827 if (*desc >= '0' && *desc <= '9')
830 assert(*desc == '[');
833 while (*desc && isdigit((unsigned char)*desc)) desc++;
834 assert(*desc == ']');
837 assert(j >= 0 && j <= n);
838 state->numbers->numbers[i] = j;
841 state->completed = state->cheated = FALSE;
846 static game_state *dup_game(const game_state *state)
848 int n = state->params.n, w = n+2, h = n+1, wh = w*h;
849 game_state *ret = snew(game_state);
851 ret->params = state->params;
854 ret->grid = snewn(wh, int);
855 memcpy(ret->grid, state->grid, wh * sizeof(int));
856 ret->edges = snewn(wh, unsigned short);
857 memcpy(ret->edges, state->edges, wh * sizeof(unsigned short));
858 ret->numbers = state->numbers;
859 ret->numbers->refcount++;
860 ret->completed = state->completed;
861 ret->cheated = state->cheated;
866 static void free_game(game_state *state)
870 if (--state->numbers->refcount <= 0) {
871 sfree(state->numbers->numbers);
872 sfree(state->numbers);
877 static char *solve_game(const game_state *state, const game_state *currstate,
878 const char *aux, char **error)
880 int n = state->params.n, w = n+2, h = n+1, wh = w*h;
890 ret = snewn(retsize, char);
891 retlen = sprintf(ret, "S");
893 for (i = 0; i < wh; i++) {
895 extra = sprintf(buf, ";D%d,%d", i, i+1);
896 else if (aux[i] == 'T')
897 extra = sprintf(buf, ";D%d,%d", i, i+w);
901 if (retlen + extra + 1 >= retsize) {
902 retsize = retlen + extra + 256;
903 ret = sresize(ret, retsize, char);
905 strcpy(ret + retlen, buf);
911 placements = snewn(wh*2, int);
912 for (i = 0; i < wh*2; i++)
914 solver(w, h, n, state->numbers->numbers, placements);
917 * First make a pass putting in edges for -1, then make a pass
918 * putting in dominoes for +1.
921 ret = snewn(retsize, char);
922 retlen = sprintf(ret, "S");
924 for (v = -1; v <= +1; v += 2)
925 for (i = 0; i < wh*2; i++)
926 if (placements[i] == v) {
928 int p2 = (i & 1) ? p1+1 : p1+w;
930 extra = sprintf(buf, ";%c%d,%d",
931 (int)(v==-1 ? 'E' : 'D'), p1, p2);
933 if (retlen + extra + 1 >= retsize) {
934 retsize = retlen + extra + 256;
935 ret = sresize(ret, retsize, char);
937 strcpy(ret + retlen, buf);
947 static int game_can_format_as_text_now(const game_params *params)
949 return params->n < 1000;
952 static void draw_domino(char *board, int start, char corner,
953 int dshort, int nshort, char cshort,
954 int dlong, int nlong, char clong)
956 int go_short = nshort*dshort, go_long = nlong*dlong, i;
958 board[start] = corner;
959 board[start + go_short] = corner;
960 board[start + go_long] = corner;
961 board[start + go_short + go_long] = corner;
963 for (i = 1; i < nshort; ++i) {
964 int j = start + i*dshort, k = start + i*dshort + go_long;
965 if (board[j] != corner) board[j] = cshort;
966 if (board[k] != corner) board[k] = cshort;
969 for (i = 1; i < nlong; ++i) {
970 int j = start + i*dlong, k = start + i*dlong + go_short;
971 if (board[j] != corner) board[j] = clong;
972 if (board[k] != corner) board[k] = clong;
976 static char *game_text_format(const game_state *state)
978 int w = state->w, h = state->h, r, c;
979 int cw = 4, ch = 2, gw = cw*w + 2, gh = ch * h + 1, len = gw * gh;
980 char *board = snewn(len + 1, char);
982 memset(board, ' ', len);
984 for (r = 0; r < h; ++r) {
985 for (c = 0; c < w; ++c) {
986 int cell = r*ch*gw + cw*c, center = cell + gw*ch/2 + cw/2;
987 int i = r*w + c, num = state->numbers->numbers[i];
990 board[center] = '0' + num % 10;
991 if (num >= 10) board[center - 1] = '0' + num / 10;
993 board[center+1] = '0' + num % 10;
994 board[center] = '0' + num / 10 % 10;
995 board[center-1] = '0' + num / 100;
998 if (state->edges[i] & EDGE_L) board[center - cw/2] = '|';
999 if (state->edges[i] & EDGE_R) board[center + cw/2] = '|';
1000 if (state->edges[i] & EDGE_T) board[center - gw] = '-';
1001 if (state->edges[i] & EDGE_B) board[center + gw] = '-';
1003 if (state->grid[i] == i) continue; /* no domino pairing */
1004 if (state->grid[i] < i) continue; /* already done */
1005 assert (state->grid[i] == i + 1 || state->grid[i] == i + w);
1006 if (state->grid[i] == i + 1)
1007 draw_domino(board, cell, '+', gw, ch, '|', +1, 2*cw, '-');
1008 else if (state->grid[i] == i + w)
1009 draw_domino(board, cell, '+', +1, cw, '-', gw, 2*ch, '|');
1011 board[r*ch*gw + gw - 1] = '\n';
1012 board[r*ch*gw + gw + gw - 1] = '\n';
1014 board[len - 1] = '\n';
1020 int cur_x, cur_y, cur_visible, highlight_1, highlight_2;
1023 static game_ui *new_ui(const game_state *state)
1025 game_ui *ui = snew(game_ui);
1026 ui->cur_x = ui->cur_y = 0;
1027 ui->cur_visible = 0;
1028 ui->highlight_1 = ui->highlight_2 = -1;
1032 static void free_ui(game_ui *ui)
1037 static char *encode_ui(const game_ui *ui)
1042 static void decode_ui(game_ui *ui, const char *encoding)
1046 static void game_changed_state(game_ui *ui, const game_state *oldstate,
1047 const game_state *newstate)
1049 if (!oldstate->completed && newstate->completed)
1050 ui->cur_visible = 0;
1053 #define PREFERRED_TILESIZE 32
1054 #define TILESIZE (ds->tilesize)
1055 #define BORDER (TILESIZE * 3 / 4)
1056 #define DOMINO_GUTTER (TILESIZE / 16)
1057 #define DOMINO_RADIUS (TILESIZE / 8)
1058 #define DOMINO_COFFSET (DOMINO_GUTTER + DOMINO_RADIUS)
1059 #define CURSOR_RADIUS (TILESIZE / 4)
1061 #define COORD(x) ( (x) * TILESIZE + BORDER )
1062 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1064 struct game_drawstate {
1067 unsigned long *visible;
1070 static char *interpret_move(const game_state *state, game_ui *ui,
1071 const game_drawstate *ds,
1072 int x, int y, int button)
1074 int w = state->w, h = state->h;
1078 * A left-click between two numbers toggles a domino covering
1079 * them. A right-click toggles an edge.
1081 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
1082 int tx = FROMCOORD(x), ty = FROMCOORD(y), t = ty*w+tx;
1086 if (tx < 0 || tx >= w || ty < 0 || ty >= h)
1090 * Now we know which square the click was in, decide which
1091 * edge of the square it was closest to.
1093 dx = 2 * (x - COORD(tx)) - TILESIZE;
1094 dy = 2 * (y - COORD(ty)) - TILESIZE;
1096 if (abs(dx) > abs(dy) && dx < 0 && tx > 0)
1097 d1 = t - 1, d2 = t; /* clicked in right side of domino */
1098 else if (abs(dx) > abs(dy) && dx > 0 && tx+1 < w)
1099 d1 = t, d2 = t + 1; /* clicked in left side of domino */
1100 else if (abs(dy) > abs(dx) && dy < 0 && ty > 0)
1101 d1 = t - w, d2 = t; /* clicked in bottom half of domino */
1102 else if (abs(dy) > abs(dx) && dy > 0 && ty+1 < h)
1103 d1 = t, d2 = t + w; /* clicked in top half of domino */
1108 * We can't mark an edge next to any domino.
1110 if (button == RIGHT_BUTTON &&
1111 (state->grid[d1] != d1 || state->grid[d2] != d2))
1114 ui->cur_visible = 0;
1115 sprintf(buf, "%c%d,%d", (int)(button == RIGHT_BUTTON ? 'E' : 'D'), d1, d2);
1117 } else if (IS_CURSOR_MOVE(button)) {
1118 ui->cur_visible = 1;
1120 move_cursor(button, &ui->cur_x, &ui->cur_y, 2*w-1, 2*h-1, 0);
1123 } else if (IS_CURSOR_SELECT(button)) {
1126 if (!((ui->cur_x ^ ui->cur_y) & 1))
1127 return NULL; /* must have exactly one dimension odd */
1128 d1 = (ui->cur_y / 2) * w + (ui->cur_x / 2);
1129 d2 = ((ui->cur_y+1) / 2) * w + ((ui->cur_x+1) / 2);
1132 * We can't mark an edge next to any domino.
1134 if (button == CURSOR_SELECT2 &&
1135 (state->grid[d1] != d1 || state->grid[d2] != d2))
1138 sprintf(buf, "%c%d,%d", (int)(button == CURSOR_SELECT2 ? 'E' : 'D'), d1, d2);
1140 } else if (isdigit(button)) {
1141 int n = state->params.n, num = button - '0';
1144 } else if (ui->highlight_1 == num) {
1145 ui->highlight_1 = -1;
1146 } else if (ui->highlight_2 == num) {
1147 ui->highlight_2 = -1;
1148 } else if (ui->highlight_1 == -1) {
1149 ui->highlight_1 = num;
1150 } else if (ui->highlight_2 == -1) {
1151 ui->highlight_2 = num;
1161 static game_state *execute_move(const game_state *state, const char *move)
1163 int n = state->params.n, w = n+2, h = n+1, wh = w*h;
1165 game_state *ret = dup_game(state);
1168 if (move[0] == 'S') {
1171 ret->cheated = TRUE;
1174 * Clear the existing edges and domino placements. We
1175 * expect the S to be followed by other commands.
1177 for (i = 0; i < wh; i++) {
1182 } else if (move[0] == 'D' &&
1183 sscanf(move+1, "%d,%d%n", &d1, &d2, &p) == 2 &&
1184 d1 >= 0 && d1 < wh && d2 >= 0 && d2 < wh && d1 < d2) {
1187 * Toggle domino presence between d1 and d2.
1189 if (ret->grid[d1] == d2) {
1190 assert(ret->grid[d2] == d1);
1195 * Erase any dominoes that might overlap the new one.
1204 * Place the new one.
1210 * Destroy any edges lurking around it.
1212 if (ret->edges[d1] & EDGE_L) {
1213 assert(d1 - 1 >= 0);
1214 ret->edges[d1 - 1] &= ~EDGE_R;
1216 if (ret->edges[d1] & EDGE_R) {
1217 assert(d1 + 1 < wh);
1218 ret->edges[d1 + 1] &= ~EDGE_L;
1220 if (ret->edges[d1] & EDGE_T) {
1221 assert(d1 - w >= 0);
1222 ret->edges[d1 - w] &= ~EDGE_B;
1224 if (ret->edges[d1] & EDGE_B) {
1225 assert(d1 + 1 < wh);
1226 ret->edges[d1 + w] &= ~EDGE_T;
1229 if (ret->edges[d2] & EDGE_L) {
1230 assert(d2 - 1 >= 0);
1231 ret->edges[d2 - 1] &= ~EDGE_R;
1233 if (ret->edges[d2] & EDGE_R) {
1234 assert(d2 + 1 < wh);
1235 ret->edges[d2 + 1] &= ~EDGE_L;
1237 if (ret->edges[d2] & EDGE_T) {
1238 assert(d2 - w >= 0);
1239 ret->edges[d2 - w] &= ~EDGE_B;
1241 if (ret->edges[d2] & EDGE_B) {
1242 assert(d2 + 1 < wh);
1243 ret->edges[d2 + w] &= ~EDGE_T;
1249 } else if (move[0] == 'E' &&
1250 sscanf(move+1, "%d,%d%n", &d1, &d2, &p) == 2 &&
1251 d1 >= 0 && d1 < wh && d2 >= 0 && d2 < wh && d1 < d2 &&
1252 ret->grid[d1] == d1 && ret->grid[d2] == d2) {
1255 * Toggle edge presence between d1 and d2.
1258 ret->edges[d1] ^= EDGE_R;
1259 ret->edges[d2] ^= EDGE_L;
1261 ret->edges[d1] ^= EDGE_B;
1262 ret->edges[d2] ^= EDGE_T;
1281 * After modifying the grid, check completion.
1283 if (!ret->completed) {
1285 unsigned char *used = snewn(TRI(n+1), unsigned char);
1287 memset(used, 0, TRI(n+1));
1288 for (i = 0; i < wh; i++)
1289 if (ret->grid[i] > i) {
1292 n1 = ret->numbers->numbers[i];
1293 n2 = ret->numbers->numbers[ret->grid[i]];
1295 di = DINDEX(n1, n2);
1296 assert(di >= 0 && di < TRI(n+1));
1305 if (ok == DCOUNT(n))
1306 ret->completed = TRUE;
1312 /* ----------------------------------------------------------------------
1316 static void game_compute_size(const game_params *params, int tilesize,
1319 int n = params->n, w = n+2, h = n+1;
1321 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1322 struct { int tilesize; } ads, *ds = &ads;
1323 ads.tilesize = tilesize;
1325 *x = w * TILESIZE + 2*BORDER;
1326 *y = h * TILESIZE + 2*BORDER;
1329 static void game_set_size(drawing *dr, game_drawstate *ds,
1330 const game_params *params, int tilesize)
1332 ds->tilesize = tilesize;
1335 static float *game_colours(frontend *fe, int *ncolours)
1337 float *ret = snewn(3 * NCOLOURS, float);
1339 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1341 ret[COL_TEXT * 3 + 0] = 0.0F;
1342 ret[COL_TEXT * 3 + 1] = 0.0F;
1343 ret[COL_TEXT * 3 + 2] = 0.0F;
1345 ret[COL_DOMINO * 3 + 0] = 0.0F;
1346 ret[COL_DOMINO * 3 + 1] = 0.0F;
1347 ret[COL_DOMINO * 3 + 2] = 0.0F;
1349 ret[COL_DOMINOCLASH * 3 + 0] = 0.5F;
1350 ret[COL_DOMINOCLASH * 3 + 1] = 0.0F;
1351 ret[COL_DOMINOCLASH * 3 + 2] = 0.0F;
1353 ret[COL_DOMINOTEXT * 3 + 0] = 1.0F;
1354 ret[COL_DOMINOTEXT * 3 + 1] = 1.0F;
1355 ret[COL_DOMINOTEXT * 3 + 2] = 1.0F;
1357 ret[COL_EDGE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2 / 3;
1358 ret[COL_EDGE * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2 / 3;
1359 ret[COL_EDGE * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2 / 3;
1361 ret[COL_HIGHLIGHT_1 * 3 + 0] = 0.85;
1362 ret[COL_HIGHLIGHT_1 * 3 + 1] = 0.20;
1363 ret[COL_HIGHLIGHT_1 * 3 + 2] = 0.20;
1365 ret[COL_HIGHLIGHT_2 * 3 + 0] = 0.30;
1366 ret[COL_HIGHLIGHT_2 * 3 + 1] = 0.85;
1367 ret[COL_HIGHLIGHT_2 * 3 + 2] = 0.20;
1369 *ncolours = NCOLOURS;
1373 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1375 struct game_drawstate *ds = snew(struct game_drawstate);
1378 ds->started = FALSE;
1381 ds->visible = snewn(ds->w * ds->h, unsigned long);
1382 ds->tilesize = 0; /* not decided yet */
1383 for (i = 0; i < ds->w * ds->h; i++)
1384 ds->visible[i] = 0xFFFF;
1389 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1404 /* These flags must be disjoint with:
1405 * the above enum (TYPE_*) [0x000 -- 0x00F]
1406 * EDGE_* [0x100 -- 0xF00]
1407 * and must fit into an unsigned long (32 bits).
1409 #define DF_HIGHLIGHT_1 0x10
1410 #define DF_HIGHLIGHT_2 0x20
1411 #define DF_FLASH 0x40
1412 #define DF_CLASH 0x80
1414 #define DF_CURSOR 0x01000
1415 #define DF_CURSOR_USEFUL 0x02000
1416 #define DF_CURSOR_XBASE 0x10000
1417 #define DF_CURSOR_XMASK 0x30000
1418 #define DF_CURSOR_YBASE 0x40000
1419 #define DF_CURSOR_YMASK 0xC0000
1421 #define CEDGE_OFF (TILESIZE / 8)
1422 #define IS_EMPTY(s,x,y) ((s)->grid[(y)*(s)->w+(x)] == ((y)*(s)->w+(x)))
1424 static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state,
1425 int x, int y, int type, int highlight_1, int highlight_2)
1427 int w = state->w /*, h = state->h */;
1428 int cx = COORD(x), cy = COORD(y);
1433 clip(dr, cx, cy, TILESIZE, TILESIZE);
1434 draw_rect(dr, cx, cy, TILESIZE, TILESIZE, COL_BACKGROUND);
1436 flags = type &~ TYPE_MASK;
1439 if (type != TYPE_BLANK) {
1443 * Draw one end of a domino. This is composed of:
1445 * - two filled circles (rounded corners)
1447 * - a slight shift in the number
1450 if (flags & DF_CLASH)
1451 bg = COL_DOMINOCLASH;
1454 nc = COL_DOMINOTEXT;
1456 if (flags & DF_FLASH) {
1462 if (type == TYPE_L || type == TYPE_T)
1463 draw_circle(dr, cx+DOMINO_COFFSET, cy+DOMINO_COFFSET,
1464 DOMINO_RADIUS, bg, bg);
1465 if (type == TYPE_R || type == TYPE_T)
1466 draw_circle(dr, cx+TILESIZE-1-DOMINO_COFFSET, cy+DOMINO_COFFSET,
1467 DOMINO_RADIUS, bg, bg);
1468 if (type == TYPE_L || type == TYPE_B)
1469 draw_circle(dr, cx+DOMINO_COFFSET, cy+TILESIZE-1-DOMINO_COFFSET,
1470 DOMINO_RADIUS, bg, bg);
1471 if (type == TYPE_R || type == TYPE_B)
1472 draw_circle(dr, cx+TILESIZE-1-DOMINO_COFFSET,
1473 cy+TILESIZE-1-DOMINO_COFFSET,
1474 DOMINO_RADIUS, bg, bg);
1476 for (i = 0; i < 2; i++) {
1479 x1 = cx + (i ? DOMINO_GUTTER : DOMINO_COFFSET);
1480 y1 = cy + (i ? DOMINO_COFFSET : DOMINO_GUTTER);
1481 x2 = cx + TILESIZE-1 - (i ? DOMINO_GUTTER : DOMINO_COFFSET);
1482 y2 = cy + TILESIZE-1 - (i ? DOMINO_COFFSET : DOMINO_GUTTER);
1484 x2 = cx + TILESIZE + TILESIZE/16;
1485 else if (type == TYPE_R)
1486 x1 = cx - TILESIZE/16;
1487 else if (type == TYPE_T)
1488 y2 = cy + TILESIZE + TILESIZE/16;
1489 else if (type == TYPE_B)
1490 y1 = cy - TILESIZE/16;
1492 draw_rect(dr, x1, y1, x2-x1+1, y2-y1+1, bg);
1496 draw_rect(dr, cx+DOMINO_GUTTER, cy,
1497 TILESIZE-2*DOMINO_GUTTER, 1, COL_EDGE);
1499 draw_rect(dr, cx+DOMINO_GUTTER, cy+TILESIZE-1,
1500 TILESIZE-2*DOMINO_GUTTER, 1, COL_EDGE);
1502 draw_rect(dr, cx, cy+DOMINO_GUTTER,
1503 1, TILESIZE-2*DOMINO_GUTTER, COL_EDGE);
1505 draw_rect(dr, cx+TILESIZE-1, cy+DOMINO_GUTTER,
1506 1, TILESIZE-2*DOMINO_GUTTER, COL_EDGE);
1510 if (flags & DF_CURSOR) {
1511 int curx = ((flags & DF_CURSOR_XMASK) / DF_CURSOR_XBASE) & 3;
1512 int cury = ((flags & DF_CURSOR_YMASK) / DF_CURSOR_YBASE) & 3;
1513 int ox = cx + curx*TILESIZE/2;
1514 int oy = cy + cury*TILESIZE/2;
1516 draw_rect_corners(dr, ox, oy, CURSOR_RADIUS, nc);
1517 if (flags & DF_CURSOR_USEFUL)
1518 draw_rect_corners(dr, ox, oy, CURSOR_RADIUS+1, nc);
1521 if (flags & DF_HIGHLIGHT_1) {
1522 nc = COL_HIGHLIGHT_1;
1523 } else if (flags & DF_HIGHLIGHT_2) {
1524 nc = COL_HIGHLIGHT_2;
1527 sprintf(str, "%d", state->numbers->numbers[y*w+x]);
1528 draw_text(dr, cx+TILESIZE/2, cy+TILESIZE/2, FONT_VARIABLE, TILESIZE/2,
1529 ALIGN_HCENTRE | ALIGN_VCENTRE, nc, str);
1531 draw_update(dr, cx, cy, TILESIZE, TILESIZE);
1535 static void game_redraw(drawing *dr, game_drawstate *ds,
1536 const game_state *oldstate, const game_state *state,
1537 int dir, const game_ui *ui,
1538 float animtime, float flashtime)
1540 int n = state->params.n, w = state->w, h = state->h, wh = w*h;
1542 unsigned char *used;
1546 game_compute_size(&state->params, TILESIZE, &pw, &ph);
1547 draw_rect(dr, 0, 0, pw, ph, COL_BACKGROUND);
1548 draw_update(dr, 0, 0, pw, ph);
1553 * See how many dominoes of each type there are, so we can
1554 * highlight clashes in red.
1556 used = snewn(TRI(n+1), unsigned char);
1557 memset(used, 0, TRI(n+1));
1558 for (i = 0; i < wh; i++)
1559 if (state->grid[i] > i) {
1562 n1 = state->numbers->numbers[i];
1563 n2 = state->numbers->numbers[state->grid[i]];
1565 di = DINDEX(n1, n2);
1566 assert(di >= 0 && di < TRI(n+1));
1572 for (y = 0; y < h; y++)
1573 for (x = 0; x < w; x++) {
1578 if (state->grid[n] == n-1)
1580 else if (state->grid[n] == n+1)
1582 else if (state->grid[n] == n-w)
1584 else if (state->grid[n] == n+w)
1589 n1 = state->numbers->numbers[n];
1590 if (c != TYPE_BLANK) {
1591 n2 = state->numbers->numbers[state->grid[n]];
1592 di = DINDEX(n1, n2);
1594 c |= DF_CLASH; /* highlight a clash */
1596 c |= state->edges[n];
1599 if (n1 == ui->highlight_1)
1600 c |= DF_HIGHLIGHT_1;
1601 if (n1 == ui->highlight_2)
1602 c |= DF_HIGHLIGHT_2;
1605 c |= DF_FLASH; /* we're flashing */
1607 if (ui->cur_visible) {
1608 unsigned curx = (unsigned)(ui->cur_x - (2*x-1));
1609 unsigned cury = (unsigned)(ui->cur_y - (2*y-1));
1610 if (curx < 3 && cury < 3) {
1612 (curx * DF_CURSOR_XBASE) |
1613 (cury * DF_CURSOR_YBASE));
1614 if ((ui->cur_x ^ ui->cur_y) & 1)
1615 c |= DF_CURSOR_USEFUL;
1619 if (ds->visible[n] != c) {
1620 draw_tile(dr, ds, state, x, y, c,
1621 ui->highlight_1, ui->highlight_2);
1629 static float game_anim_length(const game_state *oldstate,
1630 const game_state *newstate, int dir, game_ui *ui)
1635 static float game_flash_length(const game_state *oldstate,
1636 const game_state *newstate, int dir, game_ui *ui)
1638 if (!oldstate->completed && newstate->completed &&
1639 !oldstate->cheated && !newstate->cheated)
1641 ui->highlight_1 = ui->highlight_2 = -1;
1647 static int game_status(const game_state *state)
1649 return state->completed ? +1 : 0;
1652 static int game_timing_state(const game_state *state, game_ui *ui)
1657 static void game_print_size(const game_params *params, float *x, float *y)
1662 * I'll use 6mm squares by default.
1664 game_compute_size(params, 600, &pw, &ph);
1669 static void game_print(drawing *dr, const game_state *state, int tilesize)
1671 int w = state->w, h = state->h;
1674 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1675 game_drawstate ads, *ds = &ads;
1676 game_set_size(dr, ds, NULL, tilesize);
1678 c = print_mono_colour(dr, 1); assert(c == COL_BACKGROUND);
1679 c = print_mono_colour(dr, 0); assert(c == COL_TEXT);
1680 c = print_mono_colour(dr, 0); assert(c == COL_DOMINO);
1681 c = print_mono_colour(dr, 0); assert(c == COL_DOMINOCLASH);
1682 c = print_mono_colour(dr, 1); assert(c == COL_DOMINOTEXT);
1683 c = print_mono_colour(dr, 0); assert(c == COL_EDGE);
1685 for (y = 0; y < h; y++)
1686 for (x = 0; x < w; x++) {
1690 if (state->grid[n] == n-1)
1692 else if (state->grid[n] == n+1)
1694 else if (state->grid[n] == n-w)
1696 else if (state->grid[n] == n+w)
1701 draw_tile(dr, ds, state, x, y, c, -1, -1);
1706 #define thegame dominosa
1709 const struct game thegame = {
1710 "Dominosa", "games.dominosa", "dominosa",
1717 TRUE, game_configure, custom_params,
1725 TRUE, game_can_format_as_text_now, game_text_format,
1733 PREFERRED_TILESIZE, game_compute_size, game_set_size,
1736 game_free_drawstate,
1741 TRUE, FALSE, game_print_size, game_print,
1742 FALSE, /* wants_statusbar */
1743 FALSE, game_timing_state,
1747 /* vim: set shiftwidth=4 :set textwidth=80: */