2 * bridges.c: Implementation of the Nikoli game 'Bridges'.
6 * - The solver's algorithmic design is not really ideal. It makes
7 * use of the same data representation as gameplay uses, which
8 * often looks like a tempting reuse of code but isn't always a
9 * good idea. In this case, it's unpleasant that each edge of the
10 * graph ends up represented as multiple squares on a grid, with
11 * flags indicating when edges and non-edges cross; that's useful
12 * when the result can be directly translated into positions of
13 * graphics on the display, but in purely internal work it makes
14 * even simple manipulations during solving more painful than they
15 * should be, and complex ones have no choice but to modify the
16 * data structures temporarily, test things, and put them back. I
17 * envisage a complete solver rewrite along the following lines:
18 * + We have a collection of vertices (islands) and edges
19 * (potential bridge locations, i.e. pairs of horizontal or
20 * vertical islands with no other island in between).
21 * + Each edge has an associated list of edges that cross it, and
22 * hence with which it is mutually exclusive.
23 * + For each edge, we track the min and max number of bridges we
24 * currently think possible.
25 * + For each vertex, we track the number of _liberties_ it has,
26 * i.e. its clue number minus the min bridge count for each edge
28 * + We also maintain a dsf that identifies sets of vertices which
29 * are connected components of the puzzle so far, and for each
30 * equivalence class we track the total number of liberties for
31 * that component. (The dsf mechanism will also already track
32 * the size of each component, i.e. number of islands.)
33 * + So incrementing the min for an edge requires processing along
35 * - set the max for all edges crossing that one to zero
36 * - decrement the liberty count for the vertex at each end,
37 * and also for each vertex's equivalence class (NB they may
39 * - unify the two equivalence classes if they're not already,
40 * and if so, set the liberty count for the new class to be
41 * the sum of the previous two.
42 * + Decrementing the max is much easier, however.
43 * + With this data structure the really fiddly stuff in stage3()
44 * becomes more or less trivial, because it's now a quick job to
45 * find out whether an island would form an isolated subgraph if
46 * connected to a given subset of its neighbours:
47 * - identify the connected components containing the test
48 * vertex and its putative new neighbours (but be careful not
49 * to count a component more than once if two or more of the
50 * vertices involved are already in the same one)
51 * - find the sum of those components' liberty counts, and also
52 * the total number of islands involved
53 * - if the total liberty count of the connected components is
54 * exactly equal to twice the number of edges we'd be adding
55 * (of course each edge destroys two liberties, one at each
56 * end) then these components would become a subgraph with
57 * zero liberties if connected together.
58 * - therefore, if that subgraph also contains fewer than the
59 * total number of islands, it's disallowed.
60 * - As mentioned in stage3(), once we've identified such a
61 * disallowed pattern, we have two choices for what to do
62 * with it: if the candidate set of neighbours has size 1 we
63 * can reduce the max for the edge to that one neighbour,
64 * whereas if its complement has size 1 we can increase the
65 * min for the edge to the _omitted_ neighbour.
67 * - write a recursive solver?
79 /* Turn this on for hints about which lines are considered possibilities. */
82 /* --- structures for params, state, etc. --- */
86 #define PREFERRED_TILE_SIZE 24
87 #define TILE_SIZE (ds->tilesize)
88 #define BORDER (TILE_SIZE / 2)
90 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
91 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
93 #define FLASH_TIME 0.50F
98 COL_HIGHLIGHT, COL_LOWLIGHT,
99 COL_SELECTED, COL_MARK,
108 int islands, expansion; /* %age of island squares, %age chance of expansion */
109 int allowloops, difficulty;
112 /* general flags used by all structs */
113 #define G_ISLAND 0x0001
114 #define G_LINEV 0x0002 /* contains a vert. line */
115 #define G_LINEH 0x0004 /* contains a horiz. line (mutex with LINEV) */
116 #define G_LINE (G_LINEV|G_LINEH)
117 #define G_MARKV 0x0008
118 #define G_MARKH 0x0010
119 #define G_MARK (G_MARKV|G_MARKH)
120 #define G_NOLINEV 0x0020
121 #define G_NOLINEH 0x0040
122 #define G_NOLINE (G_NOLINEV|G_NOLINEH)
124 /* flags used by the error checker */
125 #define G_WARN 0x0080
127 /* flags used by the solver etc. */
128 #define G_SWEEP 0x1000
130 #define G_FLAGSH (G_LINEH|G_MARKH|G_NOLINEH)
131 #define G_FLAGSV (G_LINEV|G_MARKV|G_NOLINEV)
133 typedef unsigned int grid_type; /* change me later if we invent > 16 bits of flags. */
135 struct solver_state {
136 int *dsf, *comptspaces;
137 int *tmpdsf, *tmpcompspaces;
141 /* state->gridi is an optimisation; it stores the pointer to the island
142 * structs indexed by (x,y). It's not strictly necessary (we could use
143 * find234 instead), but Purify showed that board generation (mostly the solver)
144 * was spending 60% of its time in find234. */
146 struct surrounds { /* cloned from lightup.c */
147 struct { int x, y, dx, dy, off; } points[4];
148 int npoints, nislands;
154 struct surrounds adj;
158 int w, h, completed, solved, allowloops, maxb;
159 grid_type *grid, *scratch;
160 struct island *islands;
161 int n_islands, n_islands_alloc;
162 game_params params; /* used by the aux solver. */
163 #define N_WH_ARRAYS 5
164 char *wha, *possv, *possh, *lines, *maxv, *maxh;
165 struct island **gridi;
166 struct solver_state *solver; /* refcounted */
169 #define GRIDSZ(s) ((s)->w * (s)->h * sizeof(grid_type))
171 #define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
173 #define DINDEX(x,y) ((y)*state->w + (x))
175 #define INDEX(s,g,x,y) ((s)->g[(y)*((s)->w) + (x)])
176 #define IDX(s,g,i) ((s)->g[(i)])
177 #define GRID(s,x,y) INDEX(s,grid,x,y)
178 #define SCRATCH(s,x,y) INDEX(s,scratch,x,y)
179 #define POSSIBLES(s,dx,x,y) ((dx) ? (INDEX(s,possh,x,y)) : (INDEX(s,possv,x,y)))
180 #define MAXIMUM(s,dx,x,y) ((dx) ? (INDEX(s,maxh,x,y)) : (INDEX(s,maxv,x,y)))
182 #define GRIDCOUNT(s,x,y,f) ((GRID(s,x,y) & (f)) ? (INDEX(s,lines,x,y)) : 0)
184 #define WITHIN2(x,min,max) (((x) < (min)) ? 0 : (((x) > (max)) ? 0 : 1))
185 #define WITHIN(x,min,max) ((min) > (max) ? \
186 WITHIN2(x,max,min) : WITHIN2(x,min,max))
188 /* --- island struct and tree support functions --- */
190 #define ISLAND_ORTH(is,j,f,df) \
191 (is->f + (is->adj.points[(j)].off*is->adj.points[(j)].df))
193 #define ISLAND_ORTHX(is,j) ISLAND_ORTH(is,j,x,dx)
194 #define ISLAND_ORTHY(is,j) ISLAND_ORTH(is,j,y,dy)
196 static void fixup_islands_for_realloc(game_state *state)
200 for (i = 0; i < state->w*state->h; i++) state->gridi[i] = NULL;
201 for (i = 0; i < state->n_islands; i++) {
202 struct island *is = &state->islands[i];
204 INDEX(state, gridi, is->x, is->y) = is;
208 static int game_can_format_as_text_now(const game_params *params)
213 static char *game_text_format(const game_state *state)
220 len = (state->h) * (state->w+1) + 1;
221 ret = snewn(len, char);
224 for (y = 0; y < state->h; y++) {
225 for (x = 0; x < state->w; x++) {
226 grid = GRID(state,x,y);
227 nl = INDEX(state,lines,x,y);
228 is = INDEX(state, gridi, x, y);
230 *p++ = '0' + is->count;
231 } else if (grid & G_LINEV) {
232 *p++ = (nl > 1) ? '"' : (nl == 1) ? '|' : '!'; /* gaah, want a double-bar. */
233 } else if (grid & G_LINEH) {
234 *p++ = (nl > 1) ? '=' : (nl == 1) ? '-' : '~';
243 assert(p - ret == len);
247 static void debug_state(game_state *state)
249 char *textversion = game_text_format(state);
250 debug(("%s", textversion));
254 /*static void debug_possibles(game_state *state)
257 debug(("possh followed by possv\n"));
258 for (y = 0; y < state->h; y++) {
259 for (x = 0; x < state->w; x++) {
260 debug(("%d", POSSIBLES(state, 1, x, y)));
263 for (x = 0; x < state->w; x++) {
264 debug(("%d", POSSIBLES(state, 0, x, y)));
269 for (y = 0; y < state->h; y++) {
270 for (x = 0; x < state->w; x++) {
271 debug(("%d", MAXIMUM(state, 1, x, y)));
274 for (x = 0; x < state->w; x++) {
275 debug(("%d", MAXIMUM(state, 0, x, y)));
282 static void island_set_surrounds(struct island *is)
284 assert(INGRID(is->state,is->x,is->y));
285 is->adj.npoints = is->adj.nislands = 0;
286 #define ADDPOINT(cond,ddx,ddy) do {\
288 is->adj.points[is->adj.npoints].x = is->x+(ddx); \
289 is->adj.points[is->adj.npoints].y = is->y+(ddy); \
290 is->adj.points[is->adj.npoints].dx = (ddx); \
291 is->adj.points[is->adj.npoints].dy = (ddy); \
292 is->adj.points[is->adj.npoints].off = 0; \
295 ADDPOINT(is->x > 0, -1, 0);
296 ADDPOINT(is->x < (is->state->w-1), +1, 0);
297 ADDPOINT(is->y > 0, 0, -1);
298 ADDPOINT(is->y < (is->state->h-1), 0, +1);
301 static void island_find_orthogonal(struct island *is)
303 /* fills in the rest of the 'surrounds' structure, assuming
304 * all other islands are now in place. */
305 int i, x, y, dx, dy, off;
307 is->adj.nislands = 0;
308 for (i = 0; i < is->adj.npoints; i++) {
309 dx = is->adj.points[i].dx;
310 dy = is->adj.points[i].dy;
314 is->adj.points[i].off = 0;
315 while (INGRID(is->state, x, y)) {
316 if (GRID(is->state, x, y) & G_ISLAND) {
317 is->adj.points[i].off = off;
319 /*debug(("island (%d,%d) has orth is. %d*(%d,%d) away at (%d,%d).\n",
320 is->x, is->y, off, dx, dy,
321 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i)));*/
324 off++; x += dx; y += dy;
331 static int island_hasbridge(struct island *is, int direction)
333 int x = is->adj.points[direction].x;
334 int y = is->adj.points[direction].y;
335 grid_type gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
337 if (GRID(is->state, x, y) & gline) return 1;
341 static struct island *island_find_connection(struct island *is, int adjpt)
345 assert(adjpt < is->adj.npoints);
346 if (!is->adj.points[adjpt].off) return NULL;
347 if (!island_hasbridge(is, adjpt)) return NULL;
349 is_r = INDEX(is->state, gridi,
350 ISLAND_ORTHX(is, adjpt), ISLAND_ORTHY(is, adjpt));
356 static struct island *island_add(game_state *state, int x, int y, int count)
361 assert(!(GRID(state,x,y) & G_ISLAND));
362 GRID(state,x,y) |= G_ISLAND;
365 if (state->n_islands > state->n_islands_alloc) {
366 state->n_islands_alloc = state->n_islands * 2;
368 sresize(state->islands, state->n_islands_alloc, struct island);
371 is = &state->islands[state->n_islands-1];
373 memset(is, 0, sizeof(struct island));
378 island_set_surrounds(is);
381 fixup_islands_for_realloc(state);
383 INDEX(state, gridi, x, y) = is;
389 /* n = -1 means 'flip NOLINE flags [and set line to 0].' */
390 static void island_join(struct island *i1, struct island *i2, int n, int is_max)
392 game_state *state = i1->state;
395 assert(i1->state == i2->state);
396 assert(n >= -1 && n <= i1->state->maxb);
398 if (i1->x == i2->x) {
401 s = i1->y+1; e = i2->y-1;
403 s = i2->y+1; e = i1->y-1;
405 for (y = s; y <= e; y++) {
407 INDEX(state,maxv,x,y) = n;
410 GRID(state,x,y) ^= G_NOLINEV;
412 GRID(state,x,y) &= ~G_LINEV;
414 GRID(state,x,y) |= G_LINEV;
415 INDEX(state,lines,x,y) = n;
419 } else if (i1->y == i2->y) {
422 s = i1->x+1; e = i2->x-1;
424 s = i2->x+1; e = i1->x-1;
426 for (x = s; x <= e; x++) {
428 INDEX(state,maxh,x,y) = n;
431 GRID(state,x,y) ^= G_NOLINEH;
433 GRID(state,x,y) &= ~G_LINEH;
435 GRID(state,x,y) |= G_LINEH;
436 INDEX(state,lines,x,y) = n;
441 assert(!"island_join: islands not orthogonal.");
445 /* Counts the number of bridges currently attached to the island. */
446 static int island_countbridges(struct island *is)
450 for (i = 0; i < is->adj.npoints; i++) {
451 c += GRIDCOUNT(is->state,
452 is->adj.points[i].x, is->adj.points[i].y,
453 is->adj.points[i].dx ? G_LINEH : G_LINEV);
455 /*debug(("island count for (%d,%d) is %d.\n", is->x, is->y, c));*/
459 static int island_adjspace(struct island *is, int marks, int missing,
462 int x, y, poss, curr, dx;
463 grid_type gline, mline;
465 x = is->adj.points[direction].x;
466 y = is->adj.points[direction].y;
467 dx = is->adj.points[direction].dx;
468 gline = dx ? G_LINEH : G_LINEV;
471 mline = dx ? G_MARKH : G_MARKV;
472 if (GRID(is->state,x,y) & mline) return 0;
474 poss = POSSIBLES(is->state, dx, x, y);
475 poss = min(poss, missing);
477 curr = GRIDCOUNT(is->state, x, y, gline);
478 poss = min(poss, MAXIMUM(is->state, dx, x, y) - curr);
483 /* Counts the number of bridge spaces left around the island;
484 * expects the possibles to be up-to-date. */
485 static int island_countspaces(struct island *is, int marks)
487 int i, c = 0, missing;
489 missing = is->count - island_countbridges(is);
490 if (missing < 0) return 0;
492 for (i = 0; i < is->adj.npoints; i++) {
493 c += island_adjspace(is, marks, missing, i);
498 static int island_isadj(struct island *is, int direction)
501 grid_type gline, mline;
503 x = is->adj.points[direction].x;
504 y = is->adj.points[direction].y;
506 mline = is->adj.points[direction].dx ? G_MARKH : G_MARKV;
507 gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
508 if (GRID(is->state, x, y) & mline) {
509 /* If we're marked (i.e. the thing to attach to is complete)
510 * only count an adjacency if we're already attached. */
511 return GRIDCOUNT(is->state, x, y, gline);
513 /* If we're unmarked, count possible adjacency iff it's
514 * flagged as POSSIBLE. */
515 return POSSIBLES(is->state, is->adj.points[direction].dx, x, y);
520 /* Counts the no. of possible adjacent islands (including islands
521 * we're already connected to). */
522 static int island_countadj(struct island *is)
526 for (i = 0; i < is->adj.npoints; i++) {
527 if (island_isadj(is, i)) nadj++;
532 static void island_togglemark(struct island *is)
535 struct island *is_loop;
537 /* mark the island... */
538 GRID(is->state, is->x, is->y) ^= G_MARK;
540 /* ...remove all marks on non-island squares... */
541 for (x = 0; x < is->state->w; x++) {
542 for (y = 0; y < is->state->h; y++) {
543 if (!(GRID(is->state, x, y) & G_ISLAND))
544 GRID(is->state, x, y) &= ~G_MARK;
548 /* ...and add marks to squares around marked islands. */
549 for (i = 0; i < is->state->n_islands; i++) {
550 is_loop = &is->state->islands[i];
551 if (!(GRID(is_loop->state, is_loop->x, is_loop->y) & G_MARK))
554 for (j = 0; j < is_loop->adj.npoints; j++) {
555 /* if this direction takes us to another island, mark all
556 * squares between the two islands. */
557 if (!is_loop->adj.points[j].off) continue;
558 assert(is_loop->adj.points[j].off > 1);
559 for (o = 1; o < is_loop->adj.points[j].off; o++) {
561 is_loop->x + is_loop->adj.points[j].dx*o,
562 is_loop->y + is_loop->adj.points[j].dy*o) |=
563 is_loop->adj.points[j].dy ? G_MARKV : G_MARKH;
569 static int island_impossible(struct island *is, int strict)
571 int curr = island_countbridges(is), nspc = is->count - curr, nsurrspc;
573 struct island *is_orth;
576 debug(("island at (%d,%d) impossible because full.\n", is->x, is->y));
577 return 1; /* too many bridges */
578 } else if ((curr + island_countspaces(is, 0)) < is->count) {
579 debug(("island at (%d,%d) impossible because not enough spaces.\n", is->x, is->y));
580 return 1; /* impossible to create enough bridges */
581 } else if (strict && curr < is->count) {
582 debug(("island at (%d,%d) impossible because locked.\n", is->x, is->y));
583 return 1; /* not enough bridges and island is locked */
586 /* Count spaces in surrounding islands. */
588 for (i = 0; i < is->adj.npoints; i++) {
589 int ifree, dx = is->adj.points[i].dx;
591 if (!is->adj.points[i].off) continue;
592 poss = POSSIBLES(is->state, dx,
593 is->adj.points[i].x, is->adj.points[i].y);
594 if (poss == 0) continue;
595 is_orth = INDEX(is->state, gridi,
596 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i));
599 ifree = is_orth->count - island_countbridges(is_orth);
602 * ifree is the number of bridges unfilled in the other
603 * island, which is clearly an upper bound on the number
604 * of extra bridges this island may run to it.
606 * Another upper bound is the number of bridges unfilled
607 * on the specific line between here and there. We must
608 * take the minimum of both.
610 int bmax = MAXIMUM(is->state, dx,
611 is->adj.points[i].x, is->adj.points[i].y);
612 int bcurr = GRIDCOUNT(is->state,
613 is->adj.points[i].x, is->adj.points[i].y,
614 dx ? G_LINEH : G_LINEV);
615 assert(bcurr <= bmax);
616 nsurrspc += min(ifree, bmax - bcurr);
619 if (nsurrspc < nspc) {
620 debug(("island at (%d,%d) impossible: surr. islands %d spc, need %d.\n",
621 is->x, is->y, nsurrspc, nspc));
622 return 1; /* not enough spaces around surrounding islands to fill this one. */
628 /* --- Game parameter functions --- */
630 #define DEFAULT_PRESET 0
632 const struct game_params bridges_presets[] = {
633 { 7, 7, 2, 30, 10, 1, 0 },
634 { 7, 7, 2, 30, 10, 1, 1 },
635 { 7, 7, 2, 30, 10, 1, 2 },
636 { 10, 10, 2, 30, 10, 1, 0 },
637 { 10, 10, 2, 30, 10, 1, 1 },
638 { 10, 10, 2, 30, 10, 1, 2 },
639 { 15, 15, 2, 30, 10, 1, 0 },
640 { 15, 15, 2, 30, 10, 1, 1 },
641 { 15, 15, 2, 30, 10, 1, 2 },
644 static game_params *default_params(void)
646 game_params *ret = snew(game_params);
647 *ret = bridges_presets[DEFAULT_PRESET];
652 static int game_fetch_preset(int i, char **name, game_params **params)
657 if (i < 0 || i >= lenof(bridges_presets))
660 ret = default_params();
661 *ret = bridges_presets[i];
664 sprintf(buf, "%dx%d %s", ret->w, ret->h,
665 ret->difficulty == 0 ? "easy" :
666 ret->difficulty == 1 ? "medium" : "hard");
672 static void free_params(game_params *params)
677 static game_params *dup_params(const game_params *params)
679 game_params *ret = snew(game_params);
680 *ret = *params; /* structure copy */
684 #define EATNUM(x) do { \
685 (x) = atoi(string); \
686 while (*string && isdigit((unsigned char)*string)) string++; \
689 static void decode_params(game_params *params, char const *string)
692 params->h = params->w;
693 if (*string == 'x') {
697 if (*string == 'i') {
699 EATNUM(params->islands);
701 if (*string == 'e') {
703 EATNUM(params->expansion);
705 if (*string == 'm') {
707 EATNUM(params->maxb);
709 params->allowloops = 1;
710 if (*string == 'L') {
712 params->allowloops = 0;
714 if (*string == 'd') {
716 EATNUM(params->difficulty);
720 static char *encode_params(const game_params *params, int full)
725 sprintf(buf, "%dx%di%de%dm%d%sd%d",
726 params->w, params->h, params->islands, params->expansion,
727 params->maxb, params->allowloops ? "" : "L",
730 sprintf(buf, "%dx%dm%d%s", params->w, params->h,
731 params->maxb, params->allowloops ? "" : "L");
736 static config_item *game_configure(const game_params *params)
741 ret = snewn(8, config_item);
743 ret[0].name = "Width";
744 ret[0].type = C_STRING;
745 sprintf(buf, "%d", params->w);
746 ret[0].sval = dupstr(buf);
749 ret[1].name = "Height";
750 ret[1].type = C_STRING;
751 sprintf(buf, "%d", params->h);
752 ret[1].sval = dupstr(buf);
755 ret[2].name = "Difficulty";
756 ret[2].type = C_CHOICES;
757 ret[2].sval = ":Easy:Medium:Hard";
758 ret[2].ival = params->difficulty;
760 ret[3].name = "Allow loops";
761 ret[3].type = C_BOOLEAN;
763 ret[3].ival = params->allowloops;
765 ret[4].name = "Max. bridges per direction";
766 ret[4].type = C_CHOICES;
767 ret[4].sval = ":1:2:3:4"; /* keep up-to-date with MAX_BRIDGES */
768 ret[4].ival = params->maxb - 1;
770 ret[5].name = "%age of island squares";
771 ret[5].type = C_CHOICES;
772 ret[5].sval = ":5%:10%:15%:20%:25%:30%";
773 ret[5].ival = (params->islands / 5)-1;
775 ret[6].name = "Expansion factor (%age)";
776 ret[6].type = C_CHOICES;
777 ret[6].sval = ":0%:10%:20%:30%:40%:50%:60%:70%:80%:90%:100%";
778 ret[6].ival = params->expansion / 10;
788 static game_params *custom_params(const config_item *cfg)
790 game_params *ret = snew(game_params);
792 ret->w = atoi(cfg[0].sval);
793 ret->h = atoi(cfg[1].sval);
794 ret->difficulty = cfg[2].ival;
795 ret->allowloops = cfg[3].ival;
796 ret->maxb = cfg[4].ival + 1;
797 ret->islands = (cfg[5].ival + 1) * 5;
798 ret->expansion = cfg[6].ival * 10;
803 static char *validate_params(const game_params *params, int full)
805 if (params->w < 3 || params->h < 3)
806 return "Width and height must be at least 3";
807 if (params->maxb < 1 || params->maxb > MAX_BRIDGES)
808 return "Too many bridges.";
810 if (params->islands <= 0 || params->islands > 30)
811 return "%age of island squares must be between 1% and 30%";
812 if (params->expansion < 0 || params->expansion > 100)
813 return "Expansion factor must be between 0 and 100";
818 /* --- Game encoding and differences --- */
820 static char *encode_game(game_state *state)
823 int wh = state->w*state->h, run, x, y;
826 ret = snewn(wh + 1, char);
829 for (y = 0; y < state->h; y++) {
830 for (x = 0; x < state->w; x++) {
831 is = INDEX(state, gridi, x, y);
834 *p++ = ('a'-1) + run;
838 *p++ = '0' + is->count;
840 *p++ = 'A' + (is->count - 10);
843 *p++ = ('a'-1) + run;
851 *p++ = ('a'-1) + run;
855 assert(p - ret <= wh);
860 static char *game_state_diff(const game_state *src, const game_state *dest)
862 int movesize = 256, movelen = 0;
863 char *move = snewn(movesize, char), buf[80];
865 grid_type gline, nline;
866 struct island *is_s, *is_d, *is_orth;
868 #define APPEND do { \
869 if (movelen + len >= movesize) { \
870 movesize = movelen + len + 256; \
871 move = sresize(move, movesize, char); \
873 strcpy(move + movelen, buf); \
877 move[movelen++] = 'S';
878 move[movelen] = '\0';
880 assert(src->n_islands == dest->n_islands);
882 for (i = 0; i < src->n_islands; i++) {
883 is_s = &src->islands[i];
884 is_d = &dest->islands[i];
885 assert(is_s->x == is_d->x);
886 assert(is_s->y == is_d->y);
887 assert(is_s->adj.npoints == is_d->adj.npoints); /* more paranoia */
889 for (d = 0; d < is_s->adj.npoints; d++) {
890 if (is_s->adj.points[d].dx == -1 ||
891 is_s->adj.points[d].dy == -1) continue;
893 x = is_s->adj.points[d].x;
894 y = is_s->adj.points[d].y;
895 gline = is_s->adj.points[d].dx ? G_LINEH : G_LINEV;
896 nline = is_s->adj.points[d].dx ? G_NOLINEH : G_NOLINEV;
897 is_orth = INDEX(dest, gridi,
898 ISLAND_ORTHX(is_d, d), ISLAND_ORTHY(is_d, d));
900 if (GRIDCOUNT(src, x, y, gline) != GRIDCOUNT(dest, x, y, gline)) {
902 len = sprintf(buf, ";L%d,%d,%d,%d,%d",
903 is_s->x, is_s->y, is_orth->x, is_orth->y,
904 GRIDCOUNT(dest, x, y, gline));
907 if ((GRID(src,x,y) & nline) != (GRID(dest, x, y) & nline)) {
909 len = sprintf(buf, ";N%d,%d,%d,%d",
910 is_s->x, is_s->y, is_orth->x, is_orth->y);
914 if ((GRID(src, is_s->x, is_s->y) & G_MARK) !=
915 (GRID(dest, is_d->x, is_d->y) & G_MARK)) {
916 len = sprintf(buf, ";M%d,%d", is_s->x, is_s->y);
923 /* --- Game setup and solving utilities --- */
925 /* This function is optimised; a Quantify showed that lots of grid-generation time
926 * (>50%) was spent in here. Hence the IDX() stuff. */
928 static void map_update_possibles(game_state *state)
930 int x, y, s, e, bl, i, np, maxb, w = state->w, idx;
931 struct island *is_s = NULL, *is_f = NULL;
933 /* Run down vertical stripes [un]setting possv... */
934 for (x = 0; x < state->w; x++) {
938 maxb = state->params.maxb; /* placate optimiser */
939 /* Unset possible flags until we find an island. */
940 for (y = 0; y < state->h; y++) {
941 is_s = IDX(state, gridi, idx);
947 IDX(state, possv, idx) = 0;
950 for (; y < state->h; y++) {
951 maxb = min(maxb, IDX(state, maxv, idx));
952 is_f = IDX(state, gridi, idx);
955 np = min(maxb, is_f->count);
958 for (i = s; i <= e; i++) {
959 INDEX(state, possv, x, i) = bl ? 0 : np;
968 if (IDX(state,grid,idx) & (G_LINEH|G_NOLINEV)) bl = 1;
973 for (i = s; i <= e; i++)
974 INDEX(state, possv, x, i) = 0;
978 /* ...and now do horizontal stripes [un]setting possh. */
979 /* can we lose this clone'n'hack? */
980 for (y = 0; y < state->h; y++) {
984 maxb = state->params.maxb; /* placate optimiser */
985 for (x = 0; x < state->w; x++) {
986 is_s = IDX(state, gridi, idx);
992 IDX(state, possh, idx) = 0;
995 for (; x < state->w; x++) {
996 maxb = min(maxb, IDX(state, maxh, idx));
997 is_f = IDX(state, gridi, idx);
1000 np = min(maxb, is_f->count);
1003 for (i = s; i <= e; i++) {
1004 INDEX(state, possh, i, y) = bl ? 0 : np;
1013 if (IDX(state,grid,idx) & (G_LINEV|G_NOLINEH)) bl = 1;
1018 for (i = s; i <= e; i++)
1019 INDEX(state, possh, i, y) = 0;
1024 static void map_count(game_state *state)
1027 grid_type flag, grid;
1030 for (i = 0; i < state->n_islands; i++) {
1031 is = &state->islands[i];
1033 for (n = 0; n < is->adj.npoints; n++) {
1034 ax = is->adj.points[n].x;
1035 ay = is->adj.points[n].y;
1036 flag = (ax == is->x) ? G_LINEV : G_LINEH;
1037 grid = GRID(state,ax,ay);
1039 is->count += INDEX(state,lines,ax,ay);
1045 static void map_find_orthogonal(game_state *state)
1049 for (i = 0; i < state->n_islands; i++) {
1050 island_find_orthogonal(&state->islands[i]);
1054 static int grid_degree(game_state *state, int x, int y, int *nx_r, int *ny_r)
1056 grid_type grid = SCRATCH(state, x, y), gline = grid & G_LINE;
1058 int x1, y1, x2, y2, c = 0, i, nx, ny;
1060 nx = ny = -1; /* placate optimiser */
1061 is = INDEX(state, gridi, x, y);
1063 for (i = 0; i < is->adj.npoints; i++) {
1064 gline = is->adj.points[i].dx ? G_LINEH : G_LINEV;
1066 is->adj.points[i].x,
1067 is->adj.points[i].y) & gline) {
1068 nx = is->adj.points[i].x;
1069 ny = is->adj.points[i].y;
1074 if (gline & G_LINEV) {
1081 /* Non-island squares with edges in should never be pointing off the
1082 * edge of the grid. */
1083 assert(INGRID(state, x1, y1));
1084 assert(INGRID(state, x2, y2));
1085 if (SCRATCH(state, x1, y1) & (gline | G_ISLAND)) {
1086 nx = x1; ny = y1; c++;
1088 if (SCRATCH(state, x2, y2) & (gline | G_ISLAND)) {
1089 nx = x2; ny = y2; c++;
1093 assert(nx != -1 && ny != -1); /* paranoia */
1094 *nx_r = nx; *ny_r = ny;
1099 static int map_hasloops(game_state *state, int mark)
1101 int x, y, ox, oy, nx = 0, ny = 0, loop = 0;
1103 memcpy(state->scratch, state->grid, GRIDSZ(state));
1105 /* This algorithm is actually broken; if there are two loops connected
1106 * by bridges this will also highlight bridges. The correct algorithm
1107 * uses a dsf and a two-pass edge-detection algorithm (see check_correct
1108 * in slant.c); this is BALGE for now, especially since disallow-loops
1109 * is not the default for this puzzle. If we want to fix this later then
1110 * copy the alg in slant.c to the empty statement in map_group. */
1112 /* Remove all 1-degree edges. */
1113 for (y = 0; y < state->h; y++) {
1114 for (x = 0; x < state->w; x++) {
1116 while (grid_degree(state, ox, oy, &nx, &ny) == 1) {
1117 /*debug(("hasloops: removing 1-degree at (%d,%d).\n", ox, oy));*/
1118 SCRATCH(state, ox, oy) &= ~(G_LINE|G_ISLAND);
1123 /* Mark any remaining edges as G_WARN, if required. */
1124 for (x = 0; x < state->w; x++) {
1125 for (y = 0; y < state->h; y++) {
1126 if (GRID(state,x,y) & G_ISLAND) continue;
1128 if (SCRATCH(state, x, y) & G_LINE) {
1130 /*debug(("hasloops: marking loop square at (%d,%d).\n",
1132 GRID(state,x,y) |= G_WARN;
1135 return 1; /* short-cut as soon as we find one */
1138 GRID(state,x,y) &= ~G_WARN;
1145 static void map_group(game_state *state)
1147 int i, wh = state->w*state->h, d1, d2;
1149 int *dsf = state->solver->dsf;
1150 struct island *is, *is_join;
1152 /* Initialise dsf. */
1155 /* For each island, find connected islands right or down
1156 * and merge the dsf for the island squares as well as the
1157 * bridge squares. */
1158 for (x = 0; x < state->w; x++) {
1159 for (y = 0; y < state->h; y++) {
1160 GRID(state,x,y) &= ~(G_SWEEP|G_WARN); /* for group_full. */
1162 is = INDEX(state, gridi, x, y);
1165 for (i = 0; i < is->adj.npoints; i++) {
1166 /* only want right/down */
1167 if (is->adj.points[i].dx == -1 ||
1168 is->adj.points[i].dy == -1) continue;
1170 is_join = island_find_connection(is, i);
1171 if (!is_join) continue;
1173 d2 = DINDEX(is_join->x, is_join->y);
1174 if (dsf_canonify(dsf,d1) == dsf_canonify(dsf,d2)) {
1175 ; /* we have a loop. See comment in map_hasloops. */
1176 /* However, we still want to merge all squares joining
1177 * this side-that-makes-a-loop. */
1179 /* merge all squares between island 1 and island 2. */
1180 for (x2 = x; x2 <= is_join->x; x2++) {
1181 for (y2 = y; y2 <= is_join->y; y2++) {
1183 if (d1 != d2) dsf_merge(dsf,d1,d2);
1191 static int map_group_check(game_state *state, int canon, int warn,
1194 int *dsf = state->solver->dsf, nislands = 0;
1195 int x, y, i, allfull = 1;
1198 for (i = 0; i < state->n_islands; i++) {
1199 is = &state->islands[i];
1200 if (dsf_canonify(dsf, DINDEX(is->x,is->y)) != canon) continue;
1202 GRID(state, is->x, is->y) |= G_SWEEP;
1204 if (island_countbridges(is) != is->count)
1207 if (warn && allfull && nislands != state->n_islands) {
1208 /* we're full and this island group isn't the whole set.
1209 * Mark all squares with this dsf canon as ERR. */
1210 for (x = 0; x < state->w; x++) {
1211 for (y = 0; y < state->h; y++) {
1212 if (dsf_canonify(dsf, DINDEX(x,y)) == canon) {
1213 GRID(state,x,y) |= G_WARN;
1219 if (nislands_r) *nislands_r = nislands;
1223 static int map_group_full(game_state *state, int *ngroups_r)
1225 int *dsf = state->solver->dsf, ngroups = 0;
1229 /* NB this assumes map_group (or sth else) has cleared G_SWEEP. */
1231 for (i = 0; i < state->n_islands; i++) {
1232 is = &state->islands[i];
1233 if (GRID(state,is->x,is->y) & G_SWEEP) continue;
1236 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1241 *ngroups_r = ngroups;
1245 static int map_check(game_state *state)
1249 /* Check for loops, if necessary. */
1250 if (!state->allowloops) {
1251 if (map_hasloops(state, 1))
1255 /* Place islands into island groups and check for early
1256 * satisfied-groups. */
1257 map_group(state); /* clears WARN and SWEEP */
1258 if (map_group_full(state, &ngroups)) {
1259 if (ngroups == 1) return 1;
1264 static void map_clear(game_state *state)
1268 for (x = 0; x < state->w; x++) {
1269 for (y = 0; y < state->h; y++) {
1270 /* clear most flags; might want to be slightly more careful here. */
1271 GRID(state,x,y) &= G_ISLAND;
1276 static void solve_join(struct island *is, int direction, int n, int is_max)
1278 struct island *is_orth;
1279 int d1, d2, *dsf = is->state->solver->dsf;
1280 game_state *state = is->state; /* for DINDEX */
1282 is_orth = INDEX(is->state, gridi,
1283 ISLAND_ORTHX(is, direction),
1284 ISLAND_ORTHY(is, direction));
1286 /*debug(("...joining (%d,%d) to (%d,%d) with %d bridge(s).\n",
1287 is->x, is->y, is_orth->x, is_orth->y, n));*/
1288 island_join(is, is_orth, n, is_max);
1290 if (n > 0 && !is_max) {
1291 d1 = DINDEX(is->x, is->y);
1292 d2 = DINDEX(is_orth->x, is_orth->y);
1293 if (dsf_canonify(dsf, d1) != dsf_canonify(dsf, d2))
1294 dsf_merge(dsf, d1, d2);
1298 static int solve_fillone(struct island *is)
1302 debug(("solve_fillone for island (%d,%d).\n", is->x, is->y));
1304 for (i = 0; i < is->adj.npoints; i++) {
1305 if (island_isadj(is, i)) {
1306 if (island_hasbridge(is, i)) {
1307 /* already attached; do nothing. */;
1309 solve_join(is, i, 1, 0);
1317 static int solve_fill(struct island *is)
1319 /* for each unmarked adjacent, make sure we convert every possible bridge
1320 * to a real one, and then work out the possibles afresh. */
1321 int i, nnew, ncurr, nadded = 0, missing;
1323 debug(("solve_fill for island (%d,%d).\n", is->x, is->y));
1325 missing = is->count - island_countbridges(is);
1326 if (missing < 0) return 0;
1328 /* very like island_countspaces. */
1329 for (i = 0; i < is->adj.npoints; i++) {
1330 nnew = island_adjspace(is, 1, missing, i);
1332 ncurr = GRIDCOUNT(is->state,
1333 is->adj.points[i].x, is->adj.points[i].y,
1334 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1336 solve_join(is, i, nnew + ncurr, 0);
1343 static int solve_island_stage1(struct island *is, int *didsth_r)
1345 int bridges = island_countbridges(is);
1346 int nspaces = island_countspaces(is, 1);
1347 int nadj = island_countadj(is);
1352 /*debug(("island at (%d,%d) filled %d/%d (%d spc) nadj %d\n",
1353 is->x, is->y, bridges, is->count, nspaces, nadj));*/
1354 if (bridges > is->count) {
1355 /* We only ever add bridges when we're sure they fit, or that's
1356 * the only place they can go. If we've added bridges such that
1357 * another island has become wrong, the puzzle must not have had
1359 debug(("...island at (%d,%d) is overpopulated!\n", is->x, is->y));
1361 } else if (bridges == is->count) {
1362 /* This island is full. Make sure it's marked (and update
1363 * possibles if we did). */
1364 if (!(GRID(is->state, is->x, is->y) & G_MARK)) {
1365 debug(("...marking island (%d,%d) as full.\n", is->x, is->y));
1366 island_togglemark(is);
1369 } else if (GRID(is->state, is->x, is->y) & G_MARK) {
1370 debug(("...island (%d,%d) is marked but unfinished!\n",
1372 return 0; /* island has been marked unfinished; no solution from here. */
1374 /* This is the interesting bit; we try and fill in more information
1375 * about this island. */
1376 if (is->count == bridges + nspaces) {
1377 if (solve_fill(is) > 0) didsth = 1;
1378 } else if (is->count > ((nadj-1) * is->state->maxb)) {
1379 /* must have at least one bridge in each possible direction. */
1380 if (solve_fillone(is) > 0) didsth = 1;
1384 map_update_possibles(is->state);
1390 /* returns non-zero if a new line here would cause a loop. */
1391 static int solve_island_checkloop(struct island *is, int direction)
1393 struct island *is_orth;
1394 int *dsf = is->state->solver->dsf, d1, d2;
1395 game_state *state = is->state;
1397 if (is->state->allowloops) return 0; /* don't care anyway */
1398 if (island_hasbridge(is, direction)) return 0; /* already has a bridge */
1399 if (island_isadj(is, direction) == 0) return 0; /* no adj island */
1401 is_orth = INDEX(is->state, gridi,
1402 ISLAND_ORTHX(is,direction),
1403 ISLAND_ORTHY(is,direction));
1404 if (!is_orth) return 0;
1406 d1 = DINDEX(is->x, is->y);
1407 d2 = DINDEX(is_orth->x, is_orth->y);
1408 if (dsf_canonify(dsf, d1) == dsf_canonify(dsf, d2)) {
1409 /* two islands are connected already; don't join them. */
1415 static int solve_island_stage2(struct island *is, int *didsth_r)
1417 int added = 0, removed = 0, navail = 0, nadj, i;
1421 for (i = 0; i < is->adj.npoints; i++) {
1422 if (solve_island_checkloop(is, i)) {
1423 debug(("removing possible loop at (%d,%d) direction %d.\n",
1425 solve_join(is, i, -1, 0);
1426 map_update_possibles(is->state);
1429 navail += island_isadj(is, i);
1430 /*debug(("stage2: navail for (%d,%d) direction (%d,%d) is %d.\n",
1432 is->adj.points[i].dx, is->adj.points[i].dy,
1433 island_isadj(is, i)));*/
1437 /*debug(("island at (%d,%d) navail %d: checking...\n", is->x, is->y, navail));*/
1439 for (i = 0; i < is->adj.npoints; i++) {
1440 if (!island_hasbridge(is, i)) {
1441 nadj = island_isadj(is, i);
1442 if (nadj > 0 && (navail - nadj) < is->count) {
1443 /* we couldn't now complete the island without at
1444 * least one bridge here; put it in. */
1445 /*debug(("nadj %d, navail %d, is->count %d.\n",
1446 nadj, navail, is->count));*/
1447 debug(("island at (%d,%d) direction (%d,%d) must have 1 bridge\n",
1449 is->adj.points[i].dx, is->adj.points[i].dy));
1450 solve_join(is, i, 1, 0);
1452 /*debug_state(is->state);
1453 debug_possibles(is->state);*/
1457 if (added) map_update_possibles(is->state);
1458 if (added || removed) *didsth_r = 1;
1462 static int solve_island_subgroup(struct island *is, int direction)
1464 struct island *is_join;
1465 int nislands, *dsf = is->state->solver->dsf;
1466 game_state *state = is->state;
1468 debug(("..checking subgroups.\n"));
1470 /* if is isn't full, return 0. */
1471 if (island_countbridges(is) < is->count) {
1472 debug(("...orig island (%d,%d) not full.\n", is->x, is->y));
1476 if (direction >= 0) {
1477 is_join = INDEX(state, gridi,
1478 ISLAND_ORTHX(is, direction),
1479 ISLAND_ORTHY(is, direction));
1482 /* if is_join isn't full, return 0. */
1483 if (island_countbridges(is_join) < is_join->count) {
1484 debug(("...dest island (%d,%d) not full.\n",
1485 is_join->x, is_join->y));
1490 /* Check group membership for is->dsf; if it's full return 1. */
1491 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1493 if (nislands < state->n_islands) {
1494 /* we have a full subgroup that isn't the whole set.
1495 * This isn't allowed. */
1496 debug(("island at (%d,%d) makes full subgroup, disallowing.\n",
1500 debug(("...has finished puzzle.\n"));
1506 static int solve_island_impossible(game_state *state)
1511 /* If any islands are impossible, return 1. */
1512 for (i = 0; i < state->n_islands; i++) {
1513 is = &state->islands[i];
1514 if (island_impossible(is, 0)) {
1515 debug(("island at (%d,%d) has become impossible, disallowing.\n",
1523 /* Bear in mind that this function is really rather inefficient. */
1524 static int solve_island_stage3(struct island *is, int *didsth_r)
1526 int i, n, x, y, missing, spc, curr, maxb, didsth = 0;
1527 int wh = is->state->w * is->state->h;
1528 struct solver_state *ss = is->state->solver;
1532 missing = is->count - island_countbridges(is);
1533 if (missing <= 0) return 1;
1535 for (i = 0; i < is->adj.npoints; i++) {
1536 x = is->adj.points[i].x;
1537 y = is->adj.points[i].y;
1538 spc = island_adjspace(is, 1, missing, i);
1539 if (spc == 0) continue;
1541 curr = GRIDCOUNT(is->state, x, y,
1542 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1543 debug(("island at (%d,%d) s3, trying %d - %d bridges.\n",
1544 is->x, is->y, curr+1, curr+spc));
1546 /* Now we know that this island could have more bridges,
1547 * to bring the total from curr+1 to curr+spc. */
1549 /* We have to squirrel the dsf away and restore it afterwards;
1550 * it is additive only, and can't be removed from. */
1551 memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int));
1552 for (n = curr+1; n <= curr+spc; n++) {
1553 solve_join(is, i, n, 0);
1554 map_update_possibles(is->state);
1556 if (solve_island_subgroup(is, i) ||
1557 solve_island_impossible(is->state)) {
1559 debug(("island at (%d,%d) d(%d,%d) new max of %d bridges:\n",
1561 is->adj.points[i].dx, is->adj.points[i].dy,
1566 solve_join(is, i, curr, 0); /* put back to before. */
1567 memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int));
1570 /*debug_state(is->state);*/
1572 debug(("...adding NOLINE.\n"));
1573 solve_join(is, i, -1, 0); /* we can't have any bridges here. */
1575 debug(("...setting maximum\n"));
1576 solve_join(is, i, maxb, 1);
1580 map_update_possibles(is->state);
1583 for (i = 0; i < is->adj.npoints; i++) {
1585 * Now check to see if any currently empty direction must have
1586 * at least one bridge in order to avoid forming an isolated
1587 * subgraph. This differs from the check above in that it
1588 * considers multiple target islands. For example:
1595 * The example on the left can be handled by the above loop:
1596 * it will observe that connecting the central 2 twice to the
1597 * left would form an isolated subgraph, and hence it will
1598 * restrict that 2 to at most one bridge in that direction.
1599 * But the example on the right won't be handled by that loop,
1600 * because the deduction requires us to imagine connecting the
1601 * 3 to _both_ the 1 and 2 at once to form an isolated
1604 * This pass is necessary _as well_ as the above one, because
1605 * neither can do the other's job. In the left one,
1606 * restricting the direction which _would_ cause trouble can
1607 * be done even if it's not yet clear which of the remaining
1608 * directions has to have a compensatory bridge; whereas the
1609 * pass below that can handle the right-hand example does need
1610 * to know what direction to point the necessary bridge in.
1612 * Neither pass can handle the most general case, in which we
1613 * observe that an arbitrary subset of an island's neighbours
1614 * would form an isolated subgraph with it if it connected
1615 * maximally to them, and hence that at least one bridge must
1616 * point to some neighbour outside that subset but we don't
1617 * know which neighbour. To handle that, we'd have to have a
1618 * richer data format for the solver, which could cope with
1619 * recording the idea that at least one of two edges must have
1626 spc = island_adjspace(is, 1, missing, i);
1627 if (spc == 0) continue;
1629 for (j = 0; j < is->adj.npoints; j++)
1630 before[j] = GRIDCOUNT(is->state,
1631 is->adj.points[j].x,
1632 is->adj.points[j].y,
1633 is->adj.points[j].dx ? G_LINEH : G_LINEV);
1634 if (before[i] != 0) continue; /* this idea is pointless otherwise */
1636 memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int));
1638 for (j = 0; j < is->adj.npoints; j++) {
1639 spc = island_adjspace(is, 1, missing, j);
1640 if (spc == 0) continue;
1641 if (j == i) continue;
1642 solve_join(is, j, before[j] + spc, 0);
1644 map_update_possibles(is->state);
1646 if (solve_island_subgroup(is, -1))
1649 for (j = 0; j < is->adj.npoints; j++)
1650 solve_join(is, j, before[j], 0);
1651 memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int));
1654 debug(("island at (%d,%d) must connect in direction (%d,%d) to"
1655 " avoid full subgroup.\n",
1656 is->x, is->y, is->adj.points[i].dx, is->adj.points[i].dy));
1657 solve_join(is, i, 1, 0);
1661 map_update_possibles(is->state);
1664 if (didsth) *didsth_r = didsth;
1668 #define CONTINUE_IF_FULL do { \
1669 if (GRID(state, is->x, is->y) & G_MARK) { \
1670 /* island full, don't try fixing it */ \
1674 static int solve_sub(game_state *state, int difficulty, int depth)
1682 /* First island iteration: things we can work out by looking at
1683 * properties of the island as a whole. */
1684 for (i = 0; i < state->n_islands; i++) {
1685 is = &state->islands[i];
1686 if (!solve_island_stage1(is, &didsth)) return 0;
1688 if (didsth) continue;
1689 else if (difficulty < 1) break;
1691 /* Second island iteration: thing we can work out by looking at
1692 * properties of individual island connections. */
1693 for (i = 0; i < state->n_islands; i++) {
1694 is = &state->islands[i];
1696 if (!solve_island_stage2(is, &didsth)) return 0;
1698 if (didsth) continue;
1699 else if (difficulty < 2) break;
1701 /* Third island iteration: things we can only work out by looking
1702 * at groups of islands. */
1703 for (i = 0; i < state->n_islands; i++) {
1704 is = &state->islands[i];
1705 if (!solve_island_stage3(is, &didsth)) return 0;
1707 if (didsth) continue;
1708 else if (difficulty < 3) break;
1710 /* If we can be bothered, write a recursive solver to finish here. */
1713 if (map_check(state)) return 1; /* solved it */
1717 static void solve_for_hint(game_state *state)
1720 solve_sub(state, 10, 0);
1723 static int solve_from_scratch(game_state *state, int difficulty)
1727 map_update_possibles(state);
1728 return solve_sub(state, difficulty, 0);
1731 /* --- New game functions --- */
1733 static game_state *new_state(const game_params *params)
1735 game_state *ret = snew(game_state);
1736 int wh = params->w * params->h, i;
1740 ret->allowloops = params->allowloops;
1741 ret->maxb = params->maxb;
1742 ret->params = *params;
1744 ret->grid = snewn(wh, grid_type);
1745 memset(ret->grid, 0, GRIDSZ(ret));
1746 ret->scratch = snewn(wh, grid_type);
1747 memset(ret->scratch, 0, GRIDSZ(ret));
1749 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1750 memset(ret->wha, 0, wh*N_WH_ARRAYS*sizeof(char));
1752 ret->possv = ret->wha;
1753 ret->possh = ret->wha + wh;
1754 ret->lines = ret->wha + wh*2;
1755 ret->maxv = ret->wha + wh*3;
1756 ret->maxh = ret->wha + wh*4;
1758 memset(ret->maxv, ret->maxb, wh*sizeof(char));
1759 memset(ret->maxh, ret->maxb, wh*sizeof(char));
1761 ret->islands = NULL;
1763 ret->n_islands_alloc = 0;
1765 ret->gridi = snewn(wh, struct island *);
1766 for (i = 0; i < wh; i++) ret->gridi[i] = NULL;
1768 ret->solved = ret->completed = 0;
1770 ret->solver = snew(struct solver_state);
1771 ret->solver->dsf = snew_dsf(wh);
1772 ret->solver->tmpdsf = snewn(wh, int);
1774 ret->solver->refcount = 1;
1779 static game_state *dup_game(const game_state *state)
1781 game_state *ret = snew(game_state);
1782 int wh = state->w*state->h;
1786 ret->allowloops = state->allowloops;
1787 ret->maxb = state->maxb;
1788 ret->params = state->params;
1790 ret->grid = snewn(wh, grid_type);
1791 memcpy(ret->grid, state->grid, GRIDSZ(ret));
1792 ret->scratch = snewn(wh, grid_type);
1793 memcpy(ret->scratch, state->scratch, GRIDSZ(ret));
1795 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1796 memcpy(ret->wha, state->wha, wh*N_WH_ARRAYS*sizeof(char));
1798 ret->possv = ret->wha;
1799 ret->possh = ret->wha + wh;
1800 ret->lines = ret->wha + wh*2;
1801 ret->maxv = ret->wha + wh*3;
1802 ret->maxh = ret->wha + wh*4;
1804 ret->islands = snewn(state->n_islands, struct island);
1805 memcpy(ret->islands, state->islands, state->n_islands * sizeof(struct island));
1806 ret->n_islands = ret->n_islands_alloc = state->n_islands;
1808 ret->gridi = snewn(wh, struct island *);
1809 fixup_islands_for_realloc(ret);
1811 ret->solved = state->solved;
1812 ret->completed = state->completed;
1814 ret->solver = state->solver;
1815 ret->solver->refcount++;
1820 static void free_game(game_state *state)
1822 if (--state->solver->refcount <= 0) {
1823 sfree(state->solver->dsf);
1824 sfree(state->solver->tmpdsf);
1825 sfree(state->solver);
1828 sfree(state->islands);
1829 sfree(state->gridi);
1833 sfree(state->scratch);
1838 #define MAX_NEWISLAND_TRIES 50
1839 #define MIN_SENSIBLE_ISLANDS 3
1841 #define ORDER(a,b) do { if (a < b) { int tmp=a; int a=b; int b=tmp; } } while(0)
1843 static char *new_game_desc(const game_params *params, random_state *rs,
1844 char **aux, int interactive)
1846 game_state *tobuild = NULL;
1847 int i, j, wh = params->w * params->h, x, y, dx, dy;
1848 int minx, miny, maxx, maxy, joinx, joiny, newx, newy, diffx, diffy;
1849 int ni_req = max((params->islands * wh) / 100, MIN_SENSIBLE_ISLANDS), ni_curr, ni_bad;
1850 struct island *is, *is2;
1852 unsigned int echeck;
1854 /* pick a first island position randomly. */
1856 if (tobuild) free_game(tobuild);
1857 tobuild = new_state(params);
1859 x = random_upto(rs, params->w);
1860 y = random_upto(rs, params->h);
1861 island_add(tobuild, x, y, 0);
1864 debug(("Created initial island at (%d,%d).\n", x, y));
1866 while (ni_curr < ni_req) {
1867 /* Pick a random island to try and extend from. */
1868 i = random_upto(rs, tobuild->n_islands);
1869 is = &tobuild->islands[i];
1871 /* Pick a random direction to extend in. */
1872 j = random_upto(rs, is->adj.npoints);
1873 dx = is->adj.points[j].x - is->x;
1874 dy = is->adj.points[j].y - is->y;
1876 /* Find out limits of where we could put a new island. */
1878 minx = is->x + 2*dx; miny = is->y + 2*dy; /* closest is 2 units away. */
1879 x = is->x+dx; y = is->y+dy;
1880 if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1881 /* already a line next to the island, continue. */
1885 if (x < 0 || x >= params->w || y < 0 || y >= params->h) {
1886 /* got past the edge; put a possible at the island
1888 maxx = x-dx; maxy = y-dy;
1891 if (GRID(tobuild,x,y) & G_ISLAND) {
1892 /* could join up to an existing island... */
1893 joinx = x; joiny = y;
1894 /* ... or make a new one 2 spaces away. */
1895 maxx = x - 2*dx; maxy = y - 2*dy;
1897 } else if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1898 /* could make a new one 1 space away from the line. */
1899 maxx = x - dx; maxy = y - dy;
1906 debug(("Island at (%d,%d) with d(%d,%d) has new positions "
1907 "(%d,%d) -> (%d,%d), join (%d,%d).\n",
1908 is->x, is->y, dx, dy, minx, miny, maxx, maxy, joinx, joiny));
1909 /* Now we know where we could either put a new island
1910 * (between min and max), or (if loops are allowed) could join on
1911 * to an existing island (at join). */
1912 if (params->allowloops && joinx != -1 && joiny != -1) {
1913 if (random_upto(rs, 100) < (unsigned long)params->expansion) {
1914 is2 = INDEX(tobuild, gridi, joinx, joiny);
1915 debug(("Joining island at (%d,%d) to (%d,%d).\n",
1916 is->x, is->y, is2->x, is2->y));
1920 diffx = (maxx - minx) * dx;
1921 diffy = (maxy - miny) * dy;
1922 if (diffx < 0 || diffy < 0) goto bad;
1923 if (random_upto(rs,100) < (unsigned long)params->expansion) {
1924 newx = maxx; newy = maxy;
1925 debug(("Creating new island at (%d,%d) (expanded).\n", newx, newy));
1927 newx = minx + random_upto(rs,diffx+1)*dx;
1928 newy = miny + random_upto(rs,diffy+1)*dy;
1929 debug(("Creating new island at (%d,%d).\n", newx, newy));
1931 /* check we're not next to island in the other orthogonal direction. */
1932 if ((INGRID(tobuild,newx+dy,newy+dx) && (GRID(tobuild,newx+dy,newy+dx) & G_ISLAND)) ||
1933 (INGRID(tobuild,newx-dy,newy-dx) && (GRID(tobuild,newx-dy,newy-dx) & G_ISLAND))) {
1934 debug(("New location is adjacent to island, skipping.\n"));
1937 is2 = island_add(tobuild, newx, newy, 0);
1938 /* Must get is again at this point; the array might have
1939 * been realloced by island_add... */
1940 is = &tobuild->islands[i]; /* ...but order will not change. */
1942 ni_curr++; ni_bad = 0;
1944 island_join(is, is2, random_upto(rs, tobuild->maxb)+1, 0);
1945 debug_state(tobuild);
1950 if (ni_bad > MAX_NEWISLAND_TRIES) {
1951 debug(("Unable to create any new islands after %d tries; "
1952 "created %d [%d%%] (instead of %d [%d%%] requested).\n",
1953 MAX_NEWISLAND_TRIES,
1954 ni_curr, ni_curr * 100 / wh,
1955 ni_req, ni_req * 100 / wh));
1962 debug(("Only generated one island (!), retrying.\n"));
1965 /* Check we have at least one island on each extremity of the grid. */
1967 for (x = 0; x < params->w; x++) {
1968 if (INDEX(tobuild, gridi, x, 0)) echeck |= 1;
1969 if (INDEX(tobuild, gridi, x, params->h-1)) echeck |= 2;
1971 for (y = 0; y < params->h; y++) {
1972 if (INDEX(tobuild, gridi, 0, y)) echeck |= 4;
1973 if (INDEX(tobuild, gridi, params->w-1, y)) echeck |= 8;
1976 debug(("Generated grid doesn't fill to sides, retrying.\n"));
1981 map_find_orthogonal(tobuild);
1983 if (params->difficulty > 0) {
1984 if ((ni_curr > MIN_SENSIBLE_ISLANDS) &&
1985 (solve_from_scratch(tobuild, params->difficulty-1) > 0)) {
1986 debug(("Grid is solvable at difficulty %d (too easy); retrying.\n",
1987 params->difficulty-1));
1992 if (solve_from_scratch(tobuild, params->difficulty) == 0) {
1993 debug(("Grid not solvable at difficulty %d, (too hard); retrying.\n",
1994 params->difficulty));
1998 /* ... tobuild is now solved. We rely on this making the diff for aux. */
1999 debug_state(tobuild);
2000 ret = encode_game(tobuild);
2002 game_state *clean = dup_game(tobuild);
2004 map_update_possibles(clean);
2005 *aux = game_state_diff(clean, tobuild);
2013 static char *validate_desc(const game_params *params, const char *desc)
2015 int i, wh = params->w * params->h;
2017 for (i = 0; i < wh; i++) {
2018 if (*desc >= '1' && *desc <= '9')
2020 else if (*desc >= 'a' && *desc <= 'z')
2021 i += *desc - 'a'; /* plus the i++ */
2022 else if (*desc >= 'A' && *desc <= 'G')
2024 else if (*desc == 'V' || *desc == 'W' ||
2025 *desc == 'X' || *desc == 'Y' ||
2026 *desc == 'H' || *desc == 'I' ||
2027 *desc == 'J' || *desc == 'K')
2030 return "Game description shorter than expected";
2032 return "Game description containers unexpected character";
2035 if (*desc || i > wh)
2036 return "Game description longer than expected";
2041 static game_state *new_game_sub(const game_params *params, const char *desc)
2043 game_state *state = new_state(params);
2046 debug(("new_game[_sub]: desc = '%s'.\n", desc));
2048 for (y = 0; y < params->h; y++) {
2049 for (x = 0; x < params->w; x++) {
2055 if (c >= 'a' && c <= 'z')
2065 case '1': case '2': case '3': case '4':
2066 case '5': case '6': case '7': case '8': case '9':
2067 island_add(state, x, y, (c - '0'));
2070 case 'A': case 'B': case 'C': case 'D':
2071 case 'E': case 'F': case 'G':
2072 island_add(state, x, y, (c - 'A') + 10);
2080 assert(!"Malformed desc.");
2085 if (*desc) assert(!"Over-long desc.");
2087 map_find_orthogonal(state);
2088 map_update_possibles(state);
2093 static game_state *new_game(midend *me, const game_params *params,
2096 return new_game_sub(params, desc);
2100 int dragx_src, dragy_src; /* source; -1 means no drag */
2101 int dragx_dst, dragy_dst; /* src's closest orth island. */
2103 int dragging, drag_is_noline, nlines;
2105 int cur_x, cur_y, cur_visible; /* cursor position */
2109 static char *ui_cancel_drag(game_ui *ui)
2111 ui->dragx_src = ui->dragy_src = -1;
2112 ui->dragx_dst = ui->dragy_dst = -1;
2117 static game_ui *new_ui(const game_state *state)
2119 game_ui *ui = snew(game_ui);
2121 ui->cur_x = state->islands[0].x;
2122 ui->cur_y = state->islands[0].y;
2123 ui->cur_visible = 0;
2128 static void free_ui(game_ui *ui)
2133 static char *encode_ui(const game_ui *ui)
2138 static void decode_ui(game_ui *ui, const char *encoding)
2142 static void game_changed_state(game_ui *ui, const game_state *oldstate,
2143 const game_state *newstate)
2147 struct game_drawstate {
2150 unsigned long *grid, *newgrid;
2152 int started, dragging;
2156 * The contents of ds->grid are complicated, because of the circular
2157 * islands which overlap their own grid square into neighbouring
2158 * squares. An island square can contain pieces of the bridges in all
2159 * directions, and conversely a bridge square can be intruded on by
2160 * islands from any direction.
2162 * So we define one group of flags describing what's important about
2163 * an island, and another describing a bridge. Island squares' entries
2164 * in ds->grid contain one of the former and four of the latter; bridge
2165 * squares, four of the former and _two_ of the latter - because a
2166 * horizontal and vertical 'bridge' can cross, when one of them is a
2167 * 'no bridge here' pencil mark.
2169 * Bridge flags need to indicate 0-4 actual bridges (3 bits), a 'no
2170 * bridge' row of crosses, or a grey hint line; that's 7
2171 * possibilities, so 3 bits suffice. But then we also need to vary the
2172 * colours: the bridges can turn COL_WARNING if they're part of a loop
2173 * in no-loops mode, COL_HIGHLIGHT during a victory flash, or
2174 * COL_SELECTED if they're the bridge the user is currently dragging,
2175 * so that's 2 more bits for foreground colour. Also bridges can be
2176 * backed by COL_MARK if they're locked by the user, so that's one
2177 * more bit, making 6 bits per bridge direction.
2179 * Island flags omit the actual island clue (it never changes during
2180 * the game, so doesn't have to be stored in ds->grid to check against
2181 * the previous version), so they just need to include 2 bits for
2182 * foreground colour (an island can be normal, COL_HIGHLIGHT during
2183 * victory, COL_WARNING if its clue is unsatisfiable, or COL_SELECTED
2184 * if it's part of the user's drag) and 2 bits for background (normal,
2185 * COL_MARK for a locked island, COL_CURSOR for the keyboard cursor).
2186 * That's 4 bits per island direction. We must also indicate whether
2187 * no island is present at all (in the case where the island is
2188 * potentially intruding into the side of a line square), which we do
2189 * using the unused 4th value of the background field.
2191 * So an island square needs 4 + 4*6 = 28 bits, while a bridge square
2192 * needs 4*4 + 2*6 = 28 bits too. Both only just fit in 32 bits, which
2193 * is handy, because otherwise we'd have to faff around forever with
2196 /* Flags for line data */
2197 #define DL_COUNTMASK 0x07
2198 #define DL_COUNT_CROSS 0x06
2199 #define DL_COUNT_HINT 0x07
2200 #define DL_COLMASK 0x18
2201 #define DL_COL_NORMAL 0x00
2202 #define DL_COL_WARNING 0x08
2203 #define DL_COL_FLASH 0x10
2204 #define DL_COL_SELECTED 0x18
2205 #define DL_LOCK 0x20
2206 #define DL_MASK 0x3F
2207 /* Flags for island data */
2208 #define DI_COLMASK 0x03
2209 #define DI_COL_NORMAL 0x00
2210 #define DI_COL_FLASH 0x01
2211 #define DI_COL_WARNING 0x02
2212 #define DI_COL_SELECTED 0x03
2213 #define DI_BGMASK 0x0C
2214 #define DI_BG_NO_ISLAND 0x00
2215 #define DI_BG_NORMAL 0x04
2216 #define DI_BG_MARK 0x08
2217 #define DI_BG_CURSOR 0x0C
2218 #define DI_MASK 0x0F
2219 /* Shift counts for the format of a 32-bit word in an island square */
2220 #define D_I_ISLAND_SHIFT 0
2221 #define D_I_LINE_SHIFT_L 4
2222 #define D_I_LINE_SHIFT_R 10
2223 #define D_I_LINE_SHIFT_U 16
2224 #define D_I_LINE_SHIFT_D 24
2225 /* Shift counts for the format of a 32-bit word in a line square */
2226 #define D_L_ISLAND_SHIFT_L 0
2227 #define D_L_ISLAND_SHIFT_R 4
2228 #define D_L_ISLAND_SHIFT_U 8
2229 #define D_L_ISLAND_SHIFT_D 12
2230 #define D_L_LINE_SHIFT_H 16
2231 #define D_L_LINE_SHIFT_V 22
2233 static char *update_drag_dst(const game_state *state, game_ui *ui,
2234 const game_drawstate *ds, int nx, int ny)
2236 int ox, oy, dx, dy, i, currl, maxb;
2238 grid_type gtype, ntype, mtype, curr;
2240 if (ui->dragx_src == -1 || ui->dragy_src == -1) return NULL;
2245 /* work out which of the four directions we're closest to... */
2246 ox = COORD(ui->dragx_src) + TILE_SIZE/2;
2247 oy = COORD(ui->dragy_src) + TILE_SIZE/2;
2249 if (abs(nx-ox) < abs(ny-oy)) {
2251 dy = (ny-oy) < 0 ? -1 : 1;
2252 gtype = G_LINEV; ntype = G_NOLINEV; mtype = G_MARKV;
2253 maxb = INDEX(state, maxv, ui->dragx_src+dx, ui->dragy_src+dy);
2256 dx = (nx-ox) < 0 ? -1 : 1;
2257 gtype = G_LINEH; ntype = G_NOLINEH; mtype = G_MARKH;
2258 maxb = INDEX(state, maxh, ui->dragx_src+dx, ui->dragy_src+dy);
2260 if (ui->drag_is_noline) {
2263 curr = GRID(state, ui->dragx_src+dx, ui->dragy_src+dy);
2264 currl = INDEX(state, lines, ui->dragx_src+dx, ui->dragy_src+dy);
2267 if (currl == maxb) {
2272 ui->nlines = currl + 1;
2280 /* ... and see if there's an island off in that direction. */
2281 is = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2282 for (i = 0; i < is->adj.npoints; i++) {
2283 if (is->adj.points[i].off == 0) continue;
2284 curr = GRID(state, is->x+dx, is->y+dy);
2285 if (curr & mtype) continue; /* don't allow changes to marked lines. */
2286 if (ui->drag_is_noline) {
2287 if (curr & gtype) continue; /* no no-line where already a line */
2289 if (POSSIBLES(state, dx, is->x+dx, is->y+dy) == 0) continue; /* no line if !possible. */
2290 if (curr & ntype) continue; /* can't have a bridge where there's a no-line. */
2293 if (is->adj.points[i].dx == dx &&
2294 is->adj.points[i].dy == dy) {
2295 ui->dragx_dst = ISLAND_ORTHX(is,i);
2296 ui->dragy_dst = ISLAND_ORTHY(is,i);
2299 /*debug(("update_drag src (%d,%d) d(%d,%d) dst (%d,%d)\n",
2300 ui->dragx_src, ui->dragy_src, dx, dy,
2301 ui->dragx_dst, ui->dragy_dst));*/
2305 static char *finish_drag(const game_state *state, game_ui *ui)
2309 if (ui->dragx_src == -1 || ui->dragy_src == -1)
2311 if (ui->dragx_dst == -1 || ui->dragy_dst == -1)
2312 return ui_cancel_drag(ui);
2314 if (ui->drag_is_noline) {
2315 sprintf(buf, "N%d,%d,%d,%d",
2316 ui->dragx_src, ui->dragy_src,
2317 ui->dragx_dst, ui->dragy_dst);
2319 sprintf(buf, "L%d,%d,%d,%d,%d",
2320 ui->dragx_src, ui->dragy_src,
2321 ui->dragx_dst, ui->dragy_dst, ui->nlines);
2329 static char *interpret_move(const game_state *state, game_ui *ui,
2330 const game_drawstate *ds,
2331 int x, int y, int button)
2333 int gx = FROMCOORD(x), gy = FROMCOORD(y);
2335 grid_type ggrid = INGRID(state,gx,gy) ? GRID(state,gx,gy) : 0;
2337 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2338 if (!INGRID(state, gx, gy)) return NULL;
2339 ui->cur_visible = 0;
2340 if ((ggrid & G_ISLAND) && !(ggrid & G_MARK)) {
2345 return ui_cancel_drag(ui);
2346 } else if (button == LEFT_DRAG || button == RIGHT_DRAG) {
2347 if (gx != ui->dragx_src || gy != ui->dragy_src) {
2349 ui->drag_is_noline = (button == RIGHT_DRAG) ? 1 : 0;
2350 return update_drag_dst(state, ui, ds, x, y);
2352 /* cancel a drag when we go back to the starting point */
2357 } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) {
2359 return finish_drag(state, ui);
2362 if (!INGRID(state, gx, gy)) return NULL;
2363 if (!(GRID(state, gx, gy) & G_ISLAND)) return NULL;
2364 sprintf(buf, "M%d,%d", gx, gy);
2367 } else if (button == 'h' || button == 'H') {
2368 game_state *solved = dup_game(state);
2369 solve_for_hint(solved);
2370 ret = game_state_diff(state, solved);
2373 } else if (IS_CURSOR_MOVE(button)) {
2374 ui->cur_visible = 1;
2376 int nx = ui->cur_x, ny = ui->cur_y;
2378 move_cursor(button, &nx, &ny, state->w, state->h, 0);
2379 update_drag_dst(state, ui, ds,
2380 COORD(nx)+TILE_SIZE/2,
2381 COORD(ny)+TILE_SIZE/2);
2382 return finish_drag(state, ui);
2384 int dx = (button == CURSOR_RIGHT) ? +1 : (button == CURSOR_LEFT) ? -1 : 0;
2385 int dy = (button == CURSOR_DOWN) ? +1 : (button == CURSOR_UP) ? -1 : 0;
2386 int dorthx = 1 - abs(dx), dorthy = 1 - abs(dy);
2387 int dir, orth, nx = x, ny = y;
2389 /* 'orthorder' is a tweak to ensure that if you press RIGHT and
2390 * happen to move upwards, when you press LEFT you then tend
2391 * downwards (rather than upwards again). */
2392 int orthorder = (button == CURSOR_LEFT || button == CURSOR_UP) ? 1 : -1;
2394 /* This attempts to find an island in the direction you're
2395 * asking for, broadly speaking. If you ask to go right, for
2396 * example, it'll look for islands to the right and slightly
2397 * above or below your current horiz. position, allowing
2398 * further above/below the further away it searches. */
2400 assert(GRID(state, ui->cur_x, ui->cur_y) & G_ISLAND);
2401 /* currently this is depth-first (so orthogonally-adjacent
2402 * islands across the other side of the grid will be moved to
2403 * before closer islands slightly offset). Swap the order of
2404 * these two loops to change to breadth-first search. */
2405 for (orth = 0; ; orth++) {
2407 for (dir = 1; ; dir++) {
2410 if (orth > dir) continue; /* only search in cone outwards. */
2412 nx = ui->cur_x + dir*dx + orth*dorthx*orthorder;
2413 ny = ui->cur_y + dir*dy + orth*dorthy*orthorder;
2414 if (INGRID(state, nx, ny)) {
2415 dingrid = oingrid = 1;
2416 if (GRID(state, nx, ny) & G_ISLAND) goto found;
2419 nx = ui->cur_x + dir*dx - orth*dorthx*orthorder;
2420 ny = ui->cur_y + dir*dy - orth*dorthy*orthorder;
2421 if (INGRID(state, nx, ny)) {
2422 dingrid = oingrid = 1;
2423 if (GRID(state, nx, ny) & G_ISLAND) goto found;
2426 if (!dingrid) break;
2428 if (!oingrid) return "";
2437 } else if (IS_CURSOR_SELECT(button)) {
2438 if (!ui->cur_visible) {
2439 ui->cur_visible = 1;
2444 if (ui->dragx_dst == -1 && ui->dragy_dst == -1) {
2445 sprintf(buf, "M%d,%d", ui->cur_x, ui->cur_y);
2450 grid_type v = GRID(state, ui->cur_x, ui->cur_y);
2453 ui->dragx_src = ui->cur_x;
2454 ui->dragy_src = ui->cur_y;
2455 ui->dragx_dst = ui->dragy_dst = -1;
2456 ui->drag_is_noline = (button == CURSOR_SELECT2) ? 1 : 0;
2460 } else if (button == 'g' || button == 'G') {
2461 ui->show_hints = 1 - ui->show_hints;
2468 static game_state *execute_move(const game_state *state, const char *move)
2470 game_state *ret = dup_game(state);
2471 int x1, y1, x2, y2, nl, n;
2472 struct island *is1, *is2;
2475 debug(("execute_move: %s\n", move));
2477 if (!*move) goto badmove;
2483 } else if (c == 'L') {
2484 if (sscanf(move, "%d,%d,%d,%d,%d%n",
2485 &x1, &y1, &x2, &y2, &nl, &n) != 5)
2487 if (!INGRID(ret, x1, y1) || !INGRID(ret, x2, y2))
2489 is1 = INDEX(ret, gridi, x1, y1);
2490 is2 = INDEX(ret, gridi, x2, y2);
2491 if (!is1 || !is2) goto badmove;
2492 if (nl < 0 || nl > state->maxb) goto badmove;
2493 island_join(is1, is2, nl, 0);
2494 } else if (c == 'N') {
2495 if (sscanf(move, "%d,%d,%d,%d%n",
2496 &x1, &y1, &x2, &y2, &n) != 4)
2498 if (!INGRID(ret, x1, y1) || !INGRID(ret, x2, y2))
2500 is1 = INDEX(ret, gridi, x1, y1);
2501 is2 = INDEX(ret, gridi, x2, y2);
2502 if (!is1 || !is2) goto badmove;
2503 island_join(is1, is2, -1, 0);
2504 } else if (c == 'M') {
2505 if (sscanf(move, "%d,%d%n",
2508 if (!INGRID(ret, x1, y1))
2510 is1 = INDEX(ret, gridi, x1, y1);
2511 if (!is1) goto badmove;
2512 island_togglemark(is1);
2519 else if (*move) goto badmove;
2522 map_update_possibles(ret);
2523 if (map_check(ret)) {
2524 debug(("Game completed.\n"));
2530 debug(("%s: unrecognised move.\n", move));
2535 static char *solve_game(const game_state *state, const game_state *currstate,
2536 const char *aux, char **error)
2542 debug(("solve_game: aux = %s\n", aux));
2543 solved = execute_move(state, aux);
2545 *error = "Generated aux string is not a valid move (!).";
2549 solved = dup_game(state);
2550 /* solve with max strength... */
2551 if (solve_from_scratch(solved, 10) == 0) {
2553 *error = "Game does not have a (non-recursive) solution.";
2557 ret = game_state_diff(currstate, solved);
2559 debug(("solve_game: ret = %s\n", ret));
2563 /* ----------------------------------------------------------------------
2567 static void game_compute_size(const game_params *params, int tilesize,
2570 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2571 struct { int tilesize; } ads, *ds = &ads;
2572 ads.tilesize = tilesize;
2574 *x = TILE_SIZE * params->w + 2 * BORDER;
2575 *y = TILE_SIZE * params->h + 2 * BORDER;
2578 static void game_set_size(drawing *dr, game_drawstate *ds,
2579 const game_params *params, int tilesize)
2581 ds->tilesize = tilesize;
2584 static float *game_colours(frontend *fe, int *ncolours)
2586 float *ret = snewn(3 * NCOLOURS, float);
2589 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
2591 for (i = 0; i < 3; i++) {
2592 ret[COL_FOREGROUND * 3 + i] = 0.0F;
2593 ret[COL_HINT * 3 + i] = ret[COL_LOWLIGHT * 3 + i];
2594 ret[COL_GRID * 3 + i] =
2595 (ret[COL_HINT * 3 + i] + ret[COL_BACKGROUND * 3 + i]) * 0.5F;
2596 ret[COL_MARK * 3 + i] = ret[COL_HIGHLIGHT * 3 + i];
2598 ret[COL_WARNING * 3 + 0] = 1.0F;
2599 ret[COL_WARNING * 3 + 1] = 0.25F;
2600 ret[COL_WARNING * 3 + 2] = 0.25F;
2602 ret[COL_SELECTED * 3 + 0] = 0.25F;
2603 ret[COL_SELECTED * 3 + 1] = 1.00F;
2604 ret[COL_SELECTED * 3 + 2] = 0.25F;
2606 ret[COL_CURSOR * 3 + 0] = min(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
2607 ret[COL_CURSOR * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
2608 ret[COL_CURSOR * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
2610 *ncolours = NCOLOURS;
2614 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
2616 struct game_drawstate *ds = snew(struct game_drawstate);
2617 int wh = state->w*state->h;
2625 ds->grid = snewn(wh, unsigned long);
2626 for (i = 0; i < wh; i++)
2628 ds->newgrid = snewn(wh, unsigned long);
2629 ds->lv = snewn(wh, int);
2630 ds->lh = snewn(wh, int);
2631 memset(ds->lv, 0, wh*sizeof(int));
2632 memset(ds->lh, 0, wh*sizeof(int));
2637 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2646 #define LINE_WIDTH (TILE_SIZE/8)
2647 #define TS8(x) (((x)*TILE_SIZE)/8)
2649 #define OFFSET(thing) ((TILE_SIZE/2) - ((thing)/2))
2651 static int between_island(const game_state *state, int sx, int sy,
2654 int x = sx - dx, y = sy - dy;
2656 while (INGRID(state, x, y)) {
2657 if (GRID(state, x, y) & G_ISLAND) goto found;
2662 x = sx + dx, y = sy + dy;
2663 while (INGRID(state, x, y)) {
2664 if (GRID(state, x, y) & G_ISLAND) return 1;
2670 static void lines_lvlh(const game_state *state, const game_ui *ui,
2671 int x, int y, grid_type v, int *lv_r, int *lh_r)
2675 if (v & G_LINEV) lv = INDEX(state,lines,x,y);
2676 if (v & G_LINEH) lh = INDEX(state,lines,x,y);
2678 if (ui->show_hints) {
2679 if (between_island(state, x, y, 0, 1) && !lv) lv = 1;
2680 if (between_island(state, x, y, 1, 0) && !lh) lh = 1;
2682 /*debug(("lvlh: (%d,%d) v 0x%x lv %d lh %d.\n", x, y, v, lv, lh));*/
2683 *lv_r = lv; *lh_r = lh;
2686 static void draw_cross(drawing *dr, game_drawstate *ds,
2687 int ox, int oy, int col)
2690 draw_line(dr, ox, oy, ox+off, oy+off, col);
2691 draw_line(dr, ox+off, oy, ox, oy+off, col);
2694 static void draw_general_line(drawing *dr, game_drawstate *ds,
2695 int ox, int oy, int fx, int fy, int ax, int ay,
2696 int len, unsigned long ldata, int which)
2699 * Draw one direction of lines in a square. To permit the same
2700 * code to handle horizontal and vertical lines, fx,fy are the
2701 * 'forward' direction (along the lines) and ax,ay are the
2702 * 'across' direction.
2704 * We draw the white background for a locked bridge if (which &
2705 * 1), and draw the bridges themselves if (which & 2). This
2706 * permits us to get two overlapping locked bridges right without
2707 * one of them erasing part of the other.
2711 fg = ((ldata & DL_COUNTMASK) == DL_COUNT_HINT ? COL_HINT :
2712 (ldata & DL_COLMASK) == DL_COL_SELECTED ? COL_SELECTED :
2713 (ldata & DL_COLMASK) == DL_COL_FLASH ? COL_HIGHLIGHT :
2714 (ldata & DL_COLMASK) == DL_COL_WARNING ? COL_WARNING :
2717 if ((ldata & DL_COUNTMASK) == DL_COUNT_CROSS) {
2719 ox + TS8(1)*fx + TS8(3)*ax,
2720 oy + TS8(1)*fy + TS8(3)*ay, fg);
2722 ox + TS8(5)*fx + TS8(3)*ax,
2723 oy + TS8(5)*fy + TS8(3)*ay, fg);
2724 } else if ((ldata & DL_COUNTMASK) != 0) {
2725 int lh, lw, gw, bw, i, loff;
2727 lh = (ldata & DL_COUNTMASK);
2728 if (lh == DL_COUNT_HINT)
2731 lw = gw = LINE_WIDTH;
2732 while ((bw = lw * lh + gw * (lh+1)) > TILE_SIZE)
2738 if ((ldata & DL_LOCK) && fg != COL_HINT)
2739 draw_rect(dr, ox + loff*ax, oy + loff*ay,
2740 len*fx+bw*ax, len*fy+bw*ay, COL_MARK);
2743 for (i = 0; i < lh; i++, loff += lw + gw)
2744 draw_rect(dr, ox + (loff+gw)*ax, oy + (loff+gw)*ay,
2745 len*fx+lw*ax, len*fy+lw*ay, fg);
2750 static void draw_hline(drawing *dr, game_drawstate *ds,
2751 int ox, int oy, int w, unsigned long vdata, int which)
2753 draw_general_line(dr, ds, ox, oy, 1, 0, 0, 1, w, vdata, which);
2756 static void draw_vline(drawing *dr, game_drawstate *ds,
2757 int ox, int oy, int h, unsigned long vdata, int which)
2759 draw_general_line(dr, ds, ox, oy, 0, 1, 1, 0, h, vdata, which);
2762 #define ISLAND_RADIUS ((TILE_SIZE*12)/20)
2763 #define ISLAND_NUMSIZE(clue) \
2764 (((clue) < 10) ? (TILE_SIZE*7)/10 : (TILE_SIZE*5)/10)
2766 static void draw_island(drawing *dr, game_drawstate *ds,
2767 int ox, int oy, int clue, unsigned long idata)
2769 int half, orad, irad, fg, bg;
2771 if ((idata & DI_BGMASK) == DI_BG_NO_ISLAND)
2775 orad = ISLAND_RADIUS;
2776 irad = orad - LINE_WIDTH;
2777 fg = ((idata & DI_COLMASK) == DI_COL_SELECTED ? COL_SELECTED :
2778 (idata & DI_COLMASK) == DI_COL_WARNING ? COL_WARNING :
2779 (idata & DI_COLMASK) == DI_COL_FLASH ? COL_HIGHLIGHT :
2781 bg = ((idata & DI_BGMASK) == DI_BG_CURSOR ? COL_CURSOR :
2782 (idata & DI_BGMASK) == DI_BG_MARK ? COL_MARK :
2785 /* draw a thick circle */
2786 draw_circle(dr, ox+half, oy+half, orad, fg, fg);
2787 draw_circle(dr, ox+half, oy+half, irad, bg, bg);
2791 int textcolour = (fg == COL_SELECTED ? COL_FOREGROUND : fg);
2792 sprintf(str, "%d", clue);
2793 draw_text(dr, ox+half, oy+half, FONT_VARIABLE, ISLAND_NUMSIZE(clue),
2794 ALIGN_VCENTRE | ALIGN_HCENTRE, textcolour, str);
2798 static void draw_island_tile(drawing *dr, game_drawstate *ds,
2799 int x, int y, int clue, unsigned long data)
2801 int ox = COORD(x), oy = COORD(y);
2804 clip(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2805 draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2808 * Because of the possibility of incoming bridges just about
2809 * meeting at one corner, we must split the line-drawing into
2810 * background and foreground segments.
2812 for (which = 1; which <= 2; which <<= 1) {
2813 draw_hline(dr, ds, ox, oy, TILE_SIZE/2,
2814 (data >> D_I_LINE_SHIFT_L) & DL_MASK, which);
2815 draw_hline(dr, ds, ox + TILE_SIZE - TILE_SIZE/2, oy, TILE_SIZE/2,
2816 (data >> D_I_LINE_SHIFT_R) & DL_MASK, which);
2817 draw_vline(dr, ds, ox, oy, TILE_SIZE/2,
2818 (data >> D_I_LINE_SHIFT_U) & DL_MASK, which);
2819 draw_vline(dr, ds, ox, oy + TILE_SIZE - TILE_SIZE/2, TILE_SIZE/2,
2820 (data >> D_I_LINE_SHIFT_D) & DL_MASK, which);
2822 draw_island(dr, ds, ox, oy, clue, (data >> D_I_ISLAND_SHIFT) & DI_MASK);
2825 draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2828 static void draw_line_tile(drawing *dr, game_drawstate *ds,
2829 int x, int y, unsigned long data)
2831 int ox = COORD(x), oy = COORD(y);
2832 unsigned long hdata, vdata;
2834 clip(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2835 draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2838 * We have to think about which of the horizontal and vertical
2839 * line to draw first, if both exist.
2841 * The rule is that hint lines are drawn at the bottom, then
2842 * NOLINE crosses, then actual bridges. The enumeration in the
2843 * DL_COUNTMASK field is set up so that this drops out of a
2844 * straight comparison between the two.
2846 * Since lines crossing in this type of square cannot both be
2847 * actual bridges, there's no need to pass a nontrivial 'which'
2848 * parameter to draw_[hv]line.
2850 hdata = (data >> D_L_LINE_SHIFT_H) & DL_MASK;
2851 vdata = (data >> D_L_LINE_SHIFT_V) & DL_MASK;
2852 if ((hdata & DL_COUNTMASK) > (vdata & DL_COUNTMASK)) {
2853 draw_hline(dr, ds, ox, oy, TILE_SIZE, hdata, 3);
2854 draw_vline(dr, ds, ox, oy, TILE_SIZE, vdata, 3);
2856 draw_vline(dr, ds, ox, oy, TILE_SIZE, vdata, 3);
2857 draw_hline(dr, ds, ox, oy, TILE_SIZE, hdata, 3);
2861 * The islands drawn at the edges of a line tile don't need clue
2864 draw_island(dr, ds, ox - TILE_SIZE, oy, -1,
2865 (data >> D_L_ISLAND_SHIFT_L) & DI_MASK);
2866 draw_island(dr, ds, ox + TILE_SIZE, oy, -1,
2867 (data >> D_L_ISLAND_SHIFT_R) & DI_MASK);
2868 draw_island(dr, ds, ox, oy - TILE_SIZE, -1,
2869 (data >> D_L_ISLAND_SHIFT_U) & DI_MASK);
2870 draw_island(dr, ds, ox, oy + TILE_SIZE, -1,
2871 (data >> D_L_ISLAND_SHIFT_D) & DI_MASK);
2874 draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2877 static void draw_edge_tile(drawing *dr, game_drawstate *ds,
2878 int x, int y, int dx, int dy, unsigned long data)
2880 int ox = COORD(x), oy = COORD(y);
2881 int cx = ox, cy = oy, cw = TILE_SIZE, ch = TILE_SIZE;
2892 clip(dr, cx, cy, cw, ch);
2893 draw_rect(dr, cx, cy, cw, ch, COL_BACKGROUND);
2895 draw_island(dr, ds, ox + TILE_SIZE*dx, oy + TILE_SIZE*dy, -1,
2896 (data >> D_I_ISLAND_SHIFT) & DI_MASK);
2899 draw_update(dr, cx, cy, cw, ch);
2902 static void game_redraw(drawing *dr, game_drawstate *ds,
2903 const game_state *oldstate, const game_state *state,
2904 int dir, const game_ui *ui,
2905 float animtime, float flashtime)
2908 grid_type v, flash = 0;
2909 struct island *is, *is_drag_src = NULL, *is_drag_dst = NULL;
2912 int f = (int)(flashtime * 5 / FLASH_TIME);
2913 if (f == 1 || f == 3) flash = TRUE;
2916 /* Clear screen, if required. */
2919 TILE_SIZE * ds->w + 2 * BORDER,
2920 TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND);
2922 draw_rect_outline(dr,
2923 COORD(0)-1, COORD(0)-1,
2924 TILE_SIZE * ds->w + 2, TILE_SIZE * ds->h + 2,
2927 draw_update(dr, 0, 0,
2928 TILE_SIZE * ds->w + 2 * BORDER,
2929 TILE_SIZE * ds->h + 2 * BORDER);
2933 if (ui->dragx_src != -1 && ui->dragy_src != -1) {
2935 is_drag_src = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2936 assert(is_drag_src);
2937 if (ui->dragx_dst != -1 && ui->dragy_dst != -1) {
2938 is_drag_dst = INDEX(state, gridi, ui->dragx_dst, ui->dragy_dst);
2939 assert(is_drag_dst);
2945 * Set up ds->newgrid with the current grid contents.
2947 for (x = 0; x < ds->w; x++)
2948 for (y = 0; y < ds->h; y++)
2949 INDEX(ds,newgrid,x,y) = 0;
2951 for (x = 0; x < ds->w; x++) {
2952 for (y = 0; y < ds->h; y++) {
2953 v = GRID(state, x, y);
2957 * An island square. Compute the drawing data for the
2958 * island, and put it in this square and surrounding
2961 unsigned long idata = 0;
2963 is = INDEX(state, gridi, x, y);
2966 idata |= DI_COL_FLASH;
2967 if (is_drag_src && (is == is_drag_src ||
2968 (is_drag_dst && is == is_drag_dst)))
2969 idata |= DI_COL_SELECTED;
2970 else if (island_impossible(is, v & G_MARK) || (v & G_WARN))
2971 idata |= DI_COL_WARNING;
2973 idata |= DI_COL_NORMAL;
2975 if (ui->cur_visible &&
2976 ui->cur_x == is->x && ui->cur_y == is->y)
2977 idata |= DI_BG_CURSOR;
2978 else if (v & G_MARK)
2979 idata |= DI_BG_MARK;
2981 idata |= DI_BG_NORMAL;
2983 INDEX(ds,newgrid,x,y) |= idata << D_I_ISLAND_SHIFT;
2984 if (x > 0 && !(GRID(state,x-1,y) & G_ISLAND))
2985 INDEX(ds,newgrid,x-1,y) |= idata << D_L_ISLAND_SHIFT_R;
2986 if (x+1 < state->w && !(GRID(state,x+1,y) & G_ISLAND))
2987 INDEX(ds,newgrid,x+1,y) |= idata << D_L_ISLAND_SHIFT_L;
2988 if (y > 0 && !(GRID(state,x,y-1) & G_ISLAND))
2989 INDEX(ds,newgrid,x,y-1) |= idata << D_L_ISLAND_SHIFT_D;
2990 if (y+1 < state->h && !(GRID(state,x,y+1) & G_ISLAND))
2991 INDEX(ds,newgrid,x,y+1) |= idata << D_L_ISLAND_SHIFT_U;
2993 unsigned long hdata, vdata;
2994 int selh = FALSE, selv = FALSE;
2997 * A line (non-island) square. Compute the drawing
2998 * data for any horizontal and vertical lines in the
2999 * square, and put them in this square's entry and
3000 * optionally those for neighbouring islands too.
3004 WITHIN(x,is_drag_src->x, is_drag_dst->x) &&
3005 WITHIN(y,is_drag_src->y, is_drag_dst->y)) {
3006 if (is_drag_src->x != is_drag_dst->x)
3011 lines_lvlh(state, ui, x, y, v, &lv, &lh);
3013 hdata = (v & G_NOLINEH ? DL_COUNT_CROSS :
3016 between_island(state,x,y,1,0)) ? DL_COUNT_HINT : 0);
3017 vdata = (v & G_NOLINEV ? DL_COUNT_CROSS :
3020 between_island(state,x,y,0,1)) ? DL_COUNT_HINT : 0);
3022 hdata |= (flash ? DL_COL_FLASH :
3023 v & G_WARN ? DL_COL_WARNING :
3024 selh ? DL_COL_SELECTED :
3026 vdata |= (flash ? DL_COL_FLASH :
3027 v & G_WARN ? DL_COL_WARNING :
3028 selv ? DL_COL_SELECTED :
3036 INDEX(ds,newgrid,x,y) |= hdata << D_L_LINE_SHIFT_H;
3037 INDEX(ds,newgrid,x,y) |= vdata << D_L_LINE_SHIFT_V;
3038 if (x > 0 && (GRID(state,x-1,y) & G_ISLAND))
3039 INDEX(ds,newgrid,x-1,y) |= hdata << D_I_LINE_SHIFT_R;
3040 if (x+1 < state->w && (GRID(state,x+1,y) & G_ISLAND))
3041 INDEX(ds,newgrid,x+1,y) |= hdata << D_I_LINE_SHIFT_L;
3042 if (y > 0 && (GRID(state,x,y-1) & G_ISLAND))
3043 INDEX(ds,newgrid,x,y-1) |= vdata << D_I_LINE_SHIFT_D;
3044 if (y+1 < state->h && (GRID(state,x,y+1) & G_ISLAND))
3045 INDEX(ds,newgrid,x,y+1) |= vdata << D_I_LINE_SHIFT_U;
3051 * Now go through and draw any changed grid square.
3053 for (x = 0; x < ds->w; x++) {
3054 for (y = 0; y < ds->h; y++) {
3055 unsigned long newval = INDEX(ds,newgrid,x,y);
3056 if (INDEX(ds,grid,x,y) != newval) {
3057 v = GRID(state, x, y);
3059 is = INDEX(state, gridi, x, y);
3060 draw_island_tile(dr, ds, x, y, is->count, newval);
3063 * If this tile is right at the edge of the grid,
3064 * we must also draw the part of the island that
3065 * goes completely out of bounds. We don't bother
3066 * keeping separate entries in ds->newgrid for
3067 * these tiles; it's easier just to redraw them
3068 * iff we redraw their parent island tile.
3071 draw_edge_tile(dr, ds, x-1, y, +1, 0, newval);
3073 draw_edge_tile(dr, ds, x, y-1, 0, +1, newval);
3074 if (x == state->w-1)
3075 draw_edge_tile(dr, ds, x+1, y, -1, 0, newval);
3076 if (y == state->h-1)
3077 draw_edge_tile(dr, ds, x, y+1, 0, -1, newval);
3079 draw_line_tile(dr, ds, x, y, newval);
3081 INDEX(ds,grid,x,y) = newval;
3087 static float game_anim_length(const game_state *oldstate,
3088 const game_state *newstate, int dir, game_ui *ui)
3093 static float game_flash_length(const game_state *oldstate,
3094 const game_state *newstate, int dir, game_ui *ui)
3096 if (!oldstate->completed && newstate->completed &&
3097 !oldstate->solved && !newstate->solved)
3103 static int game_status(const game_state *state)
3105 return state->completed ? +1 : 0;
3108 static int game_timing_state(const game_state *state, game_ui *ui)
3113 static void game_print_size(const game_params *params, float *x, float *y)
3117 /* 10mm squares by default. */
3118 game_compute_size(params, 1000, &pw, &ph);
3123 static void game_print(drawing *dr, const game_state *state, int ts)
3125 int ink = print_mono_colour(dr, 0);
3126 int paper = print_mono_colour(dr, 1);
3127 int x, y, cx, cy, i, nl;
3131 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
3132 game_drawstate ads, *ds = &ads;
3135 /* I don't think this wants a border. */
3138 loff = ts / (8 * sqrt((state->params.maxb - 1)));
3139 print_line_width(dr, ts / 12);
3140 for (x = 0; x < state->w; x++) {
3141 for (y = 0; y < state->h; y++) {
3142 cx = COORD(x); cy = COORD(y);
3143 grid = GRID(state,x,y);
3144 nl = INDEX(state,lines,x,y);
3146 if (grid & G_ISLAND) continue;
3147 if (grid & G_LINEV) {
3148 for (i = 0; i < nl; i++)
3149 draw_line(dr, cx+ts/2+(2*i-nl+1)*loff, cy,
3150 cx+ts/2+(2*i-nl+1)*loff, cy+ts, ink);
3152 if (grid & G_LINEH) {
3153 for (i = 0; i < nl; i++)
3154 draw_line(dr, cx, cy+ts/2+(2*i-nl+1)*loff,
3155 cx+ts, cy+ts/2+(2*i-nl+1)*loff, ink);
3161 for (i = 0; i < state->n_islands; i++) {
3163 struct island *is = &state->islands[i];
3164 grid = GRID(state, is->x, is->y);
3165 cx = COORD(is->x) + ts/2;
3166 cy = COORD(is->y) + ts/2;
3168 draw_circle(dr, cx, cy, ISLAND_RADIUS, paper, ink);
3170 sprintf(str, "%d", is->count);
3171 draw_text(dr, cx, cy, FONT_VARIABLE, ISLAND_NUMSIZE(is->count),
3172 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
3177 #define thegame bridges
3180 const struct game thegame = {
3181 "Bridges", "games.bridges", "bridges",
3188 TRUE, game_configure, custom_params,
3196 TRUE, game_can_format_as_text_now, game_text_format,
3204 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
3207 game_free_drawstate,
3212 TRUE, FALSE, game_print_size, game_print,
3213 FALSE, /* wants_statusbar */
3214 FALSE, game_timing_state,
3215 REQUIRE_RBUTTON, /* flags */
3218 /* vim: set shiftwidth=4 tabstop=8: */