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. */
22 /* --- structures for params, state, etc. --- */
26 #define PREFERRED_TILE_SIZE 24
27 #define TILE_SIZE (ds->tilesize)
28 #define BORDER (TILE_SIZE / 2)
30 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
31 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
33 #define FLASH_TIME 0.50F
38 COL_HIGHLIGHT, COL_LOWLIGHT,
39 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
69 #define G_CURSOR 0x0800
71 /* flags used by the solver etc. */
72 #define G_SWEEP 0x1000
74 #define G_FLAGSH (G_LINEH|G_MARKH|G_NOLINEH)
75 #define G_FLAGSV (G_LINEV|G_MARKV|G_NOLINEV)
77 typedef unsigned int grid_type; /* change me later if we invent > 16 bits of flags. */
84 /* state->gridi is an optimisation; it stores the pointer to the island
85 * structs indexed by (x,y). It's not strictly necessary (we could use
86 * find234 instead), but Purify showed that board generation (mostly the solver)
87 * was spending 60% of its time in find234. */
89 struct surrounds { /* cloned from lightup.c */
90 struct { int x, y, dx, dy, off; } points[4];
91 int npoints, nislands;
101 int w, h, completed, solved, allowloops, maxb;
102 grid_type *grid, *scratch;
103 struct island *islands;
104 int n_islands, n_islands_alloc;
105 game_params params; /* used by the aux solver. */
106 #define N_WH_ARRAYS 5
107 char *wha, *possv, *possh, *lines, *maxv, *maxh;
108 struct island **gridi;
109 struct solver_state *solver; /* refcounted */
112 #define GRIDSZ(s) ((s)->w * (s)->h * sizeof(grid_type))
114 #define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
116 #define DINDEX(x,y) ((y)*state->w + (x))
118 #define INDEX(s,g,x,y) ((s)->g[(y)*((s)->w) + (x)])
119 #define IDX(s,g,i) ((s)->g[(i)])
120 #define GRID(s,x,y) INDEX(s,grid,x,y)
121 #define SCRATCH(s,x,y) INDEX(s,scratch,x,y)
122 #define POSSIBLES(s,dx,x,y) ((dx) ? (INDEX(s,possh,x,y)) : (INDEX(s,possv,x,y)))
123 #define MAXIMUM(s,dx,x,y) ((dx) ? (INDEX(s,maxh,x,y)) : (INDEX(s,maxv,x,y)))
125 #define GRIDCOUNT(s,x,y,f) ((GRID(s,x,y) & (f)) ? (INDEX(s,lines,x,y)) : 0)
127 #define WITHIN2(x,min,max) (((x) < (min)) ? 0 : (((x) > (max)) ? 0 : 1))
128 #define WITHIN(x,min,max) ((min) > (max) ? \
129 WITHIN2(x,max,min) : WITHIN2(x,min,max))
131 /* --- island struct and tree support functions --- */
133 #define ISLAND_ORTH(is,j,f,df) \
134 (is->f + (is->adj.points[(j)].off*is->adj.points[(j)].df))
136 #define ISLAND_ORTHX(is,j) ISLAND_ORTH(is,j,x,dx)
137 #define ISLAND_ORTHY(is,j) ISLAND_ORTH(is,j,y,dy)
139 static void fixup_islands_for_realloc(game_state *state)
143 for (i = 0; i < state->w*state->h; i++) state->gridi[i] = NULL;
144 for (i = 0; i < state->n_islands; i++) {
145 struct island *is = &state->islands[i];
147 INDEX(state, gridi, is->x, is->y) = is;
151 static int game_can_format_as_text_now(game_params *params)
156 static char *game_text_format(game_state *state)
163 len = (state->h) * (state->w+1) + 1;
164 ret = snewn(len, char);
167 for (y = 0; y < state->h; y++) {
168 for (x = 0; x < state->w; x++) {
169 grid = GRID(state,x,y);
170 nl = INDEX(state,lines,x,y);
171 is = INDEX(state, gridi, x, y);
173 *p++ = '0' + is->count;
174 } else if (grid & G_LINEV) {
175 *p++ = (nl > 1) ? '"' : (nl == 1) ? '|' : '!'; /* gaah, want a double-bar. */
176 } else if (grid & G_LINEH) {
177 *p++ = (nl > 1) ? '=' : (nl == 1) ? '-' : '~';
186 assert(p - ret == len);
190 static void debug_state(game_state *state)
192 char *textversion = game_text_format(state);
193 debug(("%s", textversion));
197 /*static void debug_possibles(game_state *state)
200 debug(("possh followed by possv\n"));
201 for (y = 0; y < state->h; y++) {
202 for (x = 0; x < state->w; x++) {
203 debug(("%d", POSSIBLES(state, 1, x, y)));
206 for (x = 0; x < state->w; x++) {
207 debug(("%d", POSSIBLES(state, 0, x, y)));
212 for (y = 0; y < state->h; y++) {
213 for (x = 0; x < state->w; x++) {
214 debug(("%d", MAXIMUM(state, 1, x, y)));
217 for (x = 0; x < state->w; x++) {
218 debug(("%d", MAXIMUM(state, 0, x, y)));
225 static void island_set_surrounds(struct island *is)
227 assert(INGRID(is->state,is->x,is->y));
228 is->adj.npoints = is->adj.nislands = 0;
229 #define ADDPOINT(cond,ddx,ddy) do {\
231 is->adj.points[is->adj.npoints].x = is->x+(ddx); \
232 is->adj.points[is->adj.npoints].y = is->y+(ddy); \
233 is->adj.points[is->adj.npoints].dx = (ddx); \
234 is->adj.points[is->adj.npoints].dy = (ddy); \
235 is->adj.points[is->adj.npoints].off = 0; \
238 ADDPOINT(is->x > 0, -1, 0);
239 ADDPOINT(is->x < (is->state->w-1), +1, 0);
240 ADDPOINT(is->y > 0, 0, -1);
241 ADDPOINT(is->y < (is->state->h-1), 0, +1);
244 static void island_find_orthogonal(struct island *is)
246 /* fills in the rest of the 'surrounds' structure, assuming
247 * all other islands are now in place. */
248 int i, x, y, dx, dy, off;
250 is->adj.nislands = 0;
251 for (i = 0; i < is->adj.npoints; i++) {
252 dx = is->adj.points[i].dx;
253 dy = is->adj.points[i].dy;
257 is->adj.points[i].off = 0;
258 while (INGRID(is->state, x, y)) {
259 if (GRID(is->state, x, y) & G_ISLAND) {
260 is->adj.points[i].off = off;
262 /*debug(("island (%d,%d) has orth is. %d*(%d,%d) away at (%d,%d).\n",
263 is->x, is->y, off, dx, dy,
264 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i)));*/
267 off++; x += dx; y += dy;
274 static int island_hasbridge(struct island *is, int direction)
276 int x = is->adj.points[direction].x;
277 int y = is->adj.points[direction].y;
278 grid_type gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
280 if (GRID(is->state, x, y) & gline) return 1;
284 static struct island *island_find_connection(struct island *is, int adjpt)
288 assert(adjpt < is->adj.npoints);
289 if (!is->adj.points[adjpt].off) return NULL;
290 if (!island_hasbridge(is, adjpt)) return NULL;
292 is_r = INDEX(is->state, gridi,
293 ISLAND_ORTHX(is, adjpt), ISLAND_ORTHY(is, adjpt));
299 static struct island *island_add(game_state *state, int x, int y, int count)
304 assert(!(GRID(state,x,y) & G_ISLAND));
305 GRID(state,x,y) |= G_ISLAND;
308 if (state->n_islands > state->n_islands_alloc) {
309 state->n_islands_alloc = state->n_islands * 2;
311 sresize(state->islands, state->n_islands_alloc, struct island);
314 is = &state->islands[state->n_islands-1];
316 memset(is, 0, sizeof(struct island));
321 island_set_surrounds(is);
324 fixup_islands_for_realloc(state);
326 INDEX(state, gridi, x, y) = is;
332 /* n = -1 means 'flip NOLINE flags [and set line to 0].' */
333 static void island_join(struct island *i1, struct island *i2, int n, int is_max)
335 game_state *state = i1->state;
338 assert(i1->state == i2->state);
339 assert(n >= -1 && n <= i1->state->maxb);
341 if (i1->x == i2->x) {
344 s = i1->y+1; e = i2->y-1;
346 s = i2->y+1; e = i1->y-1;
348 for (y = s; y <= e; y++) {
350 INDEX(state,maxv,x,y) = n;
353 GRID(state,x,y) ^= G_NOLINEV;
355 GRID(state,x,y) &= ~G_LINEV;
357 GRID(state,x,y) |= G_LINEV;
358 INDEX(state,lines,x,y) = n;
362 } else if (i1->y == i2->y) {
365 s = i1->x+1; e = i2->x-1;
367 s = i2->x+1; e = i1->x-1;
369 for (x = s; x <= e; x++) {
371 INDEX(state,maxh,x,y) = n;
374 GRID(state,x,y) ^= G_NOLINEH;
376 GRID(state,x,y) &= ~G_LINEH;
378 GRID(state,x,y) |= G_LINEH;
379 INDEX(state,lines,x,y) = n;
384 assert(!"island_join: islands not orthogonal.");
388 /* Counts the number of bridges currently attached to the island. */
389 static int island_countbridges(struct island *is)
393 for (i = 0; i < is->adj.npoints; i++) {
394 c += GRIDCOUNT(is->state,
395 is->adj.points[i].x, is->adj.points[i].y,
396 is->adj.points[i].dx ? G_LINEH : G_LINEV);
398 /*debug(("island count for (%d,%d) is %d.\n", is->x, is->y, c));*/
402 static int island_adjspace(struct island *is, int marks, int missing,
405 int x, y, poss, curr, dx;
406 grid_type gline, mline;
408 x = is->adj.points[direction].x;
409 y = is->adj.points[direction].y;
410 dx = is->adj.points[direction].dx;
411 gline = dx ? G_LINEH : G_LINEV;
414 mline = dx ? G_MARKH : G_MARKV;
415 if (GRID(is->state,x,y) & mline) return 0;
417 poss = POSSIBLES(is->state, dx, x, y);
418 poss = min(poss, missing);
420 curr = GRIDCOUNT(is->state, x, y, gline);
421 poss = min(poss, MAXIMUM(is->state, dx, x, y) - curr);
426 /* Counts the number of bridge spaces left around the island;
427 * expects the possibles to be up-to-date. */
428 static int island_countspaces(struct island *is, int marks)
430 int i, c = 0, missing;
432 missing = is->count - island_countbridges(is);
433 if (missing < 0) return 0;
435 for (i = 0; i < is->adj.npoints; i++) {
436 c += island_adjspace(is, marks, missing, i);
441 static int island_isadj(struct island *is, int direction)
444 grid_type gline, mline;
446 x = is->adj.points[direction].x;
447 y = is->adj.points[direction].y;
449 mline = is->adj.points[direction].dx ? G_MARKH : G_MARKV;
450 gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
451 if (GRID(is->state, x, y) & mline) {
452 /* If we're marked (i.e. the thing to attach to is complete)
453 * only count an adjacency if we're already attached. */
454 return GRIDCOUNT(is->state, x, y, gline);
456 /* If we're unmarked, count possible adjacency iff it's
457 * flagged as POSSIBLE. */
458 return POSSIBLES(is->state, is->adj.points[direction].dx, x, y);
463 /* Counts the no. of possible adjacent islands (including islands
464 * we're already connected to). */
465 static int island_countadj(struct island *is)
469 for (i = 0; i < is->adj.npoints; i++) {
470 if (island_isadj(is, i)) nadj++;
475 static void island_togglemark(struct island *is)
478 struct island *is_loop;
480 /* mark the island... */
481 GRID(is->state, is->x, is->y) ^= G_MARK;
483 /* ...remove all marks on non-island squares... */
484 for (x = 0; x < is->state->w; x++) {
485 for (y = 0; y < is->state->h; y++) {
486 if (!(GRID(is->state, x, y) & G_ISLAND))
487 GRID(is->state, x, y) &= ~G_MARK;
491 /* ...and add marks to squares around marked islands. */
492 for (i = 0; i < is->state->n_islands; i++) {
493 is_loop = &is->state->islands[i];
494 if (!(GRID(is_loop->state, is_loop->x, is_loop->y) & G_MARK))
497 for (j = 0; j < is_loop->adj.npoints; j++) {
498 /* if this direction takes us to another island, mark all
499 * squares between the two islands. */
500 if (!is_loop->adj.points[j].off) continue;
501 assert(is_loop->adj.points[j].off > 1);
502 for (o = 1; o < is_loop->adj.points[j].off; o++) {
504 is_loop->x + is_loop->adj.points[j].dx*o,
505 is_loop->y + is_loop->adj.points[j].dy*o) |=
506 is_loop->adj.points[j].dy ? G_MARKV : G_MARKH;
512 static int island_impossible(struct island *is, int strict)
514 int curr = island_countbridges(is), nspc = is->count - curr, nsurrspc;
516 struct island *is_orth;
519 debug(("island at (%d,%d) impossible because full.\n", is->x, is->y));
520 return 1; /* too many bridges */
521 } else if ((curr + island_countspaces(is, 0)) < is->count) {
522 debug(("island at (%d,%d) impossible because not enough spaces.\n", is->x, is->y));
523 return 1; /* impossible to create enough bridges */
524 } else if (strict && curr < is->count) {
525 debug(("island at (%d,%d) impossible because locked.\n", is->x, is->y));
526 return 1; /* not enough bridges and island is locked */
529 /* Count spaces in surrounding islands. */
531 for (i = 0; i < is->adj.npoints; i++) {
532 int ifree, dx = is->adj.points[i].dx;
534 if (!is->adj.points[i].off) continue;
535 poss = POSSIBLES(is->state, dx,
536 is->adj.points[i].x, is->adj.points[i].y);
537 if (poss == 0) continue;
538 is_orth = INDEX(is->state, gridi,
539 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i));
542 ifree = is_orth->count - island_countbridges(is_orth);
545 * ifree is the number of bridges unfilled in the other
546 * island, which is clearly an upper bound on the number
547 * of extra bridges this island may run to it.
549 * Another upper bound is the number of bridges unfilled
550 * on the specific line between here and there. We must
551 * take the minimum of both.
553 int bmax = MAXIMUM(is->state, dx,
554 is->adj.points[i].x, is->adj.points[i].y);
555 int bcurr = GRIDCOUNT(is->state,
556 is->adj.points[i].x, is->adj.points[i].y,
557 dx ? G_LINEH : G_LINEV);
558 assert(bcurr <= bmax);
559 nsurrspc += min(ifree, bmax - bcurr);
562 if (nsurrspc < nspc) {
563 debug(("island at (%d,%d) impossible: surr. islands %d spc, need %d.\n",
564 is->x, is->y, nsurrspc, nspc));
565 return 1; /* not enough spaces around surrounding islands to fill this one. */
571 /* --- Game parameter functions --- */
573 #define DEFAULT_PRESET 0
575 const struct game_params bridges_presets[] = {
576 { 7, 7, 2, 30, 10, 1, 0 },
577 { 7, 7, 2, 30, 10, 1, 1 },
578 { 7, 7, 2, 30, 10, 1, 2 },
579 { 10, 10, 2, 30, 10, 1, 0 },
580 { 10, 10, 2, 30, 10, 1, 1 },
581 { 10, 10, 2, 30, 10, 1, 2 },
582 { 15, 15, 2, 30, 10, 1, 0 },
583 { 15, 15, 2, 30, 10, 1, 1 },
584 { 15, 15, 2, 30, 10, 1, 2 },
587 static game_params *default_params(void)
589 game_params *ret = snew(game_params);
590 *ret = bridges_presets[DEFAULT_PRESET];
595 static int game_fetch_preset(int i, char **name, game_params **params)
600 if (i < 0 || i >= lenof(bridges_presets))
603 ret = default_params();
604 *ret = bridges_presets[i];
607 sprintf(buf, "%dx%d %s", ret->w, ret->h,
608 ret->difficulty == 0 ? "easy" :
609 ret->difficulty == 1 ? "medium" : "hard");
615 static void free_params(game_params *params)
620 static game_params *dup_params(game_params *params)
622 game_params *ret = snew(game_params);
623 *ret = *params; /* structure copy */
627 #define EATNUM(x) do { \
628 (x) = atoi(string); \
629 while (*string && isdigit((unsigned char)*string)) string++; \
632 static void decode_params(game_params *params, char const *string)
635 params->h = params->w;
636 if (*string == 'x') {
640 if (*string == 'i') {
642 EATNUM(params->islands);
644 if (*string == 'e') {
646 EATNUM(params->expansion);
648 if (*string == 'm') {
650 EATNUM(params->maxb);
652 params->allowloops = 1;
653 if (*string == 'L') {
655 params->allowloops = 0;
657 if (*string == 'd') {
659 EATNUM(params->difficulty);
663 static char *encode_params(game_params *params, int full)
668 sprintf(buf, "%dx%di%de%dm%d%sd%d",
669 params->w, params->h, params->islands, params->expansion,
670 params->maxb, params->allowloops ? "" : "L",
673 sprintf(buf, "%dx%dm%d%s", params->w, params->h,
674 params->maxb, params->allowloops ? "" : "L");
679 static config_item *game_configure(game_params *params)
684 ret = snewn(8, config_item);
686 ret[0].name = "Width";
687 ret[0].type = C_STRING;
688 sprintf(buf, "%d", params->w);
689 ret[0].sval = dupstr(buf);
692 ret[1].name = "Height";
693 ret[1].type = C_STRING;
694 sprintf(buf, "%d", params->h);
695 ret[1].sval = dupstr(buf);
698 ret[2].name = "Difficulty";
699 ret[2].type = C_CHOICES;
700 ret[2].sval = ":Easy:Medium:Hard";
701 ret[2].ival = params->difficulty;
703 ret[3].name = "Allow loops";
704 ret[3].type = C_BOOLEAN;
706 ret[3].ival = params->allowloops;
708 ret[4].name = "Max. bridges per direction";
709 ret[4].type = C_CHOICES;
710 ret[4].sval = ":1:2:3:4"; /* keep up-to-date with MAX_BRIDGES */
711 ret[4].ival = params->maxb - 1;
713 ret[5].name = "%age of island squares";
714 ret[5].type = C_CHOICES;
715 ret[5].sval = ":5%:10%:15%:20%:25%:30%";
716 ret[5].ival = (params->islands / 5)-1;
718 ret[6].name = "Expansion factor (%age)";
719 ret[6].type = C_CHOICES;
720 ret[6].sval = ":0%:10%:20%:30%:40%:50%:60%:70%:80%:90%:100%";
721 ret[6].ival = params->expansion / 10;
731 static game_params *custom_params(config_item *cfg)
733 game_params *ret = snew(game_params);
735 ret->w = atoi(cfg[0].sval);
736 ret->h = atoi(cfg[1].sval);
737 ret->difficulty = cfg[2].ival;
738 ret->allowloops = cfg[3].ival;
739 ret->maxb = cfg[4].ival + 1;
740 ret->islands = (cfg[5].ival + 1) * 5;
741 ret->expansion = cfg[6].ival * 10;
746 static char *validate_params(game_params *params, int full)
748 if (params->w < 3 || params->h < 3)
749 return "Width and height must be at least 3";
750 if (params->maxb < 1 || params->maxb > MAX_BRIDGES)
751 return "Too many bridges.";
753 if (params->islands <= 0 || params->islands > 30)
754 return "%age of island squares must be between 1% and 30%";
755 if (params->expansion < 0 || params->expansion > 100)
756 return "Expansion factor must be between 0 and 100";
761 /* --- Game encoding and differences --- */
763 static char *encode_game(game_state *state)
766 int wh = state->w*state->h, run, x, y;
769 ret = snewn(wh + 1, char);
772 for (y = 0; y < state->h; y++) {
773 for (x = 0; x < state->w; x++) {
774 is = INDEX(state, gridi, x, y);
777 *p++ = ('a'-1) + run;
781 *p++ = '0' + is->count;
783 *p++ = 'A' + (is->count - 10);
786 *p++ = ('a'-1) + run;
794 *p++ = ('a'-1) + run;
798 assert(p - ret <= wh);
803 static char *game_state_diff(game_state *src, game_state *dest)
805 int movesize = 256, movelen = 0;
806 char *move = snewn(movesize, char), buf[80];
808 grid_type gline, nline;
809 struct island *is_s, *is_d, *is_orth;
811 #define APPEND do { \
812 if (movelen + len >= movesize) { \
813 movesize = movelen + len + 256; \
814 move = sresize(move, movesize, char); \
816 strcpy(move + movelen, buf); \
820 move[movelen++] = 'S';
821 move[movelen] = '\0';
823 assert(src->n_islands == dest->n_islands);
825 for (i = 0; i < src->n_islands; i++) {
826 is_s = &src->islands[i];
827 is_d = &dest->islands[i];
828 assert(is_s->x == is_d->x);
829 assert(is_s->y == is_d->y);
830 assert(is_s->adj.npoints == is_d->adj.npoints); /* more paranoia */
832 for (d = 0; d < is_s->adj.npoints; d++) {
833 if (is_s->adj.points[d].dx == -1 ||
834 is_s->adj.points[d].dy == -1) continue;
836 x = is_s->adj.points[d].x;
837 y = is_s->adj.points[d].y;
838 gline = is_s->adj.points[d].dx ? G_LINEH : G_LINEV;
839 nline = is_s->adj.points[d].dx ? G_NOLINEH : G_NOLINEV;
840 is_orth = INDEX(dest, gridi,
841 ISLAND_ORTHX(is_d, d), ISLAND_ORTHY(is_d, d));
843 if (GRIDCOUNT(src, x, y, gline) != GRIDCOUNT(dest, x, y, gline)) {
845 len = sprintf(buf, ";L%d,%d,%d,%d,%d",
846 is_s->x, is_s->y, is_orth->x, is_orth->y,
847 GRIDCOUNT(dest, x, y, gline));
850 if ((GRID(src,x,y) & nline) != (GRID(dest, x, y) & nline)) {
852 len = sprintf(buf, ";N%d,%d,%d,%d",
853 is_s->x, is_s->y, is_orth->x, is_orth->y);
857 if ((GRID(src, is_s->x, is_s->y) & G_MARK) !=
858 (GRID(dest, is_d->x, is_d->y) & G_MARK)) {
859 len = sprintf(buf, ";M%d,%d", is_s->x, is_s->y);
866 /* --- Game setup and solving utilities --- */
868 /* This function is optimised; a Quantify showed that lots of grid-generation time
869 * (>50%) was spent in here. Hence the IDX() stuff. */
871 static void map_update_possibles(game_state *state)
873 int x, y, s, e, bl, i, np, maxb, w = state->w, idx;
874 struct island *is_s = NULL, *is_f = NULL;
876 /* Run down vertical stripes [un]setting possv... */
877 for (x = 0; x < state->w; x++) {
881 /* Unset possible flags until we find an island. */
882 for (y = 0; y < state->h; y++) {
883 is_s = IDX(state, gridi, idx);
886 IDX(state, possv, idx) = 0;
889 for (; y < state->h; y++) {
890 is_f = IDX(state, gridi, idx);
893 maxb = IDX(state, maxv, idx);
894 np = min(maxb, min(is_s->count, is_f->count));
897 for (i = s; i <= e; i++) {
898 INDEX(state, possv, x, i) = bl ? 0 : np;
906 if (IDX(state,grid,idx) & (G_LINEH|G_NOLINEV)) bl = 1;
911 for (i = s; i <= e; i++)
912 INDEX(state, possv, x, i) = 0;
916 /* ...and now do horizontal stripes [un]setting possh. */
917 /* can we lose this clone'n'hack? */
918 for (y = 0; y < state->h; y++) {
922 for (x = 0; x < state->w; x++) {
923 is_s = IDX(state, gridi, idx);
926 IDX(state, possh, idx) = 0;
929 for (; x < state->w; x++) {
930 is_f = IDX(state, gridi, idx);
933 maxb = IDX(state, maxh, idx);
934 np = min(maxb, min(is_s->count, is_f->count));
937 for (i = s; i <= e; i++) {
938 INDEX(state, possh, i, y) = bl ? 0 : np;
946 if (IDX(state,grid,idx) & (G_LINEV|G_NOLINEH)) bl = 1;
951 for (i = s; i <= e; i++)
952 INDEX(state, possh, i, y) = 0;
957 static void map_count(game_state *state)
960 grid_type flag, grid;
963 for (i = 0; i < state->n_islands; i++) {
964 is = &state->islands[i];
966 for (n = 0; n < is->adj.npoints; n++) {
967 ax = is->adj.points[n].x;
968 ay = is->adj.points[n].y;
969 flag = (ax == is->x) ? G_LINEV : G_LINEH;
970 grid = GRID(state,ax,ay);
972 is->count += INDEX(state,lines,ax,ay);
978 static void map_find_orthogonal(game_state *state)
982 for (i = 0; i < state->n_islands; i++) {
983 island_find_orthogonal(&state->islands[i]);
987 static int grid_degree(game_state *state, int x, int y, int *nx_r, int *ny_r)
989 grid_type grid = SCRATCH(state, x, y), gline = grid & G_LINE;
991 int x1, y1, x2, y2, c = 0, i, nx, ny;
993 nx = ny = -1; /* placate optimiser */
994 is = INDEX(state, gridi, x, y);
996 for (i = 0; i < is->adj.npoints; i++) {
997 gline = is->adj.points[i].dx ? G_LINEH : G_LINEV;
1000 is->adj.points[i].y) & gline) {
1001 nx = is->adj.points[i].x;
1002 ny = is->adj.points[i].y;
1007 if (gline & G_LINEV) {
1014 /* Non-island squares with edges in should never be pointing off the
1015 * edge of the grid. */
1016 assert(INGRID(state, x1, y1));
1017 assert(INGRID(state, x2, y2));
1018 if (SCRATCH(state, x1, y1) & (gline | G_ISLAND)) {
1019 nx = x1; ny = y1; c++;
1021 if (SCRATCH(state, x2, y2) & (gline | G_ISLAND)) {
1022 nx = x2; ny = y2; c++;
1026 assert(nx != -1 && ny != -1); /* paranoia */
1027 *nx_r = nx; *ny_r = ny;
1032 static int map_hasloops(game_state *state, int mark)
1034 int x, y, ox, oy, nx = 0, ny = 0, loop = 0;
1036 memcpy(state->scratch, state->grid, GRIDSZ(state));
1038 /* This algorithm is actually broken; if there are two loops connected
1039 * by bridges this will also highlight bridges. The correct algorithm
1040 * uses a dsf and a two-pass edge-detection algorithm (see check_correct
1041 * in slant.c); this is BALGE for now, especially since disallow-loops
1042 * is not the default for this puzzle. If we want to fix this later then
1043 * copy the alg in slant.c to the empty statement in map_group. */
1045 /* Remove all 1-degree edges. */
1046 for (y = 0; y < state->h; y++) {
1047 for (x = 0; x < state->w; x++) {
1049 while (grid_degree(state, ox, oy, &nx, &ny) == 1) {
1050 /*debug(("hasloops: removing 1-degree at (%d,%d).\n", ox, oy));*/
1051 SCRATCH(state, ox, oy) &= ~(G_LINE|G_ISLAND);
1056 /* Mark any remaining edges as G_WARN, if required. */
1057 for (x = 0; x < state->w; x++) {
1058 for (y = 0; y < state->h; y++) {
1059 if (GRID(state,x,y) & G_ISLAND) continue;
1061 if (SCRATCH(state, x, y) & G_LINE) {
1063 /*debug(("hasloops: marking loop square at (%d,%d).\n",
1065 GRID(state,x,y) |= G_WARN;
1068 return 1; /* short-cut as soon as we find one */
1071 GRID(state,x,y) &= ~G_WARN;
1078 static void map_group(game_state *state)
1080 int i, wh = state->w*state->h, d1, d2;
1082 int *dsf = state->solver->dsf;
1083 struct island *is, *is_join;
1085 /* Initialise dsf. */
1088 /* For each island, find connected islands right or down
1089 * and merge the dsf for the island squares as well as the
1090 * bridge squares. */
1091 for (x = 0; x < state->w; x++) {
1092 for (y = 0; y < state->h; y++) {
1093 GRID(state,x,y) &= ~(G_SWEEP|G_WARN); /* for group_full. */
1095 is = INDEX(state, gridi, x, y);
1098 for (i = 0; i < is->adj.npoints; i++) {
1099 /* only want right/down */
1100 if (is->adj.points[i].dx == -1 ||
1101 is->adj.points[i].dy == -1) continue;
1103 is_join = island_find_connection(is, i);
1104 if (!is_join) continue;
1106 d2 = DINDEX(is_join->x, is_join->y);
1107 if (dsf_canonify(dsf,d1) == dsf_canonify(dsf,d2)) {
1108 ; /* we have a loop. See comment in map_hasloops. */
1109 /* However, we still want to merge all squares joining
1110 * this side-that-makes-a-loop. */
1112 /* merge all squares between island 1 and island 2. */
1113 for (x2 = x; x2 <= is_join->x; x2++) {
1114 for (y2 = y; y2 <= is_join->y; y2++) {
1116 if (d1 != d2) dsf_merge(dsf,d1,d2);
1124 static int map_group_check(game_state *state, int canon, int warn,
1127 int *dsf = state->solver->dsf, nislands = 0;
1128 int x, y, i, allfull = 1;
1131 for (i = 0; i < state->n_islands; i++) {
1132 is = &state->islands[i];
1133 if (dsf_canonify(dsf, DINDEX(is->x,is->y)) != canon) continue;
1135 GRID(state, is->x, is->y) |= G_SWEEP;
1137 if (island_countbridges(is) != is->count)
1140 if (warn && allfull && nislands != state->n_islands) {
1141 /* we're full and this island group isn't the whole set.
1142 * Mark all squares with this dsf canon as ERR. */
1143 for (x = 0; x < state->w; x++) {
1144 for (y = 0; y < state->h; y++) {
1145 if (dsf_canonify(dsf, DINDEX(x,y)) == canon) {
1146 GRID(state,x,y) |= G_WARN;
1152 if (nislands_r) *nislands_r = nislands;
1156 static int map_group_full(game_state *state, int *ngroups_r)
1158 int *dsf = state->solver->dsf, ngroups = 0;
1162 /* NB this assumes map_group (or sth else) has cleared G_SWEEP. */
1164 for (i = 0; i < state->n_islands; i++) {
1165 is = &state->islands[i];
1166 if (GRID(state,is->x,is->y) & G_SWEEP) continue;
1169 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1174 *ngroups_r = ngroups;
1178 static int map_check(game_state *state)
1182 /* Check for loops, if necessary. */
1183 if (!state->allowloops) {
1184 if (map_hasloops(state, 1))
1188 /* Place islands into island groups and check for early
1189 * satisfied-groups. */
1190 map_group(state); /* clears WARN and SWEEP */
1191 if (map_group_full(state, &ngroups)) {
1192 if (ngroups == 1) return 1;
1197 static void map_clear(game_state *state)
1201 for (x = 0; x < state->w; x++) {
1202 for (y = 0; y < state->h; y++) {
1203 /* clear most flags; might want to be slightly more careful here. */
1204 GRID(state,x,y) &= G_ISLAND;
1209 static void solve_join(struct island *is, int direction, int n, int is_max)
1211 struct island *is_orth;
1212 int d1, d2, *dsf = is->state->solver->dsf;
1213 game_state *state = is->state; /* for DINDEX */
1215 is_orth = INDEX(is->state, gridi,
1216 ISLAND_ORTHX(is, direction),
1217 ISLAND_ORTHY(is, direction));
1219 /*debug(("...joining (%d,%d) to (%d,%d) with %d bridge(s).\n",
1220 is->x, is->y, is_orth->x, is_orth->y, n));*/
1221 island_join(is, is_orth, n, is_max);
1223 if (n > 0 && !is_max) {
1224 d1 = DINDEX(is->x, is->y);
1225 d2 = DINDEX(is_orth->x, is_orth->y);
1226 if (dsf_canonify(dsf, d1) != dsf_canonify(dsf, d2))
1227 dsf_merge(dsf, d1, d2);
1231 static int solve_fillone(struct island *is)
1235 debug(("solve_fillone for island (%d,%d).\n", is->x, is->y));
1237 for (i = 0; i < is->adj.npoints; i++) {
1238 if (island_isadj(is, i)) {
1239 if (island_hasbridge(is, i)) {
1240 /* already attached; do nothing. */;
1242 solve_join(is, i, 1, 0);
1250 static int solve_fill(struct island *is)
1252 /* for each unmarked adjacent, make sure we convert every possible bridge
1253 * to a real one, and then work out the possibles afresh. */
1254 int i, nnew, ncurr, nadded = 0, missing;
1256 debug(("solve_fill for island (%d,%d).\n", is->x, is->y));
1258 missing = is->count - island_countbridges(is);
1259 if (missing < 0) return 0;
1261 /* very like island_countspaces. */
1262 for (i = 0; i < is->adj.npoints; i++) {
1263 nnew = island_adjspace(is, 1, missing, i);
1265 ncurr = GRIDCOUNT(is->state,
1266 is->adj.points[i].x, is->adj.points[i].y,
1267 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1269 solve_join(is, i, nnew + ncurr, 0);
1276 static int solve_island_stage1(struct island *is, int *didsth_r)
1278 int bridges = island_countbridges(is);
1279 int nspaces = island_countspaces(is, 1);
1280 int nadj = island_countadj(is);
1285 /*debug(("island at (%d,%d) filled %d/%d (%d spc) nadj %d\n",
1286 is->x, is->y, bridges, is->count, nspaces, nadj));*/
1287 if (bridges > is->count) {
1288 /* We only ever add bridges when we're sure they fit, or that's
1289 * the only place they can go. If we've added bridges such that
1290 * another island has become wrong, the puzzle must not have had
1292 debug(("...island at (%d,%d) is overpopulated!\n", is->x, is->y));
1294 } else if (bridges == is->count) {
1295 /* This island is full. Make sure it's marked (and update
1296 * possibles if we did). */
1297 if (!(GRID(is->state, is->x, is->y) & G_MARK)) {
1298 debug(("...marking island (%d,%d) as full.\n", is->x, is->y));
1299 island_togglemark(is);
1302 } else if (GRID(is->state, is->x, is->y) & G_MARK) {
1303 debug(("...island (%d,%d) is marked but unfinished!\n",
1305 return 0; /* island has been marked unfinished; no solution from here. */
1307 /* This is the interesting bit; we try and fill in more information
1308 * about this island. */
1309 if (is->count == bridges + nspaces) {
1310 if (solve_fill(is) > 0) didsth = 1;
1311 } else if (is->count > ((nadj-1) * is->state->maxb)) {
1312 /* must have at least one bridge in each possible direction. */
1313 if (solve_fillone(is) > 0) didsth = 1;
1317 map_update_possibles(is->state);
1323 /* returns non-zero if a new line here would cause a loop. */
1324 static int solve_island_checkloop(struct island *is, int direction)
1326 struct island *is_orth;
1327 int *dsf = is->state->solver->dsf, d1, d2;
1328 game_state *state = is->state;
1330 if (is->state->allowloops) return 0; /* don't care anyway */
1331 if (island_hasbridge(is, direction)) return 0; /* already has a bridge */
1332 if (island_isadj(is, direction) == 0) return 0; /* no adj island */
1334 is_orth = INDEX(is->state, gridi,
1335 ISLAND_ORTHX(is,direction),
1336 ISLAND_ORTHY(is,direction));
1337 if (!is_orth) return 0;
1339 d1 = DINDEX(is->x, is->y);
1340 d2 = DINDEX(is_orth->x, is_orth->y);
1341 if (dsf_canonify(dsf, d1) == dsf_canonify(dsf, d2)) {
1342 /* two islands are connected already; don't join them. */
1348 static int solve_island_stage2(struct island *is, int *didsth_r)
1350 int added = 0, removed = 0, navail = 0, nadj, i;
1354 for (i = 0; i < is->adj.npoints; i++) {
1355 if (solve_island_checkloop(is, i)) {
1356 debug(("removing possible loop at (%d,%d) direction %d.\n",
1358 solve_join(is, i, -1, 0);
1359 map_update_possibles(is->state);
1362 navail += island_isadj(is, i);
1363 /*debug(("stage2: navail for (%d,%d) direction (%d,%d) is %d.\n",
1365 is->adj.points[i].dx, is->adj.points[i].dy,
1366 island_isadj(is, i)));*/
1370 /*debug(("island at (%d,%d) navail %d: checking...\n", is->x, is->y, navail));*/
1372 for (i = 0; i < is->adj.npoints; i++) {
1373 if (!island_hasbridge(is, i)) {
1374 nadj = island_isadj(is, i);
1375 if (nadj > 0 && (navail - nadj) < is->count) {
1376 /* we couldn't now complete the island without at
1377 * least one bridge here; put it in. */
1378 /*debug(("nadj %d, navail %d, is->count %d.\n",
1379 nadj, navail, is->count));*/
1380 debug(("island at (%d,%d) direction (%d,%d) must have 1 bridge\n",
1382 is->adj.points[i].dx, is->adj.points[i].dy));
1383 solve_join(is, i, 1, 0);
1385 /*debug_state(is->state);
1386 debug_possibles(is->state);*/
1390 if (added) map_update_possibles(is->state);
1391 if (added || removed) *didsth_r = 1;
1395 static int solve_island_subgroup(struct island *is, int direction, int n)
1397 struct island *is_join;
1398 int nislands, *dsf = is->state->solver->dsf;
1399 game_state *state = is->state;
1401 debug(("..checking subgroups.\n"));
1403 /* if is isn't full, return 0. */
1404 if (n < is->count) {
1405 debug(("...orig island (%d,%d) not full.\n", is->x, is->y));
1409 is_join = INDEX(state, gridi,
1410 ISLAND_ORTHX(is, direction),
1411 ISLAND_ORTHY(is, direction));
1414 /* if is_join isn't full, return 0. */
1415 if (island_countbridges(is_join) < is_join->count) {
1416 debug(("...dest island (%d,%d) not full.\n", is_join->x, is_join->y));
1420 /* Check group membership for is->dsf; if it's full return 1. */
1421 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1423 if (nislands < state->n_islands) {
1424 /* we have a full subgroup that isn't the whole set.
1425 * This isn't allowed. */
1426 debug(("island at (%d,%d) makes full subgroup, disallowing.\n",
1430 debug(("...has finished puzzle.\n"));
1436 static int solve_island_impossible(game_state *state)
1441 /* If any islands are impossible, return 1. */
1442 for (i = 0; i < state->n_islands; i++) {
1443 is = &state->islands[i];
1444 if (island_impossible(is, 0)) {
1445 debug(("island at (%d,%d) has become impossible, disallowing.\n",
1453 /* Bear in mind that this function is really rather inefficient. */
1454 static int solve_island_stage3(struct island *is, int *didsth_r)
1456 int i, n, x, y, missing, spc, curr, maxb, didsth = 0;
1457 int wh = is->state->w * is->state->h;
1458 struct solver_state *ss = is->state->solver;
1462 missing = is->count - island_countbridges(is);
1463 if (missing <= 0) return 1;
1465 for (i = 0; i < is->adj.npoints; i++) {
1466 /* We only do right- or down-pointing bridges. */
1467 if (is->adj.points[i].dx == -1 ||
1468 is->adj.points[i].dy == -1) continue;
1470 x = is->adj.points[i].x;
1471 y = is->adj.points[i].y;
1472 spc = island_adjspace(is, 1, missing, i);
1473 if (spc == 0) continue;
1475 curr = GRIDCOUNT(is->state, x, y,
1476 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1477 debug(("island at (%d,%d) s3, trying %d - %d bridges.\n",
1478 is->x, is->y, curr+1, curr+spc));
1480 /* Now we know that this island could have more bridges,
1481 * to bring the total from curr+1 to curr+spc. */
1483 /* We have to squirrel the dsf away and restore it afterwards;
1484 * it is additive only, and can't be removed from. */
1485 memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int));
1486 for (n = curr+1; n <= curr+spc; n++) {
1487 solve_join(is, i, n, 0);
1488 map_update_possibles(is->state);
1490 if (solve_island_subgroup(is, i, n) ||
1491 solve_island_impossible(is->state)) {
1493 debug(("island at (%d,%d) d(%d,%d) new max of %d bridges:\n",
1495 is->adj.points[i].dx, is->adj.points[i].dy,
1500 solve_join(is, i, curr, 0); /* put back to before. */
1501 memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int));
1504 /*debug_state(is->state);*/
1506 debug(("...adding NOLINE.\n"));
1507 solve_join(is, i, -1, 0); /* we can't have any bridges here. */
1509 debug(("...setting maximum\n"));
1510 solve_join(is, i, maxb, 1);
1514 map_update_possibles(is->state);
1516 if (didsth) *didsth_r = didsth;
1520 #define CONTINUE_IF_FULL do { \
1521 if (GRID(state, is->x, is->y) & G_MARK) { \
1522 /* island full, don't try fixing it */ \
1526 static int solve_sub(game_state *state, int difficulty, int depth)
1534 /* First island iteration: things we can work out by looking at
1535 * properties of the island as a whole. */
1536 for (i = 0; i < state->n_islands; i++) {
1537 is = &state->islands[i];
1538 if (!solve_island_stage1(is, &didsth)) return 0;
1540 if (didsth) continue;
1541 else if (difficulty < 1) break;
1543 /* Second island iteration: thing we can work out by looking at
1544 * properties of individual island connections. */
1545 for (i = 0; i < state->n_islands; i++) {
1546 is = &state->islands[i];
1548 if (!solve_island_stage2(is, &didsth)) return 0;
1550 if (didsth) continue;
1551 else if (difficulty < 2) break;
1553 /* Third island iteration: things we can only work out by looking
1554 * at groups of islands. */
1555 for (i = 0; i < state->n_islands; i++) {
1556 is = &state->islands[i];
1557 if (!solve_island_stage3(is, &didsth)) return 0;
1559 if (didsth) continue;
1560 else if (difficulty < 3) break;
1562 /* If we can be bothered, write a recursive solver to finish here. */
1565 if (map_check(state)) return 1; /* solved it */
1569 static void solve_for_hint(game_state *state)
1572 solve_sub(state, 10, 0);
1575 static int solve_from_scratch(game_state *state, int difficulty)
1579 map_update_possibles(state);
1580 return solve_sub(state, difficulty, 0);
1583 /* --- New game functions --- */
1585 static game_state *new_state(game_params *params)
1587 game_state *ret = snew(game_state);
1588 int wh = params->w * params->h, i;
1592 ret->allowloops = params->allowloops;
1593 ret->maxb = params->maxb;
1594 ret->params = *params;
1596 ret->grid = snewn(wh, grid_type);
1597 memset(ret->grid, 0, GRIDSZ(ret));
1598 ret->scratch = snewn(wh, grid_type);
1599 memset(ret->scratch, 0, GRIDSZ(ret));
1601 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1602 memset(ret->wha, 0, wh*N_WH_ARRAYS*sizeof(char));
1604 ret->possv = ret->wha;
1605 ret->possh = ret->wha + wh;
1606 ret->lines = ret->wha + wh*2;
1607 ret->maxv = ret->wha + wh*3;
1608 ret->maxh = ret->wha + wh*4;
1610 memset(ret->maxv, ret->maxb, wh*sizeof(char));
1611 memset(ret->maxh, ret->maxb, wh*sizeof(char));
1613 ret->islands = NULL;
1615 ret->n_islands_alloc = 0;
1617 ret->gridi = snewn(wh, struct island *);
1618 for (i = 0; i < wh; i++) ret->gridi[i] = NULL;
1620 ret->solved = ret->completed = 0;
1622 ret->solver = snew(struct solver_state);
1623 ret->solver->dsf = snew_dsf(wh);
1624 ret->solver->tmpdsf = snewn(wh, int);
1626 ret->solver->refcount = 1;
1631 static game_state *dup_game(game_state *state)
1633 game_state *ret = snew(game_state);
1634 int wh = state->w*state->h;
1638 ret->allowloops = state->allowloops;
1639 ret->maxb = state->maxb;
1640 ret->params = state->params;
1642 ret->grid = snewn(wh, grid_type);
1643 memcpy(ret->grid, state->grid, GRIDSZ(ret));
1644 ret->scratch = snewn(wh, grid_type);
1645 memcpy(ret->scratch, state->scratch, GRIDSZ(ret));
1647 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1648 memcpy(ret->wha, state->wha, wh*N_WH_ARRAYS*sizeof(char));
1650 ret->possv = ret->wha;
1651 ret->possh = ret->wha + wh;
1652 ret->lines = ret->wha + wh*2;
1653 ret->maxv = ret->wha + wh*3;
1654 ret->maxh = ret->wha + wh*4;
1656 ret->islands = snewn(state->n_islands, struct island);
1657 memcpy(ret->islands, state->islands, state->n_islands * sizeof(struct island));
1658 ret->n_islands = ret->n_islands_alloc = state->n_islands;
1660 ret->gridi = snewn(wh, struct island *);
1661 fixup_islands_for_realloc(ret);
1663 ret->solved = state->solved;
1664 ret->completed = state->completed;
1666 ret->solver = state->solver;
1667 ret->solver->refcount++;
1672 static void free_game(game_state *state)
1674 if (--state->solver->refcount <= 0) {
1675 sfree(state->solver->dsf);
1676 sfree(state->solver->tmpdsf);
1677 sfree(state->solver);
1680 sfree(state->islands);
1681 sfree(state->gridi);
1685 sfree(state->scratch);
1690 #define MAX_NEWISLAND_TRIES 50
1691 #define MIN_SENSIBLE_ISLANDS 3
1693 #define ORDER(a,b) do { if (a < b) { int tmp=a; int a=b; int b=tmp; } } while(0)
1695 static char *new_game_desc(game_params *params, random_state *rs,
1696 char **aux, int interactive)
1698 game_state *tobuild = NULL;
1699 int i, j, wh = params->w * params->h, x, y, dx, dy;
1700 int minx, miny, maxx, maxy, joinx, joiny, newx, newy, diffx, diffy;
1701 int ni_req = max((params->islands * wh) / 100, MIN_SENSIBLE_ISLANDS), ni_curr, ni_bad;
1702 struct island *is, *is2;
1704 unsigned int echeck;
1706 /* pick a first island position randomly. */
1708 if (tobuild) free_game(tobuild);
1709 tobuild = new_state(params);
1711 x = random_upto(rs, params->w);
1712 y = random_upto(rs, params->h);
1713 island_add(tobuild, x, y, 0);
1716 debug(("Created initial island at (%d,%d).\n", x, y));
1718 while (ni_curr < ni_req) {
1719 /* Pick a random island to try and extend from. */
1720 i = random_upto(rs, tobuild->n_islands);
1721 is = &tobuild->islands[i];
1723 /* Pick a random direction to extend in. */
1724 j = random_upto(rs, is->adj.npoints);
1725 dx = is->adj.points[j].x - is->x;
1726 dy = is->adj.points[j].y - is->y;
1728 /* Find out limits of where we could put a new island. */
1730 minx = is->x + 2*dx; miny = is->y + 2*dy; /* closest is 2 units away. */
1731 x = is->x+dx; y = is->y+dy;
1732 if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1733 /* already a line next to the island, continue. */
1737 if (x < 0 || x >= params->w || y < 0 || y >= params->h) {
1738 /* got past the edge; put a possible at the island
1740 maxx = x-dx; maxy = y-dy;
1743 if (GRID(tobuild,x,y) & G_ISLAND) {
1744 /* could join up to an existing island... */
1745 joinx = x; joiny = y;
1746 /* ... or make a new one 2 spaces away. */
1747 maxx = x - 2*dx; maxy = y - 2*dy;
1749 } else if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1750 /* could make a new one 1 space away from the line. */
1751 maxx = x - dx; maxy = y - dy;
1758 debug(("Island at (%d,%d) with d(%d,%d) has new positions "
1759 "(%d,%d) -> (%d,%d), join (%d,%d).\n",
1760 is->x, is->y, dx, dy, minx, miny, maxx, maxy, joinx, joiny));
1761 /* Now we know where we could either put a new island
1762 * (between min and max), or (if loops are allowed) could join on
1763 * to an existing island (at join). */
1764 if (params->allowloops && joinx != -1 && joiny != -1) {
1765 if (random_upto(rs, 100) < (unsigned long)params->expansion) {
1766 is2 = INDEX(tobuild, gridi, joinx, joiny);
1767 debug(("Joining island at (%d,%d) to (%d,%d).\n",
1768 is->x, is->y, is2->x, is2->y));
1772 diffx = (maxx - minx) * dx;
1773 diffy = (maxy - miny) * dy;
1774 if (diffx < 0 || diffy < 0) goto bad;
1775 if (random_upto(rs,100) < (unsigned long)params->expansion) {
1776 newx = maxx; newy = maxy;
1777 debug(("Creating new island at (%d,%d) (expanded).\n", newx, newy));
1779 newx = minx + random_upto(rs,diffx+1)*dx;
1780 newy = miny + random_upto(rs,diffy+1)*dy;
1781 debug(("Creating new island at (%d,%d).\n", newx, newy));
1783 /* check we're not next to island in the other orthogonal direction. */
1784 if ((INGRID(tobuild,newx+dy,newy+dx) && (GRID(tobuild,newx+dy,newy+dx) & G_ISLAND)) ||
1785 (INGRID(tobuild,newx-dy,newy-dx) && (GRID(tobuild,newx-dy,newy-dx) & G_ISLAND))) {
1786 debug(("New location is adjacent to island, skipping.\n"));
1789 is2 = island_add(tobuild, newx, newy, 0);
1790 /* Must get is again at this point; the array might have
1791 * been realloced by island_add... */
1792 is = &tobuild->islands[i]; /* ...but order will not change. */
1794 ni_curr++; ni_bad = 0;
1796 island_join(is, is2, random_upto(rs, tobuild->maxb)+1, 0);
1797 debug_state(tobuild);
1802 if (ni_bad > MAX_NEWISLAND_TRIES) {
1803 debug(("Unable to create any new islands after %d tries; "
1804 "created %d [%d%%] (instead of %d [%d%%] requested).\n",
1805 MAX_NEWISLAND_TRIES,
1806 ni_curr, ni_curr * 100 / wh,
1807 ni_req, ni_req * 100 / wh));
1814 debug(("Only generated one island (!), retrying.\n"));
1817 /* Check we have at least one island on each extremity of the grid. */
1819 for (x = 0; x < params->w; x++) {
1820 if (INDEX(tobuild, gridi, x, 0)) echeck |= 1;
1821 if (INDEX(tobuild, gridi, x, params->h-1)) echeck |= 2;
1823 for (y = 0; y < params->h; y++) {
1824 if (INDEX(tobuild, gridi, 0, y)) echeck |= 4;
1825 if (INDEX(tobuild, gridi, params->w-1, y)) echeck |= 8;
1828 debug(("Generated grid doesn't fill to sides, retrying.\n"));
1833 map_find_orthogonal(tobuild);
1835 if (params->difficulty > 0) {
1836 if ((ni_curr > MIN_SENSIBLE_ISLANDS) &&
1837 (solve_from_scratch(tobuild, params->difficulty-1) > 0)) {
1838 debug(("Grid is solvable at difficulty %d (too easy); retrying.\n",
1839 params->difficulty-1));
1844 if (solve_from_scratch(tobuild, params->difficulty) == 0) {
1845 debug(("Grid not solvable at difficulty %d, (too hard); retrying.\n",
1846 params->difficulty));
1850 /* ... tobuild is now solved. We rely on this making the diff for aux. */
1851 debug_state(tobuild);
1852 ret = encode_game(tobuild);
1854 game_state *clean = dup_game(tobuild);
1856 map_update_possibles(clean);
1857 *aux = game_state_diff(clean, tobuild);
1865 static char *validate_desc(game_params *params, char *desc)
1867 int i, wh = params->w * params->h;
1869 for (i = 0; i < wh; i++) {
1870 if (*desc >= '1' && *desc <= '9')
1872 else if (*desc >= 'a' && *desc <= 'z')
1873 i += *desc - 'a'; /* plus the i++ */
1874 else if (*desc >= 'A' && *desc <= 'G')
1876 else if (*desc == 'V' || *desc == 'W' ||
1877 *desc == 'X' || *desc == 'Y' ||
1878 *desc == 'H' || *desc == 'I' ||
1879 *desc == 'J' || *desc == 'K')
1882 return "Game description shorter than expected";
1884 return "Game description containers unexpected character";
1887 if (*desc || i > wh)
1888 return "Game description longer than expected";
1893 static game_state *new_game_sub(game_params *params, char *desc)
1895 game_state *state = new_state(params);
1898 debug(("new_game[_sub]: desc = '%s'.\n", desc));
1900 for (y = 0; y < params->h; y++) {
1901 for (x = 0; x < params->w; x++) {
1907 if (c >= 'a' && c <= 'z')
1917 case '1': case '2': case '3': case '4':
1918 case '5': case '6': case '7': case '8': case '9':
1919 island_add(state, x, y, (c - '0'));
1922 case 'A': case 'B': case 'C': case 'D':
1923 case 'E': case 'F': case 'G':
1924 island_add(state, x, y, (c - 'A') + 10);
1932 assert(!"Malformed desc.");
1937 if (*desc) assert(!"Over-long desc.");
1939 map_find_orthogonal(state);
1940 map_update_possibles(state);
1945 static game_state *new_game(midend *me, game_params *params, char *desc)
1947 return new_game_sub(params, desc);
1951 int dragx_src, dragy_src; /* source; -1 means no drag */
1952 int dragx_dst, dragy_dst; /* src's closest orth island. */
1954 int dragging, drag_is_noline, nlines;
1956 int cur_x, cur_y, cur_visible; /* cursor position */
1960 static char *ui_cancel_drag(game_ui *ui)
1962 ui->dragx_src = ui->dragy_src = -1;
1963 ui->dragx_dst = ui->dragy_dst = -1;
1968 static game_ui *new_ui(game_state *state)
1970 game_ui *ui = snew(game_ui);
1972 ui->cur_x = state->islands[0].x;
1973 ui->cur_y = state->islands[0].y;
1974 ui->cur_visible = 0;
1979 static void free_ui(game_ui *ui)
1984 static char *encode_ui(game_ui *ui)
1989 static void decode_ui(game_ui *ui, char *encoding)
1993 static void game_changed_state(game_ui *ui, game_state *oldstate,
1994 game_state *newstate)
1998 struct game_drawstate {
2003 int started, dragging;
2007 static char *update_drag_dst(game_state *state, game_ui *ui, game_drawstate *ds,
2010 int ox, oy, dx, dy, i, currl, maxb;
2012 grid_type gtype, ntype, mtype, curr;
2014 if (ui->dragx_src == -1 || ui->dragy_src == -1) return NULL;
2019 /* work out which of the four directions we're closest to... */
2020 ox = COORD(ui->dragx_src) + TILE_SIZE/2;
2021 oy = COORD(ui->dragy_src) + TILE_SIZE/2;
2023 if (abs(nx-ox) < abs(ny-oy)) {
2025 dy = (ny-oy) < 0 ? -1 : 1;
2026 gtype = G_LINEV; ntype = G_NOLINEV; mtype = G_MARKV;
2027 maxb = INDEX(state, maxv, ui->dragx_src+dx, ui->dragy_src+dy);
2030 dx = (nx-ox) < 0 ? -1 : 1;
2031 gtype = G_LINEH; ntype = G_NOLINEH; mtype = G_MARKH;
2032 maxb = INDEX(state, maxh, ui->dragx_src+dx, ui->dragy_src+dy);
2034 if (ui->drag_is_noline) {
2037 curr = GRID(state, ui->dragx_src+dx, ui->dragy_src+dy);
2038 currl = INDEX(state, lines, ui->dragx_src+dx, ui->dragy_src+dy);
2041 if (currl == maxb) {
2046 ui->nlines = currl + 1;
2054 /* ... and see if there's an island off in that direction. */
2055 is = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2056 for (i = 0; i < is->adj.npoints; i++) {
2057 if (is->adj.points[i].off == 0) continue;
2058 curr = GRID(state, is->x+dx, is->y+dy);
2059 if (curr & mtype) continue; /* don't allow changes to marked lines. */
2060 if (ui->drag_is_noline) {
2061 if (curr & gtype) continue; /* no no-line where already a line */
2063 if (POSSIBLES(state, dx, is->x+dx, is->y+dy) == 0) continue; /* no line if !possible. */
2064 if (curr & ntype) continue; /* can't have a bridge where there's a no-line. */
2067 if (is->adj.points[i].dx == dx &&
2068 is->adj.points[i].dy == dy) {
2069 ui->dragx_dst = ISLAND_ORTHX(is,i);
2070 ui->dragy_dst = ISLAND_ORTHY(is,i);
2073 /*debug(("update_drag src (%d,%d) d(%d,%d) dst (%d,%d)\n",
2074 ui->dragx_src, ui->dragy_src, dx, dy,
2075 ui->dragx_dst, ui->dragy_dst));*/
2079 static char *finish_drag(game_state *state, game_ui *ui)
2083 if (ui->dragx_src == -1 || ui->dragy_src == -1)
2085 if (ui->dragx_dst == -1 || ui->dragy_dst == -1)
2086 return ui_cancel_drag(ui);
2088 if (ui->drag_is_noline) {
2089 sprintf(buf, "N%d,%d,%d,%d",
2090 ui->dragx_src, ui->dragy_src,
2091 ui->dragx_dst, ui->dragy_dst);
2093 sprintf(buf, "L%d,%d,%d,%d,%d",
2094 ui->dragx_src, ui->dragy_src,
2095 ui->dragx_dst, ui->dragy_dst, ui->nlines);
2103 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2104 int x, int y, int button)
2106 int gx = FROMCOORD(x), gy = FROMCOORD(y);
2108 grid_type ggrid = INGRID(state,gx,gy) ? GRID(state,gx,gy) : 0;
2110 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2111 if (!INGRID(state, gx, gy)) return NULL;
2112 ui->cur_visible = 0;
2113 if ((ggrid & G_ISLAND) && !(ggrid & G_MARK)) {
2118 return ui_cancel_drag(ui);
2119 } else if (button == LEFT_DRAG || button == RIGHT_DRAG) {
2120 if (gx != ui->dragx_src || gy != ui->dragy_src) {
2122 ui->drag_is_noline = (button == RIGHT_DRAG) ? 1 : 0;
2123 return update_drag_dst(state, ui, ds, x, y);
2125 /* cancel a drag when we go back to the starting point */
2130 } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) {
2132 return finish_drag(state, ui);
2135 if (!INGRID(state, gx, gy)) return NULL;
2136 if (!(GRID(state, gx, gy) & G_ISLAND)) return NULL;
2137 sprintf(buf, "M%d,%d", gx, gy);
2140 } else if (button == 'h' || button == 'H') {
2141 game_state *solved = dup_game(state);
2142 solve_for_hint(solved);
2143 ret = game_state_diff(state, solved);
2146 } else if (IS_CURSOR_MOVE(button)) {
2147 ui->cur_visible = 1;
2149 int nx = ui->cur_x, ny = ui->cur_y;
2151 move_cursor(button, &nx, &ny, state->w, state->h, 0);
2152 update_drag_dst(state, ui, ds,
2153 COORD(nx)+TILE_SIZE/2,
2154 COORD(ny)+TILE_SIZE/2);
2155 return finish_drag(state, ui);
2157 int dx = (button == CURSOR_RIGHT) ? +1 : (button == CURSOR_LEFT) ? -1 : 0;
2158 int dy = (button == CURSOR_DOWN) ? +1 : (button == CURSOR_UP) ? -1 : 0;
2159 int dorthx = 1 - abs(dx), dorthy = 1 - abs(dy);
2160 int dir, orth, nx = x, ny = y;
2162 /* 'orthorder' is a tweak to ensure that if you press RIGHT and
2163 * happen to move upwards, when you press LEFT you then tend
2164 * downwards (rather than upwards again). */
2165 int orthorder = (button == CURSOR_LEFT || button == CURSOR_UP) ? 1 : -1;
2167 /* This attempts to find an island in the direction you're
2168 * asking for, broadly speaking. If you ask to go right, for
2169 * example, it'll look for islands to the right and slightly
2170 * above or below your current horiz. position, allowing
2171 * further above/below the further away it searches. */
2173 assert(GRID(state, ui->cur_x, ui->cur_y) & G_ISLAND);
2174 /* currently this is depth-first (so orthogonally-adjacent
2175 * islands across the other side of the grid will be moved to
2176 * before closer islands slightly offset). Swap the order of
2177 * these two loops to change to breadth-first search. */
2178 for (orth = 0; ; orth++) {
2180 for (dir = 1; ; dir++) {
2183 if (orth > dir) continue; /* only search in cone outwards. */
2185 nx = ui->cur_x + dir*dx + orth*dorthx*orthorder;
2186 ny = ui->cur_y + dir*dy + orth*dorthy*orthorder;
2187 if (INGRID(state, nx, ny)) {
2188 dingrid = oingrid = 1;
2189 if (GRID(state, nx, ny) & G_ISLAND) goto found;
2192 nx = ui->cur_x + dir*dx - orth*dorthx*orthorder;
2193 ny = ui->cur_y + dir*dy - orth*dorthy*orthorder;
2194 if (INGRID(state, nx, ny)) {
2195 dingrid = oingrid = 1;
2196 if (GRID(state, nx, ny) & G_ISLAND) goto found;
2199 if (!dingrid) break;
2201 if (!oingrid) return "";
2210 } else if (IS_CURSOR_SELECT(button)) {
2211 if (!ui->cur_visible) {
2212 ui->cur_visible = 1;
2217 if (ui->dragx_dst == -1 && ui->dragy_dst == -1) {
2218 sprintf(buf, "M%d,%d", ui->cur_x, ui->cur_y);
2223 grid_type v = GRID(state, ui->cur_x, ui->cur_y);
2226 ui->dragx_src = ui->cur_x;
2227 ui->dragy_src = ui->cur_y;
2228 ui->dragx_dst = ui->dragy_dst = -1;
2229 ui->drag_is_noline = (button == CURSOR_SELECT2) ? 1 : 0;
2233 } else if (button == 'g' || button == 'G') {
2234 ui->show_hints = 1 - ui->show_hints;
2241 static game_state *execute_move(game_state *state, char *move)
2243 game_state *ret = dup_game(state);
2244 int x1, y1, x2, y2, nl, n;
2245 struct island *is1, *is2;
2248 debug(("execute_move: %s\n", move));
2250 if (!*move) goto badmove;
2256 } else if (c == 'L') {
2257 if (sscanf(move, "%d,%d,%d,%d,%d%n",
2258 &x1, &y1, &x2, &y2, &nl, &n) != 5)
2260 if (!INGRID(ret, x1, y1) || !INGRID(ret, x2, y2))
2262 is1 = INDEX(ret, gridi, x1, y1);
2263 is2 = INDEX(ret, gridi, x2, y2);
2264 if (!is1 || !is2) goto badmove;
2265 if (nl < 0 || nl > state->maxb) goto badmove;
2266 island_join(is1, is2, nl, 0);
2267 } else if (c == 'N') {
2268 if (sscanf(move, "%d,%d,%d,%d%n",
2269 &x1, &y1, &x2, &y2, &n) != 4)
2271 if (!INGRID(ret, x1, y1) || !INGRID(ret, x2, y2))
2273 is1 = INDEX(ret, gridi, x1, y1);
2274 is2 = INDEX(ret, gridi, x2, y2);
2275 if (!is1 || !is2) goto badmove;
2276 island_join(is1, is2, -1, 0);
2277 } else if (c == 'M') {
2278 if (sscanf(move, "%d,%d%n",
2281 if (!INGRID(ret, x1, y1))
2283 is1 = INDEX(ret, gridi, x1, y1);
2284 if (!is1) goto badmove;
2285 island_togglemark(is1);
2292 else if (*move) goto badmove;
2295 map_update_possibles(ret);
2296 if (map_check(ret)) {
2297 debug(("Game completed.\n"));
2303 debug(("%s: unrecognised move.\n", move));
2308 static char *solve_game(game_state *state, game_state *currstate,
2309 char *aux, char **error)
2315 debug(("solve_game: aux = %s\n", aux));
2316 solved = execute_move(state, aux);
2318 *error = "Generated aux string is not a valid move (!).";
2322 solved = dup_game(state);
2323 /* solve with max strength... */
2324 if (solve_from_scratch(solved, 10) == 0) {
2326 *error = "Game does not have a (non-recursive) solution.";
2330 ret = game_state_diff(currstate, solved);
2332 debug(("solve_game: ret = %s\n", ret));
2336 /* ----------------------------------------------------------------------
2340 static void game_compute_size(game_params *params, int tilesize,
2343 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2344 struct { int tilesize; } ads, *ds = &ads;
2345 ads.tilesize = tilesize;
2347 *x = TILE_SIZE * params->w + 2 * BORDER;
2348 *y = TILE_SIZE * params->h + 2 * BORDER;
2351 static void game_set_size(drawing *dr, game_drawstate *ds,
2352 game_params *params, int tilesize)
2354 ds->tilesize = tilesize;
2357 static float *game_colours(frontend *fe, int *ncolours)
2359 float *ret = snewn(3 * NCOLOURS, float);
2362 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
2364 for (i = 0; i < 3; i++) {
2365 ret[COL_FOREGROUND * 3 + i] = 0.0F;
2366 ret[COL_HINT * 3 + i] = ret[COL_LOWLIGHT * 3 + i];
2367 ret[COL_GRID * 3 + i] =
2368 (ret[COL_HINT * 3 + i] + ret[COL_BACKGROUND * 3 + i]) * 0.5F;
2369 ret[COL_MARK * 3 + i] = ret[COL_HIGHLIGHT * 3 + i];
2371 ret[COL_WARNING * 3 + 0] = 1.0F;
2372 ret[COL_WARNING * 3 + 1] = 0.25F;
2373 ret[COL_WARNING * 3 + 2] = 0.25F;
2375 ret[COL_SELECTED * 3 + 0] = 0.25F;
2376 ret[COL_SELECTED * 3 + 1] = 1.00F;
2377 ret[COL_SELECTED * 3 + 2] = 0.25F;
2379 ret[COL_CURSOR * 3 + 0] = min(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
2380 ret[COL_CURSOR * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
2381 ret[COL_CURSOR * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
2383 *ncolours = NCOLOURS;
2387 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2389 struct game_drawstate *ds = snew(struct game_drawstate);
2390 int wh = state->w*state->h;
2396 ds->grid = snewn(wh, grid_type);
2397 memset(ds->grid, -1, wh*sizeof(grid_type));
2398 ds->lv = snewn(wh, int);
2399 ds->lh = snewn(wh, int);
2400 memset(ds->lv, 0, wh*sizeof(int));
2401 memset(ds->lh, 0, wh*sizeof(int));
2407 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2415 #define LINE_WIDTH (TILE_SIZE/8)
2416 #define TS8(x) (((x)*TILE_SIZE)/8)
2418 #define OFFSET(thing) ((TILE_SIZE/2) - ((thing)/2))
2420 static void lines_vert(drawing *dr, game_drawstate *ds,
2421 int ox, int oy, int lv, int col, grid_type v)
2423 int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2424 while ((bw = lw * lv + gw * (lv+1)) > TILE_SIZE)
2428 draw_rect(dr, ox + loff, oy, bw, TILE_SIZE, COL_MARK);
2429 for (i = 0; i < lv; i++, loff += lw + gw)
2430 draw_rect(dr, ox + loff + gw, oy, lw, TILE_SIZE, col);
2433 static void lines_horiz(drawing *dr, game_drawstate *ds,
2434 int ox, int oy, int lh, int col, grid_type v)
2436 int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2437 while ((bw = lw * lh + gw * (lh+1)) > TILE_SIZE)
2441 draw_rect(dr, ox, oy + loff, TILE_SIZE, bw, COL_MARK);
2442 for (i = 0; i < lh; i++, loff += lw + gw)
2443 draw_rect(dr, ox, oy + loff + gw, TILE_SIZE, lw, col);
2446 static void line_cross(drawing *dr, game_drawstate *ds,
2447 int ox, int oy, int col, grid_type v)
2450 draw_line(dr, ox, oy, ox+off, oy+off, col);
2451 draw_line(dr, ox+off, oy, ox, oy+off, col);
2454 static int between_island(game_state *state, int sx, int sy, int dx, int dy)
2456 int x = sx - dx, y = sy - dy;
2458 while (INGRID(state, x, y)) {
2459 if (GRID(state, x, y) & G_ISLAND) goto found;
2464 x = sx + dx, y = sy + dy;
2465 while (INGRID(state, x, y)) {
2466 if (GRID(state, x, y) & G_ISLAND) return 1;
2472 static void lines_lvlh(game_state *state, game_ui *ui, int x, int y, grid_type v,
2473 int *lv_r, int *lh_r)
2477 if (v & G_LINEV) lv = INDEX(state,lines,x,y);
2478 if (v & G_LINEH) lh = INDEX(state,lines,x,y);
2480 if (ui->show_hints) {
2481 if (between_island(state, x, y, 0, 1) && !lv) lv = 1;
2482 if (between_island(state, x, y, 1, 0) && !lh) lh = 1;
2484 /*debug(("lvlh: (%d,%d) v 0x%x lv %d lh %d.\n", x, y, v, lv, lh));*/
2485 *lv_r = lv; *lh_r = lh;
2488 static void dsf_debug_draw(drawing *dr,
2489 game_state *state, game_drawstate *ds,
2493 int ts = TILE_SIZE/2;
2494 int ox = COORD(x) + ts/2, oy = COORD(y) + ts/2;
2497 sprintf(str, "%d", dsf_canonify(state->solver->dsf, DINDEX(x,y)));
2498 draw_text(dr, ox, oy, FONT_VARIABLE, ts,
2499 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_WARNING, str);
2503 static void lines_redraw(drawing *dr,
2504 game_state *state, game_drawstate *ds, game_ui *ui,
2505 int x, int y, grid_type v, int lv, int lh)
2507 int ox = COORD(x), oy = COORD(y);
2508 int vcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2509 (v & G_WARN) ? COL_WARNING : COL_FOREGROUND, hcol = vcol;
2510 grid_type todraw = v & G_NOLINE;
2513 if (ui->todraw & G_FLAGSH) hcol = COL_SELECTED;
2514 if (ui->todraw & G_FLAGSV) vcol = COL_SELECTED;
2515 todraw |= ui->todraw;
2518 draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2520 draw_rect(dr, ox+TILE_SIZE/4, oy+TILE_SIZE/4,
2521 TILE_SIZE/2, TILE_SIZE/2, COL_CURSOR);*/
2524 if (ui->show_hints) {
2525 if (between_island(state, x, y, 0, 1) && !(v & G_LINEV))
2527 if (between_island(state, x, y, 1, 0) && !(v & G_LINEH))
2531 draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_GRID);
2534 if (todraw & G_NOLINEV) {
2535 line_cross(dr, ds, ox + TS8(3), oy + TS8(1), vcol, todraw);
2536 line_cross(dr, ds, ox + TS8(3), oy + TS8(5), vcol, todraw);
2538 if (todraw & G_NOLINEH) {
2539 line_cross(dr, ds, ox + TS8(1), oy + TS8(3), hcol, todraw);
2540 line_cross(dr, ds, ox + TS8(5), oy + TS8(3), hcol, todraw);
2542 /* if we're drawing a real line and a hint, make sure we draw the real
2544 if (lv && vcol == COL_HINT) lines_vert(dr, ds, ox, oy, lv, vcol, v);
2545 if (lh) lines_horiz(dr, ds, ox, oy, lh, hcol, v);
2546 if (lv && vcol != COL_HINT) lines_vert(dr, ds, ox, oy, lv, vcol, v);
2548 dsf_debug_draw(dr, state, ds, x, y);
2549 draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2552 #define ISLAND_RADIUS ((TILE_SIZE*12)/20)
2553 #define ISLAND_NUMSIZE(is) \
2554 (((is)->count < 10) ? (TILE_SIZE*7)/10 : (TILE_SIZE*5)/10)
2556 static void island_redraw(drawing *dr,
2557 game_state *state, game_drawstate *ds,
2558 struct island *is, grid_type v)
2560 /* These overlap the edges of their squares, which is why they're drawn later.
2561 * We know they can't overlap each other because they're not allowed within 2
2562 * squares of each other. */
2563 int half = TILE_SIZE/2;
2564 int ox = COORD(is->x) + half, oy = COORD(is->y) + half;
2565 int orad = ISLAND_RADIUS, irad = orad - LINE_WIDTH;
2566 int updatesz = orad*2+1;
2567 int tcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2568 (v & G_WARN) ? COL_WARNING : COL_FOREGROUND;
2569 int col = (v & G_ISSEL) ? COL_SELECTED : tcol;
2570 int bg = (v & G_CURSOR) ? COL_CURSOR :
2571 (v & G_MARK) ? COL_MARK : COL_BACKGROUND;
2575 draw_rect_outline(dr, COORD(is->x), COORD(is->y),
2576 TILE_SIZE, TILE_SIZE, COL_GRID);
2579 /* draw a thick circle */
2580 draw_circle(dr, ox, oy, orad, col, col);
2581 draw_circle(dr, ox, oy, irad, bg, bg);
2583 sprintf(str, "%d", is->count);
2584 draw_text(dr, ox, oy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2585 ALIGN_VCENTRE | ALIGN_HCENTRE, tcol, str);
2587 dsf_debug_draw(dr, state, ds, is->x, is->y);
2588 draw_update(dr, ox - orad, oy - orad, updatesz, updatesz);
2591 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2592 game_state *state, int dir, game_ui *ui,
2593 float animtime, float flashtime)
2595 int x, y, force = 0, i, j, redraw, lv, lh;
2596 grid_type v, dsv, flash = 0;
2597 struct island *is, *is_drag_src = NULL, *is_drag_dst = NULL;
2600 int f = (int)(flashtime * 5 / FLASH_TIME);
2601 if (f == 1 || f == 3) flash = G_FLASH;
2604 /* Clear screen, if required. */
2607 TILE_SIZE * ds->w + 2 * BORDER,
2608 TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND);
2610 draw_rect_outline(dr,
2611 COORD(0)-1, COORD(0)-1,
2612 TILE_SIZE * ds->w + 2, TILE_SIZE * ds->h + 2,
2615 draw_update(dr, 0, 0,
2616 TILE_SIZE * ds->w + 2 * BORDER,
2617 TILE_SIZE * ds->h + 2 * BORDER);
2622 if (ui->dragx_src != -1 && ui->dragy_src != -1) {
2624 is_drag_src = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2625 assert(is_drag_src);
2626 if (ui->dragx_dst != -1 && ui->dragy_dst != -1) {
2627 is_drag_dst = INDEX(state, gridi, ui->dragx_dst, ui->dragy_dst);
2628 assert(is_drag_dst);
2633 if (ui->show_hints != ds->show_hints) {
2635 ds->show_hints = ui->show_hints;
2638 /* Draw all lines (and hints, if we want), but *not* islands. */
2639 for (x = 0; x < ds->w; x++) {
2640 for (y = 0; y < ds->h; y++) {
2641 v = GRID(state, x, y) | flash;
2642 dsv = GRID(ds,x,y) & ~G_REDRAW;
2644 if (v & G_ISLAND) continue;
2647 if (WITHIN(x,is_drag_src->x, is_drag_dst->x) &&
2648 WITHIN(y,is_drag_src->y, is_drag_dst->y))
2651 lines_lvlh(state, ui, x, y, v, &lv, &lh);
2653 /*if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
2657 lv != INDEX(ds,lv,x,y) ||
2658 lh != INDEX(ds,lh,x,y) ||
2660 GRID(ds, x, y) = v | G_REDRAW;
2661 INDEX(ds,lv,x,y) = lv;
2662 INDEX(ds,lh,x,y) = lh;
2663 lines_redraw(dr, state, ds, ui, x, y, v, lv, lh);
2665 GRID(ds,x,y) &= ~G_REDRAW;
2670 for (i = 0; i < state->n_islands; i++) {
2671 is = &state->islands[i];
2672 v = GRID(state, is->x, is->y) | flash;
2675 for (j = 0; j < is->adj.npoints; j++) {
2676 if (GRID(ds,is->adj.points[j].x,is->adj.points[j].y) & G_REDRAW) {
2682 if (is == is_drag_src)
2684 else if (is_drag_dst && is == is_drag_dst)
2688 if (island_impossible(is, v & G_MARK)) v |= G_WARN;
2690 if (ui->cur_visible && ui->cur_x == is->x && ui->cur_y == is->y)
2693 if ((v != GRID(ds, is->x, is->y)) || force || redraw) {
2694 GRID(ds,is->x,is->y) = v;
2695 island_redraw(dr, state, ds, is, v);
2700 static float game_anim_length(game_state *oldstate, game_state *newstate,
2701 int dir, game_ui *ui)
2706 static float game_flash_length(game_state *oldstate, game_state *newstate,
2707 int dir, game_ui *ui)
2709 if (!oldstate->completed && newstate->completed &&
2710 !oldstate->solved && !newstate->solved)
2716 static int game_status(game_state *state)
2718 return state->completed ? +1 : 0;
2721 static int game_timing_state(game_state *state, game_ui *ui)
2726 static void game_print_size(game_params *params, float *x, float *y)
2730 /* 10mm squares by default. */
2731 game_compute_size(params, 1000, &pw, &ph);
2736 static void game_print(drawing *dr, game_state *state, int ts)
2738 int ink = print_mono_colour(dr, 0);
2739 int paper = print_mono_colour(dr, 1);
2740 int x, y, cx, cy, i, nl;
2744 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2745 game_drawstate ads, *ds = &ads;
2748 /* I don't think this wants a border. */
2751 loff = ts / (8 * sqrt((state->params.maxb - 1)));
2752 print_line_width(dr, ts / 12);
2753 for (x = 0; x < state->w; x++) {
2754 for (y = 0; y < state->h; y++) {
2755 cx = COORD(x); cy = COORD(y);
2756 grid = GRID(state,x,y);
2757 nl = INDEX(state,lines,x,y);
2759 if (grid & G_ISLAND) continue;
2760 if (grid & G_LINEV) {
2761 for (i = 0; i < nl; i++)
2762 draw_line(dr, cx+ts/2+(2*i-nl+1)*loff, cy,
2763 cx+ts/2+(2*i-nl+1)*loff, cy+ts, ink);
2765 if (grid & G_LINEH) {
2766 for (i = 0; i < nl; i++)
2767 draw_line(dr, cx, cy+ts/2+(2*i-nl+1)*loff,
2768 cx+ts, cy+ts/2+(2*i-nl+1)*loff, ink);
2774 for (i = 0; i < state->n_islands; i++) {
2776 struct island *is = &state->islands[i];
2777 grid = GRID(state, is->x, is->y);
2778 cx = COORD(is->x) + ts/2;
2779 cy = COORD(is->y) + ts/2;
2781 draw_circle(dr, cx, cy, ISLAND_RADIUS, paper, ink);
2783 sprintf(str, "%d", is->count);
2784 draw_text(dr, cx, cy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2785 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
2790 #define thegame bridges
2793 const struct game thegame = {
2794 "Bridges", "games.bridges", "bridges",
2801 TRUE, game_configure, custom_params,
2809 TRUE, game_can_format_as_text_now, game_text_format,
2817 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2820 game_free_drawstate,
2825 TRUE, FALSE, game_print_size, game_print,
2826 FALSE, /* wants_statusbar */
2827 FALSE, game_timing_state,
2828 REQUIRE_RBUTTON, /* flags */
2831 /* vim: set shiftwidth=4 tabstop=8: */