2 * map.c: Game involving four-colouring a map.
9 * - better four-colouring algorithm?
10 * - ability to drag a set of pencil marks?
23 * In standalone solver mode, `verbose' is a variable which can be
24 * set by command-line option; in debugging mode it's simply always
27 #if defined STANDALONE_SOLVER
28 #define SOLVER_DIAGNOSTICS
30 #elif defined SOLVER_DIAGNOSTICS
35 * I don't seriously anticipate wanting to change the number of
36 * colours used in this game, but it doesn't cost much to use a
37 * #define just in case :-)
40 #define THREE (FOUR-1)
45 * Ghastly run-time configuration option, just for Gareth (again).
47 static int flash_type = -1;
48 static float flash_length;
51 * Difficulty levels. I do some macro ickery here to ensure that my
52 * enum and the various forms of my name list always match up.
58 A(RECURSE,Unreasonable,u)
59 #define ENUM(upper,title,lower) DIFF_ ## upper,
60 #define TITLE(upper,title,lower) #title,
61 #define ENCODE(upper,title,lower) #lower
62 #define CONFIG(upper,title,lower) ":" #title
63 enum { DIFFLIST(ENUM) DIFFCOUNT };
64 static char const *const map_diffnames[] = { DIFFLIST(TITLE) };
65 static char const map_diffchars[] = DIFFLIST(ENCODE);
66 #define DIFFCONFIG DIFFLIST(CONFIG)
68 enum { TE, BE, LE, RE }; /* top/bottom/left/right edges */
73 COL_0, COL_1, COL_2, COL_3,
74 COL_ERROR, COL_ERRTEXT,
89 int *edgex, *edgey; /* position of a point on each edge */
90 int *regionx, *regiony; /* position of a point in each region */
96 int *colouring, *pencil;
97 int completed, cheated;
100 static game_params *default_params(void)
102 game_params *ret = snew(game_params);
107 ret->diff = DIFF_NORMAL;
112 static const struct game_params map_presets[] = {
113 {20, 15, 30, DIFF_EASY},
114 {20, 15, 30, DIFF_NORMAL},
115 {20, 15, 30, DIFF_HARD},
116 {20, 15, 30, DIFF_RECURSE},
117 {30, 25, 75, DIFF_NORMAL},
118 {30, 25, 75, DIFF_HARD},
121 static int game_fetch_preset(int i, char **name, game_params **params)
126 if (i < 0 || i >= lenof(map_presets))
129 ret = snew(game_params);
130 *ret = map_presets[i];
132 sprintf(str, "%dx%d, %d regions, %s", ret->w, ret->h, ret->n,
133 map_diffnames[ret->diff]);
140 static void free_params(game_params *params)
145 static game_params *dup_params(game_params *params)
147 game_params *ret = snew(game_params);
148 *ret = *params; /* structure copy */
152 static void decode_params(game_params *params, char const *string)
154 char const *p = string;
157 while (*p && isdigit((unsigned char)*p)) p++;
161 while (*p && isdigit((unsigned char)*p)) p++;
163 params->h = params->w;
168 while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
170 params->n = params->w * params->h / 8;
175 for (i = 0; i < DIFFCOUNT; i++)
176 if (*p == map_diffchars[i])
182 static char *encode_params(game_params *params, int full)
186 sprintf(ret, "%dx%dn%d", params->w, params->h, params->n);
188 sprintf(ret + strlen(ret), "d%c", map_diffchars[params->diff]);
193 static config_item *game_configure(game_params *params)
198 ret = snewn(5, config_item);
200 ret[0].name = "Width";
201 ret[0].type = C_STRING;
202 sprintf(buf, "%d", params->w);
203 ret[0].sval = dupstr(buf);
206 ret[1].name = "Height";
207 ret[1].type = C_STRING;
208 sprintf(buf, "%d", params->h);
209 ret[1].sval = dupstr(buf);
212 ret[2].name = "Regions";
213 ret[2].type = C_STRING;
214 sprintf(buf, "%d", params->n);
215 ret[2].sval = dupstr(buf);
218 ret[3].name = "Difficulty";
219 ret[3].type = C_CHOICES;
220 ret[3].sval = DIFFCONFIG;
221 ret[3].ival = params->diff;
231 static game_params *custom_params(config_item *cfg)
233 game_params *ret = snew(game_params);
235 ret->w = atoi(cfg[0].sval);
236 ret->h = atoi(cfg[1].sval);
237 ret->n = atoi(cfg[2].sval);
238 ret->diff = cfg[3].ival;
243 static char *validate_params(game_params *params, int full)
245 if (params->w < 2 || params->h < 2)
246 return "Width and height must be at least two";
248 return "Must have at least five regions";
249 if (params->n > params->w * params->h)
250 return "Too many regions to fit in grid";
254 /* ----------------------------------------------------------------------
255 * Cumulative frequency table functions.
259 * Initialise a cumulative frequency table. (Hardly worth writing
260 * this function; all it does is to initialise everything in the
263 static void cf_init(int *table, int n)
267 for (i = 0; i < n; i++)
272 * Increment the count of symbol `sym' by `count'.
274 static void cf_add(int *table, int n, int sym, int count)
291 * Cumulative frequency lookup: return the total count of symbols
292 * with value less than `sym'.
294 static int cf_clookup(int *table, int n, int sym)
296 int bit, index, limit, count;
301 assert(0 < sym && sym <= n);
303 count = table[0]; /* start with the whole table size */
313 * Find the least number with its lowest set bit in this
314 * position which is greater than or equal to sym.
316 index = ((sym + bit - 1) &~ (bit * 2 - 1)) + bit;
319 count -= table[index];
330 * Single frequency lookup: return the count of symbol `sym'.
332 static int cf_slookup(int *table, int n, int sym)
336 assert(0 <= sym && sym < n);
340 for (bit = 1; sym+bit < n && !(sym & bit); bit <<= 1)
341 count -= table[sym+bit];
347 * Return the largest symbol index such that the cumulative
348 * frequency up to that symbol is less than _or equal to_ count.
350 static int cf_whichsym(int *table, int n, int count) {
353 assert(count >= 0 && count < table[0]);
364 if (count >= top - table[sym+bit])
367 top -= table[sym+bit];
376 /* ----------------------------------------------------------------------
379 * FIXME: this isn't entirely optimal at present, because it
380 * inherently prioritises growing the largest region since there
381 * are more squares adjacent to it. This acts as a destabilising
382 * influence leading to a few large regions and mostly small ones.
383 * It might be better to do it some other way.
386 #define WEIGHT_INCREASED 2 /* for increased perimeter */
387 #define WEIGHT_DECREASED 4 /* for decreased perimeter */
388 #define WEIGHT_UNCHANGED 3 /* for unchanged perimeter */
391 * Look at a square and decide which colours can be extended into
394 * If called with index < 0, it adds together one of
395 * WEIGHT_INCREASED, WEIGHT_DECREASED or WEIGHT_UNCHANGED for each
396 * colour that has a valid extension (according to the effect that
397 * it would have on the perimeter of the region being extended) and
398 * returns the overall total.
400 * If called with index >= 0, it returns one of the possible
401 * colours depending on the value of index, in such a way that the
402 * number of possible inputs which would give rise to a given
403 * return value correspond to the weight of that value.
405 static int extend_options(int w, int h, int n, int *map,
406 int x, int y, int index)
412 if (map[y*w+x] >= 0) {
414 return 0; /* can't do this square at all */
418 * Fetch the eight neighbours of this square, in order around
421 for (dy = -1; dy <= +1; dy++)
422 for (dx = -1; dx <= +1; dx++) {
423 int index = (dy < 0 ? 6-dx : dy > 0 ? 2+dx : 2*(1+dx));
424 if (x+dx >= 0 && x+dx < w && y+dy >= 0 && y+dy < h)
425 col[index] = map[(y+dy)*w+(x+dx)];
431 * Iterate over each colour that might be feasible.
433 * FIXME: this routine currently has O(n) running time. We
434 * could turn it into O(FOUR) by only bothering to iterate over
435 * the colours mentioned in the four neighbouring squares.
438 for (c = 0; c < n; c++) {
439 int count, neighbours, runs;
442 * One of the even indices of col (representing the
443 * orthogonal neighbours of this square) must be equal to
444 * c, or else this square is not adjacent to region c and
445 * obviously cannot become an extension of it at this time.
448 for (i = 0; i < 8; i += 2)
455 * Now we know this square is adjacent to region c. The
456 * next question is, would extending it cause the region to
457 * become non-simply-connected? If so, we mustn't do it.
459 * We determine this by looking around col to see if we can
460 * find more than one separate run of colour c.
463 for (i = 0; i < 8; i++)
464 if (col[i] == c && col[(i+1) & 7] != c)
472 * This square is a possibility. Determine its effect on
473 * the region's perimeter (computed from the number of
474 * orthogonal neighbours - 1 means a perimeter increase, 3
475 * a decrease, 2 no change; 4 is impossible because the
476 * region would already not be simply connected) and we're
479 assert(neighbours > 0 && neighbours < 4);
480 count = (neighbours == 1 ? WEIGHT_INCREASED :
481 neighbours == 2 ? WEIGHT_UNCHANGED : WEIGHT_DECREASED);
484 if (index >= 0 && index < count)
495 static void genmap(int w, int h, int n, int *map, random_state *rs)
502 tmp = snewn(wh, int);
505 * Clear the map, and set up `tmp' as a list of grid indices.
507 for (i = 0; i < wh; i++) {
513 * Place the region seeds by selecting n members from `tmp'.
516 for (i = 0; i < n; i++) {
517 int j = random_upto(rs, k);
523 * Re-initialise `tmp' as a cumulative frequency table. This
524 * will store the number of possible region colours we can
525 * extend into each square.
530 * Go through the grid and set up the initial cumulative
533 for (y = 0; y < h; y++)
534 for (x = 0; x < w; x++)
535 cf_add(tmp, wh, y*w+x,
536 extend_options(w, h, n, map, x, y, -1));
539 * Now repeatedly choose a square we can extend a region into,
543 int k = random_upto(rs, tmp[0]);
548 sq = cf_whichsym(tmp, wh, k);
549 k -= cf_clookup(tmp, wh, sq);
552 colour = extend_options(w, h, n, map, x, y, k);
557 * Re-scan the nine cells around the one we've just
560 for (yy = max(y-1, 0); yy < min(y+2, h); yy++)
561 for (xx = max(x-1, 0); xx < min(x+2, w); xx++) {
562 cf_add(tmp, wh, yy*w+xx,
563 -cf_slookup(tmp, wh, yy*w+xx) +
564 extend_options(w, h, n, map, xx, yy, -1));
569 * Finally, go through and normalise the region labels into
570 * order, meaning that indistinguishable maps are actually
573 for (i = 0; i < n; i++)
576 for (i = 0; i < wh; i++) {
580 map[i] = tmp[map[i]];
586 /* ----------------------------------------------------------------------
587 * Functions to handle graphs.
591 * Having got a map in a square grid, convert it into a graph
594 static int gengraph(int w, int h, int n, int *map, int *graph)
599 * Start by setting the graph up as an adjacency matrix. We'll
600 * turn it into a list later.
602 for (i = 0; i < n*n; i++)
606 * Iterate over the map looking for all adjacencies.
608 for (y = 0; y < h; y++)
609 for (x = 0; x < w; x++) {
612 if (x+1 < w && (vx = map[y*w+(x+1)]) != v)
613 graph[v*n+vx] = graph[vx*n+v] = 1;
614 if (y+1 < h && (vy = map[(y+1)*w+x]) != v)
615 graph[v*n+vy] = graph[vy*n+v] = 1;
619 * Turn the matrix into a list.
621 for (i = j = 0; i < n*n; i++)
628 static int graph_edge_index(int *graph, int n, int ngraph, int i, int j)
635 while (top - bot > 1) {
636 mid = (top + bot) / 2;
639 else if (graph[mid] < v)
647 #define graph_adjacent(graph, n, ngraph, i, j) \
648 (graph_edge_index((graph), (n), (ngraph), (i), (j)) >= 0)
650 static int graph_vertex_start(int *graph, int n, int ngraph, int i)
657 while (top - bot > 1) {
658 mid = (top + bot) / 2;
667 /* ----------------------------------------------------------------------
668 * Generate a four-colouring of a graph.
670 * FIXME: it would be nice if we could convert this recursion into
671 * pseudo-recursion using some sort of explicit stack array, for
672 * the sake of the Palm port and its limited stack.
675 static int fourcolour_recurse(int *graph, int n, int ngraph,
676 int *colouring, int *scratch, random_state *rs)
678 int nfree, nvert, start, i, j, k, c, ci;
682 * Find the smallest number of free colours in any uncoloured
683 * vertex, and count the number of such vertices.
686 nfree = FIVE; /* start off bigger than FOUR! */
688 for (i = 0; i < n; i++)
689 if (colouring[i] < 0 && scratch[i*FIVE+FOUR] <= nfree) {
690 if (nfree > scratch[i*FIVE+FOUR]) {
691 nfree = scratch[i*FIVE+FOUR];
698 * If there aren't any uncoloured vertices at all, we're done.
701 return TRUE; /* we've got a colouring! */
704 * Pick a random vertex in that set.
706 j = random_upto(rs, nvert);
707 for (i = 0; i < n; i++)
708 if (colouring[i] < 0 && scratch[i*FIVE+FOUR] == nfree)
712 start = graph_vertex_start(graph, n, ngraph, i);
715 * Loop over the possible colours for i, and recurse for each
719 for (c = 0; c < FOUR; c++)
720 if (scratch[i*FIVE+c] == 0)
722 shuffle(cs, ci, sizeof(*cs), rs);
728 * Fill in this colour.
733 * Update the scratch space to reflect a new neighbour
734 * of this colour for each neighbour of vertex i.
736 for (j = start; j < ngraph && graph[j] < n*(i+1); j++) {
738 if (scratch[k*FIVE+c] == 0)
739 scratch[k*FIVE+FOUR]--;
746 if (fourcolour_recurse(graph, n, ngraph, colouring, scratch, rs))
747 return TRUE; /* got one! */
750 * If that didn't work, clean up and try again with a
753 for (j = start; j < ngraph && graph[j] < n*(i+1); j++) {
756 if (scratch[k*FIVE+c] == 0)
757 scratch[k*FIVE+FOUR]++;
763 * If we reach here, we were unable to find a colouring at all.
764 * (This doesn't necessarily mean the Four Colour Theorem is
765 * violated; it might just mean we've gone down a dead end and
766 * need to back up and look somewhere else. It's only an FCT
767 * violation if we get all the way back up to the top level and
773 static void fourcolour(int *graph, int n, int ngraph, int *colouring,
780 * For each vertex and each colour, we store the number of
781 * neighbours that have that colour. Also, we store the number
782 * of free colours for the vertex.
784 scratch = snewn(n * FIVE, int);
785 for (i = 0; i < n * FIVE; i++)
786 scratch[i] = (i % FIVE == FOUR ? FOUR : 0);
789 * Clear the colouring to start with.
791 for (i = 0; i < n; i++)
794 i = fourcolour_recurse(graph, n, ngraph, colouring, scratch, rs);
795 assert(i); /* by the Four Colour Theorem :-) */
800 /* ----------------------------------------------------------------------
801 * Non-recursive solver.
804 struct solver_scratch {
805 unsigned char *possible; /* bitmap of colours for each region */
813 #ifdef SOLVER_DIAGNOSTICS
820 static struct solver_scratch *new_scratch(int *graph, int n, int ngraph)
822 struct solver_scratch *sc;
824 sc = snew(struct solver_scratch);
828 sc->possible = snewn(n, unsigned char);
830 sc->bfsqueue = snewn(n, int);
831 sc->bfscolour = snewn(n, int);
832 #ifdef SOLVER_DIAGNOSTICS
833 sc->bfsprev = snewn(n, int);
839 static void free_scratch(struct solver_scratch *sc)
843 sfree(sc->bfscolour);
844 #ifdef SOLVER_DIAGNOSTICS
851 * Count the bits in a word. Only needs to cope with FOUR bits.
853 static int bitcount(int word)
855 assert(FOUR <= 4); /* or this needs changing */
856 word = ((word & 0xA) >> 1) + (word & 0x5);
857 word = ((word & 0xC) >> 2) + (word & 0x3);
861 #ifdef SOLVER_DIAGNOSTICS
862 static const char colnames[FOUR] = { 'R', 'Y', 'G', 'B' };
865 static int place_colour(struct solver_scratch *sc,
866 int *colouring, int index, int colour
867 #ifdef SOLVER_DIAGNOSTICS
872 int *graph = sc->graph, n = sc->n, ngraph = sc->ngraph;
875 if (!(sc->possible[index] & (1 << colour))) {
876 #ifdef SOLVER_DIAGNOSTICS
878 printf("%*scannot place %c in region %d\n", 2*sc->depth, "",
879 colnames[colour], index);
881 return FALSE; /* can't do it */
884 sc->possible[index] = 1 << colour;
885 colouring[index] = colour;
887 #ifdef SOLVER_DIAGNOSTICS
889 printf("%*s%s %c in region %d\n", 2*sc->depth, "",
890 verb, colnames[colour], index);
894 * Rule out this colour from all the region's neighbours.
896 for (j = graph_vertex_start(graph, n, ngraph, index);
897 j < ngraph && graph[j] < n*(index+1); j++) {
898 k = graph[j] - index*n;
899 #ifdef SOLVER_DIAGNOSTICS
900 if (verbose && (sc->possible[k] & (1 << colour)))
901 printf("%*s ruling out %c in region %d\n", 2*sc->depth, "",
902 colnames[colour], k);
904 sc->possible[k] &= ~(1 << colour);
910 #ifdef SOLVER_DIAGNOSTICS
911 static char *colourset(char *buf, int set)
917 for (i = 0; i < FOUR; i++)
918 if (set & (1 << i)) {
919 p += sprintf(p, "%s%c", sep, colnames[i]);
928 * Returns 0 for impossible, 1 for success, 2 for failure to
929 * converge (i.e. puzzle is either ambiguous or just too
932 static int map_solver(struct solver_scratch *sc,
933 int *graph, int n, int ngraph, int *colouring,
938 if (sc->depth == 0) {
940 * Initialise scratch space.
942 for (i = 0; i < n; i++)
943 sc->possible[i] = (1 << FOUR) - 1;
948 for (i = 0; i < n; i++)
949 if (colouring[i] >= 0) {
950 if (!place_colour(sc, colouring, i, colouring[i]
951 #ifdef SOLVER_DIAGNOSTICS
955 #ifdef SOLVER_DIAGNOSTICS
957 printf("%*sinitial clue set is inconsistent\n",
960 return 0; /* the clues aren't even consistent! */
966 * Now repeatedly loop until we find nothing further to do.
969 int done_something = FALSE;
971 if (difficulty < DIFF_EASY)
972 break; /* can't do anything at all! */
975 * Simplest possible deduction: find a region with only one
978 for (i = 0; i < n; i++) if (colouring[i] < 0) {
979 int p = sc->possible[i];
982 #ifdef SOLVER_DIAGNOSTICS
984 printf("%*sregion %d has no possible colours left\n",
987 return 0; /* puzzle is inconsistent */
990 if ((p & (p-1)) == 0) { /* p is a power of two */
992 for (c = 0; c < FOUR; c++)
996 ret = place_colour(sc, colouring, i, c
997 #ifdef SOLVER_DIAGNOSTICS
1002 * place_colour() can only fail if colour c was not
1003 * even a _possibility_ for region i, and we're
1004 * pretty sure it was because we checked before
1005 * calling place_colour(). So we can safely assert
1006 * here rather than having to return a nice
1007 * friendly error code.
1010 done_something = TRUE;
1017 if (difficulty < DIFF_NORMAL)
1018 break; /* can't do anything harder */
1021 * Failing that, go up one level. Look for pairs of regions
1022 * which (a) both have the same pair of possible colours,
1023 * (b) are adjacent to one another, (c) are adjacent to the
1024 * same region, and (d) that region still thinks it has one
1025 * or both of those possible colours.
1027 * Simplest way to do this is by going through the graph
1028 * edge by edge, so that we start with property (b) and
1029 * then look for (a) and finally (c) and (d).
1031 for (i = 0; i < ngraph; i++) {
1032 int j1 = graph[i] / n, j2 = graph[i] % n;
1034 #ifdef SOLVER_DIAGNOSTICS
1035 int started = FALSE;
1039 continue; /* done it already, other way round */
1041 if (colouring[j1] >= 0 || colouring[j2] >= 0)
1042 continue; /* they're not undecided */
1044 if (sc->possible[j1] != sc->possible[j2])
1045 continue; /* they don't have the same possibles */
1047 v = sc->possible[j1];
1049 * See if v contains exactly two set bits.
1051 v2 = v & -v; /* find lowest set bit */
1052 v2 = v & ~v2; /* clear it */
1053 if (v2 == 0 || (v2 & (v2-1)) != 0) /* not power of 2 */
1057 * We've found regions j1 and j2 satisfying properties
1058 * (a) and (b): they have two possible colours between
1059 * them, and since they're adjacent to one another they
1060 * must use _both_ those colours between them.
1061 * Therefore, if they are both adjacent to any other
1062 * region then that region cannot be either colour.
1064 * Go through the neighbours of j1 and see if any are
1067 for (j = graph_vertex_start(graph, n, ngraph, j1);
1068 j < ngraph && graph[j] < n*(j1+1); j++) {
1069 k = graph[j] - j1*n;
1070 if (graph_adjacent(graph, n, ngraph, k, j2) &&
1071 (sc->possible[k] & v)) {
1072 #ifdef SOLVER_DIAGNOSTICS
1076 printf("%*sadjacent regions %d,%d share colours"
1077 " %s\n", 2*sc->depth, "", j1, j2,
1080 printf("%*s ruling out %s in region %d\n",2*sc->depth,
1081 "", colourset(buf, sc->possible[k] & v), k);
1084 sc->possible[k] &= ~v;
1085 done_something = TRUE;
1093 if (difficulty < DIFF_HARD)
1094 break; /* can't do anything harder */
1097 * Right; now we get creative. Now we're going to look for
1098 * `forcing chains'. A forcing chain is a path through the
1099 * graph with the following properties:
1101 * (a) Each vertex on the path has precisely two possible
1104 * (b) Each pair of vertices which are adjacent on the
1105 * path share at least one possible colour in common.
1107 * (c) Each vertex in the middle of the path shares _both_
1108 * of its colours with at least one of its neighbours
1109 * (not the same one with both neighbours).
1111 * These together imply that at least one of the possible
1112 * colour choices at one end of the path forces _all_ the
1113 * rest of the colours along the path. In order to make
1114 * real use of this, we need further properties:
1116 * (c) Ruling out some colour C from the vertex at one end
1117 * of the path forces the vertex at the other end to
1120 * (d) The two end vertices are mutually adjacent to some
1123 * (e) That third vertex currently has C as a possibility.
1125 * If we can find all of that lot, we can deduce that at
1126 * least one of the two ends of the forcing chain has
1127 * colour C, and that therefore the mutually adjacent third
1130 * To find forcing chains, we're going to start a bfs at
1131 * each suitable vertex of the graph, once for each of its
1132 * two possible colours.
1134 for (i = 0; i < n; i++) {
1137 if (colouring[i] >= 0 || bitcount(sc->possible[i]) != 2)
1140 for (c = 0; c < FOUR; c++)
1141 if (sc->possible[i] & (1 << c)) {
1142 int j, k, gi, origc, currc, head, tail;
1144 * Try a bfs from this vertex, ruling out
1147 * Within this loop, we work in colour bitmaps
1148 * rather than actual colours, because
1149 * converting back and forth is a needless
1150 * computational expense.
1155 for (j = 0; j < n; j++) {
1156 sc->bfscolour[j] = -1;
1157 #ifdef SOLVER_DIAGNOSTICS
1158 sc->bfsprev[j] = -1;
1162 sc->bfsqueue[tail++] = i;
1163 sc->bfscolour[i] = sc->possible[i] &~ origc;
1165 while (head < tail) {
1166 j = sc->bfsqueue[head++];
1167 currc = sc->bfscolour[j];
1170 * Try neighbours of j.
1172 for (gi = graph_vertex_start(graph, n, ngraph, j);
1173 gi < ngraph && graph[gi] < n*(j+1); gi++) {
1174 k = graph[gi] - j*n;
1177 * To continue with the bfs in vertex
1178 * k, we need k to be
1179 * (a) not already visited
1180 * (b) have two possible colours
1181 * (c) those colours include currc.
1184 if (sc->bfscolour[k] < 0 &&
1186 bitcount(sc->possible[k]) == 2 &&
1187 (sc->possible[k] & currc)) {
1188 sc->bfsqueue[tail++] = k;
1190 sc->possible[k] &~ currc;
1191 #ifdef SOLVER_DIAGNOSTICS
1197 * One other possibility is that k
1198 * might be the region in which we can
1199 * make a real deduction: if it's
1200 * adjacent to i, contains currc as a
1201 * possibility, and currc is equal to
1202 * the original colour we ruled out.
1204 if (currc == origc &&
1205 graph_adjacent(graph, n, ngraph, k, i) &&
1206 (sc->possible[k] & currc)) {
1207 #ifdef SOLVER_DIAGNOSTICS
1209 char buf[80], *sep = "";
1212 printf("%*sforcing chain, colour %s, ",
1214 colourset(buf, origc));
1215 for (r = j; r != -1; r = sc->bfsprev[r]) {
1216 printf("%s%d", sep, r);
1219 printf("\n%*s ruling out %s in region"
1220 " %d\n", 2*sc->depth, "",
1221 colourset(buf, origc), k);
1224 sc->possible[k] &= ~origc;
1225 done_something = TRUE;
1234 if (!done_something)
1239 * See if we've got a complete solution, and return if so.
1241 for (i = 0; i < n; i++)
1242 if (colouring[i] < 0)
1245 #ifdef SOLVER_DIAGNOSTICS
1247 printf("%*sone solution found\n", 2*sc->depth, "");
1249 return 1; /* success! */
1253 * If recursion is not permissible, we now give up.
1255 if (difficulty < DIFF_RECURSE) {
1256 #ifdef SOLVER_DIAGNOSTICS
1258 printf("%*sunable to proceed further without recursion\n",
1261 return 2; /* unable to complete */
1265 * Now we've got to do something recursive. So first hunt for a
1266 * currently-most-constrained region.
1270 struct solver_scratch *rsc;
1271 int *subcolouring, *origcolouring;
1273 int we_already_got_one;
1278 for (i = 0; i < n; i++) if (colouring[i] < 0) {
1279 int p = sc->possible[i];
1280 enum { compile_time_assertion = 1 / (FOUR <= 4) };
1283 /* Count the set bits. */
1284 c = (p & 5) + ((p >> 1) & 5);
1285 c = (c & 3) + ((c >> 2) & 3);
1286 assert(c > 1); /* or colouring[i] would be >= 0 */
1294 assert(best >= 0); /* or we'd be solved already */
1296 #ifdef SOLVER_DIAGNOSTICS
1298 printf("%*srecursing on region %d\n", 2*sc->depth, "", best);
1302 * Now iterate over the possible colours for this region.
1304 rsc = new_scratch(graph, n, ngraph);
1305 rsc->depth = sc->depth + 1;
1306 origcolouring = snewn(n, int);
1307 memcpy(origcolouring, colouring, n * sizeof(int));
1308 subcolouring = snewn(n, int);
1309 we_already_got_one = FALSE;
1312 for (i = 0; i < FOUR; i++) {
1313 if (!(sc->possible[best] & (1 << i)))
1316 memcpy(rsc->possible, sc->possible, n);
1317 memcpy(subcolouring, origcolouring, n * sizeof(int));
1319 place_colour(rsc, subcolouring, best, i
1320 #ifdef SOLVER_DIAGNOSTICS
1325 subret = map_solver(rsc, graph, n, ngraph,
1326 subcolouring, difficulty);
1328 #ifdef SOLVER_DIAGNOSTICS
1330 printf("%*sretracting %c in region %d; found %s\n",
1331 2*sc->depth, "", colnames[i], best,
1332 subret == 0 ? "no solutions" :
1333 subret == 1 ? "one solution" : "multiple solutions");
1338 * If this possibility turned up more than one valid
1339 * solution, or if it turned up one and we already had
1340 * one, we're definitely ambiguous.
1342 if (subret == 2 || (subret == 1 && we_already_got_one)) {
1348 * If this possibility turned up one valid solution and
1349 * it's the first we've seen, copy it into the output.
1352 memcpy(colouring, subcolouring, n * sizeof(int));
1353 we_already_got_one = TRUE;
1358 * Otherwise, this guess led to a contradiction, so we
1363 sfree(subcolouring);
1366 #ifdef SOLVER_DIAGNOSTICS
1367 if (verbose && sc->depth == 0) {
1368 printf("%*s%s found\n",
1370 ret == 0 ? "no solutions" :
1371 ret == 1 ? "one solution" : "multiple solutions");
1378 /* ----------------------------------------------------------------------
1379 * Game generation main function.
1382 static char *new_game_desc(game_params *params, random_state *rs,
1383 char **aux, int interactive)
1385 struct solver_scratch *sc = NULL;
1386 int *map, *graph, ngraph, *colouring, *colouring2, *regions;
1387 int i, j, w, h, n, solveret, cfreq[FOUR];
1390 #ifdef GENERATION_DIAGNOSTICS
1394 int retlen, retsize;
1403 map = snewn(wh, int);
1404 graph = snewn(n*n, int);
1405 colouring = snewn(n, int);
1406 colouring2 = snewn(n, int);
1407 regions = snewn(n, int);
1410 * This is the minimum difficulty below which we'll completely
1411 * reject a map design. Normally we set this to one below the
1412 * requested difficulty, ensuring that we have the right
1413 * result. However, for particularly dense maps or maps with
1414 * particularly few regions it might not be possible to get the
1415 * desired difficulty, so we will eventually drop this down to
1416 * -1 to indicate that any old map will do.
1418 mindiff = params->diff;
1426 genmap(w, h, n, map, rs);
1428 #ifdef GENERATION_DIAGNOSTICS
1429 for (y = 0; y < h; y++) {
1430 for (x = 0; x < w; x++) {
1435 putchar('a' + v-36);
1437 putchar('A' + v-10);
1446 * Convert the map into a graph.
1448 ngraph = gengraph(w, h, n, map, graph);
1450 #ifdef GENERATION_DIAGNOSTICS
1451 for (i = 0; i < ngraph; i++)
1452 printf("%d-%d\n", graph[i]/n, graph[i]%n);
1458 fourcolour(graph, n, ngraph, colouring, rs);
1460 #ifdef GENERATION_DIAGNOSTICS
1461 for (i = 0; i < n; i++)
1462 printf("%d: %d\n", i, colouring[i]);
1464 for (y = 0; y < h; y++) {
1465 for (x = 0; x < w; x++) {
1466 int v = colouring[map[y*w+x]];
1468 putchar('a' + v-36);
1470 putchar('A' + v-10);
1479 * Encode the solution as an aux string.
1481 if (*aux) /* in case we've come round again */
1483 retlen = retsize = 0;
1485 for (i = 0; i < n; i++) {
1488 if (colouring[i] < 0)
1491 len = sprintf(buf, "%s%d:%d", i ? ";" : "S;", colouring[i], i);
1492 if (retlen + len >= retsize) {
1493 retsize = retlen + len + 256;
1494 ret = sresize(ret, retsize, char);
1496 strcpy(ret + retlen, buf);
1502 * Remove the region colours one by one, keeping
1503 * solubility. Also ensure that there always remains at
1504 * least one region of every colour, so that the user can
1505 * drag from somewhere.
1507 for (i = 0; i < FOUR; i++)
1509 for (i = 0; i < n; i++) {
1511 cfreq[colouring[i]]++;
1513 for (i = 0; i < FOUR; i++)
1517 shuffle(regions, n, sizeof(*regions), rs);
1519 if (sc) free_scratch(sc);
1520 sc = new_scratch(graph, n, ngraph);
1522 for (i = 0; i < n; i++) {
1525 if (cfreq[colouring[j]] == 1)
1526 continue; /* can't remove last region of colour */
1528 memcpy(colouring2, colouring, n*sizeof(int));
1530 solveret = map_solver(sc, graph, n, ngraph, colouring2,
1532 assert(solveret >= 0); /* mustn't be impossible! */
1533 if (solveret == 1) {
1534 cfreq[colouring[j]]--;
1539 #ifdef GENERATION_DIAGNOSTICS
1540 for (i = 0; i < n; i++)
1541 if (colouring[i] >= 0) {
1545 putchar('a' + i-36);
1547 putchar('A' + i-10);
1550 printf(": %d\n", colouring[i]);
1555 * Finally, check that the puzzle is _at least_ as hard as
1556 * required, and indeed that it isn't already solved.
1557 * (Calling map_solver with negative difficulty ensures the
1558 * latter - if a solver which _does nothing_ can solve it,
1561 memcpy(colouring2, colouring, n*sizeof(int));
1562 if (map_solver(sc, graph, n, ngraph, colouring2,
1563 mindiff - 1) == 1) {
1565 * Drop minimum difficulty if necessary.
1567 if (mindiff > 0 && (n < 9 || n > 2*wh/3)) {
1569 mindiff = 0; /* give up and go for Easy */
1578 * Encode as a game ID. We do this by:
1580 * - first going along the horizontal edges row by row, and
1581 * then the vertical edges column by column
1582 * - encoding the lengths of runs of edges and runs of
1584 * - the decoder will reconstitute the region boundaries from
1585 * this and automatically number them the same way we did
1586 * - then we encode the initial region colours in a Slant-like
1587 * fashion (digits 0-3 interspersed with letters giving
1588 * lengths of runs of empty spaces).
1590 retlen = retsize = 0;
1597 * Start with a notional non-edge, so that there'll be an
1598 * explicit `a' to distinguish the case where we start with
1604 for (i = 0; i < w*(h-1) + (w-1)*h; i++) {
1605 int x, y, dx, dy, v;
1608 /* Horizontal edge. */
1614 /* Vertical edge. */
1615 x = (i - w*(h-1)) / h;
1616 y = (i - w*(h-1)) % h;
1621 if (retlen + 10 >= retsize) {
1622 retsize = retlen + 256;
1623 ret = sresize(ret, retsize, char);
1626 v = (map[y*w+x] != map[(y+dy)*w+(x+dx)]);
1629 ret[retlen++] = 'a'-1 + run;
1634 * 'z' is a special case in this encoding. Rather
1635 * than meaning a run of 26 and a state switch, it
1636 * means a run of 25 and _no_ state switch, because
1637 * otherwise there'd be no way to encode runs of
1641 ret[retlen++] = 'z';
1648 ret[retlen++] = 'a'-1 + run;
1649 ret[retlen++] = ',';
1652 for (i = 0; i < n; i++) {
1653 if (retlen + 10 >= retsize) {
1654 retsize = retlen + 256;
1655 ret = sresize(ret, retsize, char);
1658 if (colouring[i] < 0) {
1660 * In _this_ encoding, 'z' is a run of 26, since
1661 * there's no implicit state switch after each run.
1662 * Confusingly different, but more compact.
1665 ret[retlen++] = 'z';
1671 ret[retlen++] = 'a'-1 + run;
1672 ret[retlen++] = '0' + colouring[i];
1677 ret[retlen++] = 'a'-1 + run;
1680 assert(retlen < retsize);
1693 static char *parse_edge_list(game_params *params, char **desc, int *map)
1695 int w = params->w, h = params->h, wh = w*h, n = params->n;
1696 int i, k, pos, state;
1699 for (i = 0; i < wh; i++)
1706 * Parse the game description to get the list of edges, and
1707 * build up a disjoint set forest as we go (by identifying
1708 * pairs of squares whenever the edge list shows a non-edge).
1710 while (*p && *p != ',') {
1711 if (*p < 'a' || *p > 'z')
1712 return "Unexpected character in edge list";
1723 } else if (pos < w*(h-1)) {
1724 /* Horizontal edge. */
1729 } else if (pos < 2*wh-w-h) {
1730 /* Vertical edge. */
1731 x = (pos - w*(h-1)) / h;
1732 y = (pos - w*(h-1)) % h;
1736 return "Too much data in edge list";
1738 dsf_merge(map+wh, y*w+x, (y+dy)*w+(x+dx));
1746 assert(pos <= 2*wh-w-h);
1748 return "Too little data in edge list";
1751 * Now go through again and allocate region numbers.
1754 for (i = 0; i < wh; i++)
1756 for (i = 0; i < wh; i++) {
1757 k = dsf_canonify(map+wh, i);
1763 return "Edge list defines the wrong number of regions";
1770 static char *validate_desc(game_params *params, char *desc)
1772 int w = params->w, h = params->h, wh = w*h, n = params->n;
1777 map = snewn(2*wh, int);
1778 ret = parse_edge_list(params, &desc, map);
1784 return "Expected comma before clue list";
1785 desc++; /* eat comma */
1789 if (*desc >= '0' && *desc < '0'+FOUR)
1791 else if (*desc >= 'a' && *desc <= 'z')
1792 area += *desc - 'a' + 1;
1794 return "Unexpected character in clue list";
1798 return "Too little data in clue list";
1800 return "Too much data in clue list";
1805 static game_state *new_game(midend *me, game_params *params, char *desc)
1807 int w = params->w, h = params->h, wh = w*h, n = params->n;
1810 game_state *state = snew(game_state);
1813 state->colouring = snewn(n, int);
1814 for (i = 0; i < n; i++)
1815 state->colouring[i] = -1;
1816 state->pencil = snewn(n, int);
1817 for (i = 0; i < n; i++)
1818 state->pencil[i] = 0;
1820 state->completed = state->cheated = FALSE;
1822 state->map = snew(struct map);
1823 state->map->refcount = 1;
1824 state->map->map = snewn(wh*4, int);
1825 state->map->graph = snewn(n*n, int);
1827 state->map->immutable = snewn(n, int);
1828 for (i = 0; i < n; i++)
1829 state->map->immutable[i] = FALSE;
1835 ret = parse_edge_list(params, &p, state->map->map);
1840 * Set up the other three quadrants in `map'.
1842 for (i = wh; i < 4*wh; i++)
1843 state->map->map[i] = state->map->map[i % wh];
1849 * Now process the clue list.
1853 if (*p >= '0' && *p < '0'+FOUR) {
1854 state->colouring[pos] = *p - '0';
1855 state->map->immutable[pos] = TRUE;
1858 assert(*p >= 'a' && *p <= 'z');
1859 pos += *p - 'a' + 1;
1865 state->map->ngraph = gengraph(w, h, n, state->map->map, state->map->graph);
1868 * Attempt to smooth out some of the more jagged region
1869 * outlines by the judicious use of diagonally divided squares.
1872 random_state *rs = random_init(desc, strlen(desc));
1873 int *squares = snewn(wh, int);
1876 for (i = 0; i < wh; i++)
1878 shuffle(squares, wh, sizeof(*squares), rs);
1881 done_something = FALSE;
1882 for (i = 0; i < wh; i++) {
1883 int y = squares[i] / w, x = squares[i] % w;
1884 int c = state->map->map[y*w+x];
1887 if (x == 0 || x == w-1 || y == 0 || y == h-1)
1890 if (state->map->map[TE * wh + y*w+x] !=
1891 state->map->map[BE * wh + y*w+x])
1894 tc = state->map->map[BE * wh + (y-1)*w+x];
1895 bc = state->map->map[TE * wh + (y+1)*w+x];
1896 lc = state->map->map[RE * wh + y*w+(x-1)];
1897 rc = state->map->map[LE * wh + y*w+(x+1)];
1900 * If this square is adjacent on two sides to one
1901 * region and on the other two sides to the other
1902 * region, and is itself one of the two regions, we can
1903 * adjust it so that it's a diagonal.
1905 if (tc != bc && (tc == c || bc == c)) {
1906 if ((lc == tc && rc == bc) ||
1907 (lc == bc && rc == tc)) {
1908 state->map->map[TE * wh + y*w+x] = tc;
1909 state->map->map[BE * wh + y*w+x] = bc;
1910 state->map->map[LE * wh + y*w+x] = lc;
1911 state->map->map[RE * wh + y*w+x] = rc;
1912 done_something = TRUE;
1916 } while (done_something);
1922 * Analyse the map to find a canonical line segment
1923 * corresponding to each edge, and a canonical point
1924 * corresponding to each region. The former are where we'll
1925 * eventually put error markers; the latter are where we'll put
1926 * per-region flags such as numbers (when in diagnostic mode).
1929 int *bestx, *besty, *an, pass;
1930 float *ax, *ay, *best;
1932 ax = snewn(state->map->ngraph + n, float);
1933 ay = snewn(state->map->ngraph + n, float);
1934 an = snewn(state->map->ngraph + n, int);
1935 bestx = snewn(state->map->ngraph + n, int);
1936 besty = snewn(state->map->ngraph + n, int);
1937 best = snewn(state->map->ngraph + n, float);
1939 for (i = 0; i < state->map->ngraph + n; i++) {
1940 bestx[i] = besty[i] = -1;
1941 best[i] = 2*(w+h)+1;
1942 ax[i] = ay[i] = 0.0F;
1947 * We make two passes over the map, finding all the line
1948 * segments separating regions and all the suitable points
1949 * within regions. In the first pass, we compute the
1950 * _average_ x and y coordinate of all the points in a
1951 * given class; in the second pass, for each such average
1952 * point, we find the candidate closest to it and call that
1955 * Line segments are considered to have coordinates in
1956 * their centre. Thus, at least one coordinate for any line
1957 * segment is always something-and-a-half; so we store our
1958 * coordinates as twice their normal value.
1960 for (pass = 0; pass < 2; pass++) {
1963 for (y = 0; y < h; y++)
1964 for (x = 0; x < w; x++) {
1965 int ex[4], ey[4], ea[4], eb[4], en = 0;
1968 * Look for an edge to the right of this
1969 * square, an edge below it, and an edge in the
1970 * middle of it. Also look to see if the point
1971 * at the bottom right of this square is on an
1972 * edge (and isn't a place where more than two
1977 ea[en] = state->map->map[RE * wh + y*w+x];
1978 eb[en] = state->map->map[LE * wh + y*w+(x+1)];
1985 ea[en] = state->map->map[BE * wh + y*w+x];
1986 eb[en] = state->map->map[TE * wh + (y+1)*w+x];
1992 ea[en] = state->map->map[TE * wh + y*w+x];
1993 eb[en] = state->map->map[BE * wh + y*w+x];
1998 if (x+1 < w && y+1 < h) {
1999 /* bottom right corner */
2000 int oct[8], othercol, nchanges;
2001 oct[0] = state->map->map[RE * wh + y*w+x];
2002 oct[1] = state->map->map[LE * wh + y*w+(x+1)];
2003 oct[2] = state->map->map[BE * wh + y*w+(x+1)];
2004 oct[3] = state->map->map[TE * wh + (y+1)*w+(x+1)];
2005 oct[4] = state->map->map[LE * wh + (y+1)*w+(x+1)];
2006 oct[5] = state->map->map[RE * wh + (y+1)*w+x];
2007 oct[6] = state->map->map[TE * wh + (y+1)*w+x];
2008 oct[7] = state->map->map[BE * wh + y*w+x];
2012 for (i = 0; i < 8; i++) {
2013 if (oct[i] != oct[0]) {
2016 else if (othercol != oct[i])
2017 break; /* three colours at this point */
2019 if (oct[i] != oct[(i+1) & 7])
2024 * Now if there are exactly two regions at
2025 * this point (not one, and not three or
2026 * more), and only two changes around the
2027 * loop, then this is a valid place to put
2030 if (i == 8 && othercol >= 0 && nchanges == 2) {
2039 * If there's exactly _one_ region at this
2040 * point, on the other hand, it's a valid
2041 * place to put a region centre.
2044 ea[en] = eb[en] = oct[0];
2052 * Now process the points we've found, one by
2055 for (i = 0; i < en; i++) {
2056 int emin = min(ea[i], eb[i]);
2057 int emax = max(ea[i], eb[i]);
2063 graph_edge_index(state->map->graph, n,
2064 state->map->ngraph, emin,
2068 gindex = state->map->ngraph + emin;
2071 assert(gindex >= 0);
2075 * In pass 0, accumulate the values
2076 * we'll use to compute the average
2079 ax[gindex] += ex[i];
2080 ay[gindex] += ey[i];
2084 * In pass 1, work out whether this
2085 * point is closer to the average than
2086 * the last one we've seen.
2090 assert(an[gindex] > 0);
2091 dx = ex[i] - ax[gindex];
2092 dy = ey[i] - ay[gindex];
2093 d = sqrt(dx*dx + dy*dy);
2094 if (d < best[gindex]) {
2096 bestx[gindex] = ex[i];
2097 besty[gindex] = ey[i];
2104 for (i = 0; i < state->map->ngraph + n; i++)
2112 state->map->edgex = snewn(state->map->ngraph, int);
2113 state->map->edgey = snewn(state->map->ngraph, int);
2114 memcpy(state->map->edgex, bestx, state->map->ngraph * sizeof(int));
2115 memcpy(state->map->edgey, besty, state->map->ngraph * sizeof(int));
2117 state->map->regionx = snewn(n, int);
2118 state->map->regiony = snewn(n, int);
2119 memcpy(state->map->regionx, bestx + state->map->ngraph, n*sizeof(int));
2120 memcpy(state->map->regiony, besty + state->map->ngraph, n*sizeof(int));
2122 for (i = 0; i < state->map->ngraph; i++)
2123 if (state->map->edgex[i] < 0) {
2124 /* Find the other representation of this edge. */
2125 int e = state->map->graph[i];
2126 int iprime = graph_edge_index(state->map->graph, n,
2127 state->map->ngraph, e%n, e/n);
2128 assert(state->map->edgex[iprime] >= 0);
2129 state->map->edgex[i] = state->map->edgex[iprime];
2130 state->map->edgey[i] = state->map->edgey[iprime];
2144 static game_state *dup_game(game_state *state)
2146 game_state *ret = snew(game_state);
2149 ret->colouring = snewn(state->p.n, int);
2150 memcpy(ret->colouring, state->colouring, state->p.n * sizeof(int));
2151 ret->pencil = snewn(state->p.n, int);
2152 memcpy(ret->pencil, state->pencil, state->p.n * sizeof(int));
2153 ret->map = state->map;
2154 ret->map->refcount++;
2155 ret->completed = state->completed;
2156 ret->cheated = state->cheated;
2161 static void free_game(game_state *state)
2163 if (--state->map->refcount <= 0) {
2164 sfree(state->map->map);
2165 sfree(state->map->graph);
2166 sfree(state->map->immutable);
2167 sfree(state->map->edgex);
2168 sfree(state->map->edgey);
2169 sfree(state->map->regionx);
2170 sfree(state->map->regiony);
2173 sfree(state->colouring);
2177 static char *solve_game(game_state *state, game_state *currstate,
2178 char *aux, char **error)
2185 struct solver_scratch *sc;
2189 int retlen, retsize;
2191 colouring = snewn(state->map->n, int);
2192 memcpy(colouring, state->colouring, state->map->n * sizeof(int));
2194 sc = new_scratch(state->map->graph, state->map->n, state->map->ngraph);
2195 sret = map_solver(sc, state->map->graph, state->map->n,
2196 state->map->ngraph, colouring, DIFFCOUNT-1);
2202 *error = "Puzzle is inconsistent";
2204 *error = "Unable to find a unique solution for this puzzle";
2209 ret = snewn(retsize, char);
2213 for (i = 0; i < state->map->n; i++) {
2216 assert(colouring[i] >= 0);
2217 if (colouring[i] == currstate->colouring[i])
2219 assert(!state->map->immutable[i]);
2221 len = sprintf(buf, ";%d:%d", colouring[i], i);
2222 if (retlen + len >= retsize) {
2223 retsize = retlen + len + 256;
2224 ret = sresize(ret, retsize, char);
2226 strcpy(ret + retlen, buf);
2237 static char *game_text_format(game_state *state)
2243 int drag_colour; /* -1 means no drag active */
2248 static game_ui *new_ui(game_state *state)
2250 game_ui *ui = snew(game_ui);
2251 ui->dragx = ui->dragy = -1;
2252 ui->drag_colour = -2;
2253 ui->show_numbers = FALSE;
2257 static void free_ui(game_ui *ui)
2262 static char *encode_ui(game_ui *ui)
2267 static void decode_ui(game_ui *ui, char *encoding)
2271 static void game_changed_state(game_ui *ui, game_state *oldstate,
2272 game_state *newstate)
2276 struct game_drawstate {
2278 unsigned long *drawn, *todraw;
2280 int dragx, dragy, drag_visible;
2284 /* Flags in `drawn'. */
2285 #define ERR_BASE 0x00800000L
2286 #define ERR_MASK 0xFF800000L
2287 #define PENCIL_T_BASE 0x00080000L
2288 #define PENCIL_T_MASK 0x00780000L
2289 #define PENCIL_B_BASE 0x00008000L
2290 #define PENCIL_B_MASK 0x00078000L
2291 #define PENCIL_MASK 0x007F8000L
2292 #define SHOW_NUMBERS 0x00004000L
2294 #define TILESIZE (ds->tilesize)
2295 #define BORDER (TILESIZE)
2296 #define COORD(x) ( (x) * TILESIZE + BORDER )
2297 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
2299 static int region_from_coords(game_state *state, game_drawstate *ds,
2302 int w = state->p.w, h = state->p.h, wh = w*h /*, n = state->p.n */;
2303 int tx = FROMCOORD(x), ty = FROMCOORD(y);
2304 int dx = x - COORD(tx), dy = y - COORD(ty);
2307 if (tx < 0 || tx >= w || ty < 0 || ty >= h)
2308 return -1; /* border */
2310 quadrant = 2 * (dx > dy) + (TILESIZE - dx > dy);
2311 quadrant = (quadrant == 0 ? BE :
2312 quadrant == 1 ? LE :
2313 quadrant == 2 ? RE : TE);
2315 return state->map->map[quadrant * wh + ty*w+tx];
2318 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2319 int x, int y, int button)
2324 * Enable or disable numeric labels on regions.
2326 if (button == 'l' || button == 'L') {
2327 ui->show_numbers = !ui->show_numbers;
2331 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2332 int r = region_from_coords(state, ds, x, y);
2335 ui->drag_colour = state->colouring[r];
2337 ui->drag_colour = -1;
2343 if ((button == LEFT_DRAG || button == RIGHT_DRAG) &&
2344 ui->drag_colour > -2) {
2350 if ((button == LEFT_RELEASE || button == RIGHT_RELEASE) &&
2351 ui->drag_colour > -2) {
2352 int r = region_from_coords(state, ds, x, y);
2353 int c = ui->drag_colour;
2356 * Cancel the drag, whatever happens.
2358 ui->drag_colour = -2;
2359 ui->dragx = ui->dragy = -1;
2362 return ""; /* drag into border; do nothing else */
2364 if (state->map->immutable[r])
2365 return ""; /* can't change this region */
2367 if (state->colouring[r] == c)
2368 return ""; /* don't _need_ to change this region */
2370 if (button == RIGHT_RELEASE && state->colouring[r] >= 0)
2371 return ""; /* can't pencil on a coloured region */
2373 sprintf(buf, "%s%c:%d", (button == RIGHT_RELEASE ? "p" : ""),
2374 (int)(c < 0 ? 'C' : '0' + c), r);
2381 static game_state *execute_move(game_state *state, char *move)
2384 game_state *ret = dup_game(state);
2395 if ((c == 'C' || (c >= '0' && c < '0'+FOUR)) &&
2396 sscanf(move+1, ":%d%n", &k, &adv) == 1 &&
2397 k >= 0 && k < state->p.n) {
2400 if (ret->colouring[k] >= 0) {
2407 ret->pencil[k] ^= 1 << (c - '0');
2409 ret->colouring[k] = (c == 'C' ? -1 : c - '0');
2412 } else if (*move == 'S') {
2414 ret->cheated = TRUE;
2420 if (*move && *move != ';') {
2429 * Check for completion.
2431 if (!ret->completed) {
2434 for (i = 0; i < n; i++)
2435 if (ret->colouring[i] < 0) {
2441 for (i = 0; i < ret->map->ngraph; i++) {
2442 int j = ret->map->graph[i] / n;
2443 int k = ret->map->graph[i] % n;
2444 if (ret->colouring[j] == ret->colouring[k]) {
2452 ret->completed = TRUE;
2458 /* ----------------------------------------------------------------------
2462 static void game_compute_size(game_params *params, int tilesize,
2465 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2466 struct { int tilesize; } ads, *ds = &ads;
2467 ads.tilesize = tilesize;
2469 *x = params->w * TILESIZE + 2 * BORDER + 1;
2470 *y = params->h * TILESIZE + 2 * BORDER + 1;
2473 static void game_set_size(drawing *dr, game_drawstate *ds,
2474 game_params *params, int tilesize)
2476 ds->tilesize = tilesize;
2479 blitter_free(dr, ds->bl);
2480 ds->bl = blitter_new(dr, TILESIZE+3, TILESIZE+3);
2483 const float map_colours[FOUR][3] = {
2487 {0.55F, 0.45F, 0.35F},
2489 const int map_hatching[FOUR] = {
2490 HATCH_VERT, HATCH_SLASH, HATCH_HORIZ, HATCH_BACKSLASH
2493 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
2495 float *ret = snewn(3 * NCOLOURS, float);
2497 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2499 ret[COL_GRID * 3 + 0] = 0.0F;
2500 ret[COL_GRID * 3 + 1] = 0.0F;
2501 ret[COL_GRID * 3 + 2] = 0.0F;
2503 memcpy(ret + COL_0 * 3, map_colours[0], 3 * sizeof(float));
2504 memcpy(ret + COL_1 * 3, map_colours[1], 3 * sizeof(float));
2505 memcpy(ret + COL_2 * 3, map_colours[2], 3 * sizeof(float));
2506 memcpy(ret + COL_3 * 3, map_colours[3], 3 * sizeof(float));
2508 ret[COL_ERROR * 3 + 0] = 1.0F;
2509 ret[COL_ERROR * 3 + 1] = 0.0F;
2510 ret[COL_ERROR * 3 + 2] = 0.0F;
2512 ret[COL_ERRTEXT * 3 + 0] = 1.0F;
2513 ret[COL_ERRTEXT * 3 + 1] = 1.0F;
2514 ret[COL_ERRTEXT * 3 + 2] = 1.0F;
2516 *ncolours = NCOLOURS;
2520 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2522 struct game_drawstate *ds = snew(struct game_drawstate);
2526 ds->drawn = snewn(state->p.w * state->p.h, unsigned long);
2527 for (i = 0; i < state->p.w * state->p.h; i++)
2528 ds->drawn[i] = 0xFFFFL;
2529 ds->todraw = snewn(state->p.w * state->p.h, unsigned long);
2530 ds->started = FALSE;
2532 ds->drag_visible = FALSE;
2533 ds->dragx = ds->dragy = -1;
2538 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2543 blitter_free(dr, ds->bl);
2547 static void draw_error(drawing *dr, game_drawstate *ds, int x, int y)
2555 coords[0] = x - TILESIZE*2/5;
2558 coords[3] = y - TILESIZE*2/5;
2559 coords[4] = x + TILESIZE*2/5;
2562 coords[7] = y + TILESIZE*2/5;
2563 draw_polygon(dr, coords, 4, COL_ERROR, COL_GRID);
2566 * Draw an exclamation mark in the diamond. This turns out to
2567 * look unpleasantly off-centre if done via draw_text, so I do
2568 * it by hand on the basis that exclamation marks aren't that
2569 * difficult to draw...
2572 yext = TILESIZE*2/5 - (xext*2+2);
2573 draw_rect(dr, x-xext, y-yext, xext*2+1, yext*2+1 - (xext*3),
2575 draw_rect(dr, x-xext, y+yext-xext*2+1, xext*2+1, xext*2, COL_ERRTEXT);
2578 static void draw_square(drawing *dr, game_drawstate *ds,
2579 game_params *params, struct map *map,
2580 int x, int y, int v)
2582 int w = params->w, h = params->h, wh = w*h;
2583 int tv, bv, xo, yo, errs, pencil, i, j, oldj;
2586 errs = v & ERR_MASK;
2588 pencil = v & PENCIL_MASK;
2590 show_numbers = v & SHOW_NUMBERS;
2595 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2598 * Draw the region colour.
2600 draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
2601 (tv == FOUR ? COL_BACKGROUND : COL_0 + tv));
2603 * Draw the second region colour, if this is a diagonally
2606 if (map->map[TE * wh + y*w+x] != map->map[BE * wh + y*w+x]) {
2608 coords[0] = COORD(x)-1;
2609 coords[1] = COORD(y+1)+1;
2610 if (map->map[LE * wh + y*w+x] == map->map[TE * wh + y*w+x])
2611 coords[2] = COORD(x+1)+1;
2613 coords[2] = COORD(x)-1;
2614 coords[3] = COORD(y)-1;
2615 coords[4] = COORD(x+1)+1;
2616 coords[5] = COORD(y+1)+1;
2617 draw_polygon(dr, coords, 3,
2618 (bv == FOUR ? COL_BACKGROUND : COL_0 + bv), COL_GRID);
2622 * Draw `pencil marks'. Currently we arrange these in a square
2623 * formation, which means we may be in trouble if the value of
2624 * FOUR changes later...
2627 for (yo = 0; yo < 4; yo++)
2628 for (xo = 0; xo < 4; xo++) {
2629 int te = map->map[TE * wh + y*w+x];
2632 e = (yo < xo && yo < 3-xo ? TE :
2633 yo > xo && yo > 3-xo ? BE :
2635 ee = map->map[e * wh + y*w+x];
2637 if (xo != (yo * 2 + 1) % 5)
2641 if (!(pencil & ((ee == te ? PENCIL_T_BASE : PENCIL_B_BASE) << c)))
2645 (map->map[TE * wh + y*w+x] != map->map[LE * wh + y*w+x]))
2646 continue; /* avoid TL-BR diagonal line */
2648 (map->map[TE * wh + y*w+x] != map->map[RE * wh + y*w+x]))
2649 continue; /* avoid BL-TR diagonal line */
2651 draw_circle(dr, COORD(x) + (xo+1)*TILESIZE/5,
2652 COORD(y) + (yo+1)*TILESIZE/5,
2653 TILESIZE/8, COL_0 + c, COL_0 + c);
2657 * Draw the grid lines, if required.
2659 if (x <= 0 || map->map[RE*wh+y*w+(x-1)] != map->map[LE*wh+y*w+x])
2660 draw_rect(dr, COORD(x), COORD(y), 1, TILESIZE, COL_GRID);
2661 if (y <= 0 || map->map[BE*wh+(y-1)*w+x] != map->map[TE*wh+y*w+x])
2662 draw_rect(dr, COORD(x), COORD(y), TILESIZE, 1, COL_GRID);
2663 if (x <= 0 || y <= 0 ||
2664 map->map[RE*wh+(y-1)*w+(x-1)] != map->map[TE*wh+y*w+x] ||
2665 map->map[BE*wh+(y-1)*w+(x-1)] != map->map[LE*wh+y*w+x])
2666 draw_rect(dr, COORD(x), COORD(y), 1, 1, COL_GRID);
2669 * Draw error markers.
2671 for (yo = 0; yo < 3; yo++)
2672 for (xo = 0; xo < 3; xo++)
2673 if (errs & (ERR_BASE << (yo*3+xo)))
2675 (COORD(x)*2+TILESIZE*xo)/2,
2676 (COORD(y)*2+TILESIZE*yo)/2);
2679 * Draw region numbers, if desired.
2683 for (i = 0; i < 2; i++) {
2684 j = map->map[(i?BE:TE)*wh+y*w+x];
2689 xo = map->regionx[j] - 2*x;
2690 yo = map->regiony[j] - 2*y;
2691 if (xo >= 0 && xo <= 2 && yo >= 0 && yo <= 2) {
2693 sprintf(buf, "%d", j);
2694 draw_text(dr, (COORD(x)*2+TILESIZE*xo)/2,
2695 (COORD(y)*2+TILESIZE*yo)/2,
2696 FONT_VARIABLE, 3*TILESIZE/5,
2697 ALIGN_HCENTRE|ALIGN_VCENTRE,
2705 draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2708 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2709 game_state *state, int dir, game_ui *ui,
2710 float animtime, float flashtime)
2712 int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
2716 if (ds->drag_visible) {
2717 blitter_load(dr, ds->bl, ds->dragx, ds->dragy);
2718 draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3);
2719 ds->drag_visible = FALSE;
2723 * The initial contents of the window are not guaranteed and
2724 * can vary with front ends. To be on the safe side, all games
2725 * should start by drawing a big background-colour rectangle
2726 * covering the whole window.
2731 game_compute_size(&state->p, TILESIZE, &ww, &wh);
2732 draw_rect(dr, 0, 0, ww, wh, COL_BACKGROUND);
2733 draw_rect(dr, COORD(0), COORD(0), w*TILESIZE+1, h*TILESIZE+1,
2736 draw_update(dr, 0, 0, ww, wh);
2741 if (flash_type == 1)
2742 flash = (int)(flashtime * FOUR / flash_length);
2744 flash = 1 + (int)(flashtime * THREE / flash_length);
2749 * Set up the `todraw' array.
2751 for (y = 0; y < h; y++)
2752 for (x = 0; x < w; x++) {
2753 int tv = state->colouring[state->map->map[TE * wh + y*w+x]];
2754 int bv = state->colouring[state->map->map[BE * wh + y*w+x]];
2763 if (flash_type == 1) {
2768 } else if (flash_type == 2) {
2773 tv = (tv + flash) % FOUR;
2775 bv = (bv + flash) % FOUR;
2784 for (i = 0; i < FOUR; i++) {
2785 if (state->colouring[state->map->map[TE * wh + y*w+x]] < 0 &&
2786 (state->pencil[state->map->map[TE * wh + y*w+x]] & (1<<i)))
2787 v |= PENCIL_T_BASE << i;
2788 if (state->colouring[state->map->map[BE * wh + y*w+x]] < 0 &&
2789 (state->pencil[state->map->map[BE * wh + y*w+x]] & (1<<i)))
2790 v |= PENCIL_B_BASE << i;
2793 if (ui->show_numbers)
2796 ds->todraw[y*w+x] = v;
2800 * Add error markers to the `todraw' array.
2802 for (i = 0; i < state->map->ngraph; i++) {
2803 int v1 = state->map->graph[i] / n;
2804 int v2 = state->map->graph[i] % n;
2807 if (state->colouring[v1] < 0 || state->colouring[v2] < 0)
2809 if (state->colouring[v1] != state->colouring[v2])
2812 x = state->map->edgex[i];
2813 y = state->map->edgey[i];
2818 ds->todraw[y*w+x] |= ERR_BASE << (yo*3+xo);
2821 ds->todraw[y*w+(x-1)] |= ERR_BASE << (yo*3+2);
2825 ds->todraw[(y-1)*w+x] |= ERR_BASE << (2*3+xo);
2827 if (xo == 0 && yo == 0) {
2828 assert(x > 0 && y > 0);
2829 ds->todraw[(y-1)*w+(x-1)] |= ERR_BASE << (2*3+2);
2834 * Now actually draw everything.
2836 for (y = 0; y < h; y++)
2837 for (x = 0; x < w; x++) {
2838 int v = ds->todraw[y*w+x];
2839 if (ds->drawn[y*w+x] != v) {
2840 draw_square(dr, ds, &state->p, state->map, x, y, v);
2841 ds->drawn[y*w+x] = v;
2846 * Draw the dragged colour blob if any.
2848 if (ui->drag_colour > -2) {
2849 ds->dragx = ui->dragx - TILESIZE/2 - 2;
2850 ds->dragy = ui->dragy - TILESIZE/2 - 2;
2851 blitter_save(dr, ds->bl, ds->dragx, ds->dragy);
2852 draw_circle(dr, ui->dragx, ui->dragy, TILESIZE/2,
2853 (ui->drag_colour < 0 ? COL_BACKGROUND :
2854 COL_0 + ui->drag_colour), COL_GRID);
2855 draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3);
2856 ds->drag_visible = TRUE;
2860 static float game_anim_length(game_state *oldstate, game_state *newstate,
2861 int dir, game_ui *ui)
2866 static float game_flash_length(game_state *oldstate, game_state *newstate,
2867 int dir, game_ui *ui)
2869 if (!oldstate->completed && newstate->completed &&
2870 !oldstate->cheated && !newstate->cheated) {
2871 if (flash_type < 0) {
2872 char *env = getenv("MAP_ALTERNATIVE_FLASH");
2874 flash_type = atoi(env);
2877 flash_length = (flash_type == 1 ? 0.50 : 0.30);
2879 return flash_length;
2884 static int game_wants_statusbar(void)
2889 static int game_timing_state(game_state *state, game_ui *ui)
2894 static void game_print_size(game_params *params, float *x, float *y)
2899 * I'll use 4mm squares by default, I think. Simplest way to
2900 * compute this size is to compute the pixel puzzle size at a
2901 * given tile size and then scale.
2903 game_compute_size(params, 400, &pw, &ph);
2908 static void game_print(drawing *dr, game_state *state, int tilesize)
2910 int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
2911 int ink, c[FOUR], i;
2913 int *coords, ncoords, coordsize;
2915 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2916 struct { int tilesize; } ads, *ds = &ads;
2917 ads.tilesize = tilesize;
2919 ink = print_mono_colour(dr, 0);
2920 for (i = 0; i < FOUR; i++)
2921 c[i] = print_rgb_colour(dr, map_hatching[i], map_colours[i][0],
2922 map_colours[i][1], map_colours[i][2]);
2927 print_line_width(dr, TILESIZE / 16);
2930 * Draw a single filled polygon around each region.
2932 for (r = 0; r < n; r++) {
2933 int octants[8], lastdir, d1, d2, ox, oy;
2936 * Start by finding a point on the region boundary. Any
2937 * point will do. To do this, we'll search for a square
2938 * containing the region and then decide which corner of it
2942 for (y = 0; y < h; y++) {
2943 for (x = 0; x < w; x++) {
2944 if (state->map->map[wh*0+y*w+x] == r ||
2945 state->map->map[wh*1+y*w+x] == r ||
2946 state->map->map[wh*2+y*w+x] == r ||
2947 state->map->map[wh*3+y*w+x] == r)
2953 assert(y < h && x < w); /* we must have found one somewhere */
2955 * This is the first square in lexicographic order which
2956 * contains part of this region. Therefore, one of the top
2957 * two corners of the square must be what we're after. The
2958 * only case in which it isn't the top left one is if the
2959 * square is diagonally divided and the region is in the
2960 * bottom right half.
2962 if (state->map->map[wh*TE+y*w+x] != r &&
2963 state->map->map[wh*LE+y*w+x] != r)
2964 x++; /* could just as well have done y++ */
2967 * Now we have a point on the region boundary. Trace around
2968 * the region until we come back to this point,
2969 * accumulating coordinates for a polygon draw operation as
2979 * There are eight possible directions we could head in
2980 * from here. We identify them by octant numbers, and
2981 * we also use octant numbers to identify the spaces
2994 octants[0] = x<w && y>0 ? state->map->map[wh*LE+(y-1)*w+x] : -1;
2995 octants[1] = x<w && y>0 ? state->map->map[wh*BE+(y-1)*w+x] : -1;
2996 octants[2] = x<w && y<h ? state->map->map[wh*TE+y*w+x] : -1;
2997 octants[3] = x<w && y<h ? state->map->map[wh*LE+y*w+x] : -1;
2998 octants[4] = x>0 && y<h ? state->map->map[wh*RE+y*w+(x-1)] : -1;
2999 octants[5] = x>0 && y<h ? state->map->map[wh*TE+y*w+(x-1)] : -1;
3000 octants[6] = x>0 && y>0 ? state->map->map[wh*BE+(y-1)*w+(x-1)] :-1;
3001 octants[7] = x>0 && y>0 ? state->map->map[wh*RE+(y-1)*w+(x-1)] :-1;
3004 for (i = 0; i < 8; i++)
3005 if ((octants[i] == r) ^ (octants[(i+1)%8] == r)) {
3013 assert(d1 != -1 && d2 != -1);
3018 * Now we're heading in direction d1. Save the current
3021 if (ncoords + 2 > coordsize) {
3023 coords = sresize(coords, coordsize, int);
3025 coords[ncoords++] = COORD(x);
3026 coords[ncoords++] = COORD(y);
3029 * Compute the new coordinates.
3031 x += (d1 % 4 == 3 ? 0 : d1 < 4 ? +1 : -1);
3032 y += (d1 % 4 == 1 ? 0 : d1 > 1 && d1 < 5 ? +1 : -1);
3033 assert(x >= 0 && x <= w && y >= 0 && y <= h);
3036 } while (x != ox || y != oy);
3038 draw_polygon(dr, coords, ncoords/2,
3039 state->colouring[r] >= 0 ?
3040 c[state->colouring[r]] : -1, ink);
3049 const struct game thegame = {
3057 TRUE, game_configure, custom_params,
3065 FALSE, game_text_format,
3073 20, game_compute_size, game_set_size,
3076 game_free_drawstate,
3080 TRUE, TRUE, game_print_size, game_print,
3081 game_wants_statusbar,
3082 FALSE, game_timing_state,
3083 0, /* mouse_priorities */
3086 #ifdef STANDALONE_SOLVER
3090 void frontend_default_colour(frontend *fe, float *output) {}
3091 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
3092 int align, int colour, char *text) {}
3093 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour) {}
3094 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour) {}
3095 void draw_polygon(drawing *dr, int *coords, int npoints,
3096 int fillcolour, int outlinecolour) {}
3097 void draw_circle(drawing *dr, int cx, int cy, int radius,
3098 int fillcolour, int outlinecolour) {}
3099 void clip(drawing *dr, int x, int y, int w, int h) {}
3100 void unclip(drawing *dr) {}
3101 void start_draw(drawing *dr) {}
3102 void draw_update(drawing *dr, int x, int y, int w, int h) {}
3103 void end_draw(drawing *dr) {}
3104 blitter *blitter_new(drawing *dr, int w, int h) {return NULL;}
3105 void blitter_free(drawing *dr, blitter *bl) {}
3106 void blitter_save(drawing *dr, blitter *bl, int x, int y) {}
3107 void blitter_load(drawing *dr, blitter *bl, int x, int y) {}
3108 int print_mono_colour(drawing *dr, int grey) { return 0; }
3109 int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b)
3111 void print_line_width(drawing *dr, int width) {}
3113 void fatal(char *fmt, ...)
3117 fprintf(stderr, "fatal error: ");
3120 vfprintf(stderr, fmt, ap);
3123 fprintf(stderr, "\n");
3127 int main(int argc, char **argv)
3131 char *id = NULL, *desc, *err;
3133 int ret, diff, really_verbose = FALSE;
3134 struct solver_scratch *sc;
3137 while (--argc > 0) {
3139 if (!strcmp(p, "-v")) {
3140 really_verbose = TRUE;
3141 } else if (!strcmp(p, "-g")) {
3143 } else if (*p == '-') {
3144 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
3152 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
3156 desc = strchr(id, ':');
3158 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
3163 p = default_params();
3164 decode_params(p, id);
3165 err = validate_desc(p, desc);
3167 fprintf(stderr, "%s: %s\n", argv[0], err);
3170 s = new_game(NULL, p, desc);
3172 sc = new_scratch(s->map->graph, s->map->n, s->map->ngraph);
3175 * When solving an Easy puzzle, we don't want to bother the
3176 * user with Hard-level deductions. For this reason, we grade
3177 * the puzzle internally before doing anything else.
3179 ret = -1; /* placate optimiser */
3180 for (diff = 0; diff < DIFFCOUNT; diff++) {
3181 for (i = 0; i < s->map->n; i++)
3182 if (!s->map->immutable[i])
3183 s->colouring[i] = -1;
3184 ret = map_solver(sc, s->map->graph, s->map->n, s->map->ngraph,
3185 s->colouring, diff);
3190 if (diff == DIFFCOUNT) {
3192 printf("Difficulty rating: harder than Hard, or ambiguous\n");
3194 printf("Unable to find a unique solution\n");
3198 printf("Difficulty rating: impossible (no solution exists)\n");
3200 printf("Difficulty rating: %s\n", map_diffnames[diff]);
3202 verbose = really_verbose;
3203 for (i = 0; i < s->map->n; i++)
3204 if (!s->map->immutable[i])
3205 s->colouring[i] = -1;
3206 ret = map_solver(sc, s->map->graph, s->map->n, s->map->ngraph,
3207 s->colouring, diff);
3209 printf("Puzzle is inconsistent\n");
3213 for (i = 0; i < s->map->n; i++) {
3214 printf("%5d <- %c%c", i, colnames[s->colouring[i]],
3215 (col < 6 && i+1 < s->map->n ? ' ' : '\n'));