2 * map.c: Game involving four-colouring a map.
9 * - more solver brains?
10 * - better four-colouring algorithm?
24 * I don't seriously anticipate wanting to change the number of
25 * colours used in this game, but it doesn't cost much to use a
26 * #define just in case :-)
29 #define THREE (FOUR-1)
34 * Ghastly run-time configuration option, just for Gareth (again).
36 static int flash_type
= -1;
37 static float flash_length
;
40 * Difficulty levels. I do some macro ickery here to ensure that my
41 * enum and the various forms of my name list always match up.
46 #define ENUM(upper,title,lower) DIFF_ ## upper,
47 #define TITLE(upper,title,lower) #title,
48 #define ENCODE(upper,title,lower) #lower
49 #define CONFIG(upper,title,lower) ":" #title
50 enum { DIFFLIST(ENUM
) DIFFCOUNT
};
51 static char const *const map_diffnames
[] = { DIFFLIST(TITLE
) };
52 static char const map_diffchars
[] = DIFFLIST(ENCODE
);
53 #define DIFFCONFIG DIFFLIST(CONFIG)
55 enum { TE
, BE
, LE
, RE
}; /* top/bottom/left/right edges */
60 COL_0
, COL_1
, COL_2
, COL_3
,
61 COL_ERROR
, COL_ERRTEXT
,
76 int *edgex
, *edgey
; /* positions of a point on each edge */
83 int completed
, cheated
;
86 static game_params
*default_params(void)
88 game_params
*ret
= snew(game_params
);
93 ret
->diff
= DIFF_NORMAL
;
98 static const struct game_params map_presets
[] = {
99 {20, 15, 30, DIFF_EASY
},
100 {20, 15, 30, DIFF_NORMAL
},
101 {30, 25, 75, DIFF_NORMAL
},
104 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
109 if (i
< 0 || i
>= lenof(map_presets
))
112 ret
= snew(game_params
);
113 *ret
= map_presets
[i
];
115 sprintf(str
, "%dx%d, %d regions, %s", ret
->w
, ret
->h
, ret
->n
,
116 map_diffnames
[ret
->diff
]);
123 static void free_params(game_params
*params
)
128 static game_params
*dup_params(game_params
*params
)
130 game_params
*ret
= snew(game_params
);
131 *ret
= *params
; /* structure copy */
135 static void decode_params(game_params
*params
, char const *string
)
137 char const *p
= string
;
140 while (*p
&& isdigit((unsigned char)*p
)) p
++;
144 while (*p
&& isdigit((unsigned char)*p
)) p
++;
146 params
->h
= params
->w
;
151 while (*p
&& (*p
== '.' || isdigit((unsigned char)*p
))) p
++;
153 params
->n
= params
->w
* params
->h
/ 8;
158 for (i
= 0; i
< DIFFCOUNT
; i
++)
159 if (*p
== map_diffchars
[i
])
165 static char *encode_params(game_params
*params
, int full
)
169 sprintf(ret
, "%dx%dn%d", params
->w
, params
->h
, params
->n
);
171 sprintf(ret
+ strlen(ret
), "d%c", map_diffchars
[params
->diff
]);
176 static config_item
*game_configure(game_params
*params
)
181 ret
= snewn(5, config_item
);
183 ret
[0].name
= "Width";
184 ret
[0].type
= C_STRING
;
185 sprintf(buf
, "%d", params
->w
);
186 ret
[0].sval
= dupstr(buf
);
189 ret
[1].name
= "Height";
190 ret
[1].type
= C_STRING
;
191 sprintf(buf
, "%d", params
->h
);
192 ret
[1].sval
= dupstr(buf
);
195 ret
[2].name
= "Regions";
196 ret
[2].type
= C_STRING
;
197 sprintf(buf
, "%d", params
->n
);
198 ret
[2].sval
= dupstr(buf
);
201 ret
[3].name
= "Difficulty";
202 ret
[3].type
= C_CHOICES
;
203 ret
[3].sval
= DIFFCONFIG
;
204 ret
[3].ival
= params
->diff
;
214 static game_params
*custom_params(config_item
*cfg
)
216 game_params
*ret
= snew(game_params
);
218 ret
->w
= atoi(cfg
[0].sval
);
219 ret
->h
= atoi(cfg
[1].sval
);
220 ret
->n
= atoi(cfg
[2].sval
);
221 ret
->diff
= cfg
[3].ival
;
226 static char *validate_params(game_params
*params
, int full
)
228 if (params
->w
< 2 || params
->h
< 2)
229 return "Width and height must be at least two";
231 return "Must have at least five regions";
232 if (params
->n
> params
->w
* params
->h
)
233 return "Too many regions to fit in grid";
237 /* ----------------------------------------------------------------------
238 * Cumulative frequency table functions.
242 * Initialise a cumulative frequency table. (Hardly worth writing
243 * this function; all it does is to initialise everything in the
246 static void cf_init(int *table
, int n
)
250 for (i
= 0; i
< n
; i
++)
255 * Increment the count of symbol `sym' by `count'.
257 static void cf_add(int *table
, int n
, int sym
, int count
)
274 * Cumulative frequency lookup: return the total count of symbols
275 * with value less than `sym'.
277 static int cf_clookup(int *table
, int n
, int sym
)
279 int bit
, index
, limit
, count
;
284 assert(0 < sym
&& sym
<= n
);
286 count
= table
[0]; /* start with the whole table size */
296 * Find the least number with its lowest set bit in this
297 * position which is greater than or equal to sym.
299 index
= ((sym
+ bit
- 1) &~ (bit
* 2 - 1)) + bit
;
302 count
-= table
[index
];
313 * Single frequency lookup: return the count of symbol `sym'.
315 static int cf_slookup(int *table
, int n
, int sym
)
319 assert(0 <= sym
&& sym
< n
);
323 for (bit
= 1; sym
+bit
< n
&& !(sym
& bit
); bit
<<= 1)
324 count
-= table
[sym
+bit
];
330 * Return the largest symbol index such that the cumulative
331 * frequency up to that symbol is less than _or equal to_ count.
333 static int cf_whichsym(int *table
, int n
, int count
) {
336 assert(count
>= 0 && count
< table
[0]);
347 if (count
>= top
- table
[sym
+bit
])
350 top
-= table
[sym
+bit
];
359 /* ----------------------------------------------------------------------
362 * FIXME: this isn't entirely optimal at present, because it
363 * inherently prioritises growing the largest region since there
364 * are more squares adjacent to it. This acts as a destabilising
365 * influence leading to a few large regions and mostly small ones.
366 * It might be better to do it some other way.
369 #define WEIGHT_INCREASED 2 /* for increased perimeter */
370 #define WEIGHT_DECREASED 4 /* for decreased perimeter */
371 #define WEIGHT_UNCHANGED 3 /* for unchanged perimeter */
374 * Look at a square and decide which colours can be extended into
377 * If called with index < 0, it adds together one of
378 * WEIGHT_INCREASED, WEIGHT_DECREASED or WEIGHT_UNCHANGED for each
379 * colour that has a valid extension (according to the effect that
380 * it would have on the perimeter of the region being extended) and
381 * returns the overall total.
383 * If called with index >= 0, it returns one of the possible
384 * colours depending on the value of index, in such a way that the
385 * number of possible inputs which would give rise to a given
386 * return value correspond to the weight of that value.
388 static int extend_options(int w
, int h
, int n
, int *map
,
389 int x
, int y
, int index
)
395 if (map
[y
*w
+x
] >= 0) {
397 return 0; /* can't do this square at all */
401 * Fetch the eight neighbours of this square, in order around
404 for (dy
= -1; dy
<= +1; dy
++)
405 for (dx
= -1; dx
<= +1; dx
++) {
406 int index
= (dy
< 0 ?
6-dx
: dy
> 0 ?
2+dx
: 2*(1+dx
));
407 if (x
+dx
>= 0 && x
+dx
< w
&& y
+dy
>= 0 && y
+dy
< h
)
408 col
[index
] = map
[(y
+dy
)*w
+(x
+dx
)];
414 * Iterate over each colour that might be feasible.
416 * FIXME: this routine currently has O(n) running time. We
417 * could turn it into O(FOUR) by only bothering to iterate over
418 * the colours mentioned in the four neighbouring squares.
421 for (c
= 0; c
< n
; c
++) {
422 int count
, neighbours
, runs
;
425 * One of the even indices of col (representing the
426 * orthogonal neighbours of this square) must be equal to
427 * c, or else this square is not adjacent to region c and
428 * obviously cannot become an extension of it at this time.
431 for (i
= 0; i
< 8; i
+= 2)
438 * Now we know this square is adjacent to region c. The
439 * next question is, would extending it cause the region to
440 * become non-simply-connected? If so, we mustn't do it.
442 * We determine this by looking around col to see if we can
443 * find more than one separate run of colour c.
446 for (i
= 0; i
< 8; i
++)
447 if (col
[i
] == c
&& col
[(i
+1) & 7] != c
)
455 * This square is a possibility. Determine its effect on
456 * the region's perimeter (computed from the number of
457 * orthogonal neighbours - 1 means a perimeter increase, 3
458 * a decrease, 2 no change; 4 is impossible because the
459 * region would already not be simply connected) and we're
462 assert(neighbours
> 0 && neighbours
< 4);
463 count
= (neighbours
== 1 ? WEIGHT_INCREASED
:
464 neighbours
== 2 ? WEIGHT_UNCHANGED
: WEIGHT_DECREASED
);
467 if (index
>= 0 && index
< count
)
478 static void genmap(int w
, int h
, int n
, int *map
, random_state
*rs
)
485 tmp
= snewn(wh
, int);
488 * Clear the map, and set up `tmp' as a list of grid indices.
490 for (i
= 0; i
< wh
; i
++) {
496 * Place the region seeds by selecting n members from `tmp'.
499 for (i
= 0; i
< n
; i
++) {
500 int j
= random_upto(rs
, k
);
506 * Re-initialise `tmp' as a cumulative frequency table. This
507 * will store the number of possible region colours we can
508 * extend into each square.
513 * Go through the grid and set up the initial cumulative
516 for (y
= 0; y
< h
; y
++)
517 for (x
= 0; x
< w
; x
++)
518 cf_add(tmp
, wh
, y
*w
+x
,
519 extend_options(w
, h
, n
, map
, x
, y
, -1));
522 * Now repeatedly choose a square we can extend a region into,
526 int k
= random_upto(rs
, tmp
[0]);
531 sq
= cf_whichsym(tmp
, wh
, k
);
532 k
-= cf_clookup(tmp
, wh
, sq
);
535 colour
= extend_options(w
, h
, n
, map
, x
, y
, k
);
540 * Re-scan the nine cells around the one we've just
543 for (yy
= max(y
-1, 0); yy
< min(y
+2, h
); yy
++)
544 for (xx
= max(x
-1, 0); xx
< min(x
+2, w
); xx
++) {
545 cf_add(tmp
, wh
, yy
*w
+xx
,
546 -cf_slookup(tmp
, wh
, yy
*w
+xx
) +
547 extend_options(w
, h
, n
, map
, xx
, yy
, -1));
552 * Finally, go through and normalise the region labels into
553 * order, meaning that indistinguishable maps are actually
556 for (i
= 0; i
< n
; i
++)
559 for (i
= 0; i
< wh
; i
++) {
563 map
[i
] = tmp
[map
[i
]];
569 /* ----------------------------------------------------------------------
570 * Functions to handle graphs.
574 * Having got a map in a square grid, convert it into a graph
577 static int gengraph(int w
, int h
, int n
, int *map
, int *graph
)
582 * Start by setting the graph up as an adjacency matrix. We'll
583 * turn it into a list later.
585 for (i
= 0; i
< n
*n
; i
++)
589 * Iterate over the map looking for all adjacencies.
591 for (y
= 0; y
< h
; y
++)
592 for (x
= 0; x
< w
; x
++) {
595 if (x
+1 < w
&& (vx
= map
[y
*w
+(x
+1)]) != v
)
596 graph
[v
*n
+vx
] = graph
[vx
*n
+v
] = 1;
597 if (y
+1 < h
&& (vy
= map
[(y
+1)*w
+x
]) != v
)
598 graph
[v
*n
+vy
] = graph
[vy
*n
+v
] = 1;
602 * Turn the matrix into a list.
604 for (i
= j
= 0; i
< n
*n
; i
++)
611 static int graph_edge_index(int *graph
, int n
, int ngraph
, int i
, int j
)
618 while (top
- bot
> 1) {
619 mid
= (top
+ bot
) / 2;
622 else if (graph
[mid
] < v
)
630 #define graph_adjacent(graph, n, ngraph, i, j) \
631 (graph_edge_index((graph), (n), (ngraph), (i), (j)) >= 0)
633 static int graph_vertex_start(int *graph
, int n
, int ngraph
, int i
)
640 while (top
- bot
> 1) {
641 mid
= (top
+ bot
) / 2;
650 /* ----------------------------------------------------------------------
651 * Generate a four-colouring of a graph.
653 * FIXME: it would be nice if we could convert this recursion into
654 * pseudo-recursion using some sort of explicit stack array, for
655 * the sake of the Palm port and its limited stack.
658 static int fourcolour_recurse(int *graph
, int n
, int ngraph
,
659 int *colouring
, int *scratch
, random_state
*rs
)
661 int nfree
, nvert
, start
, i
, j
, k
, c
, ci
;
665 * Find the smallest number of free colours in any uncoloured
666 * vertex, and count the number of such vertices.
669 nfree
= FIVE
; /* start off bigger than FOUR! */
671 for (i
= 0; i
< n
; i
++)
672 if (colouring
[i
] < 0 && scratch
[i
*FIVE
+FOUR
] <= nfree
) {
673 if (nfree
> scratch
[i
*FIVE
+FOUR
]) {
674 nfree
= scratch
[i
*FIVE
+FOUR
];
681 * If there aren't any uncoloured vertices at all, we're done.
684 return TRUE
; /* we've got a colouring! */
687 * Pick a random vertex in that set.
689 j
= random_upto(rs
, nvert
);
690 for (i
= 0; i
< n
; i
++)
691 if (colouring
[i
] < 0 && scratch
[i
*FIVE
+FOUR
] == nfree
)
695 start
= graph_vertex_start(graph
, n
, ngraph
, i
);
698 * Loop over the possible colours for i, and recurse for each
702 for (c
= 0; c
< FOUR
; c
++)
703 if (scratch
[i
*FIVE
+c
] == 0)
705 shuffle(cs
, ci
, sizeof(*cs
), rs
);
711 * Fill in this colour.
716 * Update the scratch space to reflect a new neighbour
717 * of this colour for each neighbour of vertex i.
719 for (j
= start
; j
< ngraph
&& graph
[j
] < n
*(i
+1); j
++) {
721 if (scratch
[k
*FIVE
+c
] == 0)
722 scratch
[k
*FIVE
+FOUR
]--;
729 if (fourcolour_recurse(graph
, n
, ngraph
, colouring
, scratch
, rs
))
730 return TRUE
; /* got one! */
733 * If that didn't work, clean up and try again with a
736 for (j
= start
; j
< ngraph
&& graph
[j
] < n
*(i
+1); j
++) {
739 if (scratch
[k
*FIVE
+c
] == 0)
740 scratch
[k
*FIVE
+FOUR
]++;
746 * If we reach here, we were unable to find a colouring at all.
747 * (This doesn't necessarily mean the Four Colour Theorem is
748 * violated; it might just mean we've gone down a dead end and
749 * need to back up and look somewhere else. It's only an FCT
750 * violation if we get all the way back up to the top level and
756 static void fourcolour(int *graph
, int n
, int ngraph
, int *colouring
,
763 * For each vertex and each colour, we store the number of
764 * neighbours that have that colour. Also, we store the number
765 * of free colours for the vertex.
767 scratch
= snewn(n
* FIVE
, int);
768 for (i
= 0; i
< n
* FIVE
; i
++)
769 scratch
[i
] = (i
% FIVE
== FOUR ? FOUR
: 0);
772 * Clear the colouring to start with.
774 for (i
= 0; i
< n
; i
++)
777 i
= fourcolour_recurse(graph
, n
, ngraph
, colouring
, scratch
, rs
);
778 assert(i
); /* by the Four Colour Theorem :-) */
783 /* ----------------------------------------------------------------------
784 * Non-recursive solver.
787 struct solver_scratch
{
788 unsigned char *possible
; /* bitmap of colours for each region */
794 static struct solver_scratch
*new_scratch(int *graph
, int n
, int ngraph
)
796 struct solver_scratch
*sc
;
798 sc
= snew(struct solver_scratch
);
802 sc
->possible
= snewn(n
, unsigned char);
807 static void free_scratch(struct solver_scratch
*sc
)
813 static int place_colour(struct solver_scratch
*sc
,
814 int *colouring
, int index
, int colour
)
816 int *graph
= sc
->graph
, n
= sc
->n
, ngraph
= sc
->ngraph
;
819 if (!(sc
->possible
[index
] & (1 << colour
)))
820 return FALSE
; /* can't do it */
822 sc
->possible
[index
] = 1 << colour
;
823 colouring
[index
] = colour
;
826 * Rule out this colour from all the region's neighbours.
828 for (j
= graph_vertex_start(graph
, n
, ngraph
, index
);
829 j
< ngraph
&& graph
[j
] < n
*(index
+1); j
++) {
830 k
= graph
[j
] - index
*n
;
831 sc
->possible
[k
] &= ~(1 << colour
);
838 * Returns 0 for impossible, 1 for success, 2 for failure to
839 * converge (i.e. puzzle is either ambiguous or just too
842 static int map_solver(struct solver_scratch
*sc
,
843 int *graph
, int n
, int ngraph
, int *colouring
,
849 * Initialise scratch space.
851 for (i
= 0; i
< n
; i
++)
852 sc
->possible
[i
] = (1 << FOUR
) - 1;
857 for (i
= 0; i
< n
; i
++)
858 if (colouring
[i
] >= 0) {
859 if (!place_colour(sc
, colouring
, i
, colouring
[i
]))
860 return 0; /* the clues aren't even consistent! */
864 * Now repeatedly loop until we find nothing further to do.
867 int done_something
= FALSE
;
869 if (difficulty
< DIFF_EASY
)
870 break; /* can't do anything at all! */
873 * Simplest possible deduction: find a region with only one
876 for (i
= 0; i
< n
; i
++) if (colouring
[i
] < 0) {
877 int p
= sc
->possible
[i
];
880 return 0; /* puzzle is inconsistent */
882 if ((p
& (p
-1)) == 0) { /* p is a power of two */
884 for (c
= 0; c
< FOUR
; c
++)
888 if (!place_colour(sc
, colouring
, i
, c
))
889 return 0; /* found puzzle to be inconsistent */
890 done_something
= TRUE
;
897 if (difficulty
< DIFF_NORMAL
)
898 break; /* can't do anything harder */
901 * Failing that, go up one level. Look for pairs of regions
902 * which (a) both have the same pair of possible colours,
903 * (b) are adjacent to one another, (c) are adjacent to the
904 * same region, and (d) that region still thinks it has one
905 * or both of those possible colours.
907 * Simplest way to do this is by going through the graph
908 * edge by edge, so that we start with property (b) and
909 * then look for (a) and finally (c) and (d).
911 for (i
= 0; i
< ngraph
; i
++) {
912 int j1
= graph
[i
] / n
, j2
= graph
[i
] % n
;
916 continue; /* done it already, other way round */
918 if (colouring
[j1
] >= 0 || colouring
[j2
] >= 0)
919 continue; /* they're not undecided */
921 if (sc
->possible
[j1
] != sc
->possible
[j2
])
922 continue; /* they don't have the same possibles */
924 v
= sc
->possible
[j1
];
926 * See if v contains exactly two set bits.
928 v2
= v
& -v
; /* find lowest set bit */
929 v2
= v
& ~v2
; /* clear it */
930 if (v2
== 0 || (v2
& (v2
-1)) != 0) /* not power of 2 */
934 * We've found regions j1 and j2 satisfying properties
935 * (a) and (b): they have two possible colours between
936 * them, and since they're adjacent to one another they
937 * must use _both_ those colours between them.
938 * Therefore, if they are both adjacent to any other
939 * region then that region cannot be either colour.
941 * Go through the neighbours of j1 and see if any are
944 for (j
= graph_vertex_start(graph
, n
, ngraph
, j1
);
945 j
< ngraph
&& graph
[j
] < n
*(j1
+1); j
++) {
947 if (graph_adjacent(graph
, n
, ngraph
, k
, j2
) &&
948 (sc
->possible
[k
] & v
)) {
949 sc
->possible
[k
] &= ~v
;
950 done_something
= TRUE
;
960 * We've run out of things to deduce. See if we've got the lot.
962 for (i
= 0; i
< n
; i
++)
963 if (colouring
[i
] < 0)
966 return 1; /* success! */
969 /* ----------------------------------------------------------------------
970 * Game generation main function.
973 static char *new_game_desc(game_params
*params
, random_state
*rs
,
974 char **aux
, int interactive
)
976 struct solver_scratch
*sc
= NULL
;
977 int *map
, *graph
, ngraph
, *colouring
, *colouring2
, *regions
;
978 int i
, j
, w
, h
, n
, solveret
, cfreq
[FOUR
];
981 #ifdef GENERATION_DIAGNOSTICS
994 map
= snewn(wh
, int);
995 graph
= snewn(n
*n
, int);
996 colouring
= snewn(n
, int);
997 colouring2
= snewn(n
, int);
998 regions
= snewn(n
, int);
1001 * This is the minimum difficulty below which we'll completely
1002 * reject a map design. Normally we set this to one below the
1003 * requested difficulty, ensuring that we have the right
1004 * result. However, for particularly dense maps or maps with
1005 * particularly few regions it might not be possible to get the
1006 * desired difficulty, so we will eventually drop this down to
1007 * -1 to indicate that any old map will do.
1009 mindiff
= params
->diff
;
1017 genmap(w
, h
, n
, map
, rs
);
1019 #ifdef GENERATION_DIAGNOSTICS
1020 for (y
= 0; y
< h
; y
++) {
1021 for (x
= 0; x
< w
; x
++) {
1026 putchar('a' + v
-36);
1028 putchar('A' + v
-10);
1037 * Convert the map into a graph.
1039 ngraph
= gengraph(w
, h
, n
, map
, graph
);
1041 #ifdef GENERATION_DIAGNOSTICS
1042 for (i
= 0; i
< ngraph
; i
++)
1043 printf("%d-%d\n", graph
[i
]/n
, graph
[i
]%n
);
1049 fourcolour(graph
, n
, ngraph
, colouring
, rs
);
1051 #ifdef GENERATION_DIAGNOSTICS
1052 for (i
= 0; i
< n
; i
++)
1053 printf("%d: %d\n", i
, colouring
[i
]);
1055 for (y
= 0; y
< h
; y
++) {
1056 for (x
= 0; x
< w
; x
++) {
1057 int v
= colouring
[map
[y
*w
+x
]];
1059 putchar('a' + v
-36);
1061 putchar('A' + v
-10);
1070 * Encode the solution as an aux string.
1072 if (*aux
) /* in case we've come round again */
1074 retlen
= retsize
= 0;
1076 for (i
= 0; i
< n
; i
++) {
1079 if (colouring
[i
] < 0)
1082 len
= sprintf(buf
, "%s%d:%d", i ?
";" : "S;", colouring
[i
], i
);
1083 if (retlen
+ len
>= retsize
) {
1084 retsize
= retlen
+ len
+ 256;
1085 ret
= sresize(ret
, retsize
, char);
1087 strcpy(ret
+ retlen
, buf
);
1093 * Remove the region colours one by one, keeping
1094 * solubility. Also ensure that there always remains at
1095 * least one region of every colour, so that the user can
1096 * drag from somewhere.
1098 for (i
= 0; i
< FOUR
; i
++)
1100 for (i
= 0; i
< n
; i
++) {
1102 cfreq
[colouring
[i
]]++;
1104 for (i
= 0; i
< FOUR
; i
++)
1108 shuffle(regions
, n
, sizeof(*regions
), rs
);
1110 if (sc
) free_scratch(sc
);
1111 sc
= new_scratch(graph
, n
, ngraph
);
1113 for (i
= 0; i
< n
; i
++) {
1116 if (cfreq
[colouring
[j
]] == 1)
1117 continue; /* can't remove last region of colour */
1119 memcpy(colouring2
, colouring
, n
*sizeof(int));
1121 solveret
= map_solver(sc
, graph
, n
, ngraph
, colouring2
,
1123 assert(solveret
>= 0); /* mustn't be impossible! */
1124 if (solveret
== 1) {
1125 cfreq
[colouring
[j
]]--;
1130 #ifdef GENERATION_DIAGNOSTICS
1131 for (i
= 0; i
< n
; i
++)
1132 if (colouring
[i
] >= 0) {
1136 putchar('a' + i
-36);
1138 putchar('A' + i
-10);
1141 printf(": %d\n", colouring
[i
]);
1146 * Finally, check that the puzzle is _at least_ as hard as
1147 * required, and indeed that it isn't already solved.
1148 * (Calling map_solver with negative difficulty ensures the
1149 * latter - if a solver which _does nothing_ can't solve
1150 * it, it's too easy!)
1152 memcpy(colouring2
, colouring
, n
*sizeof(int));
1153 if (map_solver(sc
, graph
, n
, ngraph
, colouring2
,
1154 mindiff
- 1) == 1) {
1156 * Drop minimum difficulty if necessary.
1158 if (mindiff
> 0 && (n
< 9 || n
> 3*wh
/2)) {
1160 mindiff
= 0; /* give up and go for Easy */
1169 * Encode as a game ID. We do this by:
1171 * - first going along the horizontal edges row by row, and
1172 * then the vertical edges column by column
1173 * - encoding the lengths of runs of edges and runs of
1175 * - the decoder will reconstitute the region boundaries from
1176 * this and automatically number them the same way we did
1177 * - then we encode the initial region colours in a Slant-like
1178 * fashion (digits 0-3 interspersed with letters giving
1179 * lengths of runs of empty spaces).
1181 retlen
= retsize
= 0;
1188 * Start with a notional non-edge, so that there'll be an
1189 * explicit `a' to distinguish the case where we start with
1195 for (i
= 0; i
< w
*(h
-1) + (w
-1)*h
; i
++) {
1196 int x
, y
, dx
, dy
, v
;
1199 /* Horizontal edge. */
1205 /* Vertical edge. */
1206 x
= (i
- w
*(h
-1)) / h
;
1207 y
= (i
- w
*(h
-1)) % h
;
1212 if (retlen
+ 10 >= retsize
) {
1213 retsize
= retlen
+ 256;
1214 ret
= sresize(ret
, retsize
, char);
1217 v
= (map
[y
*w
+x
] != map
[(y
+dy
)*w
+(x
+dx
)]);
1220 ret
[retlen
++] = 'a'-1 + run
;
1225 * 'z' is a special case in this encoding. Rather
1226 * than meaning a run of 26 and a state switch, it
1227 * means a run of 25 and _no_ state switch, because
1228 * otherwise there'd be no way to encode runs of
1232 ret
[retlen
++] = 'z';
1239 ret
[retlen
++] = 'a'-1 + run
;
1240 ret
[retlen
++] = ',';
1243 for (i
= 0; i
< n
; i
++) {
1244 if (retlen
+ 10 >= retsize
) {
1245 retsize
= retlen
+ 256;
1246 ret
= sresize(ret
, retsize
, char);
1249 if (colouring
[i
] < 0) {
1251 * In _this_ encoding, 'z' is a run of 26, since
1252 * there's no implicit state switch after each run.
1253 * Confusingly different, but more compact.
1256 ret
[retlen
++] = 'z';
1262 ret
[retlen
++] = 'a'-1 + run
;
1263 ret
[retlen
++] = '0' + colouring
[i
];
1268 ret
[retlen
++] = 'a'-1 + run
;
1271 assert(retlen
< retsize
);
1284 static char *parse_edge_list(game_params
*params
, char **desc
, int *map
)
1286 int w
= params
->w
, h
= params
->h
, wh
= w
*h
, n
= params
->n
;
1287 int i
, k
, pos
, state
;
1290 for (i
= 0; i
< wh
; i
++)
1297 * Parse the game description to get the list of edges, and
1298 * build up a disjoint set forest as we go (by identifying
1299 * pairs of squares whenever the edge list shows a non-edge).
1301 while (*p
&& *p
!= ',') {
1302 if (*p
< 'a' || *p
> 'z')
1303 return "Unexpected character in edge list";
1314 } else if (pos
< w
*(h
-1)) {
1315 /* Horizontal edge. */
1320 } else if (pos
< 2*wh
-w
-h
) {
1321 /* Vertical edge. */
1322 x
= (pos
- w
*(h
-1)) / h
;
1323 y
= (pos
- w
*(h
-1)) % h
;
1327 return "Too much data in edge list";
1329 dsf_merge(map
+wh
, y
*w
+x
, (y
+dy
)*w
+(x
+dx
));
1337 assert(pos
<= 2*wh
-w
-h
);
1339 return "Too little data in edge list";
1342 * Now go through again and allocate region numbers.
1345 for (i
= 0; i
< wh
; i
++)
1347 for (i
= 0; i
< wh
; i
++) {
1348 k
= dsf_canonify(map
+wh
, i
);
1354 return "Edge list defines the wrong number of regions";
1361 static char *validate_desc(game_params
*params
, char *desc
)
1363 int w
= params
->w
, h
= params
->h
, wh
= w
*h
, n
= params
->n
;
1368 map
= snewn(2*wh
, int);
1369 ret
= parse_edge_list(params
, &desc
, map
);
1375 return "Expected comma before clue list";
1376 desc
++; /* eat comma */
1380 if (*desc
>= '0' && *desc
< '0'+FOUR
)
1382 else if (*desc
>= 'a' && *desc
<= 'z')
1383 area
+= *desc
- 'a' + 1;
1385 return "Unexpected character in clue list";
1389 return "Too little data in clue list";
1391 return "Too much data in clue list";
1396 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1398 int w
= params
->w
, h
= params
->h
, wh
= w
*h
, n
= params
->n
;
1401 game_state
*state
= snew(game_state
);
1404 state
->colouring
= snewn(n
, int);
1405 for (i
= 0; i
< n
; i
++)
1406 state
->colouring
[i
] = -1;
1408 state
->completed
= state
->cheated
= FALSE
;
1410 state
->map
= snew(struct map
);
1411 state
->map
->refcount
= 1;
1412 state
->map
->map
= snewn(wh
*4, int);
1413 state
->map
->graph
= snewn(n
*n
, int);
1415 state
->map
->immutable
= snewn(n
, int);
1416 for (i
= 0; i
< n
; i
++)
1417 state
->map
->immutable
[i
] = FALSE
;
1423 ret
= parse_edge_list(params
, &p
, state
->map
->map
);
1428 * Set up the other three quadrants in `map'.
1430 for (i
= wh
; i
< 4*wh
; i
++)
1431 state
->map
->map
[i
] = state
->map
->map
[i
% wh
];
1437 * Now process the clue list.
1441 if (*p
>= '0' && *p
< '0'+FOUR
) {
1442 state
->colouring
[pos
] = *p
- '0';
1443 state
->map
->immutable
[pos
] = TRUE
;
1446 assert(*p
>= 'a' && *p
<= 'z');
1447 pos
+= *p
- 'a' + 1;
1453 state
->map
->ngraph
= gengraph(w
, h
, n
, state
->map
->map
, state
->map
->graph
);
1456 * Attempt to smooth out some of the more jagged region
1457 * outlines by the judicious use of diagonally divided squares.
1460 random_state
*rs
= random_init(desc
, strlen(desc
));
1461 int *squares
= snewn(wh
, int);
1464 for (i
= 0; i
< wh
; i
++)
1466 shuffle(squares
, wh
, sizeof(*squares
), rs
);
1469 done_something
= FALSE
;
1470 for (i
= 0; i
< wh
; i
++) {
1471 int y
= squares
[i
] / w
, x
= squares
[i
] % w
;
1472 int c
= state
->map
->map
[y
*w
+x
];
1475 if (x
== 0 || x
== w
-1 || y
== 0 || y
== h
-1)
1478 if (state
->map
->map
[TE
* wh
+ y
*w
+x
] !=
1479 state
->map
->map
[BE
* wh
+ y
*w
+x
])
1482 tc
= state
->map
->map
[BE
* wh
+ (y
-1)*w
+x
];
1483 bc
= state
->map
->map
[TE
* wh
+ (y
+1)*w
+x
];
1484 lc
= state
->map
->map
[RE
* wh
+ y
*w
+(x
-1)];
1485 rc
= state
->map
->map
[LE
* wh
+ y
*w
+(x
+1)];
1488 * If this square is adjacent on two sides to one
1489 * region and on the other two sides to the other
1490 * region, and is itself one of the two regions, we can
1491 * adjust it so that it's a diagonal.
1493 if (tc
!= bc
&& (tc
== c
|| bc
== c
)) {
1494 if ((lc
== tc
&& rc
== bc
) ||
1495 (lc
== bc
&& rc
== tc
)) {
1496 state
->map
->map
[TE
* wh
+ y
*w
+x
] = tc
;
1497 state
->map
->map
[BE
* wh
+ y
*w
+x
] = bc
;
1498 state
->map
->map
[LE
* wh
+ y
*w
+x
] = lc
;
1499 state
->map
->map
[RE
* wh
+ y
*w
+x
] = rc
;
1500 done_something
= TRUE
;
1504 } while (done_something
);
1510 * Analyse the map to find a canonical line segment
1511 * corresponding to each edge. These are where we'll eventually
1512 * put error markers.
1515 int *bestx
, *besty
, *an
, pass
;
1516 float *ax
, *ay
, *best
;
1518 ax
= snewn(state
->map
->ngraph
, float);
1519 ay
= snewn(state
->map
->ngraph
, float);
1520 an
= snewn(state
->map
->ngraph
, int);
1521 bestx
= snewn(state
->map
->ngraph
, int);
1522 besty
= snewn(state
->map
->ngraph
, int);
1523 best
= snewn(state
->map
->ngraph
, float);
1525 for (i
= 0; i
< state
->map
->ngraph
; i
++) {
1526 bestx
[i
] = besty
[i
] = -1;
1527 best
[i
] = 2*(w
+h
)+1;
1528 ax
[i
] = ay
[i
] = 0.0F
;
1533 * We make two passes over the map, finding all the line
1534 * segments separating regions. In the first pass, we
1535 * compute the _average_ x and y coordinate of all the line
1536 * segments separating each pair of regions; in the second
1537 * pass, for each such average point, we find the line
1538 * segment closest to it and call that canonical.
1540 * Line segments are considered to have coordinates in
1541 * their centre. Thus, at least one coordinate for any line
1542 * segment is always something-and-a-half; so we store our
1543 * coordinates as twice their normal value.
1545 for (pass
= 0; pass
< 2; pass
++) {
1548 for (y
= 0; y
< h
; y
++)
1549 for (x
= 0; x
< w
; x
++) {
1550 int ex
[3], ey
[3], ea
[3], eb
[3], en
= 0;
1553 * Look for an edge to the right of this
1554 * square, an edge below it, and an edge in the
1559 ea
[en
] = state
->map
->map
[RE
* wh
+ y
*w
+x
];
1560 eb
[en
] = state
->map
->map
[LE
* wh
+ y
*w
+(x
+1)];
1561 if (ea
[en
] != eb
[en
]) {
1569 ea
[en
] = state
->map
->map
[BE
* wh
+ y
*w
+x
];
1570 eb
[en
] = state
->map
->map
[TE
* wh
+ (y
+1)*w
+x
];
1571 if (ea
[en
] != eb
[en
]) {
1578 ea
[en
] = state
->map
->map
[TE
* wh
+ y
*w
+x
];
1579 eb
[en
] = state
->map
->map
[BE
* wh
+ y
*w
+x
];
1580 if (ea
[en
] != eb
[en
]) {
1587 * Now process the edges we've found, one by
1590 for (i
= 0; i
< en
; i
++) {
1591 int emin
= min(ea
[i
], eb
[i
]);
1592 int emax
= max(ea
[i
], eb
[i
]);
1594 graph_edge_index(state
->map
->graph
, n
,
1595 state
->map
->ngraph
, emin
, emax
);
1597 assert(gindex
>= 0);
1601 * In pass 0, accumulate the values
1602 * we'll use to compute the average
1605 ax
[gindex
] += ex
[i
];
1606 ay
[gindex
] += ey
[i
];
1610 * In pass 1, work out whether this
1611 * point is closer to the average than
1612 * the last one we've seen.
1616 assert(an
[gindex
] > 0);
1617 dx
= ex
[i
] - ax
[gindex
];
1618 dy
= ey
[i
] - ay
[gindex
];
1619 d
= sqrt(dx
*dx
+ dy
*dy
);
1620 if (d
< best
[gindex
]) {
1622 bestx
[gindex
] = ex
[i
];
1623 besty
[gindex
] = ey
[i
];
1630 for (i
= 0; i
< state
->map
->ngraph
; i
++)
1638 state
->map
->edgex
= bestx
;
1639 state
->map
->edgey
= besty
;
1641 for (i
= 0; i
< state
->map
->ngraph
; i
++)
1642 if (state
->map
->edgex
[i
] < 0) {
1643 /* Find the other representation of this edge. */
1644 int e
= state
->map
->graph
[i
];
1645 int iprime
= graph_edge_index(state
->map
->graph
, n
,
1646 state
->map
->ngraph
, e
%n
, e
/n
);
1647 assert(state
->map
->edgex
[iprime
] >= 0);
1648 state
->map
->edgex
[i
] = state
->map
->edgex
[iprime
];
1649 state
->map
->edgey
[i
] = state
->map
->edgey
[iprime
];
1661 static game_state
*dup_game(game_state
*state
)
1663 game_state
*ret
= snew(game_state
);
1666 ret
->colouring
= snewn(state
->p
.n
, int);
1667 memcpy(ret
->colouring
, state
->colouring
, state
->p
.n
* sizeof(int));
1668 ret
->map
= state
->map
;
1669 ret
->map
->refcount
++;
1670 ret
->completed
= state
->completed
;
1671 ret
->cheated
= state
->cheated
;
1676 static void free_game(game_state
*state
)
1678 if (--state
->map
->refcount
<= 0) {
1679 sfree(state
->map
->map
);
1680 sfree(state
->map
->graph
);
1681 sfree(state
->map
->immutable
);
1682 sfree(state
->map
->edgex
);
1683 sfree(state
->map
->edgey
);
1686 sfree(state
->colouring
);
1690 static char *solve_game(game_state
*state
, game_state
*currstate
,
1691 char *aux
, char **error
)
1698 struct solver_scratch
*sc
;
1702 int retlen
, retsize
;
1704 colouring
= snewn(state
->map
->n
, int);
1705 memcpy(colouring
, state
->colouring
, state
->map
->n
* sizeof(int));
1707 sc
= new_scratch(state
->map
->graph
, state
->map
->n
, state
->map
->ngraph
);
1708 sret
= map_solver(sc
, state
->map
->graph
, state
->map
->n
,
1709 state
->map
->ngraph
, colouring
, DIFFCOUNT
-1);
1715 *error
= "Puzzle is inconsistent";
1717 *error
= "Unable to find a unique solution for this puzzle";
1722 ret
= snewn(retsize
, char);
1726 for (i
= 0; i
< state
->map
->n
; i
++) {
1729 assert(colouring
[i
] >= 0);
1730 if (colouring
[i
] == currstate
->colouring
[i
])
1732 assert(!state
->map
->immutable
[i
]);
1734 len
= sprintf(buf
, ";%d:%d", colouring
[i
], i
);
1735 if (retlen
+ len
>= retsize
) {
1736 retsize
= retlen
+ len
+ 256;
1737 ret
= sresize(ret
, retsize
, char);
1739 strcpy(ret
+ retlen
, buf
);
1750 static char *game_text_format(game_state
*state
)
1756 int drag_colour
; /* -1 means no drag active */
1760 static game_ui
*new_ui(game_state
*state
)
1762 game_ui
*ui
= snew(game_ui
);
1763 ui
->dragx
= ui
->dragy
= -1;
1764 ui
->drag_colour
= -2;
1768 static void free_ui(game_ui
*ui
)
1773 static char *encode_ui(game_ui
*ui
)
1778 static void decode_ui(game_ui
*ui
, char *encoding
)
1782 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1783 game_state
*newstate
)
1787 struct game_drawstate
{
1789 unsigned short *drawn
, *todraw
;
1791 int dragx
, dragy
, drag_visible
;
1795 /* Flags in `drawn'. */
1796 #define ERR_T 0x0100
1797 #define ERR_B 0x0200
1798 #define ERR_L 0x0400
1799 #define ERR_R 0x0800
1800 #define ERR_C 0x1000
1801 #define ERR_MASK 0x1F00
1803 #define TILESIZE (ds->tilesize)
1804 #define BORDER (TILESIZE)
1805 #define COORD(x) ( (x) * TILESIZE + BORDER )
1806 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1808 static int region_from_coords(game_state
*state
, game_drawstate
*ds
,
1811 int w
= state
->p
.w
, h
= state
->p
.h
, wh
= w
*h
/*, n = state->p.n */;
1812 int tx
= FROMCOORD(x
), ty
= FROMCOORD(y
);
1813 int dx
= x
- COORD(tx
), dy
= y
- COORD(ty
);
1816 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
)
1817 return -1; /* border */
1819 quadrant
= 2 * (dx
> dy
) + (TILESIZE
- dx
> dy
);
1820 quadrant
= (quadrant
== 0 ? BE
:
1821 quadrant
== 1 ? LE
:
1822 quadrant
== 2 ? RE
: TE
);
1824 return state
->map
->map
[quadrant
* wh
+ ty
*w
+tx
];
1827 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1828 int x
, int y
, int button
)
1832 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
1833 int r
= region_from_coords(state
, ds
, x
, y
);
1836 ui
->drag_colour
= state
->colouring
[r
];
1838 ui
->drag_colour
= -1;
1844 if ((button
== LEFT_DRAG
|| button
== RIGHT_DRAG
) &&
1845 ui
->drag_colour
> -2) {
1851 if ((button
== LEFT_RELEASE
|| button
== RIGHT_RELEASE
) &&
1852 ui
->drag_colour
> -2) {
1853 int r
= region_from_coords(state
, ds
, x
, y
);
1854 int c
= ui
->drag_colour
;
1857 * Cancel the drag, whatever happens.
1859 ui
->drag_colour
= -2;
1860 ui
->dragx
= ui
->dragy
= -1;
1863 return ""; /* drag into border; do nothing else */
1865 if (state
->map
->immutable
[r
])
1866 return ""; /* can't change this region */
1868 if (state
->colouring
[r
] == c
)
1869 return ""; /* don't _need_ to change this region */
1871 sprintf(buf
, "%c:%d", (int)(c
< 0 ?
'C' : '0' + c
), r
);
1878 static game_state
*execute_move(game_state
*state
, char *move
)
1881 game_state
*ret
= dup_game(state
);
1886 if ((c
== 'C' || (c
>= '0' && c
< '0'+FOUR
)) &&
1887 sscanf(move
+1, ":%d%n", &k
, &adv
) == 1 &&
1888 k
>= 0 && k
< state
->p
.n
) {
1890 ret
->colouring
[k
] = (c
== 'C' ?
-1 : c
- '0');
1891 } else if (*move
== 'S') {
1893 ret
->cheated
= TRUE
;
1899 if (*move
&& *move
!= ';') {
1908 * Check for completion.
1910 if (!ret
->completed
) {
1913 for (i
= 0; i
< n
; i
++)
1914 if (ret
->colouring
[i
] < 0) {
1920 for (i
= 0; i
< ret
->map
->ngraph
; i
++) {
1921 int j
= ret
->map
->graph
[i
] / n
;
1922 int k
= ret
->map
->graph
[i
] % n
;
1923 if (ret
->colouring
[j
] == ret
->colouring
[k
]) {
1931 ret
->completed
= TRUE
;
1937 /* ----------------------------------------------------------------------
1941 static void game_compute_size(game_params
*params
, int tilesize
,
1944 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1945 struct { int tilesize
; } ads
, *ds
= &ads
;
1946 ads
.tilesize
= tilesize
;
1948 *x
= params
->w
* TILESIZE
+ 2 * BORDER
+ 1;
1949 *y
= params
->h
* TILESIZE
+ 2 * BORDER
+ 1;
1952 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1953 game_params
*params
, int tilesize
)
1955 ds
->tilesize
= tilesize
;
1958 blitter_free(dr
, ds
->bl
);
1959 ds
->bl
= blitter_new(dr
, TILESIZE
+3, TILESIZE
+3);
1962 const float map_colours
[FOUR
][3] = {
1966 {0.55F
, 0.45F
, 0.35F
},
1968 const int map_hatching
[FOUR
] = {
1969 HATCH_VERT
, HATCH_SLASH
, HATCH_HORIZ
, HATCH_BACKSLASH
1972 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1974 float *ret
= snewn(3 * NCOLOURS
, float);
1976 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1978 ret
[COL_GRID
* 3 + 0] = 0.0F
;
1979 ret
[COL_GRID
* 3 + 1] = 0.0F
;
1980 ret
[COL_GRID
* 3 + 2] = 0.0F
;
1982 memcpy(ret
+ COL_0
* 3, map_colours
[0], 3 * sizeof(float));
1983 memcpy(ret
+ COL_1
* 3, map_colours
[1], 3 * sizeof(float));
1984 memcpy(ret
+ COL_2
* 3, map_colours
[2], 3 * sizeof(float));
1985 memcpy(ret
+ COL_3
* 3, map_colours
[3], 3 * sizeof(float));
1987 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1988 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1989 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1991 ret
[COL_ERRTEXT
* 3 + 0] = 1.0F
;
1992 ret
[COL_ERRTEXT
* 3 + 1] = 1.0F
;
1993 ret
[COL_ERRTEXT
* 3 + 2] = 1.0F
;
1995 *ncolours
= NCOLOURS
;
1999 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2001 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2005 ds
->drawn
= snewn(state
->p
.w
* state
->p
.h
, unsigned short);
2006 for (i
= 0; i
< state
->p
.w
* state
->p
.h
; i
++)
2007 ds
->drawn
[i
] = 0xFFFF;
2008 ds
->todraw
= snewn(state
->p
.w
* state
->p
.h
, unsigned short);
2009 ds
->started
= FALSE
;
2011 ds
->drag_visible
= FALSE
;
2012 ds
->dragx
= ds
->dragy
= -1;
2017 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2022 blitter_free(dr
, ds
->bl
);
2026 static void draw_error(drawing
*dr
, game_drawstate
*ds
, int x
, int y
)
2034 coords
[0] = x
- TILESIZE
*2/5;
2037 coords
[3] = y
- TILESIZE
*2/5;
2038 coords
[4] = x
+ TILESIZE
*2/5;
2041 coords
[7] = y
+ TILESIZE
*2/5;
2042 draw_polygon(dr
, coords
, 4, COL_ERROR
, COL_GRID
);
2045 * Draw an exclamation mark in the diamond. This turns out to
2046 * look unpleasantly off-centre if done via draw_text, so I do
2047 * it by hand on the basis that exclamation marks aren't that
2048 * difficult to draw...
2051 yext
= TILESIZE
*2/5 - (xext
*2+2);
2052 draw_rect(dr
, x
-xext
, y
-yext
, xext
*2+1, yext
*2+1 - (xext
*3+1),
2054 draw_rect(dr
, x
-xext
, y
+yext
-xext
*2, xext
*2+1, xext
*2+1, COL_ERRTEXT
);
2057 static void draw_square(drawing
*dr
, game_drawstate
*ds
,
2058 game_params
*params
, struct map
*map
,
2059 int x
, int y
, int v
)
2061 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
2064 errs
= v
& ERR_MASK
;
2069 clip(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
);
2072 * Draw the region colour.
2074 draw_rect(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
,
2075 (tv
== FOUR ? COL_BACKGROUND
: COL_0
+ tv
));
2077 * Draw the second region colour, if this is a diagonally
2080 if (map
->map
[TE
* wh
+ y
*w
+x
] != map
->map
[BE
* wh
+ y
*w
+x
]) {
2082 coords
[0] = COORD(x
)-1;
2083 coords
[1] = COORD(y
+1)+1;
2084 if (map
->map
[LE
* wh
+ y
*w
+x
] == map
->map
[TE
* wh
+ y
*w
+x
])
2085 coords
[2] = COORD(x
+1)+1;
2087 coords
[2] = COORD(x
)-1;
2088 coords
[3] = COORD(y
)-1;
2089 coords
[4] = COORD(x
+1)+1;
2090 coords
[5] = COORD(y
+1)+1;
2091 draw_polygon(dr
, coords
, 3,
2092 (bv
== FOUR ? COL_BACKGROUND
: COL_0
+ bv
), COL_GRID
);
2096 * Draw the grid lines, if required.
2098 if (x
<= 0 || map
->map
[RE
*wh
+y
*w
+(x
-1)] != map
->map
[LE
*wh
+y
*w
+x
])
2099 draw_rect(dr
, COORD(x
), COORD(y
), 1, TILESIZE
, COL_GRID
);
2100 if (y
<= 0 || map
->map
[BE
*wh
+(y
-1)*w
+x
] != map
->map
[TE
*wh
+y
*w
+x
])
2101 draw_rect(dr
, COORD(x
), COORD(y
), TILESIZE
, 1, COL_GRID
);
2102 if (x
<= 0 || y
<= 0 ||
2103 map
->map
[RE
*wh
+(y
-1)*w
+(x
-1)] != map
->map
[TE
*wh
+y
*w
+x
] ||
2104 map
->map
[BE
*wh
+(y
-1)*w
+(x
-1)] != map
->map
[LE
*wh
+y
*w
+x
])
2105 draw_rect(dr
, COORD(x
), COORD(y
), 1, 1, COL_GRID
);
2108 * Draw error markers.
2111 draw_error(dr
, ds
, COORD(x
)+TILESIZE
/2, COORD(y
));
2113 draw_error(dr
, ds
, COORD(x
), COORD(y
)+TILESIZE
/2);
2115 draw_error(dr
, ds
, COORD(x
)+TILESIZE
/2, COORD(y
+1));
2117 draw_error(dr
, ds
, COORD(x
+1), COORD(y
)+TILESIZE
/2);
2119 draw_error(dr
, ds
, COORD(x
)+TILESIZE
/2, COORD(y
)+TILESIZE
/2);
2123 draw_update(dr
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
);
2126 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2127 game_state
*state
, int dir
, game_ui
*ui
,
2128 float animtime
, float flashtime
)
2130 int w
= state
->p
.w
, h
= state
->p
.h
, wh
= w
*h
, n
= state
->p
.n
;
2134 if (ds
->drag_visible
) {
2135 blitter_load(dr
, ds
->bl
, ds
->dragx
, ds
->dragy
);
2136 draw_update(dr
, ds
->dragx
, ds
->dragy
, TILESIZE
+ 3, TILESIZE
+ 3);
2137 ds
->drag_visible
= FALSE
;
2141 * The initial contents of the window are not guaranteed and
2142 * can vary with front ends. To be on the safe side, all games
2143 * should start by drawing a big background-colour rectangle
2144 * covering the whole window.
2149 game_compute_size(&state
->p
, TILESIZE
, &ww
, &wh
);
2150 draw_rect(dr
, 0, 0, ww
, wh
, COL_BACKGROUND
);
2151 draw_rect(dr
, COORD(0), COORD(0), w
*TILESIZE
+1, h
*TILESIZE
+1,
2154 draw_update(dr
, 0, 0, ww
, wh
);
2159 if (flash_type
== 1)
2160 flash
= (int)(flashtime
* FOUR
/ flash_length
);
2162 flash
= 1 + (int)(flashtime
* THREE
/ flash_length
);
2167 * Set up the `todraw' array.
2169 for (y
= 0; y
< h
; y
++)
2170 for (x
= 0; x
< w
; x
++) {
2171 int tv
= state
->colouring
[state
->map
->map
[TE
* wh
+ y
*w
+x
]];
2172 int bv
= state
->colouring
[state
->map
->map
[BE
* wh
+ y
*w
+x
]];
2181 if (flash_type
== 1) {
2186 } else if (flash_type
== 2) {
2191 tv
= (tv
+ flash
) % FOUR
;
2193 bv
= (bv
+ flash
) % FOUR
;
2199 ds
->todraw
[y
*w
+x
] = v
;
2203 * Add error markers to the `todraw' array.
2205 for (i
= 0; i
< state
->map
->ngraph
; i
++) {
2206 int v1
= state
->map
->graph
[i
] / n
;
2207 int v2
= state
->map
->graph
[i
] % n
;
2209 if (state
->colouring
[v1
] < 0 || state
->colouring
[v2
] < 0)
2211 if (state
->colouring
[v1
] != state
->colouring
[v2
])
2214 x
= state
->map
->edgex
[i
];
2215 y
= state
->map
->edgey
[i
];
2217 if (x
% 2 && y
% 2) {
2218 ds
->todraw
[(y
/2)*w
+(x
/2)] |= ERR_C
;
2220 ds
->todraw
[(y
/2-1)*w
+(x
/2)] |= ERR_B
;
2221 ds
->todraw
[(y
/2)*w
+(x
/2)] |= ERR_T
;
2224 ds
->todraw
[(y
/2)*w
+(x
/2-1)] |= ERR_R
;
2225 ds
->todraw
[(y
/2)*w
+(x
/2)] |= ERR_L
;
2230 * Now actually draw everything.
2232 for (y
= 0; y
< h
; y
++)
2233 for (x
= 0; x
< w
; x
++) {
2234 int v
= ds
->todraw
[y
*w
+x
];
2235 if (ds
->drawn
[y
*w
+x
] != v
) {
2236 draw_square(dr
, ds
, &state
->p
, state
->map
, x
, y
, v
);
2237 ds
->drawn
[y
*w
+x
] = v
;
2242 * Draw the dragged colour blob if any.
2244 if (ui
->drag_colour
> -2) {
2245 ds
->dragx
= ui
->dragx
- TILESIZE
/2 - 2;
2246 ds
->dragy
= ui
->dragy
- TILESIZE
/2 - 2;
2247 blitter_save(dr
, ds
->bl
, ds
->dragx
, ds
->dragy
);
2248 draw_circle(dr
, ui
->dragx
, ui
->dragy
, TILESIZE
/2,
2249 (ui
->drag_colour
< 0 ? COL_BACKGROUND
:
2250 COL_0
+ ui
->drag_colour
), COL_GRID
);
2251 draw_update(dr
, ds
->dragx
, ds
->dragy
, TILESIZE
+ 3, TILESIZE
+ 3);
2252 ds
->drag_visible
= TRUE
;
2256 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2257 int dir
, game_ui
*ui
)
2262 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2263 int dir
, game_ui
*ui
)
2265 if (!oldstate
->completed
&& newstate
->completed
&&
2266 !oldstate
->cheated
&& !newstate
->cheated
) {
2267 if (flash_type
< 0) {
2268 char *env
= getenv("MAP_ALTERNATIVE_FLASH");
2270 flash_type
= atoi(env
);
2273 flash_length
= (flash_type
== 1 ?
0.50 : 0.30);
2275 return flash_length
;
2280 static int game_wants_statusbar(void)
2285 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2290 static void game_print_size(game_params
*params
, float *x
, float *y
)
2295 * I'll use 4mm squares by default, I think. Simplest way to
2296 * compute this size is to compute the pixel puzzle size at a
2297 * given tile size and then scale.
2299 game_compute_size(params
, 400, &pw
, &ph
);
2304 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2306 int w
= state
->p
.w
, h
= state
->p
.h
, wh
= w
*h
, n
= state
->p
.n
;
2307 int ink
, c
[FOUR
], i
;
2309 int *coords
, ncoords
, coordsize
;
2311 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2312 struct { int tilesize
; } ads
, *ds
= &ads
;
2313 ads
.tilesize
= tilesize
;
2315 ink
= print_mono_colour(dr
, 0);
2316 for (i
= 0; i
< FOUR
; i
++)
2317 c
[i
] = print_rgb_colour(dr
, map_hatching
[i
], map_colours
[i
][0],
2318 map_colours
[i
][1], map_colours
[i
][2]);
2323 print_line_width(dr
, TILESIZE
/ 16);
2326 * Draw a single filled polygon around each region.
2328 for (r
= 0; r
< n
; r
++) {
2329 int octants
[8], lastdir
, d1
, d2
, ox
, oy
;
2332 * Start by finding a point on the region boundary. Any
2333 * point will do. To do this, we'll search for a square
2334 * containing the region and then decide which corner of it
2338 for (y
= 0; y
< h
; y
++) {
2339 for (x
= 0; x
< w
; x
++) {
2340 if (state
->map
->map
[wh
*0+y
*w
+x
] == r
||
2341 state
->map
->map
[wh
*1+y
*w
+x
] == r
||
2342 state
->map
->map
[wh
*2+y
*w
+x
] == r
||
2343 state
->map
->map
[wh
*3+y
*w
+x
] == r
)
2349 assert(y
< h
&& x
< w
); /* we must have found one somewhere */
2351 * This is the first square in lexicographic order which
2352 * contains part of this region. Therefore, one of the top
2353 * two corners of the square must be what we're after. The
2354 * only case in which it isn't the top left one is if the
2355 * square is diagonally divided and the region is in the
2356 * bottom right half.
2358 if (state
->map
->map
[wh
*TE
+y
*w
+x
] != r
&&
2359 state
->map
->map
[wh
*LE
+y
*w
+x
] != r
)
2360 x
++; /* could just as well have done y++ */
2363 * Now we have a point on the region boundary. Trace around
2364 * the region until we come back to this point,
2365 * accumulating coordinates for a polygon draw operation as
2375 * There are eight possible directions we could head in
2376 * from here. We identify them by octant numbers, and
2377 * we also use octant numbers to identify the spaces
2390 octants
[0] = x
<w
&& y
>0 ? state
->map
->map
[wh
*LE
+(y
-1)*w
+x
] : -1;
2391 octants
[1] = x
<w
&& y
>0 ? state
->map
->map
[wh
*BE
+(y
-1)*w
+x
] : -1;
2392 octants
[2] = x
<w
&& y
<h ? state
->map
->map
[wh
*TE
+y
*w
+x
] : -1;
2393 octants
[3] = x
<w
&& y
<h ? state
->map
->map
[wh
*LE
+y
*w
+x
] : -1;
2394 octants
[4] = x
>0 && y
<h ? state
->map
->map
[wh
*RE
+y
*w
+(x
-1)] : -1;
2395 octants
[5] = x
>0 && y
<h ? state
->map
->map
[wh
*TE
+y
*w
+(x
-1)] : -1;
2396 octants
[6] = x
>0 && y
>0 ? state
->map
->map
[wh
*BE
+(y
-1)*w
+(x
-1)] :-1;
2397 octants
[7] = x
>0 && y
>0 ? state
->map
->map
[wh
*RE
+(y
-1)*w
+(x
-1)] :-1;
2400 for (i
= 0; i
< 8; i
++)
2401 if ((octants
[i
] == r
) ^ (octants
[(i
+1)%8] == r
)) {
2408 /* printf("%% %d,%d r=%d: d1=%d d2=%d lastdir=%d\n", x, y, r, d1, d2, lastdir); */
2409 assert(d1
!= -1 && d2
!= -1);
2414 * Now we're heading in direction d1. Save the current
2417 if (ncoords
+ 2 > coordsize
) {
2419 coords
= sresize(coords
, coordsize
, int);
2421 coords
[ncoords
++] = COORD(x
);
2422 coords
[ncoords
++] = COORD(y
);
2425 * Compute the new coordinates.
2427 x
+= (d1
% 4 == 3 ?
0 : d1
< 4 ?
+1 : -1);
2428 y
+= (d1
% 4 == 1 ?
0 : d1
> 1 && d1
< 5 ?
+1 : -1);
2429 assert(x
>= 0 && x
<= w
&& y
>= 0 && y
<= h
);
2432 } while (x
!= ox
|| y
!= oy
);
2434 draw_polygon(dr
, coords
, ncoords
/2,
2435 state
->colouring
[r
] >= 0 ?
2436 c
[state
->colouring
[r
]] : -1, ink
);
2445 const struct game thegame
= {
2453 TRUE
, game_configure
, custom_params
,
2461 FALSE
, game_text_format
,
2469 20, game_compute_size
, game_set_size
,
2472 game_free_drawstate
,
2476 TRUE
, TRUE
, game_print_size
, game_print
,
2477 game_wants_statusbar
,
2478 FALSE
, game_timing_state
,
2479 0, /* mouse_priorities */