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 * * perhaps set analysis
25 /* nth triangular number */
26 #define TRI(n) ( (n) * ((n) + 1) / 2 )
27 /* number of dominoes for value n */
28 #define DCOUNT(n) TRI((n)+1)
29 /* map a pair of numbers to a unique domino index from 0 upwards. */
30 #define DINDEX(n1,n2) ( TRI(max(n1,n2)) + min(n1,n2) )
32 #define FLASH_TIME 0.13F
51 int *numbers
; /* h x w */
62 struct game_numbers
*numbers
;
64 unsigned short *edges
; /* h x w */
65 int completed
, cheated
;
68 static game_params
*default_params(void)
70 game_params
*ret
= snew(game_params
);
78 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
88 default: return FALSE
;
91 sprintf(buf
, "Up to double-%d", n
);
94 *params
= ret
= snew(game_params
);
101 static void free_params(game_params
*params
)
106 static game_params
*dup_params(game_params
*params
)
108 game_params
*ret
= snew(game_params
);
109 *ret
= *params
; /* structure copy */
113 static void decode_params(game_params
*params
, char const *string
)
115 params
->n
= atoi(string
);
116 while (*string
&& isdigit((unsigned char)*string
)) string
++;
118 params
->unique
= FALSE
;
121 static char *encode_params(game_params
*params
, int full
)
124 sprintf(buf
, "%d", params
->n
);
125 if (full
&& !params
->unique
)
130 static config_item
*game_configure(game_params
*params
)
135 ret
= snewn(3, config_item
);
137 ret
[0].name
= "Maximum number on dominoes";
138 ret
[0].type
= C_STRING
;
139 sprintf(buf
, "%d", params
->n
);
140 ret
[0].sval
= dupstr(buf
);
143 ret
[1].name
= "Ensure unique solution";
144 ret
[1].type
= C_BOOLEAN
;
146 ret
[1].ival
= params
->unique
;
156 static game_params
*custom_params(config_item
*cfg
)
158 game_params
*ret
= snew(game_params
);
160 ret
->n
= atoi(cfg
[0].sval
);
161 ret
->unique
= cfg
[1].ival
;
166 static char *validate_params(game_params
*params
, int full
)
169 return "Maximum face number must be at least one";
173 /* ----------------------------------------------------------------------
177 static int find_overlaps(int w
, int h
, int placement
, int *set
)
181 n
= 0; /* number of returned placements */
189 * Horizontal domino, indexed by its left end.
192 set
[n
++] = placement
-2; /* horizontal domino to the left */
194 set
[n
++] = placement
-2*w
-1;/* vertical domino above left side */
196 set
[n
++] = placement
-1; /* vertical domino below left side */
198 set
[n
++] = placement
+2; /* horizontal domino to the right */
200 set
[n
++] = placement
-2*w
+2-1;/* vertical domino above right side */
202 set
[n
++] = placement
+2-1; /* vertical domino below right side */
205 * Vertical domino, indexed by its top end.
208 set
[n
++] = placement
-2*w
; /* vertical domino above */
210 set
[n
++] = placement
-2+1; /* horizontal domino left of top */
212 set
[n
++] = placement
+1; /* horizontal domino right of top */
214 set
[n
++] = placement
+2*w
; /* vertical domino below */
216 set
[n
++] = placement
-2+2*w
+1;/* horizontal domino left of bottom */
218 set
[n
++] = placement
+2*w
+1;/* horizontal domino right of bottom */
225 * Returns 0, 1 or 2 for number of solutions. 2 means `any number
226 * more than one', or more accurately `we were unable to prove
227 * there was only one'.
229 * Outputs in a `placements' array, indexed the same way as the one
230 * within this function (see below); entries in there are <0 for a
231 * placement ruled out, 0 for an uncertain placement, and 1 for a
234 static int solver(int w
, int h
, int n
, int *grid
, int *output
)
236 int wh
= w
*h
, dc
= DCOUNT(n
);
237 int *placements
, *heads
;
241 * This array has one entry for every possible domino
242 * placement. Vertical placements are indexed by their top
243 * half, at (y*w+x)*2; horizontal placements are indexed by
244 * their left half at (y*w+x)*2+1.
246 * This array is used to link domino placements together into
247 * linked lists, so that we can track all the possible
248 * placements of each different domino. It's also used as a
249 * quick means of looking up an individual placement to see
250 * whether we still think it's possible. Actual values stored
251 * in this array are -2 (placement not possible at all), -1
252 * (end of list), or the array index of the next item.
254 * Oh, and -3 for `not even valid', used for array indices
255 * which don't even represent a plausible placement.
257 placements
= snewn(2*wh
, int);
258 for (i
= 0; i
< 2*wh
; i
++)
259 placements
[i
] = -3; /* not even valid */
262 * This array has one entry for every domino, and it is an
263 * index into `placements' denoting the head of the placement
264 * list for that domino.
266 heads
= snewn(dc
, int);
267 for (i
= 0; i
< dc
; i
++)
271 * Set up the initial possibility lists by scanning the grid.
273 for (y
= 0; y
< h
-1; y
++)
274 for (x
= 0; x
< w
; x
++) {
275 int di
= DINDEX(grid
[y
*w
+x
], grid
[(y
+1)*w
+x
]);
276 placements
[(y
*w
+x
)*2] = heads
[di
];
277 heads
[di
] = (y
*w
+x
)*2;
279 for (y
= 0; y
< h
; y
++)
280 for (x
= 0; x
< w
-1; x
++) {
281 int di
= DINDEX(grid
[y
*w
+x
], grid
[y
*w
+(x
+1)]);
282 placements
[(y
*w
+x
)*2+1] = heads
[di
];
283 heads
[di
] = (y
*w
+x
)*2+1;
286 #ifdef SOLVER_DIAGNOSTICS
287 printf("before solver:\n");
288 for (i
= 0; i
<= n
; i
++)
289 for (j
= 0; j
<= i
; j
++) {
292 printf("%2d [%d %d]:", DINDEX(i
, j
), i
, j
);
293 for (k
= heads
[DINDEX(i
,j
)]; k
>= 0; k
= placements
[k
])
294 printf(" %3d [%d,%d,%c]", k
, k
/2%w
, k
/2/w
, k
%2?
'h':'v');
300 int done_something
= FALSE
;
303 * For each domino, look at its possible placements, and
304 * for each placement consider the placements (of any
305 * domino) it overlaps. Any placement overlapped by all
306 * placements of this domino can be ruled out.
308 * Each domino placement overlaps only six others, so we
309 * need not do serious set theory to work this out.
311 for (i
= 0; i
< dc
; i
++) {
312 int permset
[6], permlen
= 0, p
;
315 if (heads
[i
] == -1) { /* no placement for this domino */
316 ret
= 0; /* therefore puzzle is impossible */
319 for (j
= heads
[i
]; j
>= 0; j
= placements
[j
]) {
320 assert(placements
[j
] != -2);
323 permlen
= find_overlaps(w
, h
, j
, permset
);
325 int tempset
[6], templen
, m
, n
, k
;
327 templen
= find_overlaps(w
, h
, j
, tempset
);
330 * Pathetically primitive set intersection
331 * algorithm, which I'm only getting away with
332 * because I know my sets are bounded by a very
335 for (m
= n
= 0; m
< permlen
; m
++) {
336 for (k
= 0; k
< templen
; k
++)
337 if (tempset
[k
] == permset
[m
])
340 permset
[n
++] = permset
[m
];
345 for (p
= 0; p
< permlen
; p
++) {
347 if (placements
[j
] != -2) {
350 done_something
= TRUE
;
353 * Rule out this placement. First find what
357 p2
= (j
& 1) ? p1
+ 1 : p1
+ w
;
358 di
= DINDEX(grid
[p1
], grid
[p2
]);
359 #ifdef SOLVER_DIAGNOSTICS
360 printf("considering domino %d: ruling out placement %d"
361 " for %d\n", i
, j
, di
);
365 * ... then walk that domino's placement list,
366 * removing this placement when we find it.
369 heads
[di
] = placements
[j
];
372 while (placements
[k
] != -1 && placements
[k
] != j
)
374 assert(placements
[k
] == j
);
375 placements
[k
] = placements
[j
];
383 * For each square, look at the available placements
384 * involving that square. If all of them are for the same
385 * domino, then rule out any placements for that domino
386 * _not_ involving this square.
388 for (i
= 0; i
< wh
; i
++) {
389 int list
[4], k
, n
, adi
;
396 list
[j
++] = 2*(i
-1)+1;
404 for (n
= k
= 0; k
< j
; k
++)
405 if (placements
[list
[k
]] >= -1)
410 for (j
= 0; j
< n
; j
++) {
415 p2
= (k
& 1) ? p1
+ 1 : p1
+ w
;
416 di
= DINDEX(grid
[p1
], grid
[p2
]);
429 * We've found something. All viable placements
430 * involving this square are for domino `adi'. If
431 * the current placement list for that domino is
432 * longer than n, reduce it to precisely this
433 * placement list and we've done something.
436 for (k
= heads
[adi
]; k
>= 0; k
= placements
[k
])
439 done_something
= TRUE
;
440 #ifdef SOLVER_DIAGNOSTICS
441 printf("considering square %d,%d: reducing placements "
442 "of domino %d\n", x
, y
, adi
);
445 * Set all other placements on the list to
450 int tmp
= placements
[k
];
455 * Set up the new list.
457 heads
[adi
] = list
[0];
458 for (k
= 0; k
< n
; k
++)
459 placements
[list
[k
]] = (k
+1 == n ?
-1 : list
[k
+1]);
468 #ifdef SOLVER_DIAGNOSTICS
469 printf("after solver:\n");
470 for (i
= 0; i
<= n
; i
++)
471 for (j
= 0; j
<= i
; j
++) {
474 printf("%2d [%d %d]:", DINDEX(i
, j
), i
, j
);
475 for (k
= heads
[DINDEX(i
,j
)]; k
>= 0; k
= placements
[k
])
476 printf(" %3d [%d,%d,%c]", k
, k
/2%w
, k
/2/w
, k
%2?
'h':'v');
482 for (i
= 0; i
< wh
*2; i
++) {
483 if (placements
[i
] == -2) {
485 output
[i
] = -1; /* ruled out */
486 } else if (placements
[i
] != -3) {
490 p2
= (i
& 1) ? p1
+ 1 : p1
+ w
;
491 di
= DINDEX(grid
[p1
], grid
[p2
]);
493 if (i
== heads
[di
] && placements
[i
] == -1) {
495 output
[i
] = 1; /* certain */
498 output
[i
] = 0; /* uncertain */
514 /* ----------------------------------------------------------------------
515 * End of solver code.
518 static char *new_game_desc(game_params
*params
, random_state
*rs
,
519 char **aux
, int interactive
)
521 int n
= params
->n
, w
= n
+2, h
= n
+1, wh
= w
*h
;
522 int *grid
, *grid2
, *list
;
523 int i
, j
, k
, m
, todo
, done
, len
;
527 * Allocate space in which to lay the grid out.
529 grid
= snewn(wh
, int);
530 grid2
= snewn(wh
, int);
531 list
= snewn(2*wh
, int);
535 * To begin with, set grid[i] = i for all i to indicate
536 * that all squares are currently singletons. Later we'll
537 * set grid[i] to be the index of the other end of the
540 for (i
= 0; i
< wh
; i
++)
544 * Now prepare a list of the possible domino locations. There
545 * are w*(h-1) possible vertical locations, and (w-1)*h
546 * horizontal ones, for a total of 2*wh - h - w.
548 * I'm going to denote the vertical domino placement with
549 * its top in square i as 2*i, and the horizontal one with
550 * its left half in square i as 2*i+1.
553 for (j
= 0; j
< h
-1; j
++)
554 for (i
= 0; i
< w
; i
++)
555 list
[k
++] = 2 * (j
*w
+i
); /* vertical positions */
556 for (j
= 0; j
< h
; j
++)
557 for (i
= 0; i
< w
-1; i
++)
558 list
[k
++] = 2 * (j
*w
+i
) + 1; /* horizontal positions */
559 assert(k
== 2*wh
- h
- w
);
564 shuffle(list
, k
, sizeof(*list
), rs
);
567 * Work down the shuffled list, placing a domino everywhere
570 for (i
= 0; i
< k
; i
++) {
575 xy2
= xy
+ (horiz ?
1 : w
);
577 if (grid
[xy
] == xy
&& grid
[xy2
] == xy2
) {
579 * We can place this domino. Do so.
586 #ifdef GENERATION_DIAGNOSTICS
587 printf("generated initial layout\n");
591 * Now we've placed as many dominoes as we can immediately
592 * manage. There will be squares remaining, but they'll be
593 * singletons. So loop round and deal with the singletons
597 #ifdef GENERATION_DIAGNOSTICS
598 for (j
= 0; j
< h
; j
++) {
599 for (i
= 0; i
< w
; i
++) {
602 int c
= (v
== xy
+1 ?
'[' : v
== xy
-1 ?
']' :
603 v
== xy
+w ?
'n' : v
== xy
-w ?
'U' : '.');
614 * First find a singleton square.
616 * Then breadth-first search out from the starting
617 * square. From that square (and any others we reach on
618 * the way), examine all four neighbours of the square.
619 * If one is an end of a domino, we move to the _other_
620 * end of that domino before looking at neighbours
621 * again. When we encounter another singleton on this
624 * This will give us a path of adjacent squares such
625 * that all but the two ends are covered in dominoes.
626 * So we can now shuffle every domino on the path up by
629 * (Chessboard colours are mathematically important
630 * here: we always end up pairing each singleton with a
631 * singleton of the other colour. However, we never
632 * have to track this manually, since it's
633 * automatically taken care of by the fact that we
634 * always make an even number of orthogonal moves.)
636 for (i
= 0; i
< wh
; i
++)
640 break; /* no more singletons; we're done. */
642 #ifdef GENERATION_DIAGNOSTICS
643 printf("starting b.f.s. at singleton %d\n", i
);
646 * Set grid2 to -1 everywhere. It will hold our
647 * distance-from-start values, and also our
648 * backtracking data, during the b.f.s.
650 for (j
= 0; j
< wh
; j
++)
652 grid2
[i
] = 0; /* starting square has distance zero */
655 * Start our to-do list of squares. It'll live in
656 * `list'; since the b.f.s can cover every square at
657 * most once there is no need for it to be circular.
658 * We'll just have two counters tracking the end of the
659 * list and the squares we've already dealt with.
666 * Now begin the b.f.s. loop.
668 while (done
< todo
) {
673 #ifdef GENERATION_DIAGNOSTICS
674 printf("b.f.s. iteration from %d\n", i
);
688 * To avoid directional bias, process the
689 * neighbours of this square in a random order.
691 shuffle(d
, nd
, sizeof(*d
), rs
);
693 for (j
= 0; j
< nd
; j
++) {
696 #ifdef GENERATION_DIAGNOSTICS
697 printf("found neighbouring singleton %d\n", k
);
700 break; /* found a target singleton! */
704 * We're moving through a domino here, so we
705 * have two entries in grid2 to fill with
706 * useful data. In grid[k] - the square
707 * adjacent to where we came from - I'm going
708 * to put the address _of_ the square we came
709 * from. In the other end of the domino - the
710 * square from which we will continue the
711 * search - I'm going to put the distance.
715 if (grid2
[m
] < 0 || grid2
[m
] > grid2
[i
]+1) {
716 #ifdef GENERATION_DIAGNOSTICS
717 printf("found neighbouring domino %d/%d\n", k
, m
);
719 grid2
[m
] = grid2
[i
]+1;
722 * And since we've now visited a new
723 * domino, add m to the to-do list.
732 #ifdef GENERATION_DIAGNOSTICS
733 printf("terminating b.f.s. loop, i = %d\n", i
);
738 i
= -1; /* just in case the loop terminates */
742 * We expect this b.f.s. to have found us a target
748 * Now we can follow the trail back to our starting
749 * singleton, re-laying dominoes as we go.
753 assert(j
>= 0 && j
< wh
);
758 #ifdef GENERATION_DIAGNOSTICS
759 printf("filling in domino %d/%d (next %d)\n", i
, j
, k
);
762 break; /* we've reached the other singleton */
765 #ifdef GENERATION_DIAGNOSTICS
766 printf("fixup path completed\n");
771 * Now we have a complete layout covering the whole
772 * rectangle with dominoes. So shuffle the actual domino
773 * values and fill the rectangle with numbers.
776 for (i
= 0; i
<= params
->n
; i
++)
777 for (j
= 0; j
<= i
; j
++) {
781 shuffle(list
, k
/2, 2*sizeof(*list
), rs
);
783 for (i
= 0; i
< wh
; i
++)
785 /* Optionally flip the domino round. */
786 int flip
= random_upto(rs
, 2);
787 grid2
[i
] = list
[j
+ flip
];
788 grid2
[grid
[i
]] = list
[j
+ 1 - flip
];
792 } while (params
->unique
&& solver(w
, h
, n
, grid2
, NULL
) > 1);
794 #ifdef GENERATION_DIAGNOSTICS
795 for (j
= 0; j
< h
; j
++) {
796 for (i
= 0; i
< w
; i
++) {
797 putchar('0' + grid2
[j
*w
+i
]);
805 * Encode the resulting game state.
807 * Our encoding is a string of digits. Any number greater than
808 * 9 is represented by a decimal integer within square
809 * brackets. We know there are n+2 of every number (it's paired
810 * with each number from 0 to n inclusive, and one of those is
811 * itself so that adds another occurrence), so we can work out
812 * the string length in advance.
816 * To work out the total length of the decimal encodings of all
817 * the numbers from 0 to n inclusive:
818 * - every number has a units digit; total is n+1.
819 * - all numbers above 9 have a tens digit; total is max(n+1-10,0).
820 * - all numbers above 99 have a hundreds digit; total is max(n+1-100,0).
824 for (i
= 10; i
<= n
; i
*= 10)
825 len
+= max(n
+ 1 - i
, 0);
826 /* Now add two square brackets for each number above 9. */
827 len
+= 2 * max(n
+ 1 - 10, 0);
828 /* And multiply by n+2 for the repeated occurrences of each number. */
832 * Now actually encode the string.
834 ret
= snewn(len
+1, char);
836 for (i
= 0; i
< wh
; i
++) {
841 j
+= sprintf(ret
+j
, "[%d]", k
);
848 * Encode the solved state as an aux_info.
851 char *auxinfo
= snewn(wh
+1, char);
853 for (i
= 0; i
< wh
; i
++) {
855 auxinfo
[i
] = (v
== i
+1 ?
'L' : v
== i
-1 ?
'R' :
856 v
== i
+w ?
'T' : v
== i
-w ?
'B' : '.');
870 static char *validate_desc(game_params
*params
, char *desc
)
872 int n
= params
->n
, w
= n
+2, h
= n
+1, wh
= w
*h
;
878 occurrences
= snewn(n
+1, int);
879 for (i
= 0; i
<= n
; i
++)
882 for (i
= 0; i
< wh
; i
++) {
884 ret
= ret ? ret
: "Game description is too short";
886 if (*desc
>= '0' && *desc
<= '9')
888 else if (*desc
== '[') {
891 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
893 ret
= ret ? ret
: "Missing ']' in game description";
898 ret
= ret ? ret
: "Invalid syntax in game description";
901 ret
= ret ? ret
: "Number out of range in game description";
908 ret
= ret ? ret
: "Game description is too long";
911 for (i
= 0; i
<= n
; i
++)
912 if (occurrences
[i
] != n
+2)
913 ret
= "Incorrect number balance in game description";
921 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
923 int n
= params
->n
, w
= n
+2, h
= n
+1, wh
= w
*h
;
924 game_state
*state
= snew(game_state
);
927 state
->params
= *params
;
931 state
->grid
= snewn(wh
, int);
932 for (i
= 0; i
< wh
; i
++)
935 state
->edges
= snewn(wh
, unsigned short);
936 for (i
= 0; i
< wh
; i
++)
939 state
->numbers
= snew(struct game_numbers
);
940 state
->numbers
->refcount
= 1;
941 state
->numbers
->numbers
= snewn(wh
, int);
943 for (i
= 0; i
< wh
; i
++) {
945 if (*desc
>= '0' && *desc
<= '9')
948 assert(*desc
== '[');
951 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
952 assert(*desc
== ']');
955 assert(j
>= 0 && j
<= n
);
956 state
->numbers
->numbers
[i
] = j
;
959 state
->completed
= state
->cheated
= FALSE
;
964 static game_state
*dup_game(game_state
*state
)
966 int n
= state
->params
.n
, w
= n
+2, h
= n
+1, wh
= w
*h
;
967 game_state
*ret
= snew(game_state
);
969 ret
->params
= state
->params
;
972 ret
->grid
= snewn(wh
, int);
973 memcpy(ret
->grid
, state
->grid
, wh
* sizeof(int));
974 ret
->edges
= snewn(wh
, unsigned short);
975 memcpy(ret
->edges
, state
->edges
, wh
* sizeof(unsigned short));
976 ret
->numbers
= state
->numbers
;
977 ret
->numbers
->refcount
++;
978 ret
->completed
= state
->completed
;
979 ret
->cheated
= state
->cheated
;
984 static void free_game(game_state
*state
)
987 if (--state
->numbers
->refcount
<= 0) {
988 sfree(state
->numbers
->numbers
);
989 sfree(state
->numbers
);
994 static char *solve_game(game_state
*state
, game_state
*currstate
,
995 char *aux
, char **error
)
997 int n
= state
->params
.n
, w
= n
+2, h
= n
+1, wh
= w
*h
;
1000 int retlen
, retsize
;
1007 ret
= snewn(retsize
, char);
1008 retlen
= sprintf(ret
, "S");
1010 for (i
= 0; i
< wh
; i
++) {
1012 extra
= sprintf(buf
, ";D%d,%d", i
, i
+1);
1013 else if (aux
[i
] == 'T')
1014 extra
= sprintf(buf
, ";D%d,%d", i
, i
+w
);
1018 if (retlen
+ extra
+ 1 >= retsize
) {
1019 retsize
= retlen
+ extra
+ 256;
1020 ret
= sresize(ret
, retsize
, char);
1022 strcpy(ret
+ retlen
, buf
);
1028 placements
= snewn(wh
*2, int);
1029 for (i
= 0; i
< wh
*2; i
++)
1031 solver(w
, h
, n
, state
->numbers
->numbers
, placements
);
1034 * First make a pass putting in edges for -1, then make a pass
1035 * putting in dominoes for +1.
1038 ret
= snewn(retsize
, char);
1039 retlen
= sprintf(ret
, "S");
1041 for (v
= -1; v
<= +1; v
+= 2)
1042 for (i
= 0; i
< wh
*2; i
++)
1043 if (placements
[i
] == v
) {
1045 int p2
= (i
& 1) ? p1
+1 : p1
+w
;
1047 extra
= sprintf(buf
, ";%c%d,%d",
1048 v
==-1 ?
'E' : 'D', p1
, p2
);
1050 if (retlen
+ extra
+ 1 >= retsize
) {
1051 retsize
= retlen
+ extra
+ 256;
1052 ret
= sresize(ret
, retsize
, char);
1054 strcpy(ret
+ retlen
, buf
);
1064 static char *game_text_format(game_state
*state
)
1069 static game_ui
*new_ui(game_state
*state
)
1074 static void free_ui(game_ui
*ui
)
1078 static char *encode_ui(game_ui
*ui
)
1083 static void decode_ui(game_ui
*ui
, char *encoding
)
1087 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1088 game_state
*newstate
)
1092 #define PREFERRED_TILESIZE 32
1093 #define TILESIZE (ds->tilesize)
1094 #define BORDER (TILESIZE * 3 / 4)
1095 #define DOMINO_GUTTER (TILESIZE / 16)
1096 #define DOMINO_RADIUS (TILESIZE / 8)
1097 #define DOMINO_COFFSET (DOMINO_GUTTER + DOMINO_RADIUS)
1099 #define COORD(x) ( (x) * TILESIZE + BORDER )
1100 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1102 struct game_drawstate
{
1105 unsigned long *visible
;
1108 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1109 int x
, int y
, int button
)
1111 int w
= state
->w
, h
= state
->h
;
1115 * A left-click between two numbers toggles a domino covering
1116 * them. A right-click toggles an edge.
1118 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
1119 int tx
= FROMCOORD(x
), ty
= FROMCOORD(y
), t
= ty
*w
+tx
;
1123 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
)
1127 * Now we know which square the click was in, decide which
1128 * edge of the square it was closest to.
1130 dx
= 2 * (x
- COORD(tx
)) - TILESIZE
;
1131 dy
= 2 * (y
- COORD(ty
)) - TILESIZE
;
1133 if (abs(dx
) > abs(dy
) && dx
< 0 && tx
> 0)
1134 d1
= t
- 1, d2
= t
; /* clicked in right side of domino */
1135 else if (abs(dx
) > abs(dy
) && dx
> 0 && tx
+1 < w
)
1136 d1
= t
, d2
= t
+ 1; /* clicked in left side of domino */
1137 else if (abs(dy
) > abs(dx
) && dy
< 0 && ty
> 0)
1138 d1
= t
- w
, d2
= t
; /* clicked in bottom half of domino */
1139 else if (abs(dy
) > abs(dx
) && dy
> 0 && ty
+1 < h
)
1140 d1
= t
, d2
= t
+ w
; /* clicked in top half of domino */
1145 * We can't mark an edge next to any domino.
1147 if (button
== RIGHT_BUTTON
&&
1148 (state
->grid
[d1
] != d1
|| state
->grid
[d2
] != d2
))
1151 sprintf(buf
, "%c%d,%d", button
== RIGHT_BUTTON ?
'E' : 'D', d1
, d2
);
1158 static game_state
*execute_move(game_state
*state
, char *move
)
1160 int n
= state
->params
.n
, w
= n
+2, h
= n
+1, wh
= w
*h
;
1162 game_state
*ret
= dup_game(state
);
1165 if (move
[0] == 'S') {
1168 ret
->cheated
= TRUE
;
1171 * Clear the existing edges and domino placements. We
1172 * expect the S to be followed by other commands.
1174 for (i
= 0; i
< wh
; i
++) {
1179 } else if (move
[0] == 'D' &&
1180 sscanf(move
+1, "%d,%d%n", &d1
, &d2
, &p
) == 2 &&
1181 d1
>= 0 && d1
< wh
&& d2
>= 0 && d2
< wh
&& d1
< d2
) {
1184 * Toggle domino presence between d1 and d2.
1186 if (ret
->grid
[d1
] == d2
) {
1187 assert(ret
->grid
[d2
] == d1
);
1192 * Erase any dominoes that might overlap the new one.
1201 * Place the new one.
1207 * Destroy any edges lurking around it.
1209 if (ret
->edges
[d1
] & EDGE_L
) {
1210 assert(d1
- 1 >= 0);
1211 ret
->edges
[d1
- 1] &= ~EDGE_R
;
1213 if (ret
->edges
[d1
] & EDGE_R
) {
1214 assert(d1
+ 1 < wh
);
1215 ret
->edges
[d1
+ 1] &= ~EDGE_L
;
1217 if (ret
->edges
[d1
] & EDGE_T
) {
1218 assert(d1
- w
>= 0);
1219 ret
->edges
[d1
- w
] &= ~EDGE_B
;
1221 if (ret
->edges
[d1
] & EDGE_B
) {
1222 assert(d1
+ 1 < wh
);
1223 ret
->edges
[d1
+ w
] &= ~EDGE_T
;
1226 if (ret
->edges
[d2
] & EDGE_L
) {
1227 assert(d2
- 1 >= 0);
1228 ret
->edges
[d2
- 1] &= ~EDGE_R
;
1230 if (ret
->edges
[d2
] & EDGE_R
) {
1231 assert(d2
+ 1 < wh
);
1232 ret
->edges
[d2
+ 1] &= ~EDGE_L
;
1234 if (ret
->edges
[d2
] & EDGE_T
) {
1235 assert(d2
- w
>= 0);
1236 ret
->edges
[d2
- w
] &= ~EDGE_B
;
1238 if (ret
->edges
[d2
] & EDGE_B
) {
1239 assert(d2
+ 1 < wh
);
1240 ret
->edges
[d2
+ w
] &= ~EDGE_T
;
1246 } else if (move
[0] == 'E' &&
1247 sscanf(move
+1, "%d,%d%n", &d1
, &d2
, &p
) == 2 &&
1248 d1
>= 0 && d1
< wh
&& d2
>= 0 && d2
< wh
&& d1
< d2
&&
1249 ret
->grid
[d1
] == d1
&& ret
->grid
[d2
] == d2
) {
1252 * Toggle edge presence between d1 and d2.
1255 ret
->edges
[d1
] ^= EDGE_R
;
1256 ret
->edges
[d2
] ^= EDGE_L
;
1258 ret
->edges
[d1
] ^= EDGE_B
;
1259 ret
->edges
[d2
] ^= EDGE_T
;
1278 * After modifying the grid, check completion.
1280 if (!ret
->completed
) {
1282 unsigned char *used
= snewn(TRI(n
+1), unsigned char);
1284 memset(used
, 0, TRI(n
+1));
1285 for (i
= 0; i
< wh
; i
++)
1286 if (ret
->grid
[i
] > i
) {
1289 n1
= ret
->numbers
->numbers
[i
];
1290 n2
= ret
->numbers
->numbers
[ret
->grid
[i
]];
1292 di
= DINDEX(n1
, n2
);
1293 assert(di
>= 0 && di
< TRI(n
+1));
1302 if (ok
== DCOUNT(n
))
1303 ret
->completed
= TRUE
;
1309 /* ----------------------------------------------------------------------
1313 static void game_compute_size(game_params
*params
, int tilesize
,
1316 int n
= params
->n
, w
= n
+2, h
= n
+1;
1318 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1319 struct { int tilesize
; } ads
, *ds
= &ads
;
1320 ads
.tilesize
= tilesize
;
1322 *x
= w
* TILESIZE
+ 2*BORDER
;
1323 *y
= h
* TILESIZE
+ 2*BORDER
;
1326 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
1329 ds
->tilesize
= tilesize
;
1332 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1334 float *ret
= snewn(3 * NCOLOURS
, float);
1336 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1338 ret
[COL_TEXT
* 3 + 0] = 0.0F
;
1339 ret
[COL_TEXT
* 3 + 1] = 0.0F
;
1340 ret
[COL_TEXT
* 3 + 2] = 0.0F
;
1342 ret
[COL_DOMINO
* 3 + 0] = 0.0F
;
1343 ret
[COL_DOMINO
* 3 + 1] = 0.0F
;
1344 ret
[COL_DOMINO
* 3 + 2] = 0.0F
;
1346 ret
[COL_DOMINOCLASH
* 3 + 0] = 0.5F
;
1347 ret
[COL_DOMINOCLASH
* 3 + 1] = 0.0F
;
1348 ret
[COL_DOMINOCLASH
* 3 + 2] = 0.0F
;
1350 ret
[COL_DOMINOTEXT
* 3 + 0] = 1.0F
;
1351 ret
[COL_DOMINOTEXT
* 3 + 1] = 1.0F
;
1352 ret
[COL_DOMINOTEXT
* 3 + 2] = 1.0F
;
1354 ret
[COL_EDGE
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 2 / 3;
1355 ret
[COL_EDGE
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 2 / 3;
1356 ret
[COL_EDGE
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 2 / 3;
1358 *ncolours
= NCOLOURS
;
1362 static game_drawstate
*game_new_drawstate(game_state
*state
)
1364 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1367 ds
->started
= FALSE
;
1370 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned long);
1371 ds
->tilesize
= 0; /* not decided yet */
1372 for (i
= 0; i
< ds
->w
* ds
->h
; i
++)
1373 ds
->visible
[i
] = 0xFFFF;
1378 static void game_free_drawstate(game_drawstate
*ds
)
1393 static void draw_tile(frontend
*fe
, game_drawstate
*ds
, game_state
*state
,
1394 int x
, int y
, int type
)
1396 int w
= state
->w
/*, h = state->h */;
1397 int cx
= COORD(x
), cy
= COORD(y
);
1402 draw_rect(fe
, cx
, cy
, TILESIZE
, TILESIZE
, COL_BACKGROUND
);
1404 flags
= type
&~ TYPE_MASK
;
1407 if (type
!= TYPE_BLANK
) {
1411 * Draw one end of a domino. This is composed of:
1413 * - two filled circles (rounded corners)
1415 * - a slight shift in the number
1419 bg
= COL_DOMINOCLASH
;
1422 nc
= COL_DOMINOTEXT
;
1430 if (type
== TYPE_L
|| type
== TYPE_T
)
1431 draw_circle(fe
, cx
+DOMINO_COFFSET
, cy
+DOMINO_COFFSET
,
1432 DOMINO_RADIUS
, bg
, bg
);
1433 if (type
== TYPE_R
|| type
== TYPE_T
)
1434 draw_circle(fe
, cx
+TILESIZE
-1-DOMINO_COFFSET
, cy
+DOMINO_COFFSET
,
1435 DOMINO_RADIUS
, bg
, bg
);
1436 if (type
== TYPE_L
|| type
== TYPE_B
)
1437 draw_circle(fe
, cx
+DOMINO_COFFSET
, cy
+TILESIZE
-1-DOMINO_COFFSET
,
1438 DOMINO_RADIUS
, bg
, bg
);
1439 if (type
== TYPE_R
|| type
== TYPE_B
)
1440 draw_circle(fe
, cx
+TILESIZE
-1-DOMINO_COFFSET
,
1441 cy
+TILESIZE
-1-DOMINO_COFFSET
,
1442 DOMINO_RADIUS
, bg
, bg
);
1444 for (i
= 0; i
< 2; i
++) {
1447 x1
= cx
+ (i ? DOMINO_GUTTER
: DOMINO_COFFSET
);
1448 y1
= cy
+ (i ? DOMINO_COFFSET
: DOMINO_GUTTER
);
1449 x2
= cx
+ TILESIZE
-1 - (i ? DOMINO_GUTTER
: DOMINO_COFFSET
);
1450 y2
= cy
+ TILESIZE
-1 - (i ? DOMINO_COFFSET
: DOMINO_GUTTER
);
1452 x2
= cx
+ TILESIZE
-1;
1453 else if (type
== TYPE_R
)
1455 else if (type
== TYPE_T
)
1456 y2
= cy
+ TILESIZE
-1;
1457 else if (type
== TYPE_B
)
1460 draw_rect(fe
, x1
, y1
, x2
-x1
+1, y2
-y1
+1, bg
);
1464 draw_rect(fe
, cx
+DOMINO_GUTTER
, cy
,
1465 TILESIZE
-2*DOMINO_GUTTER
, 1, COL_EDGE
);
1467 draw_rect(fe
, cx
+DOMINO_GUTTER
, cy
+TILESIZE
-1,
1468 TILESIZE
-2*DOMINO_GUTTER
, 1, COL_EDGE
);
1470 draw_rect(fe
, cx
, cy
+DOMINO_GUTTER
,
1471 1, TILESIZE
-2*DOMINO_GUTTER
, COL_EDGE
);
1473 draw_rect(fe
, cx
+TILESIZE
-1, cy
+DOMINO_GUTTER
,
1474 1, TILESIZE
-2*DOMINO_GUTTER
, COL_EDGE
);
1478 sprintf(str
, "%d", state
->numbers
->numbers
[y
*w
+x
]);
1479 draw_text(fe
, cx
+TILESIZE
/2, cy
+TILESIZE
/2, FONT_VARIABLE
, TILESIZE
/2,
1480 ALIGN_HCENTRE
| ALIGN_VCENTRE
, nc
, str
);
1482 draw_update(fe
, cx
, cy
, TILESIZE
, TILESIZE
);
1485 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1486 game_state
*state
, int dir
, game_ui
*ui
,
1487 float animtime
, float flashtime
)
1489 int n
= state
->params
.n
, w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1491 unsigned char *used
;
1495 game_compute_size(&state
->params
, TILESIZE
, &pw
, &ph
);
1496 draw_rect(fe
, 0, 0, pw
, ph
, COL_BACKGROUND
);
1497 draw_update(fe
, 0, 0, pw
, ph
);
1502 * See how many dominoes of each type there are, so we can
1503 * highlight clashes in red.
1505 used
= snewn(TRI(n
+1), unsigned char);
1506 memset(used
, 0, TRI(n
+1));
1507 for (i
= 0; i
< wh
; i
++)
1508 if (state
->grid
[i
] > i
) {
1511 n1
= state
->numbers
->numbers
[i
];
1512 n2
= state
->numbers
->numbers
[state
->grid
[i
]];
1514 di
= DINDEX(n1
, n2
);
1515 assert(di
>= 0 && di
< TRI(n
+1));
1521 for (y
= 0; y
< h
; y
++)
1522 for (x
= 0; x
< w
; x
++) {
1527 if (state
->grid
[n
] == n
-1)
1529 else if (state
->grid
[n
] == n
+1)
1531 else if (state
->grid
[n
] == n
-w
)
1533 else if (state
->grid
[n
] == n
+w
)
1538 if (c
!= TYPE_BLANK
) {
1539 n1
= state
->numbers
->numbers
[n
];
1540 n2
= state
->numbers
->numbers
[state
->grid
[n
]];
1541 di
= DINDEX(n1
, n2
);
1543 c
|= 0x80; /* highlight a clash */
1545 c
|= state
->edges
[n
];
1549 c
|= 0x40; /* we're flashing */
1551 if (ds
->visible
[n
] != c
) {
1552 draw_tile(fe
, ds
, state
, x
, y
, c
);
1560 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1561 int dir
, game_ui
*ui
)
1566 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1567 int dir
, game_ui
*ui
)
1569 if (!oldstate
->completed
&& newstate
->completed
&&
1570 !oldstate
->cheated
&& !newstate
->cheated
)
1575 static int game_wants_statusbar(void)
1580 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1586 #define thegame dominosa
1589 const struct game thegame
= {
1590 "Dominosa", "games.dominosa",
1597 TRUE
, game_configure
, custom_params
,
1605 FALSE
, game_text_format
,
1613 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1616 game_free_drawstate
,
1620 game_wants_statusbar
,
1621 FALSE
, game_timing_state
,
1622 0, /* mouse_priorities */