2 * map.c: Game involving four-colouring a map.
9 * - better four-colouring algorithm?
10 * - can we make the pencil marks look nicer?
11 * - ability to drag a set of pencil marks?
24 * In standalone solver mode, `verbose' is a variable which can be
25 * set by command-line option; in debugging mode it's simply always
28 #if defined STANDALONE_SOLVER
29 #define SOLVER_DIAGNOSTICS
31 #elif defined SOLVER_DIAGNOSTICS
36 * I don't seriously anticipate wanting to change the number of
37 * colours used in this game, but it doesn't cost much to use a
38 * #define just in case :-)
41 #define THREE (FOUR-1)
46 * Ghastly run-time configuration option, just for Gareth (again).
48 static int flash_type = -1;
49 static float flash_length;
52 * Difficulty levels. I do some macro ickery here to ensure that my
53 * enum and the various forms of my name list always match up.
59 A(RECURSE,Unreasonable,u)
60 #define ENUM(upper,title,lower) DIFF_ ## upper,
61 #define TITLE(upper,title,lower) #title,
62 #define ENCODE(upper,title,lower) #lower
63 #define CONFIG(upper,title,lower) ":" #title
64 enum { DIFFLIST(ENUM) DIFFCOUNT };
65 static char const *const map_diffnames[] = { DIFFLIST(TITLE) };
66 static char const map_diffchars[] = DIFFLIST(ENCODE);
67 #define DIFFCONFIG DIFFLIST(CONFIG)
69 enum { TE, BE, LE, RE }; /* top/bottom/left/right edges */
74 COL_0, COL_1, COL_2, COL_3,
75 COL_ERROR, COL_ERRTEXT,
90 int *edgex, *edgey; /* position of a point on each edge */
91 int *regionx, *regiony; /* position of a point in each region */
97 int *colouring, *pencil;
98 int completed, cheated;
101 static game_params *default_params(void)
103 game_params *ret = snew(game_params);
108 ret->diff = DIFF_NORMAL;
113 static const struct game_params map_presets[] = {
114 {20, 15, 30, DIFF_EASY},
115 {20, 15, 30, DIFF_NORMAL},
116 {20, 15, 30, DIFF_HARD},
117 {20, 15, 30, DIFF_RECURSE},
118 {30, 25, 75, DIFF_NORMAL},
119 {30, 25, 75, DIFF_HARD},
122 static int game_fetch_preset(int i, char **name, game_params **params)
127 if (i < 0 || i >= lenof(map_presets))
130 ret = snew(game_params);
131 *ret = map_presets[i];
133 sprintf(str, "%dx%d, %d regions, %s", ret->w, ret->h, ret->n,
134 map_diffnames[ret->diff]);
141 static void free_params(game_params *params)
146 static game_params *dup_params(game_params *params)
148 game_params *ret = snew(game_params);
149 *ret = *params; /* structure copy */
153 static void decode_params(game_params *params, char const *string)
155 char const *p = string;
158 while (*p && isdigit((unsigned char)*p)) p++;
162 while (*p && isdigit((unsigned char)*p)) p++;
164 params->h = params->w;
169 while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
171 params->n = params->w * params->h / 8;
176 for (i = 0; i < DIFFCOUNT; i++)
177 if (*p == map_diffchars[i])
183 static char *encode_params(game_params *params, int full)
187 sprintf(ret, "%dx%dn%d", params->w, params->h, params->n);
189 sprintf(ret + strlen(ret), "d%c", map_diffchars[params->diff]);
194 static config_item *game_configure(game_params *params)
199 ret = snewn(5, config_item);
201 ret[0].name = "Width";
202 ret[0].type = C_STRING;
203 sprintf(buf, "%d", params->w);
204 ret[0].sval = dupstr(buf);
207 ret[1].name = "Height";
208 ret[1].type = C_STRING;
209 sprintf(buf, "%d", params->h);
210 ret[1].sval = dupstr(buf);
213 ret[2].name = "Regions";
214 ret[2].type = C_STRING;
215 sprintf(buf, "%d", params->n);
216 ret[2].sval = dupstr(buf);
219 ret[3].name = "Difficulty";
220 ret[3].type = C_CHOICES;
221 ret[3].sval = DIFFCONFIG;
222 ret[3].ival = params->diff;
232 static game_params *custom_params(config_item *cfg)
234 game_params *ret = snew(game_params);
236 ret->w = atoi(cfg[0].sval);
237 ret->h = atoi(cfg[1].sval);
238 ret->n = atoi(cfg[2].sval);
239 ret->diff = cfg[3].ival;
244 static char *validate_params(game_params *params, int full)
246 if (params->w < 2 || params->h < 2)
247 return "Width and height must be at least two";
249 return "Must have at least five regions";
250 if (params->n > params->w * params->h)
251 return "Too many regions to fit in grid";
255 /* ----------------------------------------------------------------------
256 * Cumulative frequency table functions.
260 * Initialise a cumulative frequency table. (Hardly worth writing
261 * this function; all it does is to initialise everything in the
264 static void cf_init(int *table, int n)
268 for (i = 0; i < n; i++)
273 * Increment the count of symbol `sym' by `count'.
275 static void cf_add(int *table, int n, int sym, int count)
292 * Cumulative frequency lookup: return the total count of symbols
293 * with value less than `sym'.
295 static int cf_clookup(int *table, int n, int sym)
297 int bit, index, limit, count;
302 assert(0 < sym && sym <= n);
304 count = table[0]; /* start with the whole table size */
314 * Find the least number with its lowest set bit in this
315 * position which is greater than or equal to sym.
317 index = ((sym + bit - 1) &~ (bit * 2 - 1)) + bit;
320 count -= table[index];
331 * Single frequency lookup: return the count of symbol `sym'.
333 static int cf_slookup(int *table, int n, int sym)
337 assert(0 <= sym && sym < n);
341 for (bit = 1; sym+bit < n && !(sym & bit); bit <<= 1)
342 count -= table[sym+bit];
348 * Return the largest symbol index such that the cumulative
349 * frequency up to that symbol is less than _or equal to_ count.
351 static int cf_whichsym(int *table, int n, int count) {
354 assert(count >= 0 && count < table[0]);
365 if (count >= top - table[sym+bit])
368 top -= table[sym+bit];
377 /* ----------------------------------------------------------------------
380 * FIXME: this isn't entirely optimal at present, because it
381 * inherently prioritises growing the largest region since there
382 * are more squares adjacent to it. This acts as a destabilising
383 * influence leading to a few large regions and mostly small ones.
384 * It might be better to do it some other way.
387 #define WEIGHT_INCREASED 2 /* for increased perimeter */
388 #define WEIGHT_DECREASED 4 /* for decreased perimeter */
389 #define WEIGHT_UNCHANGED 3 /* for unchanged perimeter */
392 * Look at a square and decide which colours can be extended into
395 * If called with index < 0, it adds together one of
396 * WEIGHT_INCREASED, WEIGHT_DECREASED or WEIGHT_UNCHANGED for each
397 * colour that has a valid extension (according to the effect that
398 * it would have on the perimeter of the region being extended) and
399 * returns the overall total.
401 * If called with index >= 0, it returns one of the possible
402 * colours depending on the value of index, in such a way that the
403 * number of possible inputs which would give rise to a given
404 * return value correspond to the weight of that value.
406 static int extend_options(int w, int h, int n, int *map,
407 int x, int y, int index)
413 if (map[y*w+x] >= 0) {
415 return 0; /* can't do this square at all */
419 * Fetch the eight neighbours of this square, in order around
422 for (dy = -1; dy <= +1; dy++)
423 for (dx = -1; dx <= +1; dx++) {
424 int index = (dy < 0 ? 6-dx : dy > 0 ? 2+dx : 2*(1+dx));
425 if (x+dx >= 0 && x+dx < w && y+dy >= 0 && y+dy < h)
426 col[index] = map[(y+dy)*w+(x+dx)];
432 * Iterate over each colour that might be feasible.
434 * FIXME: this routine currently has O(n) running time. We
435 * could turn it into O(FOUR) by only bothering to iterate over
436 * the colours mentioned in the four neighbouring squares.
439 for (c = 0; c < n; c++) {
440 int count, neighbours, runs;
443 * One of the even indices of col (representing the
444 * orthogonal neighbours of this square) must be equal to
445 * c, or else this square is not adjacent to region c and
446 * obviously cannot become an extension of it at this time.
449 for (i = 0; i < 8; i += 2)
456 * Now we know this square is adjacent to region c. The
457 * next question is, would extending it cause the region to
458 * become non-simply-connected? If so, we mustn't do it.
460 * We determine this by looking around col to see if we can
461 * find more than one separate run of colour c.
464 for (i = 0; i < 8; i++)
465 if (col[i] == c && col[(i+1) & 7] != c)
473 * This square is a possibility. Determine its effect on
474 * the region's perimeter (computed from the number of
475 * orthogonal neighbours - 1 means a perimeter increase, 3
476 * a decrease, 2 no change; 4 is impossible because the
477 * region would already not be simply connected) and we're
480 assert(neighbours > 0 && neighbours < 4);
481 count = (neighbours == 1 ? WEIGHT_INCREASED :
482 neighbours == 2 ? WEIGHT_UNCHANGED : WEIGHT_DECREASED);
485 if (index >= 0 && index < count)
496 static void genmap(int w, int h, int n, int *map, random_state *rs)
503 tmp = snewn(wh, int);
506 * Clear the map, and set up `tmp' as a list of grid indices.
508 for (i = 0; i < wh; i++) {
514 * Place the region seeds by selecting n members from `tmp'.
517 for (i = 0; i < n; i++) {
518 int j = random_upto(rs, k);
524 * Re-initialise `tmp' as a cumulative frequency table. This
525 * will store the number of possible region colours we can
526 * extend into each square.
531 * Go through the grid and set up the initial cumulative
534 for (y = 0; y < h; y++)
535 for (x = 0; x < w; x++)
536 cf_add(tmp, wh, y*w+x,
537 extend_options(w, h, n, map, x, y, -1));
540 * Now repeatedly choose a square we can extend a region into,
544 int k = random_upto(rs, tmp[0]);
549 sq = cf_whichsym(tmp, wh, k);
550 k -= cf_clookup(tmp, wh, sq);
553 colour = extend_options(w, h, n, map, x, y, k);
558 * Re-scan the nine cells around the one we've just
561 for (yy = max(y-1, 0); yy < min(y+2, h); yy++)
562 for (xx = max(x-1, 0); xx < min(x+2, w); xx++) {
563 cf_add(tmp, wh, yy*w+xx,
564 -cf_slookup(tmp, wh, yy*w+xx) +
565 extend_options(w, h, n, map, xx, yy, -1));
570 * Finally, go through and normalise the region labels into
571 * order, meaning that indistinguishable maps are actually
574 for (i = 0; i < n; i++)
577 for (i = 0; i < wh; i++) {
581 map[i] = tmp[map[i]];
587 /* ----------------------------------------------------------------------
588 * Functions to handle graphs.
592 * Having got a map in a square grid, convert it into a graph
595 static int gengraph(int w, int h, int n, int *map, int *graph)
600 * Start by setting the graph up as an adjacency matrix. We'll
601 * turn it into a list later.
603 for (i = 0; i < n*n; i++)
607 * Iterate over the map looking for all adjacencies.
609 for (y = 0; y < h; y++)
610 for (x = 0; x < w; x++) {
613 if (x+1 < w && (vx = map[y*w+(x+1)]) != v)
614 graph[v*n+vx] = graph[vx*n+v] = 1;
615 if (y+1 < h && (vy = map[(y+1)*w+x]) != v)
616 graph[v*n+vy] = graph[vy*n+v] = 1;
620 * Turn the matrix into a list.
622 for (i = j = 0; i < n*n; i++)
629 static int graph_edge_index(int *graph, int n, int ngraph, int i, int j)
636 while (top - bot > 1) {
637 mid = (top + bot) / 2;
640 else if (graph[mid] < v)
648 #define graph_adjacent(graph, n, ngraph, i, j) \
649 (graph_edge_index((graph), (n), (ngraph), (i), (j)) >= 0)
651 static int graph_vertex_start(int *graph, int n, int ngraph, int i)
658 while (top - bot > 1) {
659 mid = (top + bot) / 2;
668 /* ----------------------------------------------------------------------
669 * Generate a four-colouring of a graph.
671 * FIXME: it would be nice if we could convert this recursion into
672 * pseudo-recursion using some sort of explicit stack array, for
673 * the sake of the Palm port and its limited stack.
676 static int fourcolour_recurse(int *graph, int n, int ngraph,
677 int *colouring, int *scratch, random_state *rs)
679 int nfree, nvert, start, i, j, k, c, ci;
683 * Find the smallest number of free colours in any uncoloured
684 * vertex, and count the number of such vertices.
687 nfree = FIVE; /* start off bigger than FOUR! */
689 for (i = 0; i < n; i++)
690 if (colouring[i] < 0 && scratch[i*FIVE+FOUR] <= nfree) {
691 if (nfree > scratch[i*FIVE+FOUR]) {
692 nfree = scratch[i*FIVE+FOUR];
699 * If there aren't any uncoloured vertices at all, we're done.
702 return TRUE; /* we've got a colouring! */
705 * Pick a random vertex in that set.
707 j = random_upto(rs, nvert);
708 for (i = 0; i < n; i++)
709 if (colouring[i] < 0 && scratch[i*FIVE+FOUR] == nfree)
713 start = graph_vertex_start(graph, n, ngraph, i);
716 * Loop over the possible colours for i, and recurse for each
720 for (c = 0; c < FOUR; c++)
721 if (scratch[i*FIVE+c] == 0)
723 shuffle(cs, ci, sizeof(*cs), rs);
729 * Fill in this colour.
734 * Update the scratch space to reflect a new neighbour
735 * of this colour for each neighbour of vertex i.
737 for (j = start; j < ngraph && graph[j] < n*(i+1); j++) {
739 if (scratch[k*FIVE+c] == 0)
740 scratch[k*FIVE+FOUR]--;
747 if (fourcolour_recurse(graph, n, ngraph, colouring, scratch, rs))
748 return TRUE; /* got one! */
751 * If that didn't work, clean up and try again with a
754 for (j = start; j < ngraph && graph[j] < n*(i+1); j++) {
757 if (scratch[k*FIVE+c] == 0)
758 scratch[k*FIVE+FOUR]++;
764 * If we reach here, we were unable to find a colouring at all.
765 * (This doesn't necessarily mean the Four Colour Theorem is
766 * violated; it might just mean we've gone down a dead end and
767 * need to back up and look somewhere else. It's only an FCT
768 * violation if we get all the way back up to the top level and
774 static void fourcolour(int *graph, int n, int ngraph, int *colouring,
781 * For each vertex and each colour, we store the number of
782 * neighbours that have that colour. Also, we store the number
783 * of free colours for the vertex.
785 scratch = snewn(n * FIVE, int);
786 for (i = 0; i < n * FIVE; i++)
787 scratch[i] = (i % FIVE == FOUR ? FOUR : 0);
790 * Clear the colouring to start with.
792 for (i = 0; i < n; i++)
795 i = fourcolour_recurse(graph, n, ngraph, colouring, scratch, rs);
796 assert(i); /* by the Four Colour Theorem :-) */
801 /* ----------------------------------------------------------------------
802 * Non-recursive solver.
805 struct solver_scratch {
806 unsigned char *possible; /* bitmap of colours for each region */
814 #ifdef SOLVER_DIAGNOSTICS
821 static struct solver_scratch *new_scratch(int *graph, int n, int ngraph)
823 struct solver_scratch *sc;
825 sc = snew(struct solver_scratch);
829 sc->possible = snewn(n, unsigned char);
831 sc->bfsqueue = snewn(n, int);
832 sc->bfscolour = snewn(n, int);
833 #ifdef SOLVER_DIAGNOSTICS
834 sc->bfsprev = snewn(n, int);
840 static void free_scratch(struct solver_scratch *sc)
844 sfree(sc->bfscolour);
845 #ifdef SOLVER_DIAGNOSTICS
852 * Count the bits in a word. Only needs to cope with FOUR bits.
854 static int bitcount(int word)
856 assert(FOUR <= 4); /* or this needs changing */
857 word = ((word & 0xA) >> 1) + (word & 0x5);
858 word = ((word & 0xC) >> 2) + (word & 0x3);
862 #ifdef SOLVER_DIAGNOSTICS
863 static const char colnames[FOUR] = { 'R', 'Y', 'G', 'B' };
866 static int place_colour(struct solver_scratch *sc,
867 int *colouring, int index, int colour
868 #ifdef SOLVER_DIAGNOSTICS
873 int *graph = sc->graph, n = sc->n, ngraph = sc->ngraph;
876 if (!(sc->possible[index] & (1 << colour))) {
877 #ifdef SOLVER_DIAGNOSTICS
879 printf("%*scannot place %c in region %d\n", 2*sc->depth, "",
880 colnames[colour], index);
882 return FALSE; /* can't do it */
885 sc->possible[index] = 1 << colour;
886 colouring[index] = colour;
888 #ifdef SOLVER_DIAGNOSTICS
890 printf("%*s%s %c in region %d\n", 2*sc->depth, "",
891 verb, colnames[colour], index);
895 * Rule out this colour from all the region's neighbours.
897 for (j = graph_vertex_start(graph, n, ngraph, index);
898 j < ngraph && graph[j] < n*(index+1); j++) {
899 k = graph[j] - index*n;
900 #ifdef SOLVER_DIAGNOSTICS
901 if (verbose && (sc->possible[k] & (1 << colour)))
902 printf("%*s ruling out %c in region %d\n", 2*sc->depth, "",
903 colnames[colour], k);
905 sc->possible[k] &= ~(1 << colour);
911 #ifdef SOLVER_DIAGNOSTICS
912 static char *colourset(char *buf, int set)
918 for (i = 0; i < FOUR; i++)
919 if (set & (1 << i)) {
920 p += sprintf(p, "%s%c", sep, colnames[i]);
929 * Returns 0 for impossible, 1 for success, 2 for failure to
930 * converge (i.e. puzzle is either ambiguous or just too
933 static int map_solver(struct solver_scratch *sc,
934 int *graph, int n, int ngraph, int *colouring,
939 if (sc->depth == 0) {
941 * Initialise scratch space.
943 for (i = 0; i < n; i++)
944 sc->possible[i] = (1 << FOUR) - 1;
949 for (i = 0; i < n; i++)
950 if (colouring[i] >= 0) {
951 if (!place_colour(sc, colouring, i, colouring[i]
952 #ifdef SOLVER_DIAGNOSTICS
956 #ifdef SOLVER_DIAGNOSTICS
958 printf("%*sinitial clue set is inconsistent\n",
961 return 0; /* the clues aren't even consistent! */
967 * Now repeatedly loop until we find nothing further to do.
970 int done_something = FALSE;
972 if (difficulty < DIFF_EASY)
973 break; /* can't do anything at all! */
976 * Simplest possible deduction: find a region with only one
979 for (i = 0; i < n; i++) if (colouring[i] < 0) {
980 int p = sc->possible[i];
983 #ifdef SOLVER_DIAGNOSTICS
985 printf("%*sregion %d has no possible colours left\n",
988 return 0; /* puzzle is inconsistent */
991 if ((p & (p-1)) == 0) { /* p is a power of two */
993 for (c = 0; c < FOUR; c++)
997 ret = place_colour(sc, colouring, i, c
998 #ifdef SOLVER_DIAGNOSTICS
1003 * place_colour() can only fail if colour c was not
1004 * even a _possibility_ for region i, and we're
1005 * pretty sure it was because we checked before
1006 * calling place_colour(). So we can safely assert
1007 * here rather than having to return a nice
1008 * friendly error code.
1011 done_something = TRUE;
1018 if (difficulty < DIFF_NORMAL)
1019 break; /* can't do anything harder */
1022 * Failing that, go up one level. Look for pairs of regions
1023 * which (a) both have the same pair of possible colours,
1024 * (b) are adjacent to one another, (c) are adjacent to the
1025 * same region, and (d) that region still thinks it has one
1026 * or both of those possible colours.
1028 * Simplest way to do this is by going through the graph
1029 * edge by edge, so that we start with property (b) and
1030 * then look for (a) and finally (c) and (d).
1032 for (i = 0; i < ngraph; i++) {
1033 int j1 = graph[i] / n, j2 = graph[i] % n;
1035 #ifdef SOLVER_DIAGNOSTICS
1036 int started = FALSE;
1040 continue; /* done it already, other way round */
1042 if (colouring[j1] >= 0 || colouring[j2] >= 0)
1043 continue; /* they're not undecided */
1045 if (sc->possible[j1] != sc->possible[j2])
1046 continue; /* they don't have the same possibles */
1048 v = sc->possible[j1];
1050 * See if v contains exactly two set bits.
1052 v2 = v & -v; /* find lowest set bit */
1053 v2 = v & ~v2; /* clear it */
1054 if (v2 == 0 || (v2 & (v2-1)) != 0) /* not power of 2 */
1058 * We've found regions j1 and j2 satisfying properties
1059 * (a) and (b): they have two possible colours between
1060 * them, and since they're adjacent to one another they
1061 * must use _both_ those colours between them.
1062 * Therefore, if they are both adjacent to any other
1063 * region then that region cannot be either colour.
1065 * Go through the neighbours of j1 and see if any are
1068 for (j = graph_vertex_start(graph, n, ngraph, j1);
1069 j < ngraph && graph[j] < n*(j1+1); j++) {
1070 k = graph[j] - j1*n;
1071 if (graph_adjacent(graph, n, ngraph, k, j2) &&
1072 (sc->possible[k] & v)) {
1073 #ifdef SOLVER_DIAGNOSTICS
1077 printf("%*sadjacent regions %d,%d share colours"
1078 " %s\n", 2*sc->depth, "", j1, j2,
1081 printf("%*s ruling out %s in region %d\n",2*sc->depth,
1082 "", colourset(buf, sc->possible[k] & v), k);
1085 sc->possible[k] &= ~v;
1086 done_something = TRUE;
1094 if (difficulty < DIFF_HARD)
1095 break; /* can't do anything harder */
1098 * Right; now we get creative. Now we're going to look for
1099 * `forcing chains'. A forcing chain is a path through the
1100 * graph with the following properties:
1102 * (a) Each vertex on the path has precisely two possible
1105 * (b) Each pair of vertices which are adjacent on the
1106 * path share at least one possible colour in common.
1108 * (c) Each vertex in the middle of the path shares _both_
1109 * of its colours with at least one of its neighbours
1110 * (not the same one with both neighbours).
1112 * These together imply that at least one of the possible
1113 * colour choices at one end of the path forces _all_ the
1114 * rest of the colours along the path. In order to make
1115 * real use of this, we need further properties:
1117 * (c) Ruling out some colour C from the vertex at one end
1118 * of the path forces the vertex at the other end to
1121 * (d) The two end vertices are mutually adjacent to some
1124 * (e) That third vertex currently has C as a possibility.
1126 * If we can find all of that lot, we can deduce that at
1127 * least one of the two ends of the forcing chain has
1128 * colour C, and that therefore the mutually adjacent third
1131 * To find forcing chains, we're going to start a bfs at
1132 * each suitable vertex of the graph, once for each of its
1133 * two possible colours.
1135 for (i = 0; i < n; i++) {
1138 if (colouring[i] >= 0 || bitcount(sc->possible[i]) != 2)
1141 for (c = 0; c < FOUR; c++)
1142 if (sc->possible[i] & (1 << c)) {
1143 int j, k, gi, origc, currc, head, tail;
1145 * Try a bfs from this vertex, ruling out
1148 * Within this loop, we work in colour bitmaps
1149 * rather than actual colours, because
1150 * converting back and forth is a needless
1151 * computational expense.
1156 for (j = 0; j < n; j++) {
1157 sc->bfscolour[j] = -1;
1158 #ifdef SOLVER_DIAGNOSTICS
1159 sc->bfsprev[j] = -1;
1163 sc->bfsqueue[tail++] = i;
1164 sc->bfscolour[i] = sc->possible[i] &~ origc;
1166 while (head < tail) {
1167 j = sc->bfsqueue[head++];
1168 currc = sc->bfscolour[j];
1171 * Try neighbours of j.
1173 for (gi = graph_vertex_start(graph, n, ngraph, j);
1174 gi < ngraph && graph[gi] < n*(j+1); gi++) {
1175 k = graph[gi] - j*n;
1178 * To continue with the bfs in vertex
1179 * k, we need k to be
1180 * (a) not already visited
1181 * (b) have two possible colours
1182 * (c) those colours include currc.
1185 if (sc->bfscolour[k] < 0 &&
1187 bitcount(sc->possible[k]) == 2 &&
1188 (sc->possible[k] & currc)) {
1189 sc->bfsqueue[tail++] = k;
1191 sc->possible[k] &~ currc;
1192 #ifdef SOLVER_DIAGNOSTICS
1198 * One other possibility is that k
1199 * might be the region in which we can
1200 * make a real deduction: if it's
1201 * adjacent to i, contains currc as a
1202 * possibility, and currc is equal to
1203 * the original colour we ruled out.
1205 if (currc == origc &&
1206 graph_adjacent(graph, n, ngraph, k, i) &&
1207 (sc->possible[k] & currc)) {
1208 #ifdef SOLVER_DIAGNOSTICS
1210 char buf[80], *sep = "";
1213 printf("%*sforcing chain, colour %s, ",
1215 colourset(buf, origc));
1216 for (r = j; r != -1; r = sc->bfsprev[r]) {
1217 printf("%s%d", sep, r);
1220 printf("\n%*s ruling out %s in region"
1221 " %d\n", 2*sc->depth, "",
1222 colourset(buf, origc), k);
1225 sc->possible[k] &= ~origc;
1226 done_something = TRUE;
1235 if (!done_something)
1240 * See if we've got a complete solution, and return if so.
1242 for (i = 0; i < n; i++)
1243 if (colouring[i] < 0)
1246 #ifdef SOLVER_DIAGNOSTICS
1248 printf("%*sone solution found\n", 2*sc->depth, "");
1250 return 1; /* success! */
1254 * If recursion is not permissible, we now give up.
1256 if (difficulty < DIFF_RECURSE) {
1257 #ifdef SOLVER_DIAGNOSTICS
1259 printf("%*sunable to proceed further without recursion\n",
1262 return 2; /* unable to complete */
1266 * Now we've got to do something recursive. So first hunt for a
1267 * currently-most-constrained region.
1271 struct solver_scratch *rsc;
1272 int *subcolouring, *origcolouring;
1274 int we_already_got_one;
1279 for (i = 0; i < n; i++) if (colouring[i] < 0) {
1280 int p = sc->possible[i];
1281 enum { compile_time_assertion = 1 / (FOUR <= 4) };
1284 /* Count the set bits. */
1285 c = (p & 5) + ((p >> 1) & 5);
1286 c = (c & 3) + ((c >> 2) & 3);
1287 assert(c > 1); /* or colouring[i] would be >= 0 */
1295 assert(best >= 0); /* or we'd be solved already */
1297 #ifdef SOLVER_DIAGNOSTICS
1299 printf("%*srecursing on region %d\n", 2*sc->depth, "", best);
1303 * Now iterate over the possible colours for this region.
1305 rsc = new_scratch(graph, n, ngraph);
1306 rsc->depth = sc->depth + 1;
1307 origcolouring = snewn(n, int);
1308 memcpy(origcolouring, colouring, n * sizeof(int));
1309 subcolouring = snewn(n, int);
1310 we_already_got_one = FALSE;
1313 for (i = 0; i < FOUR; i++) {
1314 if (!(sc->possible[best] & (1 << i)))
1317 memcpy(rsc->possible, sc->possible, n);
1318 memcpy(subcolouring, origcolouring, n * sizeof(int));
1320 place_colour(rsc, subcolouring, best, i
1321 #ifdef SOLVER_DIAGNOSTICS
1326 subret = map_solver(rsc, graph, n, ngraph,
1327 subcolouring, difficulty);
1329 #ifdef SOLVER_DIAGNOSTICS
1331 printf("%*sretracting %c in region %d; found %s\n",
1332 2*sc->depth, "", colnames[i], best,
1333 subret == 0 ? "no solutions" :
1334 subret == 1 ? "one solution" : "multiple solutions");
1339 * If this possibility turned up more than one valid
1340 * solution, or if it turned up one and we already had
1341 * one, we're definitely ambiguous.
1343 if (subret == 2 || (subret == 1 && we_already_got_one)) {
1349 * If this possibility turned up one valid solution and
1350 * it's the first we've seen, copy it into the output.
1353 memcpy(colouring, subcolouring, n * sizeof(int));
1354 we_already_got_one = TRUE;
1359 * Otherwise, this guess led to a contradiction, so we
1364 sfree(subcolouring);
1367 #ifdef SOLVER_DIAGNOSTICS
1368 if (verbose && sc->depth == 0) {
1369 printf("%*s%s found\n",
1371 ret == 0 ? "no solutions" :
1372 ret == 1 ? "one solution" : "multiple solutions");
1379 /* ----------------------------------------------------------------------
1380 * Game generation main function.
1383 static char *new_game_desc(game_params *params, random_state *rs,
1384 char **aux, int interactive)
1386 struct solver_scratch *sc = NULL;
1387 int *map, *graph, ngraph, *colouring, *colouring2, *regions;
1388 int i, j, w, h, n, solveret, cfreq[FOUR];
1391 #ifdef GENERATION_DIAGNOSTICS
1395 int retlen, retsize;
1404 map = snewn(wh, int);
1405 graph = snewn(n*n, int);
1406 colouring = snewn(n, int);
1407 colouring2 = snewn(n, int);
1408 regions = snewn(n, int);
1411 * This is the minimum difficulty below which we'll completely
1412 * reject a map design. Normally we set this to one below the
1413 * requested difficulty, ensuring that we have the right
1414 * result. However, for particularly dense maps or maps with
1415 * particularly few regions it might not be possible to get the
1416 * desired difficulty, so we will eventually drop this down to
1417 * -1 to indicate that any old map will do.
1419 mindiff = params->diff;
1427 genmap(w, h, n, map, rs);
1429 #ifdef GENERATION_DIAGNOSTICS
1430 for (y = 0; y < h; y++) {
1431 for (x = 0; x < w; x++) {
1436 putchar('a' + v-36);
1438 putchar('A' + v-10);
1447 * Convert the map into a graph.
1449 ngraph = gengraph(w, h, n, map, graph);
1451 #ifdef GENERATION_DIAGNOSTICS
1452 for (i = 0; i < ngraph; i++)
1453 printf("%d-%d\n", graph[i]/n, graph[i]%n);
1459 fourcolour(graph, n, ngraph, colouring, rs);
1461 #ifdef GENERATION_DIAGNOSTICS
1462 for (i = 0; i < n; i++)
1463 printf("%d: %d\n", i, colouring[i]);
1465 for (y = 0; y < h; y++) {
1466 for (x = 0; x < w; x++) {
1467 int v = colouring[map[y*w+x]];
1469 putchar('a' + v-36);
1471 putchar('A' + v-10);
1480 * Encode the solution as an aux string.
1482 if (*aux) /* in case we've come round again */
1484 retlen = retsize = 0;
1486 for (i = 0; i < n; i++) {
1489 if (colouring[i] < 0)
1492 len = sprintf(buf, "%s%d:%d", i ? ";" : "S;", colouring[i], i);
1493 if (retlen + len >= retsize) {
1494 retsize = retlen + len + 256;
1495 ret = sresize(ret, retsize, char);
1497 strcpy(ret + retlen, buf);
1503 * Remove the region colours one by one, keeping
1504 * solubility. Also ensure that there always remains at
1505 * least one region of every colour, so that the user can
1506 * drag from somewhere.
1508 for (i = 0; i < FOUR; i++)
1510 for (i = 0; i < n; i++) {
1512 cfreq[colouring[i]]++;
1514 for (i = 0; i < FOUR; i++)
1518 shuffle(regions, n, sizeof(*regions), rs);
1520 if (sc) free_scratch(sc);
1521 sc = new_scratch(graph, n, ngraph);
1523 for (i = 0; i < n; i++) {
1526 if (cfreq[colouring[j]] == 1)
1527 continue; /* can't remove last region of colour */
1529 memcpy(colouring2, colouring, n*sizeof(int));
1531 solveret = map_solver(sc, graph, n, ngraph, colouring2,
1533 assert(solveret >= 0); /* mustn't be impossible! */
1534 if (solveret == 1) {
1535 cfreq[colouring[j]]--;
1540 #ifdef GENERATION_DIAGNOSTICS
1541 for (i = 0; i < n; i++)
1542 if (colouring[i] >= 0) {
1546 putchar('a' + i-36);
1548 putchar('A' + i-10);
1551 printf(": %d\n", colouring[i]);
1556 * Finally, check that the puzzle is _at least_ as hard as
1557 * required, and indeed that it isn't already solved.
1558 * (Calling map_solver with negative difficulty ensures the
1559 * latter - if a solver which _does nothing_ can solve it,
1562 memcpy(colouring2, colouring, n*sizeof(int));
1563 if (map_solver(sc, graph, n, ngraph, colouring2,
1564 mindiff - 1) == 1) {
1566 * Drop minimum difficulty if necessary.
1568 if (mindiff > 0 && (n < 9 || n > 2*wh/3)) {
1570 mindiff = 0; /* give up and go for Easy */
1579 * Encode as a game ID. We do this by:
1581 * - first going along the horizontal edges row by row, and
1582 * then the vertical edges column by column
1583 * - encoding the lengths of runs of edges and runs of
1585 * - the decoder will reconstitute the region boundaries from
1586 * this and automatically number them the same way we did
1587 * - then we encode the initial region colours in a Slant-like
1588 * fashion (digits 0-3 interspersed with letters giving
1589 * lengths of runs of empty spaces).
1591 retlen = retsize = 0;
1598 * Start with a notional non-edge, so that there'll be an
1599 * explicit `a' to distinguish the case where we start with
1605 for (i = 0; i < w*(h-1) + (w-1)*h; i++) {
1606 int x, y, dx, dy, v;
1609 /* Horizontal edge. */
1615 /* Vertical edge. */
1616 x = (i - w*(h-1)) / h;
1617 y = (i - w*(h-1)) % h;
1622 if (retlen + 10 >= retsize) {
1623 retsize = retlen + 256;
1624 ret = sresize(ret, retsize, char);
1627 v = (map[y*w+x] != map[(y+dy)*w+(x+dx)]);
1630 ret[retlen++] = 'a'-1 + run;
1635 * 'z' is a special case in this encoding. Rather
1636 * than meaning a run of 26 and a state switch, it
1637 * means a run of 25 and _no_ state switch, because
1638 * otherwise there'd be no way to encode runs of
1642 ret[retlen++] = 'z';
1649 ret[retlen++] = 'a'-1 + run;
1650 ret[retlen++] = ',';
1653 for (i = 0; i < n; i++) {
1654 if (retlen + 10 >= retsize) {
1655 retsize = retlen + 256;
1656 ret = sresize(ret, retsize, char);
1659 if (colouring[i] < 0) {
1661 * In _this_ encoding, 'z' is a run of 26, since
1662 * there's no implicit state switch after each run.
1663 * Confusingly different, but more compact.
1666 ret[retlen++] = 'z';
1672 ret[retlen++] = 'a'-1 + run;
1673 ret[retlen++] = '0' + colouring[i];
1678 ret[retlen++] = 'a'-1 + run;
1681 assert(retlen < retsize);
1694 static char *parse_edge_list(game_params *params, char **desc, int *map)
1696 int w = params->w, h = params->h, wh = w*h, n = params->n;
1697 int i, k, pos, state;
1700 for (i = 0; i < wh; i++)
1707 * Parse the game description to get the list of edges, and
1708 * build up a disjoint set forest as we go (by identifying
1709 * pairs of squares whenever the edge list shows a non-edge).
1711 while (*p && *p != ',') {
1712 if (*p < 'a' || *p > 'z')
1713 return "Unexpected character in edge list";
1724 } else if (pos < w*(h-1)) {
1725 /* Horizontal edge. */
1730 } else if (pos < 2*wh-w-h) {
1731 /* Vertical edge. */
1732 x = (pos - w*(h-1)) / h;
1733 y = (pos - w*(h-1)) % h;
1737 return "Too much data in edge list";
1739 dsf_merge(map+wh, y*w+x, (y+dy)*w+(x+dx));
1747 assert(pos <= 2*wh-w-h);
1749 return "Too little data in edge list";
1752 * Now go through again and allocate region numbers.
1755 for (i = 0; i < wh; i++)
1757 for (i = 0; i < wh; i++) {
1758 k = dsf_canonify(map+wh, i);
1764 return "Edge list defines the wrong number of regions";
1771 static char *validate_desc(game_params *params, char *desc)
1773 int w = params->w, h = params->h, wh = w*h, n = params->n;
1778 map = snewn(2*wh, int);
1779 ret = parse_edge_list(params, &desc, map);
1785 return "Expected comma before clue list";
1786 desc++; /* eat comma */
1790 if (*desc >= '0' && *desc < '0'+FOUR)
1792 else if (*desc >= 'a' && *desc <= 'z')
1793 area += *desc - 'a' + 1;
1795 return "Unexpected character in clue list";
1799 return "Too little data in clue list";
1801 return "Too much data in clue list";
1806 static game_state *new_game(midend *me, game_params *params, char *desc)
1808 int w = params->w, h = params->h, wh = w*h, n = params->n;
1811 game_state *state = snew(game_state);
1814 state->colouring = snewn(n, int);
1815 for (i = 0; i < n; i++)
1816 state->colouring[i] = -1;
1817 state->pencil = snewn(n, int);
1818 for (i = 0; i < n; i++)
1819 state->pencil[i] = 0;
1821 state->completed = state->cheated = FALSE;
1823 state->map = snew(struct map);
1824 state->map->refcount = 1;
1825 state->map->map = snewn(wh*4, int);
1826 state->map->graph = snewn(n*n, int);
1828 state->map->immutable = snewn(n, int);
1829 for (i = 0; i < n; i++)
1830 state->map->immutable[i] = FALSE;
1836 ret = parse_edge_list(params, &p, state->map->map);
1841 * Set up the other three quadrants in `map'.
1843 for (i = wh; i < 4*wh; i++)
1844 state->map->map[i] = state->map->map[i % wh];
1850 * Now process the clue list.
1854 if (*p >= '0' && *p < '0'+FOUR) {
1855 state->colouring[pos] = *p - '0';
1856 state->map->immutable[pos] = TRUE;
1859 assert(*p >= 'a' && *p <= 'z');
1860 pos += *p - 'a' + 1;
1866 state->map->ngraph = gengraph(w, h, n, state->map->map, state->map->graph);
1869 * Attempt to smooth out some of the more jagged region
1870 * outlines by the judicious use of diagonally divided squares.
1873 random_state *rs = random_init(desc, strlen(desc));
1874 int *squares = snewn(wh, int);
1877 for (i = 0; i < wh; i++)
1879 shuffle(squares, wh, sizeof(*squares), rs);
1882 done_something = FALSE;
1883 for (i = 0; i < wh; i++) {
1884 int y = squares[i] / w, x = squares[i] % w;
1885 int c = state->map->map[y*w+x];
1888 if (x == 0 || x == w-1 || y == 0 || y == h-1)
1891 if (state->map->map[TE * wh + y*w+x] !=
1892 state->map->map[BE * wh + y*w+x])
1895 tc = state->map->map[BE * wh + (y-1)*w+x];
1896 bc = state->map->map[TE * wh + (y+1)*w+x];
1897 lc = state->map->map[RE * wh + y*w+(x-1)];
1898 rc = state->map->map[LE * wh + y*w+(x+1)];
1901 * If this square is adjacent on two sides to one
1902 * region and on the other two sides to the other
1903 * region, and is itself one of the two regions, we can
1904 * adjust it so that it's a diagonal.
1906 if (tc != bc && (tc == c || bc == c)) {
1907 if ((lc == tc && rc == bc) ||
1908 (lc == bc && rc == tc)) {
1909 state->map->map[TE * wh + y*w+x] = tc;
1910 state->map->map[BE * wh + y*w+x] = bc;
1911 state->map->map[LE * wh + y*w+x] = lc;
1912 state->map->map[RE * wh + y*w+x] = rc;
1913 done_something = TRUE;
1917 } while (done_something);
1923 * Analyse the map to find a canonical line segment
1924 * corresponding to each edge, and a canonical point
1925 * corresponding to each region. The former are where we'll
1926 * eventually put error markers; the latter are where we'll put
1927 * per-region flags such as numbers (when in diagnostic mode).
1930 int *bestx, *besty, *an, pass;
1931 float *ax, *ay, *best;
1933 ax = snewn(state->map->ngraph + n, float);
1934 ay = snewn(state->map->ngraph + n, float);
1935 an = snewn(state->map->ngraph + n, int);
1936 bestx = snewn(state->map->ngraph + n, int);
1937 besty = snewn(state->map->ngraph + n, int);
1938 best = snewn(state->map->ngraph + n, float);
1940 for (i = 0; i < state->map->ngraph + n; i++) {
1941 bestx[i] = besty[i] = -1;
1942 best[i] = 2*(w+h)+1;
1943 ax[i] = ay[i] = 0.0F;
1948 * We make two passes over the map, finding all the line
1949 * segments separating regions and all the suitable points
1950 * within regions. In the first pass, we compute the
1951 * _average_ x and y coordinate of all the points in a
1952 * given class; in the second pass, for each such average
1953 * point, we find the candidate closest to it and call that
1956 * Line segments are considered to have coordinates in
1957 * their centre. Thus, at least one coordinate for any line
1958 * segment is always something-and-a-half; so we store our
1959 * coordinates as twice their normal value.
1961 for (pass = 0; pass < 2; pass++) {
1964 for (y = 0; y < h; y++)
1965 for (x = 0; x < w; x++) {
1966 int ex[4], ey[4], ea[4], eb[4], en = 0;
1969 * Look for an edge to the right of this
1970 * square, an edge below it, and an edge in the
1971 * middle of it. Also look to see if the point
1972 * at the bottom right of this square is on an
1973 * edge (and isn't a place where more than two
1978 ea[en] = state->map->map[RE * wh + y*w+x];
1979 eb[en] = state->map->map[LE * wh + y*w+(x+1)];
1986 ea[en] = state->map->map[BE * wh + y*w+x];
1987 eb[en] = state->map->map[TE * wh + (y+1)*w+x];
1993 ea[en] = state->map->map[TE * wh + y*w+x];
1994 eb[en] = state->map->map[BE * wh + y*w+x];
1999 if (x+1 < w && y+1 < h) {
2000 /* bottom right corner */
2001 int oct[8], othercol, nchanges;
2002 oct[0] = state->map->map[RE * wh + y*w+x];
2003 oct[1] = state->map->map[LE * wh + y*w+(x+1)];
2004 oct[2] = state->map->map[BE * wh + y*w+(x+1)];
2005 oct[3] = state->map->map[TE * wh + (y+1)*w+(x+1)];
2006 oct[4] = state->map->map[LE * wh + (y+1)*w+(x+1)];
2007 oct[5] = state->map->map[RE * wh + (y+1)*w+x];
2008 oct[6] = state->map->map[TE * wh + (y+1)*w+x];
2009 oct[7] = state->map->map[BE * wh + y*w+x];
2013 for (i = 0; i < 8; i++) {
2014 if (oct[i] != oct[0]) {
2017 else if (othercol != oct[i])
2018 break; /* three colours at this point */
2020 if (oct[i] != oct[(i+1) & 7])
2025 * Now if there are exactly two regions at
2026 * this point (not one, and not three or
2027 * more), and only two changes around the
2028 * loop, then this is a valid place to put
2031 if (i == 8 && othercol >= 0 && nchanges == 2) {
2040 * If there's exactly _one_ region at this
2041 * point, on the other hand, it's a valid
2042 * place to put a region centre.
2045 ea[en] = eb[en] = oct[0];
2053 * Now process the points we've found, one by
2056 for (i = 0; i < en; i++) {
2057 int emin = min(ea[i], eb[i]);
2058 int emax = max(ea[i], eb[i]);
2064 graph_edge_index(state->map->graph, n,
2065 state->map->ngraph, emin,
2069 gindex = state->map->ngraph + emin;
2072 assert(gindex >= 0);
2076 * In pass 0, accumulate the values
2077 * we'll use to compute the average
2080 ax[gindex] += ex[i];
2081 ay[gindex] += ey[i];
2085 * In pass 1, work out whether this
2086 * point is closer to the average than
2087 * the last one we've seen.
2091 assert(an[gindex] > 0);
2092 dx = ex[i] - ax[gindex];
2093 dy = ey[i] - ay[gindex];
2094 d = sqrt(dx*dx + dy*dy);
2095 if (d < best[gindex]) {
2097 bestx[gindex] = ex[i];
2098 besty[gindex] = ey[i];
2105 for (i = 0; i < state->map->ngraph + n; i++)
2113 state->map->edgex = snewn(state->map->ngraph, int);
2114 state->map->edgey = snewn(state->map->ngraph, int);
2115 memcpy(state->map->edgex, bestx, state->map->ngraph * sizeof(int));
2116 memcpy(state->map->edgey, besty, state->map->ngraph * sizeof(int));
2118 state->map->regionx = snewn(n, int);
2119 state->map->regiony = snewn(n, int);
2120 memcpy(state->map->regionx, bestx + state->map->ngraph, n*sizeof(int));
2121 memcpy(state->map->regiony, besty + state->map->ngraph, n*sizeof(int));
2123 for (i = 0; i < state->map->ngraph; i++)
2124 if (state->map->edgex[i] < 0) {
2125 /* Find the other representation of this edge. */
2126 int e = state->map->graph[i];
2127 int iprime = graph_edge_index(state->map->graph, n,
2128 state->map->ngraph, e%n, e/n);
2129 assert(state->map->edgex[iprime] >= 0);
2130 state->map->edgex[i] = state->map->edgex[iprime];
2131 state->map->edgey[i] = state->map->edgey[iprime];
2145 static game_state *dup_game(game_state *state)
2147 game_state *ret = snew(game_state);
2150 ret->colouring = snewn(state->p.n, int);
2151 memcpy(ret->colouring, state->colouring, state->p.n * sizeof(int));
2152 ret->pencil = snewn(state->p.n, int);
2153 memcpy(ret->pencil, state->pencil, state->p.n * sizeof(int));
2154 ret->map = state->map;
2155 ret->map->refcount++;
2156 ret->completed = state->completed;
2157 ret->cheated = state->cheated;
2162 static void free_game(game_state *state)
2164 if (--state->map->refcount <= 0) {
2165 sfree(state->map->map);
2166 sfree(state->map->graph);
2167 sfree(state->map->immutable);
2168 sfree(state->map->edgex);
2169 sfree(state->map->edgey);
2170 sfree(state->map->regionx);
2171 sfree(state->map->regiony);
2174 sfree(state->colouring);
2178 static char *solve_game(game_state *state, game_state *currstate,
2179 char *aux, char **error)
2186 struct solver_scratch *sc;
2190 int retlen, retsize;
2192 colouring = snewn(state->map->n, int);
2193 memcpy(colouring, state->colouring, state->map->n * sizeof(int));
2195 sc = new_scratch(state->map->graph, state->map->n, state->map->ngraph);
2196 sret = map_solver(sc, state->map->graph, state->map->n,
2197 state->map->ngraph, colouring, DIFFCOUNT-1);
2203 *error = "Puzzle is inconsistent";
2205 *error = "Unable to find a unique solution for this puzzle";
2210 ret = snewn(retsize, char);
2214 for (i = 0; i < state->map->n; i++) {
2217 assert(colouring[i] >= 0);
2218 if (colouring[i] == currstate->colouring[i])
2220 assert(!state->map->immutable[i]);
2222 len = sprintf(buf, ";%d:%d", colouring[i], i);
2223 if (retlen + len >= retsize) {
2224 retsize = retlen + len + 256;
2225 ret = sresize(ret, retsize, char);
2227 strcpy(ret + retlen, buf);
2238 static char *game_text_format(game_state *state)
2244 int drag_colour; /* -1 means no drag active */
2249 static game_ui *new_ui(game_state *state)
2251 game_ui *ui = snew(game_ui);
2252 ui->dragx = ui->dragy = -1;
2253 ui->drag_colour = -2;
2254 ui->show_numbers = FALSE;
2258 static void free_ui(game_ui *ui)
2263 static char *encode_ui(game_ui *ui)
2268 static void decode_ui(game_ui *ui, char *encoding)
2272 static void game_changed_state(game_ui *ui, game_state *oldstate,
2273 game_state *newstate)
2277 struct game_drawstate {
2279 unsigned long *drawn, *todraw;
2281 int dragx, dragy, drag_visible;
2285 /* Flags in `drawn'. */
2286 #define ERR_BASE 0x00800000L
2287 #define ERR_MASK 0xFF800000L
2288 #define PENCIL_T_BASE 0x00080000L
2289 #define PENCIL_T_MASK 0x00780000L
2290 #define PENCIL_B_BASE 0x00008000L
2291 #define PENCIL_B_MASK 0x00078000L
2292 #define PENCIL_MASK 0x007F8000L
2293 #define SHOW_NUMBERS 0x00004000L
2295 #define TILESIZE (ds->tilesize)
2296 #define BORDER (TILESIZE)
2297 #define COORD(x) ( (x) * TILESIZE + BORDER )
2298 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
2300 static int region_from_coords(game_state *state, game_drawstate *ds,
2303 int w = state->p.w, h = state->p.h, wh = w*h /*, n = state->p.n */;
2304 int tx = FROMCOORD(x), ty = FROMCOORD(y);
2305 int dx = x - COORD(tx), dy = y - COORD(ty);
2308 if (tx < 0 || tx >= w || ty < 0 || ty >= h)
2309 return -1; /* border */
2311 quadrant = 2 * (dx > dy) + (TILESIZE - dx > dy);
2312 quadrant = (quadrant == 0 ? BE :
2313 quadrant == 1 ? LE :
2314 quadrant == 2 ? RE : TE);
2316 return state->map->map[quadrant * wh + ty*w+tx];
2319 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2320 int x, int y, int button)
2325 * Enable or disable numeric labels on regions.
2327 if (button == 'l' || button == 'L') {
2328 ui->show_numbers = !ui->show_numbers;
2332 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2333 int r = region_from_coords(state, ds, x, y);
2336 ui->drag_colour = state->colouring[r];
2338 ui->drag_colour = -1;
2344 if ((button == LEFT_DRAG || button == RIGHT_DRAG) &&
2345 ui->drag_colour > -2) {
2351 if ((button == LEFT_RELEASE || button == RIGHT_RELEASE) &&
2352 ui->drag_colour > -2) {
2353 int r = region_from_coords(state, ds, x, y);
2354 int c = ui->drag_colour;
2357 * Cancel the drag, whatever happens.
2359 ui->drag_colour = -2;
2360 ui->dragx = ui->dragy = -1;
2363 return ""; /* drag into border; do nothing else */
2365 if (state->map->immutable[r])
2366 return ""; /* can't change this region */
2368 if (state->colouring[r] == c)
2369 return ""; /* don't _need_ to change this region */
2371 if (button == RIGHT_RELEASE && state->colouring[r] >= 0)
2372 return ""; /* can't pencil on a coloured region */
2374 sprintf(buf, "%s%c:%d", (button == RIGHT_RELEASE ? "p" : ""),
2375 (int)(c < 0 ? 'C' : '0' + c), r);
2382 static game_state *execute_move(game_state *state, char *move)
2385 game_state *ret = dup_game(state);
2396 if ((c == 'C' || (c >= '0' && c < '0'+FOUR)) &&
2397 sscanf(move+1, ":%d%n", &k, &adv) == 1 &&
2398 k >= 0 && k < state->p.n) {
2401 if (ret->colouring[k] >= 0) {
2408 ret->pencil[k] ^= 1 << (c - '0');
2410 ret->colouring[k] = (c == 'C' ? -1 : c - '0');
2413 } else if (*move == 'S') {
2415 ret->cheated = TRUE;
2421 if (*move && *move != ';') {
2430 * Check for completion.
2432 if (!ret->completed) {
2435 for (i = 0; i < n; i++)
2436 if (ret->colouring[i] < 0) {
2442 for (i = 0; i < ret->map->ngraph; i++) {
2443 int j = ret->map->graph[i] / n;
2444 int k = ret->map->graph[i] % n;
2445 if (ret->colouring[j] == ret->colouring[k]) {
2453 ret->completed = TRUE;
2459 /* ----------------------------------------------------------------------
2463 static void game_compute_size(game_params *params, int tilesize,
2466 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2467 struct { int tilesize; } ads, *ds = &ads;
2468 ads.tilesize = tilesize;
2470 *x = params->w * TILESIZE + 2 * BORDER + 1;
2471 *y = params->h * TILESIZE + 2 * BORDER + 1;
2474 static void game_set_size(drawing *dr, game_drawstate *ds,
2475 game_params *params, int tilesize)
2477 ds->tilesize = tilesize;
2480 blitter_free(dr, ds->bl);
2481 ds->bl = blitter_new(dr, TILESIZE+3, TILESIZE+3);
2484 const float map_colours[FOUR][3] = {
2488 {0.55F, 0.45F, 0.35F},
2490 const int map_hatching[FOUR] = {
2491 HATCH_VERT, HATCH_SLASH, HATCH_HORIZ, HATCH_BACKSLASH
2494 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
2496 float *ret = snewn(3 * NCOLOURS, float);
2498 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2500 ret[COL_GRID * 3 + 0] = 0.0F;
2501 ret[COL_GRID * 3 + 1] = 0.0F;
2502 ret[COL_GRID * 3 + 2] = 0.0F;
2504 memcpy(ret + COL_0 * 3, map_colours[0], 3 * sizeof(float));
2505 memcpy(ret + COL_1 * 3, map_colours[1], 3 * sizeof(float));
2506 memcpy(ret + COL_2 * 3, map_colours[2], 3 * sizeof(float));
2507 memcpy(ret + COL_3 * 3, map_colours[3], 3 * sizeof(float));
2509 ret[COL_ERROR * 3 + 0] = 1.0F;
2510 ret[COL_ERROR * 3 + 1] = 0.0F;
2511 ret[COL_ERROR * 3 + 2] = 0.0F;
2513 ret[COL_ERRTEXT * 3 + 0] = 1.0F;
2514 ret[COL_ERRTEXT * 3 + 1] = 1.0F;
2515 ret[COL_ERRTEXT * 3 + 2] = 1.0F;
2517 *ncolours = NCOLOURS;
2521 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2523 struct game_drawstate *ds = snew(struct game_drawstate);
2527 ds->drawn = snewn(state->p.w * state->p.h, unsigned long);
2528 for (i = 0; i < state->p.w * state->p.h; i++)
2529 ds->drawn[i] = 0xFFFFL;
2530 ds->todraw = snewn(state->p.w * state->p.h, unsigned long);
2531 ds->started = FALSE;
2533 ds->drag_visible = FALSE;
2534 ds->dragx = ds->dragy = -1;
2539 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2544 blitter_free(dr, ds->bl);
2548 static void draw_error(drawing *dr, game_drawstate *ds, int x, int y)
2556 coords[0] = x - TILESIZE*2/5;
2559 coords[3] = y - TILESIZE*2/5;
2560 coords[4] = x + TILESIZE*2/5;
2563 coords[7] = y + TILESIZE*2/5;
2564 draw_polygon(dr, coords, 4, COL_ERROR, COL_GRID);
2567 * Draw an exclamation mark in the diamond. This turns out to
2568 * look unpleasantly off-centre if done via draw_text, so I do
2569 * it by hand on the basis that exclamation marks aren't that
2570 * difficult to draw...
2573 yext = TILESIZE*2/5 - (xext*2+2);
2574 draw_rect(dr, x-xext, y-yext, xext*2+1, yext*2+1 - (xext*3),
2576 draw_rect(dr, x-xext, y+yext-xext*2+1, xext*2+1, xext*2, COL_ERRTEXT);
2579 static void draw_square(drawing *dr, game_drawstate *ds,
2580 game_params *params, struct map *map,
2581 int x, int y, int v)
2583 int w = params->w, h = params->h, wh = w*h;
2584 int tv, bv, xo, yo, errs, pencil, i, j, oldj;
2587 errs = v & ERR_MASK;
2589 pencil = v & PENCIL_MASK;
2591 show_numbers = v & SHOW_NUMBERS;
2596 clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2599 * Draw the region colour.
2601 draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
2602 (tv == FOUR ? COL_BACKGROUND : COL_0 + tv));
2604 * Draw the second region colour, if this is a diagonally
2607 if (map->map[TE * wh + y*w+x] != map->map[BE * wh + y*w+x]) {
2609 coords[0] = COORD(x)-1;
2610 coords[1] = COORD(y+1)+1;
2611 if (map->map[LE * wh + y*w+x] == map->map[TE * wh + y*w+x])
2612 coords[2] = COORD(x+1)+1;
2614 coords[2] = COORD(x)-1;
2615 coords[3] = COORD(y)-1;
2616 coords[4] = COORD(x+1)+1;
2617 coords[5] = COORD(y+1)+1;
2618 draw_polygon(dr, coords, 3,
2619 (bv == FOUR ? COL_BACKGROUND : COL_0 + bv), COL_GRID);
2623 * Draw `pencil marks'. Currently we arrange these in a square
2624 * formation, which means we may be in trouble if the value of
2625 * FOUR changes later...
2628 for (yo = 0; yo < 4; yo++)
2629 for (xo = 0; xo < 4; xo++) {
2630 int te = map->map[TE * wh + y*w+x];
2633 e = (yo < xo && yo < 3-xo ? TE :
2634 yo > xo && yo > 3-xo ? BE :
2636 ee = map->map[e * wh + y*w+x];
2638 c = (yo & 1) * 2 + (xo & 1);
2640 if (!(pencil & ((ee == te ? PENCIL_T_BASE : PENCIL_B_BASE) << c)))
2644 (map->map[TE * wh + y*w+x] != map->map[LE * wh + y*w+x]))
2645 continue; /* avoid TL-BR diagonal line */
2647 (map->map[TE * wh + y*w+x] != map->map[RE * wh + y*w+x]))
2648 continue; /* avoid BL-TR diagonal line */
2650 draw_rect(dr, COORD(x) + (5*xo+1)*TILESIZE/20,
2651 COORD(y) + (5*yo+1)*TILESIZE/20,
2652 4*TILESIZE/20, 4*TILESIZE/20, COL_0 + c);
2656 * Draw the grid lines, if required.
2658 if (x <= 0 || map->map[RE*wh+y*w+(x-1)] != map->map[LE*wh+y*w+x])
2659 draw_rect(dr, COORD(x), COORD(y), 1, TILESIZE, COL_GRID);
2660 if (y <= 0 || map->map[BE*wh+(y-1)*w+x] != map->map[TE*wh+y*w+x])
2661 draw_rect(dr, COORD(x), COORD(y), TILESIZE, 1, COL_GRID);
2662 if (x <= 0 || y <= 0 ||
2663 map->map[RE*wh+(y-1)*w+(x-1)] != map->map[TE*wh+y*w+x] ||
2664 map->map[BE*wh+(y-1)*w+(x-1)] != map->map[LE*wh+y*w+x])
2665 draw_rect(dr, COORD(x), COORD(y), 1, 1, COL_GRID);
2668 * Draw error markers.
2670 for (yo = 0; yo < 3; yo++)
2671 for (xo = 0; xo < 3; xo++)
2672 if (errs & (ERR_BASE << (yo*3+xo)))
2674 (COORD(x)*2+TILESIZE*xo)/2,
2675 (COORD(y)*2+TILESIZE*yo)/2);
2678 * Draw region numbers, if desired.
2682 for (i = 0; i < 2; i++) {
2683 j = map->map[(i?BE:TE)*wh+y*w+x];
2688 xo = map->regionx[j] - 2*x;
2689 yo = map->regiony[j] - 2*y;
2690 if (xo >= 0 && xo <= 2 && yo >= 0 && yo <= 2) {
2692 sprintf(buf, "%d", j);
2693 draw_text(dr, (COORD(x)*2+TILESIZE*xo)/2,
2694 (COORD(y)*2+TILESIZE*yo)/2,
2695 FONT_VARIABLE, 3*TILESIZE/5,
2696 ALIGN_HCENTRE|ALIGN_VCENTRE,
2704 draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2707 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2708 game_state *state, int dir, game_ui *ui,
2709 float animtime, float flashtime)
2711 int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
2715 if (ds->drag_visible) {
2716 blitter_load(dr, ds->bl, ds->dragx, ds->dragy);
2717 draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3);
2718 ds->drag_visible = FALSE;
2722 * The initial contents of the window are not guaranteed and
2723 * can vary with front ends. To be on the safe side, all games
2724 * should start by drawing a big background-colour rectangle
2725 * covering the whole window.
2730 game_compute_size(&state->p, TILESIZE, &ww, &wh);
2731 draw_rect(dr, 0, 0, ww, wh, COL_BACKGROUND);
2732 draw_rect(dr, COORD(0), COORD(0), w*TILESIZE+1, h*TILESIZE+1,
2735 draw_update(dr, 0, 0, ww, wh);
2740 if (flash_type == 1)
2741 flash = (int)(flashtime * FOUR / flash_length);
2743 flash = 1 + (int)(flashtime * THREE / flash_length);
2748 * Set up the `todraw' array.
2750 for (y = 0; y < h; y++)
2751 for (x = 0; x < w; x++) {
2752 int tv = state->colouring[state->map->map[TE * wh + y*w+x]];
2753 int bv = state->colouring[state->map->map[BE * wh + y*w+x]];
2762 if (flash_type == 1) {
2767 } else if (flash_type == 2) {
2772 tv = (tv + flash) % FOUR;
2774 bv = (bv + flash) % FOUR;
2783 for (i = 0; i < FOUR; i++) {
2784 if (state->colouring[state->map->map[TE * wh + y*w+x]] < 0 &&
2785 (state->pencil[state->map->map[TE * wh + y*w+x]] & (1<<i)))
2786 v |= PENCIL_T_BASE << i;
2787 if (state->colouring[state->map->map[BE * wh + y*w+x]] < 0 &&
2788 (state->pencil[state->map->map[BE * wh + y*w+x]] & (1<<i)))
2789 v |= PENCIL_B_BASE << i;
2792 if (ui->show_numbers)
2795 ds->todraw[y*w+x] = v;
2799 * Add error markers to the `todraw' array.
2801 for (i = 0; i < state->map->ngraph; i++) {
2802 int v1 = state->map->graph[i] / n;
2803 int v2 = state->map->graph[i] % n;
2806 if (state->colouring[v1] < 0 || state->colouring[v2] < 0)
2808 if (state->colouring[v1] != state->colouring[v2])
2811 x = state->map->edgex[i];
2812 y = state->map->edgey[i];
2817 ds->todraw[y*w+x] |= ERR_BASE << (yo*3+xo);
2820 ds->todraw[y*w+(x-1)] |= ERR_BASE << (yo*3+2);
2824 ds->todraw[(y-1)*w+x] |= ERR_BASE << (2*3+xo);
2826 if (xo == 0 && yo == 0) {
2827 assert(x > 0 && y > 0);
2828 ds->todraw[(y-1)*w+(x-1)] |= ERR_BASE << (2*3+2);
2833 * Now actually draw everything.
2835 for (y = 0; y < h; y++)
2836 for (x = 0; x < w; x++) {
2837 int v = ds->todraw[y*w+x];
2838 if (ds->drawn[y*w+x] != v) {
2839 draw_square(dr, ds, &state->p, state->map, x, y, v);
2840 ds->drawn[y*w+x] = v;
2845 * Draw the dragged colour blob if any.
2847 if (ui->drag_colour > -2) {
2848 ds->dragx = ui->dragx - TILESIZE/2 - 2;
2849 ds->dragy = ui->dragy - TILESIZE/2 - 2;
2850 blitter_save(dr, ds->bl, ds->dragx, ds->dragy);
2851 draw_circle(dr, ui->dragx, ui->dragy, TILESIZE/2,
2852 (ui->drag_colour < 0 ? COL_BACKGROUND :
2853 COL_0 + ui->drag_colour), COL_GRID);
2854 draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3);
2855 ds->drag_visible = TRUE;
2859 static float game_anim_length(game_state *oldstate, game_state *newstate,
2860 int dir, game_ui *ui)
2865 static float game_flash_length(game_state *oldstate, game_state *newstate,
2866 int dir, game_ui *ui)
2868 if (!oldstate->completed && newstate->completed &&
2869 !oldstate->cheated && !newstate->cheated) {
2870 if (flash_type < 0) {
2871 char *env = getenv("MAP_ALTERNATIVE_FLASH");
2873 flash_type = atoi(env);
2876 flash_length = (flash_type == 1 ? 0.50 : 0.30);
2878 return flash_length;
2883 static int game_wants_statusbar(void)
2888 static int game_timing_state(game_state *state, game_ui *ui)
2893 static void game_print_size(game_params *params, float *x, float *y)
2898 * I'll use 4mm squares by default, I think. Simplest way to
2899 * compute this size is to compute the pixel puzzle size at a
2900 * given tile size and then scale.
2902 game_compute_size(params, 400, &pw, &ph);
2907 static void game_print(drawing *dr, game_state *state, int tilesize)
2909 int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
2910 int ink, c[FOUR], i;
2912 int *coords, ncoords, coordsize;
2914 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2915 struct { int tilesize; } ads, *ds = &ads;
2916 ads.tilesize = tilesize;
2918 ink = print_mono_colour(dr, 0);
2919 for (i = 0; i < FOUR; i++)
2920 c[i] = print_rgb_colour(dr, map_hatching[i], map_colours[i][0],
2921 map_colours[i][1], map_colours[i][2]);
2926 print_line_width(dr, TILESIZE / 16);
2929 * Draw a single filled polygon around each region.
2931 for (r = 0; r < n; r++) {
2932 int octants[8], lastdir, d1, d2, ox, oy;
2935 * Start by finding a point on the region boundary. Any
2936 * point will do. To do this, we'll search for a square
2937 * containing the region and then decide which corner of it
2941 for (y = 0; y < h; y++) {
2942 for (x = 0; x < w; x++) {
2943 if (state->map->map[wh*0+y*w+x] == r ||
2944 state->map->map[wh*1+y*w+x] == r ||
2945 state->map->map[wh*2+y*w+x] == r ||
2946 state->map->map[wh*3+y*w+x] == r)
2952 assert(y < h && x < w); /* we must have found one somewhere */
2954 * This is the first square in lexicographic order which
2955 * contains part of this region. Therefore, one of the top
2956 * two corners of the square must be what we're after. The
2957 * only case in which it isn't the top left one is if the
2958 * square is diagonally divided and the region is in the
2959 * bottom right half.
2961 if (state->map->map[wh*TE+y*w+x] != r &&
2962 state->map->map[wh*LE+y*w+x] != r)
2963 x++; /* could just as well have done y++ */
2966 * Now we have a point on the region boundary. Trace around
2967 * the region until we come back to this point,
2968 * accumulating coordinates for a polygon draw operation as
2978 * There are eight possible directions we could head in
2979 * from here. We identify them by octant numbers, and
2980 * we also use octant numbers to identify the spaces
2993 octants[0] = x<w && y>0 ? state->map->map[wh*LE+(y-1)*w+x] : -1;
2994 octants[1] = x<w && y>0 ? state->map->map[wh*BE+(y-1)*w+x] : -1;
2995 octants[2] = x<w && y<h ? state->map->map[wh*TE+y*w+x] : -1;
2996 octants[3] = x<w && y<h ? state->map->map[wh*LE+y*w+x] : -1;
2997 octants[4] = x>0 && y<h ? state->map->map[wh*RE+y*w+(x-1)] : -1;
2998 octants[5] = x>0 && y<h ? state->map->map[wh*TE+y*w+(x-1)] : -1;
2999 octants[6] = x>0 && y>0 ? state->map->map[wh*BE+(y-1)*w+(x-1)] :-1;
3000 octants[7] = x>0 && y>0 ? state->map->map[wh*RE+(y-1)*w+(x-1)] :-1;
3003 for (i = 0; i < 8; i++)
3004 if ((octants[i] == r) ^ (octants[(i+1)%8] == r)) {
3012 assert(d1 != -1 && d2 != -1);
3017 * Now we're heading in direction d1. Save the current
3020 if (ncoords + 2 > coordsize) {
3022 coords = sresize(coords, coordsize, int);
3024 coords[ncoords++] = COORD(x);
3025 coords[ncoords++] = COORD(y);
3028 * Compute the new coordinates.
3030 x += (d1 % 4 == 3 ? 0 : d1 < 4 ? +1 : -1);
3031 y += (d1 % 4 == 1 ? 0 : d1 > 1 && d1 < 5 ? +1 : -1);
3032 assert(x >= 0 && x <= w && y >= 0 && y <= h);
3035 } while (x != ox || y != oy);
3037 draw_polygon(dr, coords, ncoords/2,
3038 state->colouring[r] >= 0 ?
3039 c[state->colouring[r]] : -1, ink);
3048 const struct game thegame = {
3056 TRUE, game_configure, custom_params,
3064 FALSE, game_text_format,
3072 20, game_compute_size, game_set_size,
3075 game_free_drawstate,
3079 TRUE, TRUE, game_print_size, game_print,
3080 game_wants_statusbar,
3081 FALSE, game_timing_state,
3082 0, /* mouse_priorities */
3085 #ifdef STANDALONE_SOLVER
3089 void frontend_default_colour(frontend *fe, float *output) {}
3090 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
3091 int align, int colour, char *text) {}
3092 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour) {}
3093 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour) {}
3094 void draw_polygon(drawing *dr, int *coords, int npoints,
3095 int fillcolour, int outlinecolour) {}
3096 void draw_circle(drawing *dr, int cx, int cy, int radius,
3097 int fillcolour, int outlinecolour) {}
3098 void clip(drawing *dr, int x, int y, int w, int h) {}
3099 void unclip(drawing *dr) {}
3100 void start_draw(drawing *dr) {}
3101 void draw_update(drawing *dr, int x, int y, int w, int h) {}
3102 void end_draw(drawing *dr) {}
3103 blitter *blitter_new(drawing *dr, int w, int h) {return NULL;}
3104 void blitter_free(drawing *dr, blitter *bl) {}
3105 void blitter_save(drawing *dr, blitter *bl, int x, int y) {}
3106 void blitter_load(drawing *dr, blitter *bl, int x, int y) {}
3107 int print_mono_colour(drawing *dr, int grey) { return 0; }
3108 int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b)
3110 void print_line_width(drawing *dr, int width) {}
3112 void fatal(char *fmt, ...)
3116 fprintf(stderr, "fatal error: ");
3119 vfprintf(stderr, fmt, ap);
3122 fprintf(stderr, "\n");
3126 int main(int argc, char **argv)
3130 char *id = NULL, *desc, *err;
3132 int ret, diff, really_verbose = FALSE;
3133 struct solver_scratch *sc;
3136 while (--argc > 0) {
3138 if (!strcmp(p, "-v")) {
3139 really_verbose = TRUE;
3140 } else if (!strcmp(p, "-g")) {
3142 } else if (*p == '-') {
3143 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
3151 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
3155 desc = strchr(id, ':');
3157 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
3162 p = default_params();
3163 decode_params(p, id);
3164 err = validate_desc(p, desc);
3166 fprintf(stderr, "%s: %s\n", argv[0], err);
3169 s = new_game(NULL, p, desc);
3171 sc = new_scratch(s->map->graph, s->map->n, s->map->ngraph);
3174 * When solving an Easy puzzle, we don't want to bother the
3175 * user with Hard-level deductions. For this reason, we grade
3176 * the puzzle internally before doing anything else.
3178 ret = -1; /* placate optimiser */
3179 for (diff = 0; diff < DIFFCOUNT; diff++) {
3180 for (i = 0; i < s->map->n; i++)
3181 if (!s->map->immutable[i])
3182 s->colouring[i] = -1;
3183 ret = map_solver(sc, s->map->graph, s->map->n, s->map->ngraph,
3184 s->colouring, diff);
3189 if (diff == DIFFCOUNT) {
3191 printf("Difficulty rating: harder than Hard, or ambiguous\n");
3193 printf("Unable to find a unique solution\n");
3197 printf("Difficulty rating: impossible (no solution exists)\n");
3199 printf("Difficulty rating: %s\n", map_diffnames[diff]);
3201 verbose = really_verbose;
3202 for (i = 0; i < s->map->n; i++)
3203 if (!s->map->immutable[i])
3204 s->colouring[i] = -1;
3205 ret = map_solver(sc, s->map->graph, s->map->n, s->map->ngraph,
3206 s->colouring, diff);
3208 printf("Puzzle is inconsistent\n");
3212 for (i = 0; i < s->map->n; i++) {
3213 printf("%5d <- %c%c", i, colnames[s->colouring[i]],
3214 (col < 6 && i+1 < s->map->n ? ' ' : '\n'));