2
* map.c: Game involving four-colouring a map.
9
* - better four-colouring algorithm?
22
* In standalone solver mode, `verbose' is a variable which can be
23
* set by command-line option; in debugging mode it's simply always
26
#if defined STANDALONE_SOLVER
27
#define SOLVER_DIAGNOSTICS
29
#elif defined SOLVER_DIAGNOSTICS
34
* I don't seriously anticipate wanting to change the number of
35
* colours used in this game, but it doesn't cost much to use a
36
* #define just in case :-)
39
#define THREE (FOUR-1)
44
* Ghastly run-time configuration option, just for Gareth (again).
46
static int flash_type = -1;
47
static float flash_length;
50
* Difficulty levels. I do some macro ickery here to ensure that my
51
* enum and the various forms of my name list always match up.
57
A(RECURSE,Unreasonable,u)
58
#define ENUM(upper,title,lower) DIFF_ ## upper,
59
#define TITLE(upper,title,lower) #title,
60
#define ENCODE(upper,title,lower) #lower
61
#define CONFIG(upper,title,lower) ":" #title
62
enum { DIFFLIST(ENUM) DIFFCOUNT };
63
static char const *const map_diffnames[] = { DIFFLIST(TITLE) };
64
static char const map_diffchars[] = DIFFLIST(ENCODE);
65
#define DIFFCONFIG DIFFLIST(CONFIG)
67
enum { TE, BE, LE, RE }; /* top/bottom/left/right edges */
72
COL_0, COL_1, COL_2, COL_3,
73
COL_ERROR, COL_ERRTEXT,
88
int *edgex, *edgey; /* position of a point on each edge */
89
int *regionx, *regiony; /* position of a point in each region */
95
int *colouring, *pencil;
96
int completed, cheated;
99
static game_params *default_params(void)
101
game_params *ret = snew(game_params);
106
ret->diff = DIFF_NORMAL;
111
static const struct game_params map_presets[] = {
112
{20, 15, 30, DIFF_EASY},
113
{20, 15, 30, DIFF_NORMAL},
114
{20, 15, 30, DIFF_HARD},
115
{20, 15, 30, DIFF_RECURSE},
116
{30, 25, 75, DIFF_NORMAL},
117
{30, 25, 75, DIFF_HARD},
120
static int game_fetch_preset(int i, char **name, game_params **params)
125
if (i < 0 || i >= lenof(map_presets))
128
ret = snew(game_params);
129
*ret = map_presets[i];
131
sprintf(str, "%dx%d, %d regions, %s", ret->w, ret->h, ret->n,
132
map_diffnames[ret->diff]);
139
static void free_params(game_params *params)
144
static game_params *dup_params(game_params *params)
146
game_params *ret = snew(game_params);
147
*ret = *params; /* structure copy */
151
static void decode_params(game_params *params, char const *string)
153
char const *p = string;
156
while (*p && isdigit((unsigned char)*p)) p++;
160
while (*p && isdigit((unsigned char)*p)) p++;
162
params->h = params->w;
167
while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
169
params->n = params->w * params->h / 8;
174
for (i = 0; i < DIFFCOUNT; i++)
175
if (*p == map_diffchars[i])
181
static char *encode_params(game_params *params, int full)
185
sprintf(ret, "%dx%dn%d", params->w, params->h, params->n);
187
sprintf(ret + strlen(ret), "d%c", map_diffchars[params->diff]);
192
static config_item *game_configure(game_params *params)
197
ret = snewn(5, config_item);
199
ret[0].name = "Width";
200
ret[0].type = C_STRING;
201
sprintf(buf, "%d", params->w);
202
ret[0].sval = dupstr(buf);
205
ret[1].name = "Height";
206
ret[1].type = C_STRING;
207
sprintf(buf, "%d", params->h);
208
ret[1].sval = dupstr(buf);
211
ret[2].name = "Regions";
212
ret[2].type = C_STRING;
213
sprintf(buf, "%d", params->n);
214
ret[2].sval = dupstr(buf);
217
ret[3].name = "Difficulty";
218
ret[3].type = C_CHOICES;
219
ret[3].sval = DIFFCONFIG;
220
ret[3].ival = params->diff;
230
static game_params *custom_params(config_item *cfg)
232
game_params *ret = snew(game_params);
234
ret->w = atoi(cfg[0].sval);
235
ret->h = atoi(cfg[1].sval);
236
ret->n = atoi(cfg[2].sval);
237
ret->diff = cfg[3].ival;
242
static char *validate_params(game_params *params, int full)
244
if (params->w < 2 || params->h < 2)
245
return "Width and height must be at least two";
247
return "Must have at least five regions";
248
if (params->n > params->w * params->h)
249
return "Too many regions to fit in grid";
253
/* ----------------------------------------------------------------------
254
* Cumulative frequency table functions.
258
* Initialise a cumulative frequency table. (Hardly worth writing
259
* this function; all it does is to initialise everything in the
262
static void cf_init(int *table, int n)
266
for (i = 0; i < n; i++)
271
* Increment the count of symbol `sym' by `count'.
273
static void cf_add(int *table, int n, int sym, int count)
290
* Cumulative frequency lookup: return the total count of symbols
291
* with value less than `sym'.
293
static int cf_clookup(int *table, int n, int sym)
295
int bit, index, limit, count;
300
assert(0 < sym && sym <= n);
302
count = table[0]; /* start with the whole table size */
312
* Find the least number with its lowest set bit in this
313
* position which is greater than or equal to sym.
315
index = ((sym + bit - 1) &~ (bit * 2 - 1)) + bit;
318
count -= table[index];
329
* Single frequency lookup: return the count of symbol `sym'.
331
static int cf_slookup(int *table, int n, int sym)
335
assert(0 <= sym && sym < n);
339
for (bit = 1; sym+bit < n && !(sym & bit); bit <<= 1)
340
count -= table[sym+bit];
346
* Return the largest symbol index such that the cumulative
347
* frequency up to that symbol is less than _or equal to_ count.
349
static int cf_whichsym(int *table, int n, int count) {
352
assert(count >= 0 && count < table[0]);
363
if (count >= top - table[sym+bit])
366
top -= table[sym+bit];
375
/* ----------------------------------------------------------------------
378
* FIXME: this isn't entirely optimal at present, because it
379
* inherently prioritises growing the largest region since there
380
* are more squares adjacent to it. This acts as a destabilising
381
* influence leading to a few large regions and mostly small ones.
382
* It might be better to do it some other way.
385
#define WEIGHT_INCREASED 2 /* for increased perimeter */
386
#define WEIGHT_DECREASED 4 /* for decreased perimeter */
387
#define WEIGHT_UNCHANGED 3 /* for unchanged perimeter */
390
* Look at a square and decide which colours can be extended into
393
* If called with index < 0, it adds together one of
394
* WEIGHT_INCREASED, WEIGHT_DECREASED or WEIGHT_UNCHANGED for each
395
* colour that has a valid extension (according to the effect that
396
* it would have on the perimeter of the region being extended) and
397
* returns the overall total.
399
* If called with index >= 0, it returns one of the possible
400
* colours depending on the value of index, in such a way that the
401
* number of possible inputs which would give rise to a given
402
* return value correspond to the weight of that value.
404
static int extend_options(int w, int h, int n, int *map,
405
int x, int y, int index)
411
if (map[y*w+x] >= 0) {
413
return 0; /* can't do this square at all */
417
* Fetch the eight neighbours of this square, in order around
420
for (dy = -1; dy <= +1; dy++)
421
for (dx = -1; dx <= +1; dx++) {
422
int index = (dy < 0 ? 6-dx : dy > 0 ? 2+dx : 2*(1+dx));
423
if (x+dx >= 0 && x+dx < w && y+dy >= 0 && y+dy < h)
424
col[index] = map[(y+dy)*w+(x+dx)];
430
* Iterate over each colour that might be feasible.
432
* FIXME: this routine currently has O(n) running time. We
433
* could turn it into O(FOUR) by only bothering to iterate over
434
* the colours mentioned in the four neighbouring squares.
437
for (c = 0; c < n; c++) {
438
int count, neighbours, runs;
441
* One of the even indices of col (representing the
442
* orthogonal neighbours of this square) must be equal to
443
* c, or else this square is not adjacent to region c and
444
* obviously cannot become an extension of it at this time.
447
for (i = 0; i < 8; i += 2)
454
* Now we know this square is adjacent to region c. The
455
* next question is, would extending it cause the region to
456
* become non-simply-connected? If so, we mustn't do it.
458
* We determine this by looking around col to see if we can
459
* find more than one separate run of colour c.
462
for (i = 0; i < 8; i++)
463
if (col[i] == c && col[(i+1) & 7] != c)
471
* This square is a possibility. Determine its effect on
472
* the region's perimeter (computed from the number of
473
* orthogonal neighbours - 1 means a perimeter increase, 3
474
* a decrease, 2 no change; 4 is impossible because the
475
* region would already not be simply connected) and we're
478
assert(neighbours > 0 && neighbours < 4);
479
count = (neighbours == 1 ? WEIGHT_INCREASED :
480
neighbours == 2 ? WEIGHT_UNCHANGED : WEIGHT_DECREASED);
483
if (index >= 0 && index < count)
494
static void genmap(int w, int h, int n, int *map, random_state *rs)
501
tmp = snewn(wh, int);
504
* Clear the map, and set up `tmp' as a list of grid indices.
506
for (i = 0; i < wh; i++) {
512
* Place the region seeds by selecting n members from `tmp'.
515
for (i = 0; i < n; i++) {
516
int j = random_upto(rs, k);
522
* Re-initialise `tmp' as a cumulative frequency table. This
523
* will store the number of possible region colours we can
524
* extend into each square.
529
* Go through the grid and set up the initial cumulative
532
for (y = 0; y < h; y++)
533
for (x = 0; x < w; x++)
534
cf_add(tmp, wh, y*w+x,
535
extend_options(w, h, n, map, x, y, -1));
538
* Now repeatedly choose a square we can extend a region into,
542
int k = random_upto(rs, tmp[0]);
547
sq = cf_whichsym(tmp, wh, k);
548
k -= cf_clookup(tmp, wh, sq);
551
colour = extend_options(w, h, n, map, x, y, k);
556
* Re-scan the nine cells around the one we've just
559
for (yy = max(y-1, 0); yy < min(y+2, h); yy++)
560
for (xx = max(x-1, 0); xx < min(x+2, w); xx++) {
561
cf_add(tmp, wh, yy*w+xx,
562
-cf_slookup(tmp, wh, yy*w+xx) +
563
extend_options(w, h, n, map, xx, yy, -1));
568
* Finally, go through and normalise the region labels into
569
* order, meaning that indistinguishable maps are actually
572
for (i = 0; i < n; i++)
575
for (i = 0; i < wh; i++) {
579
map[i] = tmp[map[i]];
585
/* ----------------------------------------------------------------------
586
* Functions to handle graphs.
590
* Having got a map in a square grid, convert it into a graph
593
static int gengraph(int w, int h, int n, int *map, int *graph)
598
* Start by setting the graph up as an adjacency matrix. We'll
599
* turn it into a list later.
601
for (i = 0; i < n*n; i++)
605
* Iterate over the map looking for all adjacencies.
607
for (y = 0; y < h; y++)
608
for (x = 0; x < w; x++) {
611
if (x+1 < w && (vx = map[y*w+(x+1)]) != v)
612
graph[v*n+vx] = graph[vx*n+v] = 1;
613
if (y+1 < h && (vy = map[(y+1)*w+x]) != v)
614
graph[v*n+vy] = graph[vy*n+v] = 1;
618
* Turn the matrix into a list.
620
for (i = j = 0; i < n*n; i++)
627
static int graph_edge_index(int *graph, int n, int ngraph, int i, int j)
634
while (top - bot > 1) {
635
mid = (top + bot) / 2;
638
else if (graph[mid] < v)
646
#define graph_adjacent(graph, n, ngraph, i, j) \
647
(graph_edge_index((graph), (n), (ngraph), (i), (j)) >= 0)
649
static int graph_vertex_start(int *graph, int n, int ngraph, int i)
656
while (top - bot > 1) {
657
mid = (top + bot) / 2;
666
/* ----------------------------------------------------------------------
667
* Generate a four-colouring of a graph.
669
* FIXME: it would be nice if we could convert this recursion into
670
* pseudo-recursion using some sort of explicit stack array, for
671
* the sake of the Palm port and its limited stack.
674
static int fourcolour_recurse(int *graph, int n, int ngraph,
675
int *colouring, int *scratch, random_state *rs)
677
int nfree, nvert, start, i, j, k, c, ci;
681
* Find the smallest number of free colours in any uncoloured
682
* vertex, and count the number of such vertices.
685
nfree = FIVE; /* start off bigger than FOUR! */
687
for (i = 0; i < n; i++)
688
if (colouring[i] < 0 && scratch[i*FIVE+FOUR] <= nfree) {
689
if (nfree > scratch[i*FIVE+FOUR]) {
690
nfree = scratch[i*FIVE+FOUR];
697
* If there aren't any uncoloured vertices at all, we're done.
700
return TRUE; /* we've got a colouring! */
703
* Pick a random vertex in that set.
705
j = random_upto(rs, nvert);
706
for (i = 0; i < n; i++)
707
if (colouring[i] < 0 && scratch[i*FIVE+FOUR] == nfree)
711
start = graph_vertex_start(graph, n, ngraph, i);
714
* Loop over the possible colours for i, and recurse for each
718
for (c = 0; c < FOUR; c++)
719
if (scratch[i*FIVE+c] == 0)
721
shuffle(cs, ci, sizeof(*cs), rs);
727
* Fill in this colour.
732
* Update the scratch space to reflect a new neighbour
733
* of this colour for each neighbour of vertex i.
735
for (j = start; j < ngraph && graph[j] < n*(i+1); j++) {
737
if (scratch[k*FIVE+c] == 0)
738
scratch[k*FIVE+FOUR]--;
745
if (fourcolour_recurse(graph, n, ngraph, colouring, scratch, rs))
746
return TRUE; /* got one! */
749
* If that didn't work, clean up and try again with a
752
for (j = start; j < ngraph && graph[j] < n*(i+1); j++) {
755
if (scratch[k*FIVE+c] == 0)
756
scratch[k*FIVE+FOUR]++;
762
* If we reach here, we were unable to find a colouring at all.
763
* (This doesn't necessarily mean the Four Colour Theorem is
764
* violated; it might just mean we've gone down a dead end and
765
* need to back up and look somewhere else. It's only an FCT
766
* violation if we get all the way back up to the top level and
772
static void fourcolour(int *graph, int n, int ngraph, int *colouring,
779
* For each vertex and each colour, we store the number of
780
* neighbours that have that colour. Also, we store the number
781
* of free colours for the vertex.
783
scratch = snewn(n * FIVE, int);
784
for (i = 0; i < n * FIVE; i++)
785
scratch[i] = (i % FIVE == FOUR ? FOUR : 0);
788
* Clear the colouring to start with.
790
for (i = 0; i < n; i++)
793
i = fourcolour_recurse(graph, n, ngraph, colouring, scratch, rs);
794
assert(i); /* by the Four Colour Theorem :-) */
799
/* ----------------------------------------------------------------------
800
* Non-recursive solver.
803
struct solver_scratch {
804
unsigned char *possible; /* bitmap of colours for each region */
812
#ifdef SOLVER_DIAGNOSTICS
819
static struct solver_scratch *new_scratch(int *graph, int n, int ngraph)
821
struct solver_scratch *sc;
823
sc = snew(struct solver_scratch);
827
sc->possible = snewn(n, unsigned char);
829
sc->bfsqueue = snewn(n, int);
830
sc->bfscolour = snewn(n, int);
831
#ifdef SOLVER_DIAGNOSTICS
832
sc->bfsprev = snewn(n, int);
838
static void free_scratch(struct solver_scratch *sc)
842
sfree(sc->bfscolour);
843
#ifdef SOLVER_DIAGNOSTICS
850
* Count the bits in a word. Only needs to cope with FOUR bits.
852
static int bitcount(int word)
854
assert(FOUR <= 4); /* or this needs changing */
855
word = ((word & 0xA) >> 1) + (word & 0x5);
856
word = ((word & 0xC) >> 2) + (word & 0x3);
860
#ifdef SOLVER_DIAGNOSTICS
861
static const char colnames[FOUR] = { 'R', 'Y', 'G', 'B' };
864
static int place_colour(struct solver_scratch *sc,
865
int *colouring, int index, int colour
866
#ifdef SOLVER_DIAGNOSTICS
871
int *graph = sc->graph, n = sc->n, ngraph = sc->ngraph;
874
if (!(sc->possible[index] & (1 << colour))) {
875
#ifdef SOLVER_DIAGNOSTICS
877
printf("%*scannot place %c in region %d\n", 2*sc->depth, "",
878
colnames[colour], index);
880
return FALSE; /* can't do it */
883
sc->possible[index] = 1 << colour;
884
colouring[index] = colour;
886
#ifdef SOLVER_DIAGNOSTICS
888
printf("%*s%s %c in region %d\n", 2*sc->depth, "",
889
verb, colnames[colour], index);
893
* Rule out this colour from all the region's neighbours.
895
for (j = graph_vertex_start(graph, n, ngraph, index);
896
j < ngraph && graph[j] < n*(index+1); j++) {
897
k = graph[j] - index*n;
898
#ifdef SOLVER_DIAGNOSTICS
899
if (verbose && (sc->possible[k] & (1 << colour)))
900
printf("%*s ruling out %c in region %d\n", 2*sc->depth, "",
901
colnames[colour], k);
903
sc->possible[k] &= ~(1 << colour);
909
#ifdef SOLVER_DIAGNOSTICS
910
static char *colourset(char *buf, int set)
916
for (i = 0; i < FOUR; i++)
917
if (set & (1 << i)) {
918
p += sprintf(p, "%s%c", sep, colnames[i]);
927
* Returns 0 for impossible, 1 for success, 2 for failure to
928
* converge (i.e. puzzle is either ambiguous or just too
931
static int map_solver(struct solver_scratch *sc,
932
int *graph, int n, int ngraph, int *colouring,
937
if (sc->depth == 0) {
939
* Initialise scratch space.
941
for (i = 0; i < n; i++)
942
sc->possible[i] = (1 << FOUR) - 1;
947
for (i = 0; i < n; i++)
948
if (colouring[i] >= 0) {
949
if (!place_colour(sc, colouring, i, colouring[i]
950
#ifdef SOLVER_DIAGNOSTICS
954
#ifdef SOLVER_DIAGNOSTICS
956
printf("%*sinitial clue set is inconsistent\n",
959
return 0; /* the clues aren't even consistent! */
965
* Now repeatedly loop until we find nothing further to do.
968
int done_something = FALSE;
970
if (difficulty < DIFF_EASY)
971
break; /* can't do anything at all! */
974
* Simplest possible deduction: find a region with only one
977
for (i = 0; i < n; i++) if (colouring[i] < 0) {
978
int p = sc->possible[i];
981
#ifdef SOLVER_DIAGNOSTICS
983
printf("%*sregion %d has no possible colours left\n",
986
return 0; /* puzzle is inconsistent */
989
if ((p & (p-1)) == 0) { /* p is a power of two */
991
for (c = 0; c < FOUR; c++)
995
ret = place_colour(sc, colouring, i, c
996
#ifdef SOLVER_DIAGNOSTICS
1001
* place_colour() can only fail if colour c was not
1002
* even a _possibility_ for region i, and we're
1003
* pretty sure it was because we checked before
1004
* calling place_colour(). So we can safely assert
1005
* here rather than having to return a nice
1006
* friendly error code.
1009
done_something = TRUE;
1016
if (difficulty < DIFF_NORMAL)
1017
break; /* can't do anything harder */
1020
* Failing that, go up one level. Look for pairs of regions
1021
* which (a) both have the same pair of possible colours,
1022
* (b) are adjacent to one another, (c) are adjacent to the
1023
* same region, and (d) that region still thinks it has one
1024
* or both of those possible colours.
1026
* Simplest way to do this is by going through the graph
1027
* edge by edge, so that we start with property (b) and
1028
* then look for (a) and finally (c) and (d).
1030
for (i = 0; i < ngraph; i++) {
1031
int j1 = graph[i] / n, j2 = graph[i] % n;
1033
#ifdef SOLVER_DIAGNOSTICS
1034
int started = FALSE;
1038
continue; /* done it already, other way round */
1040
if (colouring[j1] >= 0 || colouring[j2] >= 0)
1041
continue; /* they're not undecided */
1043
if (sc->possible[j1] != sc->possible[j2])
1044
continue; /* they don't have the same possibles */
1046
v = sc->possible[j1];
1048
* See if v contains exactly two set bits.
1050
v2 = v & -v; /* find lowest set bit */
1051
v2 = v & ~v2; /* clear it */
1052
if (v2 == 0 || (v2 & (v2-1)) != 0) /* not power of 2 */
1056
* We've found regions j1 and j2 satisfying properties
1057
* (a) and (b): they have two possible colours between
1058
* them, and since they're adjacent to one another they
1059
* must use _both_ those colours between them.
1060
* Therefore, if they are both adjacent to any other
1061
* region then that region cannot be either colour.
1063
* Go through the neighbours of j1 and see if any are
1066
for (j = graph_vertex_start(graph, n, ngraph, j1);
1067
j < ngraph && graph[j] < n*(j1+1); j++) {
1068
k = graph[j] - j1*n;
1069
if (graph_adjacent(graph, n, ngraph, k, j2) &&
1070
(sc->possible[k] & v)) {
1071
#ifdef SOLVER_DIAGNOSTICS
1075
printf("%*sadjacent regions %d,%d share colours"
1076
" %s\n", 2*sc->depth, "", j1, j2,
1079
printf("%*s ruling out %s in region %d\n",2*sc->depth,
1080
"", colourset(buf, sc->possible[k] & v), k);
1083
sc->possible[k] &= ~v;
1084
done_something = TRUE;
1092
if (difficulty < DIFF_HARD)
1093
break; /* can't do anything harder */
1096
* Right; now we get creative. Now we're going to look for
1097
* `forcing chains'. A forcing chain is a path through the
1098
* graph with the following properties:
1100
* (a) Each vertex on the path has precisely two possible
1103
* (b) Each pair of vertices which are adjacent on the
1104
* path share at least one possible colour in common.
1106
* (c) Each vertex in the middle of the path shares _both_
1107
* of its colours with at least one of its neighbours
1108
* (not the same one with both neighbours).
1110
* These together imply that at least one of the possible
1111
* colour choices at one end of the path forces _all_ the
1112
* rest of the colours along the path. In order to make
1113
* real use of this, we need further properties:
1115
* (c) Ruling out some colour C from the vertex at one end
1116
* of the path forces the vertex at the other end to
1119
* (d) The two end vertices are mutually adjacent to some
1122
* (e) That third vertex currently has C as a possibility.
1124
* If we can find all of that lot, we can deduce that at
1125
* least one of the two ends of the forcing chain has
1126
* colour C, and that therefore the mutually adjacent third
1129
* To find forcing chains, we're going to start a bfs at
1130
* each suitable vertex of the graph, once for each of its
1131
* two possible colours.
1133
for (i = 0; i < n; i++) {
1136
if (colouring[i] >= 0 || bitcount(sc->possible[i]) != 2)
1139
for (c = 0; c < FOUR; c++)
1140
if (sc->possible[i] & (1 << c)) {
1141
int j, k, gi, origc, currc, head, tail;
1143
* Try a bfs from this vertex, ruling out
1146
* Within this loop, we work in colour bitmaps
1147
* rather than actual colours, because
1148
* converting back and forth is a needless
1149
* computational expense.
1154
for (j = 0; j < n; j++) {
1155
sc->bfscolour[j] = -1;
1156
#ifdef SOLVER_DIAGNOSTICS
1157
sc->bfsprev[j] = -1;
1161
sc->bfsqueue[tail++] = i;
1162
sc->bfscolour[i] = sc->possible[i] &~ origc;
1164
while (head < tail) {
1165
j = sc->bfsqueue[head++];
1166
currc = sc->bfscolour[j];
1169
* Try neighbours of j.
1171
for (gi = graph_vertex_start(graph, n, ngraph, j);
1172
gi < ngraph && graph[gi] < n*(j+1); gi++) {
1173
k = graph[gi] - j*n;
1176
* To continue with the bfs in vertex
1177
* k, we need k to be
1178
* (a) not already visited
1179
* (b) have two possible colours
1180
* (c) those colours include currc.
1183
if (sc->bfscolour[k] < 0 &&
1185
bitcount(sc->possible[k]) == 2 &&
1186
(sc->possible[k] & currc)) {
1187
sc->bfsqueue[tail++] = k;
1189
sc->possible[k] &~ currc;
1190
#ifdef SOLVER_DIAGNOSTICS
1196
* One other possibility is that k
1197
* might be the region in which we can
1198
* make a real deduction: if it's
1199
* adjacent to i, contains currc as a
1200
* possibility, and currc is equal to
1201
* the original colour we ruled out.
1203
if (currc == origc &&
1204
graph_adjacent(graph, n, ngraph, k, i) &&
1205
(sc->possible[k] & currc)) {
1206
#ifdef SOLVER_DIAGNOSTICS
1208
char buf[80], *sep = "";
1211
printf("%*sforcing chain, colour %s, ",
1213
colourset(buf, origc));
1214
for (r = j; r != -1; r = sc->bfsprev[r]) {
1215
printf("%s%d", sep, r);
1218
printf("\n%*s ruling out %s in region"
1219
" %d\n", 2*sc->depth, "",
1220
colourset(buf, origc), k);
1223
sc->possible[k] &= ~origc;
1224
done_something = TRUE;
1233
if (!done_something)
1238
* See if we've got a complete solution, and return if so.
1240
for (i = 0; i < n; i++)
1241
if (colouring[i] < 0)
1244
#ifdef SOLVER_DIAGNOSTICS
1246
printf("%*sone solution found\n", 2*sc->depth, "");
1248
return 1; /* success! */
1252
* If recursion is not permissible, we now give up.
1254
if (difficulty < DIFF_RECURSE) {
1255
#ifdef SOLVER_DIAGNOSTICS
1257
printf("%*sunable to proceed further without recursion\n",
1260
return 2; /* unable to complete */
1264
* Now we've got to do something recursive. So first hunt for a
1265
* currently-most-constrained region.
1269
struct solver_scratch *rsc;
1270
int *subcolouring, *origcolouring;
1272
int we_already_got_one;
1277
for (i = 0; i < n; i++) if (colouring[i] < 0) {
1278
int p = sc->possible[i];
1279
enum { compile_time_assertion = 1 / (FOUR <= 4) };
1282
/* Count the set bits. */
1283
c = (p & 5) + ((p >> 1) & 5);
1284
c = (c & 3) + ((c >> 2) & 3);
1285
assert(c > 1); /* or colouring[i] would be >= 0 */
1293
assert(best >= 0); /* or we'd be solved already */
1295
#ifdef SOLVER_DIAGNOSTICS
1297
printf("%*srecursing on region %d\n", 2*sc->depth, "", best);
1301
* Now iterate over the possible colours for this region.
1303
rsc = new_scratch(graph, n, ngraph);
1304
rsc->depth = sc->depth + 1;
1305
origcolouring = snewn(n, int);
1306
memcpy(origcolouring, colouring, n * sizeof(int));
1307
subcolouring = snewn(n, int);
1308
we_already_got_one = FALSE;
1311
for (i = 0; i < FOUR; i++) {
1312
if (!(sc->possible[best] & (1 << i)))
1315
memcpy(rsc->possible, sc->possible, n);
1316
memcpy(subcolouring, origcolouring, n * sizeof(int));
1318
place_colour(rsc, subcolouring, best, i
1319
#ifdef SOLVER_DIAGNOSTICS
1324
subret = map_solver(rsc, graph, n, ngraph,
1325
subcolouring, difficulty);
1327
#ifdef SOLVER_DIAGNOSTICS
1329
printf("%*sretracting %c in region %d; found %s\n",
1330
2*sc->depth, "", colnames[i], best,
1331
subret == 0 ? "no solutions" :
1332
subret == 1 ? "one solution" : "multiple solutions");
1337
* If this possibility turned up more than one valid
1338
* solution, or if it turned up one and we already had
1339
* one, we're definitely ambiguous.
1341
if (subret == 2 || (subret == 1 && we_already_got_one)) {
1347
* If this possibility turned up one valid solution and
1348
* it's the first we've seen, copy it into the output.
1351
memcpy(colouring, subcolouring, n * sizeof(int));
1352
we_already_got_one = TRUE;
1357
* Otherwise, this guess led to a contradiction, so we
1362
sfree(subcolouring);
1365
#ifdef SOLVER_DIAGNOSTICS
1366
if (verbose && sc->depth == 0) {
1367
printf("%*s%s found\n",
1369
ret == 0 ? "no solutions" :
1370
ret == 1 ? "one solution" : "multiple solutions");
1377
/* ----------------------------------------------------------------------
1378
* Game generation main function.
1381
static char *new_game_desc(game_params *params, random_state *rs,
1382
char **aux, int interactive)
1384
struct solver_scratch *sc = NULL;
1385
int *map, *graph, ngraph, *colouring, *colouring2, *regions;
1386
int i, j, w, h, n, solveret, cfreq[FOUR];
1389
#ifdef GENERATION_DIAGNOSTICS
1393
int retlen, retsize;
1402
map = snewn(wh, int);
1403
graph = snewn(n*n, int);
1404
colouring = snewn(n, int);
1405
colouring2 = snewn(n, int);
1406
regions = snewn(n, int);
1409
* This is the minimum difficulty below which we'll completely
1410
* reject a map design. Normally we set this to one below the
1411
* requested difficulty, ensuring that we have the right
1412
* result. However, for particularly dense maps or maps with
1413
* particularly few regions it might not be possible to get the
1414
* desired difficulty, so we will eventually drop this down to
1415
* -1 to indicate that any old map will do.
1417
mindiff = params->diff;
1425
genmap(w, h, n, map, rs);
1427
#ifdef GENERATION_DIAGNOSTICS
1428
for (y = 0; y < h; y++) {
1429
for (x = 0; x < w; x++) {
1434
putchar('a' + v-36);
1436
putchar('A' + v-10);
1445
* Convert the map into a graph.
1447
ngraph = gengraph(w, h, n, map, graph);
1449
#ifdef GENERATION_DIAGNOSTICS
1450
for (i = 0; i < ngraph; i++)
1451
printf("%d-%d\n", graph[i]/n, graph[i]%n);
1457
fourcolour(graph, n, ngraph, colouring, rs);
1459
#ifdef GENERATION_DIAGNOSTICS
1460
for (i = 0; i < n; i++)
1461
printf("%d: %d\n", i, colouring[i]);
1463
for (y = 0; y < h; y++) {
1464
for (x = 0; x < w; x++) {
1465
int v = colouring[map[y*w+x]];
1467
putchar('a' + v-36);
1469
putchar('A' + v-10);
1478
* Encode the solution as an aux string.
1480
if (*aux) /* in case we've come round again */
1482
retlen = retsize = 0;
1484
for (i = 0; i < n; i++) {
1487
if (colouring[i] < 0)
1490
len = sprintf(buf, "%s%d:%d", i ? ";" : "S;", colouring[i], i);
1491
if (retlen + len >= retsize) {
1492
retsize = retlen + len + 256;
1493
ret = sresize(ret, retsize, char);
1495
strcpy(ret + retlen, buf);
1501
* Remove the region colours one by one, keeping
1502
* solubility. Also ensure that there always remains at
1503
* least one region of every colour, so that the user can
1504
* drag from somewhere.
1506
for (i = 0; i < FOUR; i++)
1508
for (i = 0; i < n; i++) {
1510
cfreq[colouring[i]]++;
1512
for (i = 0; i < FOUR; i++)
1516
shuffle(regions, n, sizeof(*regions), rs);
1518
if (sc) free_scratch(sc);
1519
sc = new_scratch(graph, n, ngraph);
1521
for (i = 0; i < n; i++) {
1524
if (cfreq[colouring[j]] == 1)
1525
continue; /* can't remove last region of colour */
1527
memcpy(colouring2, colouring, n*sizeof(int));
1529
solveret = map_solver(sc, graph, n, ngraph, colouring2,
1531
assert(solveret >= 0); /* mustn't be impossible! */
1532
if (solveret == 1) {
1533
cfreq[colouring[j]]--;
1538
#ifdef GENERATION_DIAGNOSTICS
1539
for (i = 0; i < n; i++)
1540
if (colouring[i] >= 0) {
1544
putchar('a' + i-36);
1546
putchar('A' + i-10);
1549
printf(": %d\n", colouring[i]);
1554
* Finally, check that the puzzle is _at least_ as hard as
1555
* required, and indeed that it isn't already solved.
1556
* (Calling map_solver with negative difficulty ensures the
1557
* latter - if a solver which _does nothing_ can solve it,
1560
memcpy(colouring2, colouring, n*sizeof(int));
1561
if (map_solver(sc, graph, n, ngraph, colouring2,
1562
mindiff - 1) == 1) {
1564
* Drop minimum difficulty if necessary.
1566
if (mindiff > 0 && (n < 9 || n > 2*wh/3)) {
1568
mindiff = 0; /* give up and go for Easy */
1577
* Encode as a game ID. We do this by:
1579
* - first going along the horizontal edges row by row, and
1580
* then the vertical edges column by column
1581
* - encoding the lengths of runs of edges and runs of
1583
* - the decoder will reconstitute the region boundaries from
1584
* this and automatically number them the same way we did
1585
* - then we encode the initial region colours in a Slant-like
1586
* fashion (digits 0-3 interspersed with letters giving
1587
* lengths of runs of empty spaces).
1589
retlen = retsize = 0;
1596
* Start with a notional non-edge, so that there'll be an
1597
* explicit `a' to distinguish the case where we start with
1603
for (i = 0; i < w*(h-1) + (w-1)*h; i++) {
1604
int x, y, dx, dy, v;
1607
/* Horizontal edge. */
1613
/* Vertical edge. */
1614
x = (i - w*(h-1)) / h;
1615
y = (i - w*(h-1)) % h;
1620
if (retlen + 10 >= retsize) {
1621
retsize = retlen + 256;
1622
ret = sresize(ret, retsize, char);
1625
v = (map[y*w+x] != map[(y+dy)*w+(x+dx)]);
1628
ret[retlen++] = 'a'-1 + run;
1633
* 'z' is a special case in this encoding. Rather
1634
* than meaning a run of 26 and a state switch, it
1635
* means a run of 25 and _no_ state switch, because
1636
* otherwise there'd be no way to encode runs of
1640
ret[retlen++] = 'z';
1647
ret[retlen++] = 'a'-1 + run;
1648
ret[retlen++] = ',';
1651
for (i = 0; i < n; i++) {
1652
if (retlen + 10 >= retsize) {
1653
retsize = retlen + 256;
1654
ret = sresize(ret, retsize, char);
1657
if (colouring[i] < 0) {
1659
* In _this_ encoding, 'z' is a run of 26, since
1660
* there's no implicit state switch after each run.
1661
* Confusingly different, but more compact.
1664
ret[retlen++] = 'z';
1670
ret[retlen++] = 'a'-1 + run;
1671
ret[retlen++] = '0' + colouring[i];
1676
ret[retlen++] = 'a'-1 + run;
1679
assert(retlen < retsize);
1692
static char *parse_edge_list(game_params *params, char **desc, int *map)
1694
int w = params->w, h = params->h, wh = w*h, n = params->n;
1695
int i, k, pos, state;
1698
for (i = 0; i < wh; i++)
1705
* Parse the game description to get the list of edges, and
1706
* build up a disjoint set forest as we go (by identifying
1707
* pairs of squares whenever the edge list shows a non-edge).
1709
while (*p && *p != ',') {
1710
if (*p < 'a' || *p > 'z')
1711
return "Unexpected character in edge list";
1722
} else if (pos < w*(h-1)) {
1723
/* Horizontal edge. */
1728
} else if (pos < 2*wh-w-h) {
1729
/* Vertical edge. */
1730
x = (pos - w*(h-1)) / h;
1731
y = (pos - w*(h-1)) % h;
1735
return "Too much data in edge list";
1737
dsf_merge(map+wh, y*w+x, (y+dy)*w+(x+dx));
1745
assert(pos <= 2*wh-w-h);
1747
return "Too little data in edge list";
1750
* Now go through again and allocate region numbers.
1753
for (i = 0; i < wh; i++)
1755
for (i = 0; i < wh; i++) {
1756
k = dsf_canonify(map+wh, i);
1762
return "Edge list defines the wrong number of regions";
1769
static char *validate_desc(game_params *params, char *desc)
1771
int w = params->w, h = params->h, wh = w*h, n = params->n;
1776
map = snewn(2*wh, int);
1777
ret = parse_edge_list(params, &desc, map);
1783
return "Expected comma before clue list";
1784
desc++; /* eat comma */
1788
if (*desc >= '0' && *desc < '0'+FOUR)
1790
else if (*desc >= 'a' && *desc <= 'z')
1791
area += *desc - 'a' + 1;
1793
return "Unexpected character in clue list";
1797
return "Too little data in clue list";
1799
return "Too much data in clue list";
1804
static game_state *new_game(midend *me, game_params *params, char *desc)
1806
int w = params->w, h = params->h, wh = w*h, n = params->n;
1809
game_state *state = snew(game_state);
1812
state->colouring = snewn(n, int);
1813
for (i = 0; i < n; i++)
1814
state->colouring[i] = -1;
1815
state->pencil = snewn(n, int);
1816
for (i = 0; i < n; i++)
1817
state->pencil[i] = 0;
1819
state->completed = state->cheated = FALSE;
1821
state->map = snew(struct map);
1822
state->map->refcount = 1;
1823
state->map->map = snewn(wh*4, int);
1824
state->map->graph = snewn(n*n, int);
1826
state->map->immutable = snewn(n, int);
1827
for (i = 0; i < n; i++)
1828
state->map->immutable[i] = FALSE;
1834
ret = parse_edge_list(params, &p, state->map->map);
1839
* Set up the other three quadrants in `map'.
1841
for (i = wh; i < 4*wh; i++)
1842
state->map->map[i] = state->map->map[i % wh];
1848
* Now process the clue list.
1852
if (*p >= '0' && *p < '0'+FOUR) {
1853
state->colouring[pos] = *p - '0';
1854
state->map->immutable[pos] = TRUE;
1857
assert(*p >= 'a' && *p <= 'z');
1858
pos += *p - 'a' + 1;
1864
state->map->ngraph = gengraph(w, h, n, state->map->map, state->map->graph);
1867
* Attempt to smooth out some of the more jagged region
1868
* outlines by the judicious use of diagonally divided squares.
1871
random_state *rs = random_new(desc, strlen(desc));
1872
int *squares = snewn(wh, int);
1875
for (i = 0; i < wh; i++)
1877
shuffle(squares, wh, sizeof(*squares), rs);
1880
done_something = FALSE;
1881
for (i = 0; i < wh; i++) {
1882
int y = squares[i] / w, x = squares[i] % w;
1883
int c = state->map->map[y*w+x];
1886
if (x == 0 || x == w-1 || y == 0 || y == h-1)
1889
if (state->map->map[TE * wh + y*w+x] !=
1890
state->map->map[BE * wh + y*w+x])
1893
tc = state->map->map[BE * wh + (y-1)*w+x];
1894
bc = state->map->map[TE * wh + (y+1)*w+x];
1895
lc = state->map->map[RE * wh + y*w+(x-1)];
1896
rc = state->map->map[LE * wh + y*w+(x+1)];
1899
* If this square is adjacent on two sides to one
1900
* region and on the other two sides to the other
1901
* region, and is itself one of the two regions, we can
1902
* adjust it so that it's a diagonal.
1904
if (tc != bc && (tc == c || bc == c)) {
1905
if ((lc == tc && rc == bc) ||
1906
(lc == bc && rc == tc)) {
1907
state->map->map[TE * wh + y*w+x] = tc;
1908
state->map->map[BE * wh + y*w+x] = bc;
1909
state->map->map[LE * wh + y*w+x] = lc;
1910
state->map->map[RE * wh + y*w+x] = rc;
1911
done_something = TRUE;
1915
} while (done_something);
1921
* Analyse the map to find a canonical line segment
1922
* corresponding to each edge, and a canonical point
1923
* corresponding to each region. The former are where we'll
1924
* eventually put error markers; the latter are where we'll put
1925
* per-region flags such as numbers (when in diagnostic mode).
1928
int *bestx, *besty, *an, pass;
1929
float *ax, *ay, *best;
1931
ax = snewn(state->map->ngraph + n, float);
1932
ay = snewn(state->map->ngraph + n, float);
1933
an = snewn(state->map->ngraph + n, int);
1934
bestx = snewn(state->map->ngraph + n, int);
1935
besty = snewn(state->map->ngraph + n, int);
1936
best = snewn(state->map->ngraph + n, float);
1938
for (i = 0; i < state->map->ngraph + n; i++) {
1939
bestx[i] = besty[i] = -1;
1940
best[i] = 2*(w+h)+1;
1941
ax[i] = ay[i] = 0.0F;
1946
* We make two passes over the map, finding all the line
1947
* segments separating regions and all the suitable points
1948
* within regions. In the first pass, we compute the
1949
* _average_ x and y coordinate of all the points in a
1950
* given class; in the second pass, for each such average
1951
* point, we find the candidate closest to it and call that
1954
* Line segments are considered to have coordinates in
1955
* their centre. Thus, at least one coordinate for any line
1956
* segment is always something-and-a-half; so we store our
1957
* coordinates as twice their normal value.
1959
for (pass = 0; pass < 2; pass++) {
1962
for (y = 0; y < h; y++)
1963
for (x = 0; x < w; x++) {
1964
int ex[4], ey[4], ea[4], eb[4], en = 0;
1967
* Look for an edge to the right of this
1968
* square, an edge below it, and an edge in the
1969
* middle of it. Also look to see if the point
1970
* at the bottom right of this square is on an
1971
* edge (and isn't a place where more than two
1976
ea[en] = state->map->map[RE * wh + y*w+x];
1977
eb[en] = state->map->map[LE * wh + y*w+(x+1)];
1984
ea[en] = state->map->map[BE * wh + y*w+x];
1985
eb[en] = state->map->map[TE * wh + (y+1)*w+x];
1991
ea[en] = state->map->map[TE * wh + y*w+x];
1992
eb[en] = state->map->map[BE * wh + y*w+x];
1997
if (x+1 < w && y+1 < h) {
1998
/* bottom right corner */
1999
int oct[8], othercol, nchanges;
2000
oct[0] = state->map->map[RE * wh + y*w+x];
2001
oct[1] = state->map->map[LE * wh + y*w+(x+1)];
2002
oct[2] = state->map->map[BE * wh + y*w+(x+1)];
2003
oct[3] = state->map->map[TE * wh + (y+1)*w+(x+1)];
2004
oct[4] = state->map->map[LE * wh + (y+1)*w+(x+1)];
2005
oct[5] = state->map->map[RE * wh + (y+1)*w+x];
2006
oct[6] = state->map->map[TE * wh + (y+1)*w+x];
2007
oct[7] = state->map->map[BE * wh + y*w+x];
2011
for (i = 0; i < 8; i++) {
2012
if (oct[i] != oct[0]) {
2015
else if (othercol != oct[i])
2016
break; /* three colours at this point */
2018
if (oct[i] != oct[(i+1) & 7])
2023
* Now if there are exactly two regions at
2024
* this point (not one, and not three or
2025
* more), and only two changes around the
2026
* loop, then this is a valid place to put
2029
if (i == 8 && othercol >= 0 && nchanges == 2) {
2038
* If there's exactly _one_ region at this
2039
* point, on the other hand, it's a valid
2040
* place to put a region centre.
2043
ea[en] = eb[en] = oct[0];
2051
* Now process the points we've found, one by
2054
for (i = 0; i < en; i++) {
2055
int emin = min(ea[i], eb[i]);
2056
int emax = max(ea[i], eb[i]);
2062
graph_edge_index(state->map->graph, n,
2063
state->map->ngraph, emin,
2067
gindex = state->map->ngraph + emin;
2070
assert(gindex >= 0);
2074
* In pass 0, accumulate the values
2075
* we'll use to compute the average
2078
ax[gindex] += ex[i];
2079
ay[gindex] += ey[i];
2083
* In pass 1, work out whether this
2084
* point is closer to the average than
2085
* the last one we've seen.
2089
assert(an[gindex] > 0);
2090
dx = ex[i] - ax[gindex];
2091
dy = ey[i] - ay[gindex];
2092
d = sqrt(dx*dx + dy*dy);
2093
if (d < best[gindex]) {
2095
bestx[gindex] = ex[i];
2096
besty[gindex] = ey[i];
2103
for (i = 0; i < state->map->ngraph + n; i++)
2111
state->map->edgex = snewn(state->map->ngraph, int);
2112
state->map->edgey = snewn(state->map->ngraph, int);
2113
memcpy(state->map->edgex, bestx, state->map->ngraph * sizeof(int));
2114
memcpy(state->map->edgey, besty, state->map->ngraph * sizeof(int));
2116
state->map->regionx = snewn(n, int);
2117
state->map->regiony = snewn(n, int);
2118
memcpy(state->map->regionx, bestx + state->map->ngraph, n*sizeof(int));
2119
memcpy(state->map->regiony, besty + state->map->ngraph, n*sizeof(int));
2121
for (i = 0; i < state->map->ngraph; i++)
2122
if (state->map->edgex[i] < 0) {
2123
/* Find the other representation of this edge. */
2124
int e = state->map->graph[i];
2125
int iprime = graph_edge_index(state->map->graph, n,
2126
state->map->ngraph, e%n, e/n);
2127
assert(state->map->edgex[iprime] >= 0);
2128
state->map->edgex[i] = state->map->edgex[iprime];
2129
state->map->edgey[i] = state->map->edgey[iprime];
2143
static game_state *dup_game(game_state *state)
2145
game_state *ret = snew(game_state);
2148
ret->colouring = snewn(state->p.n, int);
2149
memcpy(ret->colouring, state->colouring, state->p.n * sizeof(int));
2150
ret->pencil = snewn(state->p.n, int);
2151
memcpy(ret->pencil, state->pencil, state->p.n * sizeof(int));
2152
ret->map = state->map;
2153
ret->map->refcount++;
2154
ret->completed = state->completed;
2155
ret->cheated = state->cheated;
2160
static void free_game(game_state *state)
2162
if (--state->map->refcount <= 0) {
2163
sfree(state->map->map);
2164
sfree(state->map->graph);
2165
sfree(state->map->immutable);
2166
sfree(state->map->edgex);
2167
sfree(state->map->edgey);
2168
sfree(state->map->regionx);
2169
sfree(state->map->regiony);
2172
sfree(state->pencil);
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)
2246
* - -2 means no drag currently active.
2247
* - >=0 means we're dragging a solid colour.
2248
* - -1 means we're dragging a blank space, and drag_pencil
2249
* might or might not add some pencil-mark stipples to that.
2257
static game_ui *new_ui(game_state *state)
2259
game_ui *ui = snew(game_ui);
2260
ui->dragx = ui->dragy = -1;
2261
ui->drag_colour = -2;
2262
ui->show_numbers = FALSE;
2266
static void free_ui(game_ui *ui)
2271
static char *encode_ui(game_ui *ui)
2276
static void decode_ui(game_ui *ui, char *encoding)
2280
static void game_changed_state(game_ui *ui, game_state *oldstate,
2281
game_state *newstate)
2285
struct game_drawstate {
2287
unsigned long *drawn, *todraw;
2289
int dragx, dragy, drag_visible;
2293
/* Flags in `drawn'. */
2294
#define ERR_BASE 0x00800000L
2295
#define ERR_MASK 0xFF800000L
2296
#define PENCIL_T_BASE 0x00080000L
2297
#define PENCIL_T_MASK 0x00780000L
2298
#define PENCIL_B_BASE 0x00008000L
2299
#define PENCIL_B_MASK 0x00078000L
2300
#define PENCIL_MASK 0x007F8000L
2301
#define SHOW_NUMBERS 0x00004000L
2303
#define TILESIZE (ds->tilesize)
2304
#define BORDER (TILESIZE)
2305
#define COORD(x) ( (x) * TILESIZE + BORDER )
2306
#define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
2308
static int region_from_coords(game_state *state, game_drawstate *ds,
2311
int w = state->p.w, h = state->p.h, wh = w*h /*, n = state->p.n */;
2312
int tx = FROMCOORD(x), ty = FROMCOORD(y);
2313
int dx = x - COORD(tx), dy = y - COORD(ty);
2316
if (tx < 0 || tx >= w || ty < 0 || ty >= h)
2317
return -1; /* border */
2319
quadrant = 2 * (dx > dy) + (TILESIZE - dx > dy);
2320
quadrant = (quadrant == 0 ? BE :
2321
quadrant == 1 ? LE :
2322
quadrant == 2 ? RE : TE);
2324
return state->map->map[quadrant * wh + ty*w+tx];
2327
static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2328
int x, int y, int button)
2330
char *bufp, buf[256];
2333
* Enable or disable numeric labels on regions.
2335
if (button == 'l' || button == 'L') {
2336
ui->show_numbers = !ui->show_numbers;
2340
if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2341
int r = region_from_coords(state, ds, x, y);
2344
ui->drag_colour = state->colouring[r];
2345
ui->drag_pencil = state->pencil[r];
2346
if (ui->drag_colour >= 0)
2347
ui->drag_pencil = 0; /* should be already, but double-check */
2349
ui->drag_colour = -1;
2350
ui->drag_pencil = 0;
2357
if ((button == LEFT_DRAG || button == RIGHT_DRAG) &&
2358
ui->drag_colour > -2) {
2364
if ((button == LEFT_RELEASE || button == RIGHT_RELEASE) &&
2365
ui->drag_colour > -2) {
2366
int r = region_from_coords(state, ds, x, y);
2367
int c = ui->drag_colour;
2368
int p = ui->drag_pencil;
2372
* Cancel the drag, whatever happens.
2374
ui->drag_colour = -2;
2375
ui->dragx = ui->dragy = -1;
2378
return ""; /* drag into border; do nothing else */
2380
if (state->map->immutable[r])
2381
return ""; /* can't change this region */
2383
if (state->colouring[r] == c && state->pencil[r] == p)
2384
return ""; /* don't _need_ to change this region */
2386
if (button == RIGHT_RELEASE) {
2387
if (state->colouring[r] >= 0) {
2388
/* Can't pencil on a coloured region */
2390
} else if (c >= 0) {
2391
/* Right-dragging from colour to blank toggles one pencil */
2392
p = state->pencil[r] ^ (1 << c);
2395
/* Otherwise, right-dragging from blank to blank is equivalent
2396
* to left-dragging. */
2400
oldp = state->pencil[r];
2401
if (c != state->colouring[r]) {
2402
bufp += sprintf(bufp, ";%c:%d", (int)(c < 0 ? 'C' : '0' + c), r);
2408
for (i = 0; i < FOUR; i++)
2409
if ((oldp ^ p) & (1 << i))
2410
bufp += sprintf(bufp, ";p%c:%d", (int)('0' + i), r);
2413
return dupstr(buf+1); /* ignore first semicolon */
2419
static game_state *execute_move(game_state *state, char *move)
2422
game_state *ret = dup_game(state);
2433
if ((c == 'C' || (c >= '0' && c < '0'+FOUR)) &&
2434
sscanf(move+1, ":%d%n", &k, &adv) == 1 &&
2435
k >= 0 && k < state->p.n) {
2438
if (ret->colouring[k] >= 0) {
2445
ret->pencil[k] ^= 1 << (c - '0');
2447
ret->colouring[k] = (c == 'C' ? -1 : c - '0');
2450
} else if (*move == 'S') {
2452
ret->cheated = TRUE;
2458
if (*move && *move != ';') {
2467
* Check for completion.
2469
if (!ret->completed) {
2472
for (i = 0; i < n; i++)
2473
if (ret->colouring[i] < 0) {
2479
for (i = 0; i < ret->map->ngraph; i++) {
2480
int j = ret->map->graph[i] / n;
2481
int k = ret->map->graph[i] % n;
2482
if (ret->colouring[j] == ret->colouring[k]) {
2490
ret->completed = TRUE;
2496
/* ----------------------------------------------------------------------
2500
static void game_compute_size(game_params *params, int tilesize,
2503
/* Ick: fake up `ds->tilesize' for macro expansion purposes */
2504
struct { int tilesize; } ads, *ds = &ads;
2505
ads.tilesize = tilesize;
2507
*x = params->w * TILESIZE + 2 * BORDER + 1;
2508
*y = params->h * TILESIZE + 2 * BORDER + 1;
2511
static void game_set_size(drawing *dr, game_drawstate *ds,
2512
game_params *params, int tilesize)
2514
ds->tilesize = tilesize;
2516
assert(!ds->bl); /* set_size is never called twice */
2517
ds->bl = blitter_new(dr, TILESIZE+3, TILESIZE+3);
2520
const float map_colours[FOUR][3] = {
2524
{0.55F, 0.45F, 0.35F},
2526
const int map_hatching[FOUR] = {
2527
HATCH_VERT, HATCH_SLASH, HATCH_HORIZ, HATCH_BACKSLASH
2530
static float *game_colours(frontend *fe, int *ncolours)
2532
float *ret = snewn(3 * NCOLOURS, float);
2534
frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2536
ret[COL_GRID * 3 + 0] = 0.0F;
2537
ret[COL_GRID * 3 + 1] = 0.0F;
2538
ret[COL_GRID * 3 + 2] = 0.0F;
2540
memcpy(ret + COL_0 * 3, map_colours[0], 3 * sizeof(float));
2541
memcpy(ret + COL_1 * 3, map_colours[1], 3 * sizeof(float));
2542
memcpy(ret + COL_2 * 3, map_colours[2], 3 * sizeof(float));
2543
memcpy(ret + COL_3 * 3, map_colours[3], 3 * sizeof(float));
2545
ret[COL_ERROR * 3 + 0] = 1.0F;
2546
ret[COL_ERROR * 3 + 1] = 0.0F;
2547
ret[COL_ERROR * 3 + 2] = 0.0F;
2549
ret[COL_ERRTEXT * 3 + 0] = 1.0F;
2550
ret[COL_ERRTEXT * 3 + 1] = 1.0F;
2551
ret[COL_ERRTEXT * 3 + 2] = 1.0F;
2553
*ncolours = NCOLOURS;
2557
static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2559
struct game_drawstate *ds = snew(struct game_drawstate);
2563
ds->drawn = snewn(state->p.w * state->p.h, unsigned long);
2564
for (i = 0; i < state->p.w * state->p.h; i++)
2565
ds->drawn[i] = 0xFFFFL;
2566
ds->todraw = snewn(state->p.w * state->p.h, unsigned long);
2567
ds->started = FALSE;
2569
ds->drag_visible = FALSE;
2570
ds->dragx = ds->dragy = -1;
2575
static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2580
blitter_free(dr, ds->bl);
2584
static void draw_error(drawing *dr, game_drawstate *ds, int x, int y)
2592
coords[0] = x - TILESIZE*2/5;
2595
coords[3] = y - TILESIZE*2/5;
2596
coords[4] = x + TILESIZE*2/5;
2599
coords[7] = y + TILESIZE*2/5;
2600
draw_polygon(dr, coords, 4, COL_ERROR, COL_GRID);
2603
* Draw an exclamation mark in the diamond. This turns out to
2604
* look unpleasantly off-centre if done via draw_text, so I do
2605
* it by hand on the basis that exclamation marks aren't that
2606
* difficult to draw...
2609
yext = TILESIZE*2/5 - (xext*2+2);
2610
draw_rect(dr, x-xext, y-yext, xext*2+1, yext*2+1 - (xext*3),
2612
draw_rect(dr, x-xext, y+yext-xext*2+1, xext*2+1, xext*2, COL_ERRTEXT);
2615
static void draw_square(drawing *dr, game_drawstate *ds,
2616
game_params *params, struct map *map,
2617
int x, int y, unsigned long v)
2619
int w = params->w, h = params->h, wh = w*h;
2620
int tv, bv, xo, yo, i, j, oldj;
2621
unsigned long errs, pencil, show_numbers;
2623
errs = v & ERR_MASK;
2625
pencil = v & PENCIL_MASK;
2627
show_numbers = v & SHOW_NUMBERS;
2632
clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2635
* Draw the region colour.
2637
draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
2638
(tv == FOUR ? COL_BACKGROUND : COL_0 + tv));
2640
* Draw the second region colour, if this is a diagonally
2643
if (map->map[TE * wh + y*w+x] != map->map[BE * wh + y*w+x]) {
2645
coords[0] = COORD(x)-1;
2646
coords[1] = COORD(y+1)+1;
2647
if (map->map[LE * wh + y*w+x] == map->map[TE * wh + y*w+x])
2648
coords[2] = COORD(x+1)+1;
2650
coords[2] = COORD(x)-1;
2651
coords[3] = COORD(y)-1;
2652
coords[4] = COORD(x+1)+1;
2653
coords[5] = COORD(y+1)+1;
2654
draw_polygon(dr, coords, 3,
2655
(bv == FOUR ? COL_BACKGROUND : COL_0 + bv), COL_GRID);
2659
* Draw `pencil marks'. Currently we arrange these in a square
2660
* formation, which means we may be in trouble if the value of
2661
* FOUR changes later...
2664
for (yo = 0; yo < 4; yo++)
2665
for (xo = 0; xo < 4; xo++) {
2666
int te = map->map[TE * wh + y*w+x];
2669
e = (yo < xo && yo < 3-xo ? TE :
2670
yo > xo && yo > 3-xo ? BE :
2672
ee = map->map[e * wh + y*w+x];
2674
if (xo != (yo * 2 + 1) % 5)
2678
if (!(pencil & ((ee == te ? PENCIL_T_BASE : PENCIL_B_BASE) << c)))
2682
(map->map[TE * wh + y*w+x] != map->map[LE * wh + y*w+x]))
2683
continue; /* avoid TL-BR diagonal line */
2685
(map->map[TE * wh + y*w+x] != map->map[RE * wh + y*w+x]))
2686
continue; /* avoid BL-TR diagonal line */
2688
draw_circle(dr, COORD(x) + (xo+1)*TILESIZE/5,
2689
COORD(y) + (yo+1)*TILESIZE/5,
2690
TILESIZE/7, COL_0 + c, COL_0 + c);
2694
* Draw the grid lines, if required.
2696
if (x <= 0 || map->map[RE*wh+y*w+(x-1)] != map->map[LE*wh+y*w+x])
2697
draw_rect(dr, COORD(x), COORD(y), 1, TILESIZE, COL_GRID);
2698
if (y <= 0 || map->map[BE*wh+(y-1)*w+x] != map->map[TE*wh+y*w+x])
2699
draw_rect(dr, COORD(x), COORD(y), TILESIZE, 1, COL_GRID);
2700
if (x <= 0 || y <= 0 ||
2701
map->map[RE*wh+(y-1)*w+(x-1)] != map->map[TE*wh+y*w+x] ||
2702
map->map[BE*wh+(y-1)*w+(x-1)] != map->map[LE*wh+y*w+x])
2703
draw_rect(dr, COORD(x), COORD(y), 1, 1, COL_GRID);
2706
* Draw error markers.
2708
for (yo = 0; yo < 3; yo++)
2709
for (xo = 0; xo < 3; xo++)
2710
if (errs & (ERR_BASE << (yo*3+xo)))
2712
(COORD(x)*2+TILESIZE*xo)/2,
2713
(COORD(y)*2+TILESIZE*yo)/2);
2716
* Draw region numbers, if desired.
2720
for (i = 0; i < 2; i++) {
2721
j = map->map[(i?BE:TE)*wh+y*w+x];
2726
xo = map->regionx[j] - 2*x;
2727
yo = map->regiony[j] - 2*y;
2728
if (xo >= 0 && xo <= 2 && yo >= 0 && yo <= 2) {
2730
sprintf(buf, "%d", j);
2731
draw_text(dr, (COORD(x)*2+TILESIZE*xo)/2,
2732
(COORD(y)*2+TILESIZE*yo)/2,
2733
FONT_VARIABLE, 3*TILESIZE/5,
2734
ALIGN_HCENTRE|ALIGN_VCENTRE,
2742
draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
2745
static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2746
game_state *state, int dir, game_ui *ui,
2747
float animtime, float flashtime)
2749
int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
2753
if (ds->drag_visible) {
2754
blitter_load(dr, ds->bl, ds->dragx, ds->dragy);
2755
draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3);
2756
ds->drag_visible = FALSE;
2760
* The initial contents of the window are not guaranteed and
2761
* can vary with front ends. To be on the safe side, all games
2762
* should start by drawing a big background-colour rectangle
2763
* covering the whole window.
2768
game_compute_size(&state->p, TILESIZE, &ww, &wh);
2769
draw_rect(dr, 0, 0, ww, wh, COL_BACKGROUND);
2770
draw_rect(dr, COORD(0), COORD(0), w*TILESIZE+1, h*TILESIZE+1,
2773
draw_update(dr, 0, 0, ww, wh);
2778
if (flash_type == 1)
2779
flash = (int)(flashtime * FOUR / flash_length);
2781
flash = 1 + (int)(flashtime * THREE / flash_length);
2786
* Set up the `todraw' array.
2788
for (y = 0; y < h; y++)
2789
for (x = 0; x < w; x++) {
2790
int tv = state->colouring[state->map->map[TE * wh + y*w+x]];
2791
int bv = state->colouring[state->map->map[BE * wh + y*w+x]];
2800
if (flash_type == 1) {
2805
} else if (flash_type == 2) {
2810
tv = (tv + flash) % FOUR;
2812
bv = (bv + flash) % FOUR;
2821
for (i = 0; i < FOUR; i++) {
2822
if (state->colouring[state->map->map[TE * wh + y*w+x]] < 0 &&
2823
(state->pencil[state->map->map[TE * wh + y*w+x]] & (1<<i)))
2824
v |= PENCIL_T_BASE << i;
2825
if (state->colouring[state->map->map[BE * wh + y*w+x]] < 0 &&
2826
(state->pencil[state->map->map[BE * wh + y*w+x]] & (1<<i)))
2827
v |= PENCIL_B_BASE << i;
2830
if (ui->show_numbers)
2833
ds->todraw[y*w+x] = v;
2837
* Add error markers to the `todraw' array.
2839
for (i = 0; i < state->map->ngraph; i++) {
2840
int v1 = state->map->graph[i] / n;
2841
int v2 = state->map->graph[i] % n;
2844
if (state->colouring[v1] < 0 || state->colouring[v2] < 0)
2846
if (state->colouring[v1] != state->colouring[v2])
2849
x = state->map->edgex[i];
2850
y = state->map->edgey[i];
2855
ds->todraw[y*w+x] |= ERR_BASE << (yo*3+xo);
2858
ds->todraw[y*w+(x-1)] |= ERR_BASE << (yo*3+2);
2862
ds->todraw[(y-1)*w+x] |= ERR_BASE << (2*3+xo);
2864
if (xo == 0 && yo == 0) {
2865
assert(x > 0 && y > 0);
2866
ds->todraw[(y-1)*w+(x-1)] |= ERR_BASE << (2*3+2);
2871
* Now actually draw everything.
2873
for (y = 0; y < h; y++)
2874
for (x = 0; x < w; x++) {
2875
unsigned long v = ds->todraw[y*w+x];
2876
if (ds->drawn[y*w+x] != v) {
2877
draw_square(dr, ds, &state->p, state->map, x, y, v);
2878
ds->drawn[y*w+x] = v;
2883
* Draw the dragged colour blob if any.
2885
if (ui->drag_colour > -2) {
2886
ds->dragx = ui->dragx - TILESIZE/2 - 2;
2887
ds->dragy = ui->dragy - TILESIZE/2 - 2;
2888
blitter_save(dr, ds->bl, ds->dragx, ds->dragy);
2889
draw_circle(dr, ui->dragx, ui->dragy, TILESIZE/2,
2890
(ui->drag_colour < 0 ? COL_BACKGROUND :
2891
COL_0 + ui->drag_colour), COL_GRID);
2892
for (i = 0; i < FOUR; i++)
2893
if (ui->drag_pencil & (1 << i))
2894
draw_circle(dr, ui->dragx + ((i*4+2)%10-3) * TILESIZE/10,
2895
ui->dragy + (i*2-3) * TILESIZE/10,
2896
TILESIZE/8, COL_0 + i, COL_0 + i);
2897
draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3);
2898
ds->drag_visible = TRUE;
2902
static float game_anim_length(game_state *oldstate, game_state *newstate,
2903
int dir, game_ui *ui)
2908
static float game_flash_length(game_state *oldstate, game_state *newstate,
2909
int dir, game_ui *ui)
2911
if (!oldstate->completed && newstate->completed &&
2912
!oldstate->cheated && !newstate->cheated) {
2913
if (flash_type < 0) {
2914
char *env = getenv("MAP_ALTERNATIVE_FLASH");
2916
flash_type = atoi(env);
2919
flash_length = (flash_type == 1 ? 0.50 : 0.30);
2921
return flash_length;
2926
static int game_timing_state(game_state *state, game_ui *ui)
2931
static void game_print_size(game_params *params, float *x, float *y)
2936
* I'll use 4mm squares by default, I think. Simplest way to
2937
* compute this size is to compute the pixel puzzle size at a
2938
* given tile size and then scale.
2940
game_compute_size(params, 400, &pw, &ph);
2945
static void game_print(drawing *dr, game_state *state, int tilesize)
2947
int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
2948
int ink, c[FOUR], i;
2950
int *coords, ncoords, coordsize;
2952
/* Ick: fake up `ds->tilesize' for macro expansion purposes */
2953
struct { int tilesize; } ads, *ds = &ads;
2954
/* We can't call game_set_size() here because we don't want a blitter */
2955
ads.tilesize = tilesize;
2957
ink = print_mono_colour(dr, 0);
2958
for (i = 0; i < FOUR; i++)
2959
c[i] = print_rgb_colour(dr, map_hatching[i], map_colours[i][0],
2960
map_colours[i][1], map_colours[i][2]);
2965
print_line_width(dr, TILESIZE / 16);
2968
* Draw a single filled polygon around each region.
2970
for (r = 0; r < n; r++) {
2971
int octants[8], lastdir, d1, d2, ox, oy;
2974
* Start by finding a point on the region boundary. Any
2975
* point will do. To do this, we'll search for a square
2976
* containing the region and then decide which corner of it
2980
for (y = 0; y < h; y++) {
2981
for (x = 0; x < w; x++) {
2982
if (state->map->map[wh*0+y*w+x] == r ||
2983
state->map->map[wh*1+y*w+x] == r ||
2984
state->map->map[wh*2+y*w+x] == r ||
2985
state->map->map[wh*3+y*w+x] == r)
2991
assert(y < h && x < w); /* we must have found one somewhere */
2993
* This is the first square in lexicographic order which
2994
* contains part of this region. Therefore, one of the top
2995
* two corners of the square must be what we're after. The
2996
* only case in which it isn't the top left one is if the
2997
* square is diagonally divided and the region is in the
2998
* bottom right half.
3000
if (state->map->map[wh*TE+y*w+x] != r &&
3001
state->map->map[wh*LE+y*w+x] != r)
3002
x++; /* could just as well have done y++ */
3005
* Now we have a point on the region boundary. Trace around
3006
* the region until we come back to this point,
3007
* accumulating coordinates for a polygon draw operation as
3017
* There are eight possible directions we could head in
3018
* from here. We identify them by octant numbers, and
3019
* we also use octant numbers to identify the spaces
3032
octants[0] = x<w && y>0 ? state->map->map[wh*LE+(y-1)*w+x] : -1;
3033
octants[1] = x<w && y>0 ? state->map->map[wh*BE+(y-1)*w+x] : -1;
3034
octants[2] = x<w && y<h ? state->map->map[wh*TE+y*w+x] : -1;
3035
octants[3] = x<w && y<h ? state->map->map[wh*LE+y*w+x] : -1;
3036
octants[4] = x>0 && y<h ? state->map->map[wh*RE+y*w+(x-1)] : -1;
3037
octants[5] = x>0 && y<h ? state->map->map[wh*TE+y*w+(x-1)] : -1;
3038
octants[6] = x>0 && y>0 ? state->map->map[wh*BE+(y-1)*w+(x-1)] :-1;
3039
octants[7] = x>0 && y>0 ? state->map->map[wh*RE+(y-1)*w+(x-1)] :-1;
3042
for (i = 0; i < 8; i++)
3043
if ((octants[i] == r) ^ (octants[(i+1)%8] == r)) {
3051
assert(d1 != -1 && d2 != -1);
3056
* Now we're heading in direction d1. Save the current
3059
if (ncoords + 2 > coordsize) {
3061
coords = sresize(coords, coordsize, int);
3063
coords[ncoords++] = COORD(x);
3064
coords[ncoords++] = COORD(y);
3067
* Compute the new coordinates.
3069
x += (d1 % 4 == 3 ? 0 : d1 < 4 ? +1 : -1);
3070
y += (d1 % 4 == 1 ? 0 : d1 > 1 && d1 < 5 ? +1 : -1);
3071
assert(x >= 0 && x <= w && y >= 0 && y <= h);
3074
} while (x != ox || y != oy);
3076
draw_polygon(dr, coords, ncoords/2,
3077
state->colouring[r] >= 0 ?
3078
c[state->colouring[r]] : -1, ink);
3087
const struct game thegame = {
3095
TRUE, game_configure, custom_params,
3103
FALSE, game_text_format,
3111
20, game_compute_size, game_set_size,
3114
game_free_drawstate,
3118
TRUE, TRUE, game_print_size, game_print,
3119
FALSE, /* wants_statusbar */
3120
FALSE, game_timing_state,
3124
#ifdef STANDALONE_SOLVER
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'));