2 * bridges.c: Implementation of the Nikoli game 'Bridges'.
6 * * write a recursive solver?
18 /* Turn this on for hints about which lines are considered possibilities. */
23 /* --- structures for params, state, etc. --- */
27 #define PREFERRED_TILE_SIZE 24
28 #define TILE_SIZE (ds->tilesize)
29 #define BORDER (TILE_SIZE / 2)
31 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
32 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
34 #define FLASH_TIME 0.50F
39 COL_HIGHLIGHT, COL_LOWLIGHT,
40 COL_SELECTED, COL_MARK,
48 int islands, expansion; /* %age of island squares, %age chance of expansion */
49 int allowloops, difficulty;
52 /* general flags used by all structs */
53 #define G_ISLAND 0x0001
54 #define G_LINEV 0x0002 /* contains a vert. line */
55 #define G_LINEH 0x0004 /* contains a horiz. line (mutex with LINEV) */
56 #define G_LINE (G_LINEV|G_LINEH)
57 #define G_MARKV 0x0008
58 #define G_MARKH 0x0010
59 #define G_MARK (G_MARKV|G_MARKH)
60 #define G_NOLINEV 0x0020
61 #define G_NOLINEH 0x0040
62 #define G_NOLINE (G_NOLINEV|G_NOLINEH)
64 /* flags used by the drawstate */
65 #define G_ISSEL 0x0080
66 #define G_REDRAW 0x0100
67 #define G_FLASH 0x0200
70 /* flags used by the solver etc. */
71 #define G_SWEEP 0x0800
73 #define G_FLAGSH (G_LINEH|G_MARKH|G_NOLINEH)
74 #define G_FLAGSV (G_LINEV|G_MARKV|G_NOLINEV)
76 typedef unsigned int grid_type; /* change me later if we invent > 16 bits of flags. */
83 /* state->gridi is an optimisation; it stores the pointer to the island
84 * structs indexed by (x,y). It's not strictly necessary (we could use
85 * find234 instead), but Purify showed that board generation (mostly the solver)
86 * was spending 60% of its time in find234. */
88 struct surrounds { /* cloned from lightup.c */
89 struct { int x, y, dx, dy, off; } points[4];
90 int npoints, nislands;
100 int w, h, completed, solved, allowloops, maxb;
101 grid_type *grid, *scratch;
102 struct island *islands;
103 int n_islands, n_islands_alloc;
104 game_params params; /* used by the aux solver. */
105 #define N_WH_ARRAYS 5
106 char *wha, *possv, *possh, *lines, *maxv, *maxh;
107 struct island **gridi;
108 struct solver_state *solver; /* refcounted */
111 #define GRIDSZ(s) ((s)->w * (s)->h * sizeof(grid_type))
113 #define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
115 #define DINDEX(x,y) ((y)*state->w + (x))
117 #define INDEX(s,g,x,y) ((s)->g[(y)*((s)->w) + (x)])
118 #define IDX(s,g,i) ((s)->g[(i)])
119 #define GRID(s,x,y) INDEX(s,grid,x,y)
120 #define SCRATCH(s,x,y) INDEX(s,scratch,x,y)
121 #define POSSIBLES(s,dx,x,y) ((dx) ? (INDEX(s,possh,x,y)) : (INDEX(s,possv,x,y)))
122 #define MAXIMUM(s,dx,x,y) ((dx) ? (INDEX(s,maxh,x,y)) : (INDEX(s,maxv,x,y)))
124 #define GRIDCOUNT(s,x,y,f) ((GRID(s,x,y) & (f)) ? (INDEX(s,lines,x,y)) : 0)
126 #define WITHIN2(x,min,max) (((x) < (min)) ? 0 : (((x) > (max)) ? 0 : 1))
127 #define WITHIN(x,min,max) ((min) > (max) ? \
128 WITHIN2(x,max,min) : WITHIN2(x,min,max))
130 /* --- island struct and tree support functions --- */
132 #define ISLAND_ORTH(is,j,f,df) \
133 (is->f + (is->adj.points[(j)].off*is->adj.points[(j)].df))
135 #define ISLAND_ORTHX(is,j) ISLAND_ORTH(is,j,x,dx)
136 #define ISLAND_ORTHY(is,j) ISLAND_ORTH(is,j,y,dy)
138 static void fixup_islands_for_realloc(game_state *state)
142 for (i = 0; i < state->w*state->h; i++) state->gridi[i] = NULL;
143 for (i = 0; i < state->n_islands; i++) {
144 struct island *is = &state->islands[i];
146 INDEX(state, gridi, is->x, is->y) = is;
150 static char *game_text_format(game_state *state)
157 len = (state->h) * (state->w+1) + 1;
158 ret = snewn(len, char);
161 for (y = 0; y < state->h; y++) {
162 for (x = 0; x < state->w; x++) {
163 grid = GRID(state,x,y);
164 nl = INDEX(state,lines,x,y);
165 is = INDEX(state, gridi, x, y);
167 *p++ = '0' + is->count;
168 } else if (grid & G_LINEV) {
169 *p++ = (nl > 1) ? '"' : (nl == 1) ? '|' : '!'; /* gaah, want a double-bar. */
170 } else if (grid & G_LINEH) {
171 *p++ = (nl > 1) ? '=' : (nl == 1) ? '-' : '~';
180 assert(p - ret == len);
184 static void debug_state(game_state *state)
186 char *textversion = game_text_format(state);
187 debug(("%s", textversion));
191 /*static void debug_possibles(game_state *state)
194 debug(("possh followed by possv\n"));
195 for (y = 0; y < state->h; y++) {
196 for (x = 0; x < state->w; x++) {
197 debug(("%d", POSSIBLES(state, 1, x, y)));
200 for (x = 0; x < state->w; x++) {
201 debug(("%d", POSSIBLES(state, 0, x, y)));
206 for (y = 0; y < state->h; y++) {
207 for (x = 0; x < state->w; x++) {
208 debug(("%d", MAXIMUM(state, 1, x, y)));
211 for (x = 0; x < state->w; x++) {
212 debug(("%d", MAXIMUM(state, 0, x, y)));
219 static void island_set_surrounds(struct island *is)
221 assert(INGRID(is->state,is->x,is->y));
222 is->adj.npoints = is->adj.nislands = 0;
223 #define ADDPOINT(cond,ddx,ddy) do {\
225 is->adj.points[is->adj.npoints].x = is->x+(ddx); \
226 is->adj.points[is->adj.npoints].y = is->y+(ddy); \
227 is->adj.points[is->adj.npoints].dx = (ddx); \
228 is->adj.points[is->adj.npoints].dy = (ddy); \
229 is->adj.points[is->adj.npoints].off = 0; \
232 ADDPOINT(is->x > 0, -1, 0);
233 ADDPOINT(is->x < (is->state->w-1), +1, 0);
234 ADDPOINT(is->y > 0, 0, -1);
235 ADDPOINT(is->y < (is->state->h-1), 0, +1);
238 static void island_find_orthogonal(struct island *is)
240 /* fills in the rest of the 'surrounds' structure, assuming
241 * all other islands are now in place. */
242 int i, x, y, dx, dy, off;
244 is->adj.nislands = 0;
245 for (i = 0; i < is->adj.npoints; i++) {
246 dx = is->adj.points[i].dx;
247 dy = is->adj.points[i].dy;
251 is->adj.points[i].off = 0;
252 while (INGRID(is->state, x, y)) {
253 if (GRID(is->state, x, y) & G_ISLAND) {
254 is->adj.points[i].off = off;
256 /*debug(("island (%d,%d) has orth is. %d*(%d,%d) away at (%d,%d).\n",
257 is->x, is->y, off, dx, dy,
258 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i)));*/
261 off++; x += dx; y += dy;
268 static int island_hasbridge(struct island *is, int direction)
270 int x = is->adj.points[direction].x;
271 int y = is->adj.points[direction].y;
272 grid_type gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
274 if (GRID(is->state, x, y) & gline) return 1;
278 static struct island *island_find_connection(struct island *is, int adjpt)
282 assert(adjpt < is->adj.npoints);
283 if (!is->adj.points[adjpt].off) return NULL;
284 if (!island_hasbridge(is, adjpt)) return NULL;
286 is_r = INDEX(is->state, gridi,
287 ISLAND_ORTHX(is, adjpt), ISLAND_ORTHY(is, adjpt));
293 static struct island *island_add(game_state *state, int x, int y, int count)
298 assert(!(GRID(state,x,y) & G_ISLAND));
299 GRID(state,x,y) |= G_ISLAND;
302 if (state->n_islands > state->n_islands_alloc) {
303 state->n_islands_alloc = state->n_islands * 2;
305 sresize(state->islands, state->n_islands_alloc, struct island);
308 is = &state->islands[state->n_islands-1];
310 memset(is, 0, sizeof(struct island));
315 island_set_surrounds(is);
318 fixup_islands_for_realloc(state);
320 INDEX(state, gridi, x, y) = is;
326 /* n = -1 means 'flip NOLINE flags [and set line to 0].' */
327 static void island_join(struct island *i1, struct island *i2, int n, int is_max)
329 game_state *state = i1->state;
332 assert(i1->state == i2->state);
333 assert(n >= -1 && n <= i1->state->maxb);
335 if (i1->x == i2->x) {
338 s = i1->y+1; e = i2->y-1;
340 s = i2->y+1; e = i1->y-1;
342 for (y = s; y <= e; y++) {
344 INDEX(state,maxv,x,y) = n;
347 GRID(state,x,y) ^= G_NOLINEV;
349 GRID(state,x,y) &= ~G_LINEV;
351 GRID(state,x,y) |= G_LINEV;
352 INDEX(state,lines,x,y) = n;
356 } else if (i1->y == i2->y) {
359 s = i1->x+1; e = i2->x-1;
361 s = i2->x+1; e = i1->x-1;
363 for (x = s; x <= e; x++) {
365 INDEX(state,maxh,x,y) = n;
368 GRID(state,x,y) ^= G_NOLINEH;
370 GRID(state,x,y) &= ~G_LINEH;
372 GRID(state,x,y) |= G_LINEH;
373 INDEX(state,lines,x,y) = n;
378 assert(!"island_join: islands not orthogonal.");
382 /* Counts the number of bridges currently attached to the island. */
383 static int island_countbridges(struct island *is)
387 for (i = 0; i < is->adj.npoints; i++) {
388 c += GRIDCOUNT(is->state,
389 is->adj.points[i].x, is->adj.points[i].y,
390 is->adj.points[i].dx ? G_LINEH : G_LINEV);
392 /*debug(("island count for (%d,%d) is %d.\n", is->x, is->y, c));*/
396 static int island_adjspace(struct island *is, int marks, int missing,
399 int x, y, poss, curr, dx;
400 grid_type gline, mline;
402 x = is->adj.points[direction].x;
403 y = is->adj.points[direction].y;
404 dx = is->adj.points[direction].dx;
405 gline = dx ? G_LINEH : G_LINEV;
408 mline = dx ? G_MARKH : G_MARKV;
409 if (GRID(is->state,x,y) & mline) return 0;
411 poss = POSSIBLES(is->state, dx, x, y);
412 poss = min(poss, missing);
414 curr = GRIDCOUNT(is->state, x, y, gline);
415 poss = min(poss, MAXIMUM(is->state, dx, x, y) - curr);
420 /* Counts the number of bridge spaces left around the island;
421 * expects the possibles to be up-to-date. */
422 static int island_countspaces(struct island *is, int marks)
424 int i, c = 0, missing;
426 missing = is->count - island_countbridges(is);
427 if (missing < 0) return 0;
429 for (i = 0; i < is->adj.npoints; i++) {
430 c += island_adjspace(is, marks, missing, i);
435 static int island_isadj(struct island *is, int direction)
438 grid_type gline, mline;
440 x = is->adj.points[direction].x;
441 y = is->adj.points[direction].y;
443 mline = is->adj.points[direction].dx ? G_MARKH : G_MARKV;
444 gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
445 if (GRID(is->state, x, y) & mline) {
446 /* If we're marked (i.e. the thing to attach to is complete)
447 * only count an adjacency if we're already attached. */
448 return GRIDCOUNT(is->state, x, y, gline);
450 /* If we're unmarked, count possible adjacency iff it's
451 * flagged as POSSIBLE. */
452 return POSSIBLES(is->state, is->adj.points[direction].dx, x, y);
457 /* Counts the no. of possible adjacent islands (including islands
458 * we're already connected to). */
459 static int island_countadj(struct island *is)
463 for (i = 0; i < is->adj.npoints; i++) {
464 if (island_isadj(is, i)) nadj++;
469 static void island_togglemark(struct island *is)
472 struct island *is_loop;
474 /* mark the island... */
475 GRID(is->state, is->x, is->y) ^= G_MARK;
477 /* ...remove all marks on non-island squares... */
478 for (x = 0; x < is->state->w; x++) {
479 for (y = 0; y < is->state->h; y++) {
480 if (!(GRID(is->state, x, y) & G_ISLAND))
481 GRID(is->state, x, y) &= ~G_MARK;
485 /* ...and add marks to squares around marked islands. */
486 for (i = 0; i < is->state->n_islands; i++) {
487 is_loop = &is->state->islands[i];
488 if (!(GRID(is_loop->state, is_loop->x, is_loop->y) & G_MARK))
491 for (j = 0; j < is_loop->adj.npoints; j++) {
492 /* if this direction takes us to another island, mark all
493 * squares between the two islands. */
494 if (!is_loop->adj.points[j].off) continue;
495 assert(is_loop->adj.points[j].off > 1);
496 for (o = 1; o < is_loop->adj.points[j].off; o++) {
498 is_loop->x + is_loop->adj.points[j].dx*o,
499 is_loop->y + is_loop->adj.points[j].dy*o) |=
500 is_loop->adj.points[j].dy ? G_MARKV : G_MARKH;
506 static int island_impossible(struct island *is, int strict)
508 int curr = island_countbridges(is), nspc = is->count - curr, nsurrspc;
511 struct island *is_orth;
514 debug(("island at (%d,%d) impossible because full.\n", is->x, is->y));
515 return 1; /* too many bridges */
516 } else if ((curr + island_countspaces(is, 0)) < is->count) {
517 debug(("island at (%d,%d) impossible because not enough spaces.\n", is->x, is->y));
518 return 1; /* impossible to create enough bridges */
519 } else if (strict && curr < is->count) {
520 debug(("island at (%d,%d) impossible because locked.\n", is->x, is->y));
521 return 1; /* not enough bridges and island is locked */
524 /* Count spaces in surrounding islands. */
526 for (i = 0; i < is->adj.npoints; i++) {
527 int ifree, dx = is->adj.points[i].dx;
529 if (!is->adj.points[i].off) continue;
530 v = GRID(is->state, is->adj.points[i].x, is->adj.points[i].y);
531 poss = POSSIBLES(is->state, dx,
532 is->adj.points[i].x, is->adj.points[i].y);
533 if (poss == 0) continue;
534 is_orth = INDEX(is->state, gridi,
535 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i));
538 ifree = is_orth->count - island_countbridges(is_orth);
540 nsurrspc += min(ifree, MAXIMUM(is->state, dx,
541 is->adj.points[i].x, is->adj.points[i].y));
543 if (nsurrspc < nspc) {
544 debug(("island at (%d,%d) impossible: surr. islands %d spc, need %d.\n",
545 is->x, is->y, nsurrspc, nspc));
546 return 1; /* not enough spaces around surrounding islands to fill this one. */
552 /* --- Game parameter functions --- */
554 #define DEFAULT_PRESET 0
556 const struct game_params bridges_presets[] = {
557 { 7, 7, 2, 30, 10, 1, 0 },
558 { 7, 7, 2, 30, 10, 1, 1 },
559 { 7, 7, 2, 30, 10, 1, 2 },
560 { 10, 10, 2, 30, 10, 1, 0 },
561 { 10, 10, 2, 30, 10, 1, 1 },
562 { 10, 10, 2, 30, 10, 1, 2 },
563 { 15, 15, 2, 30, 10, 1, 0 },
564 { 15, 15, 2, 30, 10, 1, 1 },
565 { 15, 15, 2, 30, 10, 1, 2 },
568 static game_params *default_params(void)
570 game_params *ret = snew(game_params);
571 *ret = bridges_presets[DEFAULT_PRESET];
576 static int game_fetch_preset(int i, char **name, game_params **params)
581 if (i < 0 || i >= lenof(bridges_presets))
584 ret = default_params();
585 *ret = bridges_presets[i];
588 sprintf(buf, "%dx%d %s", ret->w, ret->h,
589 ret->difficulty == 0 ? "easy" :
590 ret->difficulty == 1 ? "medium" : "hard");
596 static void free_params(game_params *params)
601 static game_params *dup_params(game_params *params)
603 game_params *ret = snew(game_params);
604 *ret = *params; /* structure copy */
608 #define EATNUM(x) do { \
609 (x) = atoi(string); \
610 while (*string && isdigit((unsigned char)*string)) string++; \
613 static void decode_params(game_params *params, char const *string)
616 params->h = params->w;
617 if (*string == 'x') {
621 if (*string == 'i') {
623 EATNUM(params->islands);
625 if (*string == 'e') {
627 EATNUM(params->expansion);
629 if (*string == 'm') {
631 EATNUM(params->maxb);
633 params->allowloops = 1;
634 if (*string == 'L') {
636 params->allowloops = 0;
638 if (*string == 'd') {
640 EATNUM(params->difficulty);
644 static char *encode_params(game_params *params, int full)
649 sprintf(buf, "%dx%di%de%dm%d%sd%d",
650 params->w, params->h, params->islands, params->expansion,
651 params->maxb, params->allowloops ? "" : "L",
654 sprintf(buf, "%dx%dm%d%s", params->w, params->h,
655 params->maxb, params->allowloops ? "" : "L");
660 static config_item *game_configure(game_params *params)
665 ret = snewn(8, config_item);
667 ret[0].name = "Width";
668 ret[0].type = C_STRING;
669 sprintf(buf, "%d", params->w);
670 ret[0].sval = dupstr(buf);
673 ret[1].name = "Height";
674 ret[1].type = C_STRING;
675 sprintf(buf, "%d", params->h);
676 ret[1].sval = dupstr(buf);
679 ret[2].name = "Difficulty";
680 ret[2].type = C_CHOICES;
681 ret[2].sval = ":Easy:Medium:Hard";
682 ret[2].ival = params->difficulty;
684 ret[3].name = "Allow loops";
685 ret[3].type = C_BOOLEAN;
687 ret[3].ival = params->allowloops;
689 ret[4].name = "Max. bridges per direction";
690 ret[4].type = C_CHOICES;
691 ret[4].sval = ":1:2:3:4"; /* keep up-to-date with MAX_BRIDGES */
692 ret[4].ival = params->maxb - 1;
694 ret[5].name = "%age of island squares";
695 ret[5].type = C_CHOICES;
696 ret[5].sval = ":5%:10%:15%:20%:25%:30%";
697 ret[5].ival = (params->islands / 5)-1;
699 ret[6].name = "Expansion factor (%age)";
700 ret[6].type = C_CHOICES;
701 ret[6].sval = ":0%:10%:20%:30%:40%:50%:60%:70%:80%:90%:100%";
702 ret[6].ival = params->expansion / 10;
712 static game_params *custom_params(config_item *cfg)
714 game_params *ret = snew(game_params);
716 ret->w = atoi(cfg[0].sval);
717 ret->h = atoi(cfg[1].sval);
718 ret->difficulty = cfg[2].ival;
719 ret->allowloops = cfg[3].ival;
720 ret->maxb = cfg[4].ival + 1;
721 ret->islands = (cfg[5].ival + 1) * 5;
722 ret->expansion = cfg[6].ival * 10;
727 static char *validate_params(game_params *params, int full)
729 if (params->w < 3 || params->h < 3)
730 return "Width and height must be at least 3";
731 if (params->maxb < 1 || params->maxb > MAX_BRIDGES)
732 return "Too many bridges.";
734 if (params->islands <= 0 || params->islands > 30)
735 return "%age of island squares must be between 1% and 30%";
736 if (params->expansion < 0 || params->expansion > 100)
737 return "Expansion factor must be between 0 and 100";
742 /* --- Game encoding and differences --- */
744 static char *encode_game(game_state *state)
747 int wh = state->w*state->h, run, x, y;
750 ret = snewn(wh + 1, char);
753 for (y = 0; y < state->h; y++) {
754 for (x = 0; x < state->w; x++) {
755 is = INDEX(state, gridi, x, y);
758 *p++ = ('a'-1) + run;
762 *p++ = '0' + is->count;
764 *p++ = 'A' + (is->count - 10);
767 *p++ = ('a'-1) + run;
775 *p++ = ('a'-1) + run;
779 assert(p - ret <= wh);
784 static char *game_state_diff(game_state *src, game_state *dest)
786 int movesize = 256, movelen = 0;
787 char *move = snewn(movesize, char), buf[80];
789 grid_type gline, nline;
790 struct island *is_s, *is_d, *is_orth;
792 #define APPEND do { \
793 if (movelen + len >= movesize) { \
794 movesize = movelen + len + 256; \
795 move = sresize(move, movesize, char); \
797 strcpy(move + movelen, buf); \
801 move[movelen++] = 'S';
802 move[movelen] = '\0';
804 assert(src->n_islands == dest->n_islands);
806 for (i = 0; i < src->n_islands; i++) {
807 is_s = &src->islands[i];
808 is_d = &dest->islands[i];
809 assert(is_s->x == is_d->x);
810 assert(is_s->y == is_d->y);
811 assert(is_s->adj.npoints == is_d->adj.npoints); /* more paranoia */
813 for (d = 0; d < is_s->adj.npoints; d++) {
814 if (is_s->adj.points[d].dx == -1 ||
815 is_s->adj.points[d].dy == -1) continue;
817 x = is_s->adj.points[d].x;
818 y = is_s->adj.points[d].y;
819 gline = is_s->adj.points[d].dx ? G_LINEH : G_LINEV;
820 nline = is_s->adj.points[d].dx ? G_NOLINEH : G_NOLINEV;
821 is_orth = INDEX(dest, gridi,
822 ISLAND_ORTHX(is_d, d), ISLAND_ORTHY(is_d, d));
824 if (GRIDCOUNT(src, x, y, gline) != GRIDCOUNT(dest, x, y, gline)) {
826 len = sprintf(buf, ";L%d,%d,%d,%d,%d",
827 is_s->x, is_s->y, is_orth->x, is_orth->y,
828 GRIDCOUNT(dest, x, y, gline));
831 if ((GRID(src,x,y) & nline) != (GRID(dest, x, y) & nline)) {
833 len = sprintf(buf, ";N%d,%d,%d,%d",
834 is_s->x, is_s->y, is_orth->x, is_orth->y);
838 if ((GRID(src, is_s->x, is_s->y) & G_MARK) !=
839 (GRID(dest, is_d->x, is_d->y) & G_MARK)) {
840 len = sprintf(buf, ";M%d,%d", is_s->x, is_s->y);
847 /* --- Game setup and solving utilities --- */
849 /* This function is optimised; a Quantify showed that lots of grid-generation time
850 * (>50%) was spent in here. Hence the IDX() stuff. */
852 static void map_update_possibles(game_state *state)
854 int x, y, s, e, bl, i, np, maxb, w = state->w, idx;
855 struct island *is_s = NULL, *is_f = NULL;
857 /* Run down vertical stripes [un]setting possv... */
858 for (x = 0; x < state->w; x++) {
862 /* Unset possible flags until we find an island. */
863 for (y = 0; y < state->h; y++) {
864 is_s = IDX(state, gridi, idx);
867 IDX(state, possv, idx) = 0;
870 for (; y < state->h; y++) {
871 is_f = IDX(state, gridi, idx);
874 maxb = IDX(state, maxv, idx);
875 np = min(maxb, min(is_s->count, is_f->count));
878 for (i = s; i <= e; i++) {
879 INDEX(state, possv, x, i) = bl ? 0 : np;
887 if (IDX(state,grid,idx) & (G_LINEH|G_NOLINEV)) bl = 1;
892 for (i = s; i <= e; i++)
893 INDEX(state, possv, x, i) = 0;
897 /* ...and now do horizontal stripes [un]setting possh. */
898 /* can we lose this clone'n'hack? */
899 for (y = 0; y < state->h; y++) {
903 for (x = 0; x < state->w; x++) {
904 is_s = IDX(state, gridi, idx);
907 IDX(state, possh, idx) = 0;
910 for (; x < state->w; x++) {
911 is_f = IDX(state, gridi, idx);
914 maxb = IDX(state, maxh, idx);
915 np = min(maxb, min(is_s->count, is_f->count));
918 for (i = s; i <= e; i++) {
919 INDEX(state, possh, i, y) = bl ? 0 : np;
927 if (IDX(state,grid,idx) & (G_LINEV|G_NOLINEH)) bl = 1;
932 for (i = s; i <= e; i++)
933 INDEX(state, possh, i, y) = 0;
938 static void map_count(game_state *state)
941 grid_type flag, grid;
944 for (i = 0; i < state->n_islands; i++) {
945 is = &state->islands[i];
947 for (n = 0; n < is->adj.npoints; n++) {
948 ax = is->adj.points[n].x;
949 ay = is->adj.points[n].y;
950 flag = (ax == is->x) ? G_LINEV : G_LINEH;
951 grid = GRID(state,ax,ay);
953 is->count += INDEX(state,lines,ax,ay);
959 static void map_find_orthogonal(game_state *state)
963 for (i = 0; i < state->n_islands; i++) {
964 island_find_orthogonal(&state->islands[i]);
968 static int grid_degree(game_state *state, int x, int y, int *nx_r, int *ny_r)
970 grid_type grid = SCRATCH(state, x, y), gline = grid & G_LINE;
972 int x1, y1, x2, y2, c = 0, i, nx, ny;
974 nx = ny = -1; /* placate optimiser */
975 is = INDEX(state, gridi, x, y);
977 for (i = 0; i < is->adj.npoints; i++) {
978 gline = is->adj.points[i].dx ? G_LINEH : G_LINEV;
981 is->adj.points[i].y) & gline) {
982 nx = is->adj.points[i].x;
983 ny = is->adj.points[i].y;
988 if (gline & G_LINEV) {
995 /* Non-island squares with edges in should never be pointing off the
996 * edge of the grid. */
997 assert(INGRID(state, x1, y1));
998 assert(INGRID(state, x2, y2));
999 if (SCRATCH(state, x1, y1) & (gline | G_ISLAND)) {
1000 nx = x1; ny = y1; c++;
1002 if (SCRATCH(state, x2, y2) & (gline | G_ISLAND)) {
1003 nx = x2; ny = y2; c++;
1007 assert(nx != -1 && ny != -1); /* paranoia */
1008 *nx_r = nx; *ny_r = ny;
1013 static int map_hasloops(game_state *state, int mark)
1015 int x, y, ox, oy, nx = 0, ny = 0, loop = 0;
1017 memcpy(state->scratch, state->grid, GRIDSZ(state));
1019 /* This algorithm is actually broken; if there are two loops connected
1020 * by bridges this will also highlight bridges. The correct algorithm
1021 * uses a dsf and a two-pass edge-detection algorithm (see check_correct
1022 * in slant.c); this is BALGE for now, especially since disallow-loops
1023 * is not the default for this puzzle. If we want to fix this later then
1024 * copy the alg in slant.c to the empty statement in map_group. */
1026 /* Remove all 1-degree edges. */
1027 for (y = 0; y < state->h; y++) {
1028 for (x = 0; x < state->w; x++) {
1030 while (grid_degree(state, ox, oy, &nx, &ny) == 1) {
1031 /*debug(("hasloops: removing 1-degree at (%d,%d).\n", ox, oy));*/
1032 SCRATCH(state, ox, oy) &= ~(G_LINE|G_ISLAND);
1037 /* Mark any remaining edges as G_WARN, if required. */
1038 for (x = 0; x < state->w; x++) {
1039 for (y = 0; y < state->h; y++) {
1040 if (GRID(state,x,y) & G_ISLAND) continue;
1042 if (SCRATCH(state, x, y) & G_LINE) {
1044 /*debug(("hasloops: marking loop square at (%d,%d).\n",
1046 GRID(state,x,y) |= G_WARN;
1049 return 1; /* short-cut as soon as we find one */
1052 GRID(state,x,y) &= ~G_WARN;
1059 static void map_group(game_state *state)
1061 int i, wh = state->w*state->h, d1, d2;
1063 int *dsf = state->solver->dsf;
1064 struct island *is, *is_join;
1066 /* Initialise dsf. */
1069 /* For each island, find connected islands right or down
1070 * and merge the dsf for the island squares as well as the
1071 * bridge squares. */
1072 for (x = 0; x < state->w; x++) {
1073 for (y = 0; y < state->h; y++) {
1074 GRID(state,x,y) &= ~(G_SWEEP|G_WARN); /* for group_full. */
1076 is = INDEX(state, gridi, x, y);
1079 for (i = 0; i < is->adj.npoints; i++) {
1080 /* only want right/down */
1081 if (is->adj.points[i].dx == -1 ||
1082 is->adj.points[i].dy == -1) continue;
1084 is_join = island_find_connection(is, i);
1085 if (!is_join) continue;
1087 d2 = DINDEX(is_join->x, is_join->y);
1088 if (dsf_canonify(dsf,d1) == dsf_canonify(dsf,d2)) {
1089 ; /* we have a loop. See comment in map_hasloops. */
1090 /* However, we still want to merge all squares joining
1091 * this side-that-makes-a-loop. */
1093 /* merge all squares between island 1 and island 2. */
1094 for (x2 = x; x2 <= is_join->x; x2++) {
1095 for (y2 = y; y2 <= is_join->y; y2++) {
1097 if (d1 != d2) dsf_merge(dsf,d1,d2);
1105 static int map_group_check(game_state *state, int canon, int warn,
1108 int *dsf = state->solver->dsf, nislands = 0;
1109 int x, y, i, allfull = 1;
1112 for (i = 0; i < state->n_islands; i++) {
1113 is = &state->islands[i];
1114 if (dsf_canonify(dsf, DINDEX(is->x,is->y)) != canon) continue;
1116 GRID(state, is->x, is->y) |= G_SWEEP;
1118 if (island_countbridges(is) != is->count)
1121 if (warn && allfull && nislands != state->n_islands) {
1122 /* we're full and this island group isn't the whole set.
1123 * Mark all squares with this dsf canon as ERR. */
1124 for (x = 0; x < state->w; x++) {
1125 for (y = 0; y < state->h; y++) {
1126 if (dsf_canonify(dsf, DINDEX(x,y)) == canon) {
1127 GRID(state,x,y) |= G_WARN;
1133 if (nislands_r) *nislands_r = nislands;
1137 static int map_group_full(game_state *state, int *ngroups_r)
1139 int *dsf = state->solver->dsf, ngroups = 0;
1143 /* NB this assumes map_group (or sth else) has cleared G_SWEEP. */
1145 for (i = 0; i < state->n_islands; i++) {
1146 is = &state->islands[i];
1147 if (GRID(state,is->x,is->y) & G_SWEEP) continue;
1150 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1155 *ngroups_r = ngroups;
1159 static int map_check(game_state *state)
1163 /* Check for loops, if necessary. */
1164 if (!state->allowloops) {
1165 if (map_hasloops(state, 1))
1169 /* Place islands into island groups and check for early
1170 * satisfied-groups. */
1171 map_group(state); /* clears WARN and SWEEP */
1172 if (map_group_full(state, &ngroups)) {
1173 if (ngroups == 1) return 1;
1178 static void map_clear(game_state *state)
1182 for (x = 0; x < state->w; x++) {
1183 for (y = 0; y < state->h; y++) {
1184 /* clear most flags; might want to be slightly more careful here. */
1185 GRID(state,x,y) &= G_ISLAND;
1190 static void solve_join(struct island *is, int direction, int n, int is_max)
1192 struct island *is_orth;
1193 int d1, d2, *dsf = is->state->solver->dsf;
1194 game_state *state = is->state; /* for DINDEX */
1196 is_orth = INDEX(is->state, gridi,
1197 ISLAND_ORTHX(is, direction),
1198 ISLAND_ORTHY(is, direction));
1200 /*debug(("...joining (%d,%d) to (%d,%d) with %d bridge(s).\n",
1201 is->x, is->y, is_orth->x, is_orth->y, n));*/
1202 island_join(is, is_orth, n, is_max);
1204 if (n > 0 && !is_max) {
1205 d1 = DINDEX(is->x, is->y);
1206 d2 = DINDEX(is_orth->x, is_orth->y);
1207 if (dsf_canonify(dsf, d1) != dsf_canonify(dsf, d2))
1208 dsf_merge(dsf, d1, d2);
1212 static int solve_fillone(struct island *is)
1216 debug(("solve_fillone for island (%d,%d).\n", is->x, is->y));
1218 for (i = 0; i < is->adj.npoints; i++) {
1219 if (island_isadj(is, i)) {
1220 if (island_hasbridge(is, i)) {
1221 /* already attached; do nothing. */;
1223 solve_join(is, i, 1, 0);
1231 static int solve_fill(struct island *is)
1233 /* for each unmarked adjacent, make sure we convert every possible bridge
1234 * to a real one, and then work out the possibles afresh. */
1235 int i, nnew, ncurr, nadded = 0, missing;
1237 debug(("solve_fill for island (%d,%d).\n", is->x, is->y));
1239 missing = is->count - island_countbridges(is);
1240 if (missing < 0) return 0;
1242 /* very like island_countspaces. */
1243 for (i = 0; i < is->adj.npoints; i++) {
1244 nnew = island_adjspace(is, 1, missing, i);
1246 ncurr = GRIDCOUNT(is->state,
1247 is->adj.points[i].x, is->adj.points[i].y,
1248 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1250 solve_join(is, i, nnew + ncurr, 0);
1257 static int solve_island_stage1(struct island *is, int *didsth_r)
1259 int bridges = island_countbridges(is);
1260 int nspaces = island_countspaces(is, 1);
1261 int nadj = island_countadj(is);
1266 /*debug(("island at (%d,%d) filled %d/%d (%d spc) nadj %d\n",
1267 is->x, is->y, bridges, is->count, nspaces, nadj));*/
1268 if (bridges > is->count) {
1269 /* We only ever add bridges when we're sure they fit, or that's
1270 * the only place they can go. If we've added bridges such that
1271 * another island has become wrong, the puzzle must not have had
1273 debug(("...island at (%d,%d) is overpopulated!\n", is->x, is->y));
1275 } else if (bridges == is->count) {
1276 /* This island is full. Make sure it's marked (and update
1277 * possibles if we did). */
1278 if (!(GRID(is->state, is->x, is->y) & G_MARK)) {
1279 debug(("...marking island (%d,%d) as full.\n", is->x, is->y));
1280 island_togglemark(is);
1283 } else if (GRID(is->state, is->x, is->y) & G_MARK) {
1284 debug(("...island (%d,%d) is marked but unfinished!\n",
1286 return 0; /* island has been marked unfinished; no solution from here. */
1288 /* This is the interesting bit; we try and fill in more information
1289 * about this island. */
1290 if (is->count == bridges + nspaces) {
1291 if (solve_fill(is) > 0) didsth = 1;
1292 } else if (is->count > ((nadj-1) * is->state->maxb)) {
1293 /* must have at least one bridge in each possible direction. */
1294 if (solve_fillone(is) > 0) didsth = 1;
1298 map_update_possibles(is->state);
1304 /* returns non-zero if a new line here would cause a loop. */
1305 static int solve_island_checkloop(struct island *is, int direction)
1307 struct island *is_orth;
1308 int *dsf = is->state->solver->dsf, d1, d2;
1309 game_state *state = is->state;
1311 if (is->state->allowloops) return 0; /* don't care anyway */
1312 if (island_hasbridge(is, direction)) return 0; /* already has a bridge */
1313 if (island_isadj(is, direction) == 0) return 0; /* no adj island */
1315 is_orth = INDEX(is->state, gridi,
1316 ISLAND_ORTHX(is,direction),
1317 ISLAND_ORTHY(is,direction));
1318 if (!is_orth) return 0;
1320 d1 = DINDEX(is->x, is->y);
1321 d2 = DINDEX(is_orth->x, is_orth->y);
1322 if (dsf_canonify(dsf, d1) == dsf_canonify(dsf, d2)) {
1323 /* two islands are connected already; don't join them. */
1329 static int solve_island_stage2(struct island *is, int *didsth_r)
1331 int added = 0, removed = 0, navail = 0, nadj, i;
1335 for (i = 0; i < is->adj.npoints; i++) {
1336 if (solve_island_checkloop(is, i)) {
1337 debug(("removing possible loop at (%d,%d) direction %d.\n",
1339 solve_join(is, i, -1, 0);
1340 map_update_possibles(is->state);
1343 navail += island_isadj(is, i);
1344 /*debug(("stage2: navail for (%d,%d) direction (%d,%d) is %d.\n",
1346 is->adj.points[i].dx, is->adj.points[i].dy,
1347 island_isadj(is, i)));*/
1351 /*debug(("island at (%d,%d) navail %d: checking...\n", is->x, is->y, navail));*/
1353 for (i = 0; i < is->adj.npoints; i++) {
1354 if (!island_hasbridge(is, i)) {
1355 nadj = island_isadj(is, i);
1356 if (nadj > 0 && (navail - nadj) < is->count) {
1357 /* we couldn't now complete the island without at
1358 * least one bridge here; put it in. */
1359 /*debug(("nadj %d, navail %d, is->count %d.\n",
1360 nadj, navail, is->count));*/
1361 debug(("island at (%d,%d) direction (%d,%d) must have 1 bridge\n",
1363 is->adj.points[i].dx, is->adj.points[i].dy));
1364 solve_join(is, i, 1, 0);
1366 /*debug_state(is->state);
1367 debug_possibles(is->state);*/
1371 if (added) map_update_possibles(is->state);
1372 if (added || removed) *didsth_r = 1;
1376 static int solve_island_subgroup(struct island *is, int direction, int n)
1378 struct island *is_join;
1379 int nislands, *dsf = is->state->solver->dsf;
1380 game_state *state = is->state;
1382 debug(("..checking subgroups.\n"));
1384 /* if is isn't full, return 0. */
1385 if (n < is->count) {
1386 debug(("...orig island (%d,%d) not full.\n", is->x, is->y));
1390 is_join = INDEX(state, gridi,
1391 ISLAND_ORTHX(is, direction),
1392 ISLAND_ORTHY(is, direction));
1395 /* if is_join isn't full, return 0. */
1396 if (island_countbridges(is_join) < is_join->count) {
1397 debug(("...dest island (%d,%d) not full.\n", is_join->x, is_join->y));
1401 /* Check group membership for is->dsf; if it's full return 1. */
1402 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1404 if (nislands < state->n_islands) {
1405 /* we have a full subgroup that isn't the whole set.
1406 * This isn't allowed. */
1407 debug(("island at (%d,%d) makes full subgroup, disallowing.\n",
1411 debug(("...has finished puzzle.\n"));
1417 static int solve_island_impossible(game_state *state)
1422 /* If any islands are impossible, return 1. */
1423 for (i = 0; i < state->n_islands; i++) {
1424 is = &state->islands[i];
1425 if (island_impossible(is, 0)) {
1426 debug(("island at (%d,%d) has become impossible, disallowing.\n",
1434 /* Bear in mind that this function is really rather inefficient. */
1435 static int solve_island_stage3(struct island *is, int *didsth_r)
1437 int i, n, x, y, missing, spc, curr, maxb, didsth = 0;
1438 int wh = is->state->w * is->state->h;
1439 struct solver_state *ss = is->state->solver;
1443 missing = is->count - island_countbridges(is);
1444 if (missing <= 0) return 1;
1446 for (i = 0; i < is->adj.npoints; i++) {
1447 /* We only do right- or down-pointing bridges. */
1448 if (is->adj.points[i].dx == -1 ||
1449 is->adj.points[i].dy == -1) continue;
1451 x = is->adj.points[i].x;
1452 y = is->adj.points[i].y;
1453 spc = island_adjspace(is, 1, missing, i);
1454 if (spc == 0) continue;
1456 curr = GRIDCOUNT(is->state, x, y,
1457 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1458 debug(("island at (%d,%d) s3, trying %d - %d bridges.\n",
1459 is->x, is->y, curr+1, curr+spc));
1461 /* Now we know that this island could have more bridges,
1462 * to bring the total from curr+1 to curr+spc. */
1464 /* We have to squirrel the dsf away and restore it afterwards;
1465 * it is additive only, and can't be removed from. */
1466 memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int));
1467 for (n = curr+1; n <= curr+spc; n++) {
1468 solve_join(is, i, n, 0);
1469 map_update_possibles(is->state);
1471 if (solve_island_subgroup(is, i, n) ||
1472 solve_island_impossible(is->state)) {
1474 debug(("island at (%d,%d) d(%d,%d) new max of %d bridges:\n",
1476 is->adj.points[i].dx, is->adj.points[i].dy,
1481 solve_join(is, i, curr, 0); /* put back to before. */
1482 memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int));
1485 /*debug_state(is->state);*/
1487 debug(("...adding NOLINE.\n"));
1488 solve_join(is, i, -1, 0); /* we can't have any bridges here. */
1491 debug(("...setting maximum\n"));
1492 solve_join(is, i, maxb, 1);
1495 map_update_possibles(is->state);
1497 if (didsth) *didsth_r = didsth;
1501 #define CONTINUE_IF_FULL do { \
1502 if (GRID(state, is->x, is->y) & G_MARK) { \
1503 /* island full, don't try fixing it */ \
1507 static int solve_sub(game_state *state, int difficulty, int depth)
1515 /* First island iteration: things we can work out by looking at
1516 * properties of the island as a whole. */
1517 for (i = 0; i < state->n_islands; i++) {
1518 is = &state->islands[i];
1519 if (!solve_island_stage1(is, &didsth)) return 0;
1521 if (didsth) continue;
1522 else if (difficulty < 1) break;
1524 /* Second island iteration: thing we can work out by looking at
1525 * properties of individual island connections. */
1526 for (i = 0; i < state->n_islands; i++) {
1527 is = &state->islands[i];
1529 if (!solve_island_stage2(is, &didsth)) return 0;
1531 if (didsth) continue;
1532 else if (difficulty < 2) break;
1534 /* Third island iteration: things we can only work out by looking
1535 * at groups of islands. */
1536 for (i = 0; i < state->n_islands; i++) {
1537 is = &state->islands[i];
1538 if (!solve_island_stage3(is, &didsth)) return 0;
1540 if (didsth) continue;
1541 else if (difficulty < 3) break;
1543 /* If we can be bothered, write a recursive solver to finish here. */
1546 if (map_check(state)) return 1; /* solved it */
1550 static void solve_for_hint(game_state *state)
1553 solve_sub(state, 10, 0);
1556 static int solve_from_scratch(game_state *state, int difficulty)
1560 map_update_possibles(state);
1561 return solve_sub(state, difficulty, 0);
1564 /* --- New game functions --- */
1566 static game_state *new_state(game_params *params)
1568 game_state *ret = snew(game_state);
1569 int wh = params->w * params->h, i;
1573 ret->allowloops = params->allowloops;
1574 ret->maxb = params->maxb;
1575 ret->params = *params;
1577 ret->grid = snewn(wh, grid_type);
1578 memset(ret->grid, 0, GRIDSZ(ret));
1579 ret->scratch = snewn(wh, grid_type);
1580 memset(ret->scratch, 0, GRIDSZ(ret));
1582 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1583 memset(ret->wha, 0, wh*N_WH_ARRAYS*sizeof(char));
1585 ret->possv = ret->wha;
1586 ret->possh = ret->wha + wh;
1587 ret->lines = ret->wha + wh*2;
1588 ret->maxv = ret->wha + wh*3;
1589 ret->maxh = ret->wha + wh*4;
1591 memset(ret->maxv, ret->maxb, wh*sizeof(char));
1592 memset(ret->maxh, ret->maxb, wh*sizeof(char));
1594 ret->islands = NULL;
1596 ret->n_islands_alloc = 0;
1598 ret->gridi = snewn(wh, struct island *);
1599 for (i = 0; i < wh; i++) ret->gridi[i] = NULL;
1601 ret->solved = ret->completed = 0;
1603 ret->solver = snew(struct solver_state);
1604 ret->solver->dsf = snew_dsf(wh);
1605 ret->solver->tmpdsf = snewn(wh, int);
1607 ret->solver->refcount = 1;
1612 static game_state *dup_game(game_state *state)
1614 game_state *ret = snew(game_state);
1615 int wh = state->w*state->h;
1619 ret->allowloops = state->allowloops;
1620 ret->maxb = state->maxb;
1621 ret->params = state->params;
1623 ret->grid = snewn(wh, grid_type);
1624 memcpy(ret->grid, state->grid, GRIDSZ(ret));
1625 ret->scratch = snewn(wh, grid_type);
1626 memcpy(ret->scratch, state->scratch, GRIDSZ(ret));
1628 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1629 memcpy(ret->wha, state->wha, wh*N_WH_ARRAYS*sizeof(char));
1631 ret->possv = ret->wha;
1632 ret->possh = ret->wha + wh;
1633 ret->lines = ret->wha + wh*2;
1634 ret->maxv = ret->wha + wh*3;
1635 ret->maxh = ret->wha + wh*4;
1637 ret->islands = snewn(state->n_islands, struct island);
1638 memcpy(ret->islands, state->islands, state->n_islands * sizeof(struct island));
1639 ret->n_islands = ret->n_islands_alloc = state->n_islands;
1641 ret->gridi = snewn(wh, struct island *);
1642 fixup_islands_for_realloc(ret);
1644 ret->solved = state->solved;
1645 ret->completed = state->completed;
1647 ret->solver = state->solver;
1648 ret->solver->refcount++;
1653 static void free_game(game_state *state)
1655 if (--state->solver->refcount <= 0) {
1656 sfree(state->solver->dsf);
1657 sfree(state->solver->tmpdsf);
1658 sfree(state->solver);
1661 sfree(state->islands);
1662 sfree(state->gridi);
1666 sfree(state->scratch);
1671 #define MAX_NEWISLAND_TRIES 50
1673 #define ORDER(a,b) do { if (a < b) { int tmp=a; int a=b; int b=tmp; } } while(0)
1675 static char *new_game_desc(game_params *params, random_state *rs,
1676 char **aux, int interactive)
1678 game_state *tobuild = NULL;
1679 int i, j, wh = params->w * params->h, x, y, dx, dy;
1680 int minx, miny, maxx, maxy, joinx, joiny, newx, newy, diffx, diffy;
1681 int ni_req = max((params->islands * wh) / 100, 2), ni_curr, ni_bad;
1682 struct island *is, *is2;
1684 unsigned int echeck;
1686 /* pick a first island position randomly. */
1688 if (tobuild) free_game(tobuild);
1689 tobuild = new_state(params);
1691 x = random_upto(rs, params->w);
1692 y = random_upto(rs, params->h);
1693 island_add(tobuild, x, y, 0);
1696 debug(("Created initial island at (%d,%d).\n", x, y));
1698 while (ni_curr < ni_req) {
1699 /* Pick a random island to try and extend from. */
1700 i = random_upto(rs, tobuild->n_islands);
1701 is = &tobuild->islands[i];
1703 /* Pick a random direction to extend in. */
1704 j = random_upto(rs, is->adj.npoints);
1705 dx = is->adj.points[j].x - is->x;
1706 dy = is->adj.points[j].y - is->y;
1708 /* Find out limits of where we could put a new island. */
1710 minx = is->x + 2*dx; miny = is->y + 2*dy; /* closest is 2 units away. */
1711 x = is->x+dx; y = is->y+dy;
1712 if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1713 /* already a line next to the island, continue. */
1717 if (x < 0 || x >= params->w || y < 0 || y >= params->h) {
1718 /* got past the edge; put a possible at the island
1720 maxx = x-dx; maxy = y-dy;
1723 if (GRID(tobuild,x,y) & G_ISLAND) {
1724 /* could join up to an existing island... */
1725 joinx = x; joiny = y;
1726 /* ... or make a new one 2 spaces away. */
1727 maxx = x - 2*dx; maxy = y - 2*dy;
1729 } else if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1730 /* could make a new one 1 space away from the line. */
1731 maxx = x - dx; maxy = y - dy;
1738 debug(("Island at (%d,%d) with d(%d,%d) has new positions "
1739 "(%d,%d) -> (%d,%d), join (%d,%d).\n",
1740 is->x, is->y, dx, dy, minx, miny, maxx, maxy, joinx, joiny));
1741 /* Now we know where we could either put a new island
1742 * (between min and max), or (if loops are allowed) could join on
1743 * to an existing island (at join). */
1744 if (params->allowloops && joinx != -1 && joiny != -1) {
1745 if (random_upto(rs, 100) < (unsigned long)params->expansion) {
1746 is2 = INDEX(tobuild, gridi, joinx, joiny);
1747 debug(("Joining island at (%d,%d) to (%d,%d).\n",
1748 is->x, is->y, is2->x, is2->y));
1752 diffx = (maxx - minx) * dx;
1753 diffy = (maxy - miny) * dy;
1754 if (diffx < 0 || diffy < 0) goto bad;
1755 if (random_upto(rs,100) < (unsigned long)params->expansion) {
1756 newx = maxx; newy = maxy;
1757 debug(("Creating new island at (%d,%d) (expanded).\n", newx, newy));
1759 newx = minx + random_upto(rs,diffx+1)*dx;
1760 newy = miny + random_upto(rs,diffy+1)*dy;
1761 debug(("Creating new island at (%d,%d).\n", newx, newy));
1763 /* check we're not next to island in the other orthogonal direction. */
1764 if ((INGRID(tobuild,newx+dy,newy+dx) && (GRID(tobuild,newx+dy,newy+dx) & G_ISLAND)) ||
1765 (INGRID(tobuild,newx-dy,newy-dx) && (GRID(tobuild,newx-dy,newy-dx) & G_ISLAND))) {
1766 debug(("New location is adjacent to island, skipping.\n"));
1769 is2 = island_add(tobuild, newx, newy, 0);
1770 /* Must get is again at this point; the array might have
1771 * been realloced by island_add... */
1772 is = &tobuild->islands[i]; /* ...but order will not change. */
1774 ni_curr++; ni_bad = 0;
1776 island_join(is, is2, random_upto(rs, tobuild->maxb)+1, 0);
1777 debug_state(tobuild);
1782 if (ni_bad > MAX_NEWISLAND_TRIES) {
1783 debug(("Unable to create any new islands after %d tries; "
1784 "created %d [%d%%] (instead of %d [%d%%] requested).\n",
1785 MAX_NEWISLAND_TRIES,
1786 ni_curr, ni_curr * 100 / wh,
1787 ni_req, ni_req * 100 / wh));
1794 debug(("Only generated one island (!), retrying.\n"));
1797 /* Check we have at least one island on each extremity of the grid. */
1799 for (x = 0; x < params->w; x++) {
1800 if (INDEX(tobuild, gridi, x, 0)) echeck |= 1;
1801 if (INDEX(tobuild, gridi, x, params->h-1)) echeck |= 2;
1803 for (y = 0; y < params->h; y++) {
1804 if (INDEX(tobuild, gridi, 0, y)) echeck |= 4;
1805 if (INDEX(tobuild, gridi, params->w-1, y)) echeck |= 8;
1808 debug(("Generated grid doesn't fill to sides, retrying.\n"));
1813 map_find_orthogonal(tobuild);
1815 if (params->difficulty > 0) {
1816 if (solve_from_scratch(tobuild, params->difficulty-1) > 0) {
1817 debug(("Grid is solvable at difficulty %d (too easy); retrying.\n",
1818 params->difficulty-1));
1823 if (solve_from_scratch(tobuild, params->difficulty) == 0) {
1824 debug(("Grid not solvable at difficulty %d, (too hard); retrying.\n",
1825 params->difficulty));
1829 /* ... tobuild is now solved. We rely on this making the diff for aux. */
1830 debug_state(tobuild);
1831 ret = encode_game(tobuild);
1833 game_state *clean = dup_game(tobuild);
1835 map_update_possibles(clean);
1836 *aux = game_state_diff(clean, tobuild);
1844 static char *validate_desc(game_params *params, char *desc)
1846 int i, wh = params->w * params->h;
1848 for (i = 0; i < wh; i++) {
1849 if (*desc >= '1' && *desc <= '9')
1851 else if (*desc >= 'a' && *desc <= 'z')
1852 i += *desc - 'a'; /* plus the i++ */
1853 else if (*desc >= 'A' && *desc <= 'G')
1855 else if (*desc == 'V' || *desc == 'W' ||
1856 *desc == 'X' || *desc == 'Y' ||
1857 *desc == 'H' || *desc == 'I' ||
1858 *desc == 'J' || *desc == 'K')
1861 return "Game description shorter than expected";
1863 return "Game description containers unexpected character";
1866 if (*desc || i > wh)
1867 return "Game description longer than expected";
1872 static game_state *new_game_sub(game_params *params, char *desc)
1874 game_state *state = new_state(params);
1877 debug(("new_game[_sub]: desc = '%s'.\n", desc));
1879 for (y = 0; y < params->h; y++) {
1880 for (x = 0; x < params->w; x++) {
1886 if (c >= 'a' && c <= 'z')
1896 case '1': case '2': case '3': case '4':
1897 case '5': case '6': case '7': case '8': case '9':
1898 island_add(state, x, y, (c - '0'));
1901 case 'A': case 'B': case 'C': case 'D':
1902 case 'E': case 'F': case 'G':
1903 island_add(state, x, y, (c - 'A') + 10);
1911 assert(!"Malformed desc.");
1916 if (*desc) assert(!"Over-long desc.");
1918 map_find_orthogonal(state);
1919 map_update_possibles(state);
1924 static game_state *new_game(midend *me, game_params *params, char *desc)
1926 return new_game_sub(params, desc);
1930 int dragx_src, dragy_src; /* source; -1 means no drag */
1931 int dragx_dst, dragy_dst; /* src's closest orth island. */
1933 int dragging, drag_is_noline, nlines;
1936 static char *ui_cancel_drag(game_ui *ui)
1938 ui->dragx_src = ui->dragy_src = -1;
1939 ui->dragx_dst = ui->dragy_dst = -1;
1944 static game_ui *new_ui(game_state *state)
1946 game_ui *ui = snew(game_ui);
1951 static void free_ui(game_ui *ui)
1956 static char *encode_ui(game_ui *ui)
1961 static void decode_ui(game_ui *ui, char *encoding)
1965 static void game_changed_state(game_ui *ui, game_state *oldstate,
1966 game_state *newstate)
1970 struct game_drawstate {
1975 int started, dragging;
1978 static char *update_drag_dst(game_state *state, game_ui *ui, game_drawstate *ds,
1981 int ox, oy, dx, dy, i, currl, maxb;
1983 grid_type gtype, ntype, mtype, curr;
1985 if (ui->dragx_src == -1 || ui->dragy_src == -1) return NULL;
1990 /* work out which of the four directions we're closest to... */
1991 ox = COORD(ui->dragx_src) + TILE_SIZE/2;
1992 oy = COORD(ui->dragy_src) + TILE_SIZE/2;
1994 if (abs(nx-ox) < abs(ny-oy)) {
1996 dy = (ny-oy) < 0 ? -1 : 1;
1997 gtype = G_LINEV; ntype = G_NOLINEV; mtype = G_MARKV;
1998 maxb = INDEX(state, maxv, ui->dragx_src+dx, ui->dragy_src+dy);
2001 dx = (nx-ox) < 0 ? -1 : 1;
2002 gtype = G_LINEH; ntype = G_NOLINEH; mtype = G_MARKH;
2003 maxb = INDEX(state, maxh, ui->dragx_src+dx, ui->dragy_src+dy);
2005 if (ui->drag_is_noline) {
2008 curr = GRID(state, ui->dragx_src+dx, ui->dragy_src+dy);
2009 currl = INDEX(state, lines, ui->dragx_src+dx, ui->dragy_src+dy);
2012 if (currl == maxb) {
2017 ui->nlines = currl + 1;
2025 /* ... and see if there's an island off in that direction. */
2026 is = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2027 for (i = 0; i < is->adj.npoints; i++) {
2028 if (is->adj.points[i].off == 0) continue;
2029 curr = GRID(state, is->x+dx, is->y+dy);
2030 if (curr & mtype) continue; /* don't allow changes to marked lines. */
2031 if (ui->drag_is_noline) {
2032 if (curr & gtype) continue; /* no no-line where already a line */
2034 if (POSSIBLES(state, dx, is->x+dx, is->y+dy) == 0) continue; /* no line if !possible. */
2035 if (curr & ntype) continue; /* can't have a bridge where there's a no-line. */
2038 if (is->adj.points[i].dx == dx &&
2039 is->adj.points[i].dy == dy) {
2040 ui->dragx_dst = ISLAND_ORTHX(is,i);
2041 ui->dragy_dst = ISLAND_ORTHY(is,i);
2044 /*debug(("update_drag src (%d,%d) d(%d,%d) dst (%d,%d)\n",
2045 ui->dragx_src, ui->dragy_src, dx, dy,
2046 ui->dragx_dst, ui->dragy_dst));*/
2050 static char *finish_drag(game_state *state, game_ui *ui)
2054 if (ui->dragx_src == -1 || ui->dragy_src == -1)
2056 if (ui->dragx_dst == -1 || ui->dragy_dst == -1)
2057 return ui_cancel_drag(ui);
2059 if (ui->drag_is_noline) {
2060 sprintf(buf, "N%d,%d,%d,%d",
2061 ui->dragx_src, ui->dragy_src,
2062 ui->dragx_dst, ui->dragy_dst);
2064 sprintf(buf, "L%d,%d,%d,%d,%d",
2065 ui->dragx_src, ui->dragy_src,
2066 ui->dragx_dst, ui->dragy_dst, ui->nlines);
2074 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2075 int x, int y, int button)
2077 int gx = FROMCOORD(x), gy = FROMCOORD(y);
2079 grid_type ggrid = INGRID(state,gx,gy) ? GRID(state,gx,gy) : 0;
2081 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2082 if (!INGRID(state, gx, gy)) return NULL;
2083 if ((ggrid & G_ISLAND) && !(ggrid & G_MARK)) {
2088 return ui_cancel_drag(ui);
2089 } else if (button == LEFT_DRAG || button == RIGHT_DRAG) {
2090 if (gx != ui->dragx_src || gy != ui->dragy_src) {
2092 ui->drag_is_noline = (button == RIGHT_DRAG) ? 1 : 0;
2093 return update_drag_dst(state, ui, ds, x, y);
2095 /* cancel a drag when we go back to the starting point */
2100 } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) {
2102 return finish_drag(state, ui);
2105 if (!INGRID(state, gx, gy)) return NULL;
2106 if (!(GRID(state, gx, gy) & G_ISLAND)) return NULL;
2107 sprintf(buf, "M%d,%d", gx, gy);
2110 } else if (button == 'h' || button == 'H') {
2111 game_state *solved = dup_game(state);
2112 solve_for_hint(solved);
2113 ret = game_state_diff(state, solved);
2121 static game_state *execute_move(game_state *state, char *move)
2123 game_state *ret = dup_game(state);
2124 int x1, y1, x2, y2, nl, n;
2125 struct island *is1, *is2;
2128 debug(("execute_move: %s\n", move));
2130 if (!*move) goto badmove;
2136 } else if (c == 'L') {
2137 if (sscanf(move, "%d,%d,%d,%d,%d%n",
2138 &x1, &y1, &x2, &y2, &nl, &n) != 5)
2140 is1 = INDEX(ret, gridi, x1, y1);
2141 is2 = INDEX(ret, gridi, x2, y2);
2142 if (!is1 || !is2) goto badmove;
2143 if (nl < 0 || nl > state->maxb) goto badmove;
2144 island_join(is1, is2, nl, 0);
2145 } else if (c == 'N') {
2146 if (sscanf(move, "%d,%d,%d,%d%n",
2147 &x1, &y1, &x2, &y2, &n) != 4)
2149 is1 = INDEX(ret, gridi, x1, y1);
2150 is2 = INDEX(ret, gridi, x2, y2);
2151 if (!is1 || !is2) goto badmove;
2152 island_join(is1, is2, -1, 0);
2153 } else if (c == 'M') {
2154 if (sscanf(move, "%d,%d%n",
2157 is1 = INDEX(ret, gridi, x1, y1);
2158 if (!is1) goto badmove;
2159 island_togglemark(is1);
2166 else if (*move) goto badmove;
2169 map_update_possibles(ret);
2170 if (map_check(ret)) {
2171 debug(("Game completed.\n"));
2177 debug(("%s: unrecognised move.\n", move));
2182 static char *solve_game(game_state *state, game_state *currstate,
2183 char *aux, char **error)
2189 debug(("solve_game: aux = %s\n", aux));
2190 solved = execute_move(state, aux);
2192 *error = "Generated aux string is not a valid move (!).";
2196 solved = dup_game(state);
2197 /* solve with max strength... */
2198 if (solve_from_scratch(solved, 10) == 0) {
2200 *error = "Game does not have a (non-recursive) solution.";
2204 ret = game_state_diff(currstate, solved);
2206 debug(("solve_game: ret = %s\n", ret));
2210 /* ----------------------------------------------------------------------
2214 static void game_compute_size(game_params *params, int tilesize,
2217 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2218 struct { int tilesize; } ads, *ds = &ads;
2219 ads.tilesize = tilesize;
2221 *x = TILE_SIZE * params->w + 2 * BORDER;
2222 *y = TILE_SIZE * params->h + 2 * BORDER;
2225 static void game_set_size(drawing *dr, game_drawstate *ds,
2226 game_params *params, int tilesize)
2228 ds->tilesize = tilesize;
2231 static float *game_colours(frontend *fe, int *ncolours)
2233 float *ret = snewn(3 * NCOLOURS, float);
2236 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
2238 for (i = 0; i < 3; i++) {
2239 ret[COL_FOREGROUND * 3 + i] = 0.0F;
2240 ret[COL_HINT * 3 + i] = ret[COL_LOWLIGHT * 3 + i];
2241 ret[COL_GRID * 3 + i] =
2242 (ret[COL_HINT * 3 + i] + ret[COL_BACKGROUND * 3 + i]) * 0.5F;
2243 ret[COL_MARK * 3 + i] = ret[COL_HIGHLIGHT * 3 + i];
2245 ret[COL_WARNING * 3 + 0] = 1.0F;
2246 ret[COL_WARNING * 3 + 1] = 0.25F;
2247 ret[COL_WARNING * 3 + 2] = 0.25F;
2249 ret[COL_SELECTED * 3 + 0] = 0.25F;
2250 ret[COL_SELECTED * 3 + 1] = 1.00F;
2251 ret[COL_SELECTED * 3 + 2] = 0.25F;
2253 *ncolours = NCOLOURS;
2257 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2259 struct game_drawstate *ds = snew(struct game_drawstate);
2260 int wh = state->w*state->h;
2266 ds->grid = snewn(wh, grid_type);
2267 memset(ds->grid, -1, wh*sizeof(grid_type));
2268 ds->lv = snewn(wh, int);
2269 ds->lh = snewn(wh, int);
2270 memset(ds->lv, 0, wh*sizeof(int));
2271 memset(ds->lh, 0, wh*sizeof(int));
2276 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2284 #define LINE_WIDTH (TILE_SIZE/8)
2285 #define TS8(x) (((x)*TILE_SIZE)/8)
2287 #define OFFSET(thing) ((TILE_SIZE/2) - ((thing)/2))
2289 static void lines_vert(drawing *dr, game_drawstate *ds,
2290 int ox, int oy, int lv, int col, grid_type v)
2292 int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2293 while ((bw = lw * lv + gw * (lv+1)) > TILE_SIZE)
2297 draw_rect(dr, ox + loff, oy, bw, TILE_SIZE, COL_MARK);
2298 for (i = 0; i < lv; i++, loff += lw + gw)
2299 draw_rect(dr, ox + loff + gw, oy, lw, TILE_SIZE, col);
2302 static void lines_horiz(drawing *dr, game_drawstate *ds,
2303 int ox, int oy, int lh, int col, grid_type v)
2305 int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2306 while ((bw = lw * lh + gw * (lh+1)) > TILE_SIZE)
2310 draw_rect(dr, ox, oy + loff, TILE_SIZE, bw, COL_MARK);
2311 for (i = 0; i < lh; i++, loff += lw + gw)
2312 draw_rect(dr, ox, oy + loff + gw, TILE_SIZE, lw, col);
2315 static void line_cross(drawing *dr, game_drawstate *ds,
2316 int ox, int oy, int col, grid_type v)
2319 draw_line(dr, ox, oy, ox+off, oy+off, col);
2320 draw_line(dr, ox+off, oy, ox, oy+off, col);
2323 static void lines_lvlh(game_state *state, int x, int y, grid_type v,
2324 int *lv_r, int *lh_r)
2328 if (v & G_LINEV) lv = INDEX(state,lines,x,y);
2329 if (v & G_LINEH) lh = INDEX(state,lines,x,y);
2332 if (INDEX(state, possv, x, y) && !lv) {
2333 lv = INDEX(state, possv, x, y);
2335 if (INDEX(state, possh, x, y) && !lh) {
2336 lh = INDEX(state, possh, x, y);
2339 /*debug(("lvlh: (%d,%d) v 0x%x lv %d lh %d.\n", x, y, v, lv, lh));*/
2340 *lv_r = lv; *lh_r = lh;
2343 static void dsf_debug_draw(drawing *dr,
2344 game_state *state, game_drawstate *ds,
2348 int ts = TILE_SIZE/2;
2349 int ox = COORD(x) + ts/2, oy = COORD(y) + ts/2;
2352 sprintf(str, "%d", dsf_canonify(state->solver->dsf, DINDEX(x,y)));
2353 draw_text(dr, ox, oy, FONT_VARIABLE, ts,
2354 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_WARNING, str);
2358 static void lines_redraw(drawing *dr,
2359 game_state *state, game_drawstate *ds, game_ui *ui,
2360 int x, int y, grid_type v, int lv, int lh)
2362 int ox = COORD(x), oy = COORD(y);
2363 int vcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2364 (v & G_WARN) ? COL_WARNING : COL_FOREGROUND, hcol = vcol;
2365 grid_type todraw = v & G_NOLINE;
2368 if (ui->todraw & G_FLAGSH) hcol = COL_SELECTED;
2369 if (ui->todraw & G_FLAGSV) vcol = COL_SELECTED;
2370 todraw |= ui->todraw;
2373 draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2376 if (INDEX(state, possv, x, y) && !(v & G_LINEV))
2378 if (INDEX(state, possh, x, y) && !(v & G_LINEH))
2382 draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_GRID);
2385 if (todraw & G_NOLINEV) {
2386 line_cross(dr, ds, ox + TS8(3), oy + TS8(1), vcol, todraw);
2387 line_cross(dr, ds, ox + TS8(3), oy + TS8(5), vcol, todraw);
2389 if (todraw & G_NOLINEH) {
2390 line_cross(dr, ds, ox + TS8(1), oy + TS8(3), hcol, todraw);
2391 line_cross(dr, ds, ox + TS8(5), oy + TS8(3), hcol, todraw);
2394 lines_vert(dr, ds, ox, oy, lv, vcol, v);
2396 lines_horiz(dr, ds, ox, oy, lh, hcol, v);
2398 dsf_debug_draw(dr, state, ds, x, y);
2399 draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2402 #define ISLAND_RADIUS ((TILE_SIZE*12)/20)
2403 #define ISLAND_NUMSIZE(is) \
2404 (((is)->count < 10) ? (TILE_SIZE*7)/10 : (TILE_SIZE*5)/10)
2406 static void island_redraw(drawing *dr,
2407 game_state *state, game_drawstate *ds,
2408 struct island *is, grid_type v)
2410 /* These overlap the edges of their squares, which is why they're drawn later.
2411 * We know they can't overlap each other because they're not allowed within 2
2412 * squares of each other. */
2413 int half = TILE_SIZE/2;
2414 int ox = COORD(is->x) + half, oy = COORD(is->y) + half;
2415 int orad = ISLAND_RADIUS, irad = orad - LINE_WIDTH;
2416 int updatesz = orad*2+1;
2417 int tcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2418 (v & G_WARN) ? COL_WARNING : COL_FOREGROUND;
2419 int col = (v & G_ISSEL) ? COL_SELECTED : tcol;
2420 int bg = (v & G_MARK) ? COL_MARK : COL_BACKGROUND;
2424 draw_rect_outline(dr, COORD(is->x), COORD(is->y),
2425 TILE_SIZE, TILE_SIZE, COL_GRID);
2428 /* draw a thick circle */
2429 draw_circle(dr, ox, oy, orad, col, col);
2430 draw_circle(dr, ox, oy, irad, bg, bg);
2432 sprintf(str, "%d", is->count);
2433 draw_text(dr, ox, oy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2434 ALIGN_VCENTRE | ALIGN_HCENTRE, tcol, str);
2436 dsf_debug_draw(dr, state, ds, is->x, is->y);
2437 draw_update(dr, ox - orad, oy - orad, updatesz, updatesz);
2440 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2441 game_state *state, int dir, game_ui *ui,
2442 float animtime, float flashtime)
2444 int x, y, force = 0, i, j, redraw, lv, lh;
2445 grid_type v, dsv, flash = 0;
2446 struct island *is, *is_drag_src = NULL, *is_drag_dst = NULL;
2449 int f = (int)(flashtime * 5 / FLASH_TIME);
2450 if (f == 1 || f == 3) flash = G_FLASH;
2453 /* Clear screen, if required. */
2456 TILE_SIZE * ds->w + 2 * BORDER,
2457 TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND);
2459 draw_rect_outline(dr,
2460 COORD(0)-1, COORD(0)-1,
2461 TILE_SIZE * ds->w + 2, TILE_SIZE * ds->h + 2,
2464 draw_update(dr, 0, 0,
2465 TILE_SIZE * ds->w + 2 * BORDER,
2466 TILE_SIZE * ds->h + 2 * BORDER);
2471 if (ui->dragx_src != -1 && ui->dragy_src != -1) {
2473 is_drag_src = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2474 assert(is_drag_src);
2475 if (ui->dragx_dst != -1 && ui->dragy_dst != -1) {
2476 is_drag_dst = INDEX(state, gridi, ui->dragx_dst, ui->dragy_dst);
2477 assert(is_drag_dst);
2482 /* Draw all lines (and hints, if we want), but *not* islands. */
2483 for (x = 0; x < ds->w; x++) {
2484 for (y = 0; y < ds->h; y++) {
2485 v = GRID(state, x, y) | flash;
2486 dsv = GRID(ds,x,y) & ~G_REDRAW;
2488 if (v & G_ISLAND) continue;
2491 if (WITHIN(x,is_drag_src->x, is_drag_dst->x) &&
2492 WITHIN(y,is_drag_src->y, is_drag_dst->y))
2495 lines_lvlh(state, x, y, v, &lv, &lh);
2498 lv != INDEX(ds,lv,x,y) ||
2499 lh != INDEX(ds,lh,x,y) ||
2501 GRID(ds, x, y) = v | G_REDRAW;
2502 INDEX(ds,lv,x,y) = lv;
2503 INDEX(ds,lh,x,y) = lh;
2504 lines_redraw(dr, state, ds, ui, x, y, v, lv, lh);
2506 GRID(ds,x,y) &= ~G_REDRAW;
2511 for (i = 0; i < state->n_islands; i++) {
2512 is = &state->islands[i];
2513 v = GRID(state, is->x, is->y) | flash;
2516 for (j = 0; j < is->adj.npoints; j++) {
2517 if (GRID(ds,is->adj.points[j].x,is->adj.points[j].y) & G_REDRAW) {
2523 if (is == is_drag_src)
2525 else if (is_drag_dst && is == is_drag_dst)
2529 if (island_impossible(is, v & G_MARK)) v |= G_WARN;
2531 if ((v != GRID(ds, is->x, is->y)) || force || redraw) {
2532 GRID(ds,is->x,is->y) = v;
2533 island_redraw(dr, state, ds, is, v);
2538 static float game_anim_length(game_state *oldstate, game_state *newstate,
2539 int dir, game_ui *ui)
2544 static float game_flash_length(game_state *oldstate, game_state *newstate,
2545 int dir, game_ui *ui)
2547 if (!oldstate->completed && newstate->completed &&
2548 !oldstate->solved && !newstate->solved)
2554 static int game_timing_state(game_state *state, game_ui *ui)
2559 static void game_print_size(game_params *params, float *x, float *y)
2563 /* 10mm squares by default. */
2564 game_compute_size(params, 1000, &pw, &ph);
2569 static void game_print(drawing *dr, game_state *state, int ts)
2571 int ink = print_mono_colour(dr, 0);
2572 int paper = print_mono_colour(dr, 1);
2573 int x, y, cx, cy, i, nl;
2577 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2578 game_drawstate ads, *ds = &ads;
2581 /* I don't think this wants a border. */
2584 print_line_width(dr, ts / 12);
2585 for (x = 0; x < state->w; x++) {
2586 for (y = 0; y < state->h; y++) {
2587 cx = COORD(x); cy = COORD(y);
2588 grid = GRID(state,x,y);
2589 nl = INDEX(state,lines,x,y);
2591 if (grid & G_ISLAND) continue;
2592 if (grid & G_LINEV) {
2594 draw_line(dr, cx+ts/2-loff, cy, cx+ts/2-loff, cy+ts, ink);
2595 draw_line(dr, cx+ts/2+loff, cy, cx+ts/2+loff, cy+ts, ink);
2597 draw_line(dr, cx+ts/2, cy, cx+ts/2, cy+ts, ink);
2600 if (grid & G_LINEH) {
2602 draw_line(dr, cx, cy+ts/2-loff, cx+ts, cy+ts/2-loff, ink);
2603 draw_line(dr, cx, cy+ts/2+loff, cx+ts, cy+ts/2+loff, ink);
2605 draw_line(dr, cx, cy+ts/2, cx+ts, cy+ts/2, ink);
2612 for (i = 0; i < state->n_islands; i++) {
2614 struct island *is = &state->islands[i];
2615 grid = GRID(state, is->x, is->y);
2616 cx = COORD(is->x) + ts/2;
2617 cy = COORD(is->y) + ts/2;
2619 draw_circle(dr, cx, cy, ISLAND_RADIUS, paper, ink);
2621 sprintf(str, "%d", is->count);
2622 draw_text(dr, cx, cy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2623 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
2628 #define thegame bridges
2631 const struct game thegame = {
2632 "Bridges", "games.bridges", "bridges",
2639 TRUE, game_configure, custom_params,
2647 TRUE, game_text_format,
2655 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2658 game_free_drawstate,
2662 TRUE, FALSE, game_print_size, game_print,
2663 FALSE, /* wants_statusbar */
2664 FALSE, game_timing_state,
2668 /* vim: set shiftwidth=4 tabstop=8: */