Fix incorrect uses of ctype.h (passing it uncast chars, or other
[sgt-puzzles/ydirson.git] / bridges.c
blob4219df4c0d07a22d083d2de2ce2170c3a35dee56
1 /*
2 * bridges.c: Implementation of the Nikoli game 'Bridges'.
4 * Things still to do:
6 * * write a recursive solver?
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <ctype.h>
14 #include <math.h>
16 #include "puzzles.h"
18 /* Turn this on for hints about which lines are considered possibilities. */
19 #undef DRAW_GRID
20 #undef DRAW_DSF
22 /* --- structures for params, state, etc. --- */
24 #define MAX_BRIDGES 4
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
35 enum {
36 COL_BACKGROUND,
37 COL_FOREGROUND,
38 COL_HIGHLIGHT, COL_LOWLIGHT,
39 COL_SELECTED, COL_MARK,
40 COL_HINT, COL_GRID,
41 COL_WARNING,
42 COL_CURSOR,
43 NCOLOURS
46 struct game_params {
47 int w, h, maxb;
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
68 #define G_WARN 0x0400
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. */
79 struct solver_state {
80 int *dsf, *tmpdsf;
81 int refcount;
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;
94 struct island {
95 game_state *state;
96 int x, y, count;
97 struct surrounds adj;
100 struct game_state {
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)
141 int i;
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];
146 is->state = state;
147 INDEX(state, gridi, is->x, is->y) = is;
151 static int game_can_format_as_text_now(game_params *params)
153 return TRUE;
156 static char *game_text_format(game_state *state)
158 int x, y, len, nl;
159 char *ret, *p;
160 struct island *is;
161 grid_type grid;
163 len = (state->h) * (state->w+1) + 1;
164 ret = snewn(len, char);
165 p = ret;
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);
172 if (is) {
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) ? '-' : '~';
178 } else {
179 *p++ = '.';
182 *p++ = '\n';
184 *p++ = '\0';
186 assert(p - ret == len);
187 return ret;
190 static void debug_state(game_state *state)
192 char *textversion = game_text_format(state);
193 debug(("%s", textversion));
194 sfree(textversion);
197 /*static void debug_possibles(game_state *state)
199 int x, y;
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)));
205 debug((" "));
206 for (x = 0; x < state->w; x++) {
207 debug(("%d", POSSIBLES(state, 0, x, y)));
209 debug(("\n"));
211 debug(("\n"));
212 for (y = 0; y < state->h; y++) {
213 for (x = 0; x < state->w; x++) {
214 debug(("%d", MAXIMUM(state, 1, x, y)));
216 debug((" "));
217 for (x = 0; x < state->w; x++) {
218 debug(("%d", MAXIMUM(state, 0, x, y)));
220 debug(("\n"));
222 debug(("\n"));
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 {\
230 if (cond) { \
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; \
236 is->adj.npoints++; \
237 } } while(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;
254 x = is->x + dx;
255 y = is->y + dy;
256 off = 1;
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;
261 is->adj.nislands++;
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)));*/
265 goto foundisland;
267 off++; x += dx; y += dy;
269 foundisland:
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;
281 return 0;
284 static struct island *island_find_connection(struct island *is, int adjpt)
286 struct island *is_r;
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));
294 assert(is_r);
296 return is_r;
299 static struct island *island_add(game_state *state, int x, int y, int count)
301 struct island *is;
302 int realloced = 0;
304 assert(!(GRID(state,x,y) & G_ISLAND));
305 GRID(state,x,y) |= G_ISLAND;
307 state->n_islands++;
308 if (state->n_islands > state->n_islands_alloc) {
309 state->n_islands_alloc = state->n_islands * 2;
310 state->islands =
311 sresize(state->islands, state->n_islands_alloc, struct island);
312 realloced = 1;
314 is = &state->islands[state->n_islands-1];
316 memset(is, 0, sizeof(struct island));
317 is->state = state;
318 is->x = x;
319 is->y = y;
320 is->count = count;
321 island_set_surrounds(is);
323 if (realloced)
324 fixup_islands_for_realloc(state);
325 else
326 INDEX(state, gridi, x, y) = is;
328 return 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;
336 int s, e, x, y;
338 assert(i1->state == i2->state);
339 assert(n >= -1 && n <= i1->state->maxb);
341 if (i1->x == i2->x) {
342 x = i1->x;
343 if (i1->y < i2->y) {
344 s = i1->y+1; e = i2->y-1;
345 } else {
346 s = i2->y+1; e = i1->y-1;
348 for (y = s; y <= e; y++) {
349 if (is_max) {
350 INDEX(state,maxv,x,y) = n;
351 } else {
352 if (n < 0) {
353 GRID(state,x,y) ^= G_NOLINEV;
354 } else if (n == 0) {
355 GRID(state,x,y) &= ~G_LINEV;
356 } else {
357 GRID(state,x,y) |= G_LINEV;
358 INDEX(state,lines,x,y) = n;
362 } else if (i1->y == i2->y) {
363 y = i1->y;
364 if (i1->x < i2->x) {
365 s = i1->x+1; e = i2->x-1;
366 } else {
367 s = i2->x+1; e = i1->x-1;
369 for (x = s; x <= e; x++) {
370 if (is_max) {
371 INDEX(state,maxh,x,y) = n;
372 } else {
373 if (n < 0) {
374 GRID(state,x,y) ^= G_NOLINEH;
375 } else if (n == 0) {
376 GRID(state,x,y) &= ~G_LINEH;
377 } else {
378 GRID(state,x,y) |= G_LINEH;
379 INDEX(state,lines,x,y) = n;
383 } else {
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)
391 int i, c = 0;
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));*/
399 return c;
402 static int island_adjspace(struct island *is, int marks, int missing,
403 int direction)
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;
413 if (marks) {
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);
423 return poss;
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);
438 return c;
441 static int island_isadj(struct island *is, int direction)
443 int x, y;
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);
455 } else {
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);
460 return 0;
463 /* Counts the no. of possible adjacent islands (including islands
464 * we're already connected to). */
465 static int island_countadj(struct island *is)
467 int i, nadj = 0;
469 for (i = 0; i < is->adj.npoints; i++) {
470 if (island_isadj(is, i)) nadj++;
472 return nadj;
475 static void island_togglemark(struct island *is)
477 int i, j, x, y, o;
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))
495 continue;
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++) {
503 GRID(is_loop->state,
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;
515 int i, poss;
516 grid_type v;
517 struct island *is_orth;
519 if (nspc < 0) {
520 debug(("island at (%d,%d) impossible because full.\n", is->x, is->y));
521 return 1; /* too many bridges */
522 } else if ((curr + island_countspaces(is, 0)) < is->count) {
523 debug(("island at (%d,%d) impossible because not enough spaces.\n", is->x, is->y));
524 return 1; /* impossible to create enough bridges */
525 } else if (strict && curr < is->count) {
526 debug(("island at (%d,%d) impossible because locked.\n", is->x, is->y));
527 return 1; /* not enough bridges and island is locked */
530 /* Count spaces in surrounding islands. */
531 nsurrspc = 0;
532 for (i = 0; i < is->adj.npoints; i++) {
533 int ifree, dx = is->adj.points[i].dx;
535 if (!is->adj.points[i].off) continue;
536 v = GRID(is->state, is->adj.points[i].x, is->adj.points[i].y);
537 poss = POSSIBLES(is->state, dx,
538 is->adj.points[i].x, is->adj.points[i].y);
539 if (poss == 0) continue;
540 is_orth = INDEX(is->state, gridi,
541 ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i));
542 assert(is_orth);
544 ifree = is_orth->count - island_countbridges(is_orth);
545 if (ifree > 0) {
547 * ifree is the number of bridges unfilled in the other
548 * island, which is clearly an upper bound on the number
549 * of extra bridges this island may run to it.
551 * Another upper bound is the number of bridges unfilled
552 * on the specific line between here and there. We must
553 * take the minimum of both.
555 int bmax = MAXIMUM(is->state, dx,
556 is->adj.points[i].x, is->adj.points[i].y);
557 int bcurr = GRIDCOUNT(is->state,
558 is->adj.points[i].x, is->adj.points[i].y,
559 dx ? G_LINEH : G_LINEV);
560 assert(bcurr <= bmax);
561 nsurrspc += min(ifree, bmax - bcurr);
564 if (nsurrspc < nspc) {
565 debug(("island at (%d,%d) impossible: surr. islands %d spc, need %d.\n",
566 is->x, is->y, nsurrspc, nspc));
567 return 1; /* not enough spaces around surrounding islands to fill this one. */
570 return 0;
573 /* --- Game parameter functions --- */
575 #define DEFAULT_PRESET 0
577 const struct game_params bridges_presets[] = {
578 { 7, 7, 2, 30, 10, 1, 0 },
579 { 7, 7, 2, 30, 10, 1, 1 },
580 { 7, 7, 2, 30, 10, 1, 2 },
581 { 10, 10, 2, 30, 10, 1, 0 },
582 { 10, 10, 2, 30, 10, 1, 1 },
583 { 10, 10, 2, 30, 10, 1, 2 },
584 { 15, 15, 2, 30, 10, 1, 0 },
585 { 15, 15, 2, 30, 10, 1, 1 },
586 { 15, 15, 2, 30, 10, 1, 2 },
589 static game_params *default_params(void)
591 game_params *ret = snew(game_params);
592 *ret = bridges_presets[DEFAULT_PRESET];
594 return ret;
597 static int game_fetch_preset(int i, char **name, game_params **params)
599 game_params *ret;
600 char buf[80];
602 if (i < 0 || i >= lenof(bridges_presets))
603 return FALSE;
605 ret = default_params();
606 *ret = bridges_presets[i];
607 *params = ret;
609 sprintf(buf, "%dx%d %s", ret->w, ret->h,
610 ret->difficulty == 0 ? "easy" :
611 ret->difficulty == 1 ? "medium" : "hard");
612 *name = dupstr(buf);
614 return TRUE;
617 static void free_params(game_params *params)
619 sfree(params);
622 static game_params *dup_params(game_params *params)
624 game_params *ret = snew(game_params);
625 *ret = *params; /* structure copy */
626 return ret;
629 #define EATNUM(x) do { \
630 (x) = atoi(string); \
631 while (*string && isdigit((unsigned char)*string)) string++; \
632 } while(0)
634 static void decode_params(game_params *params, char const *string)
636 EATNUM(params->w);
637 params->h = params->w;
638 if (*string == 'x') {
639 string++;
640 EATNUM(params->h);
642 if (*string == 'i') {
643 string++;
644 EATNUM(params->islands);
646 if (*string == 'e') {
647 string++;
648 EATNUM(params->expansion);
650 if (*string == 'm') {
651 string++;
652 EATNUM(params->maxb);
654 params->allowloops = 1;
655 if (*string == 'L') {
656 string++;
657 params->allowloops = 0;
659 if (*string == 'd') {
660 string++;
661 EATNUM(params->difficulty);
665 static char *encode_params(game_params *params, int full)
667 char buf[80];
669 if (full) {
670 sprintf(buf, "%dx%di%de%dm%d%sd%d",
671 params->w, params->h, params->islands, params->expansion,
672 params->maxb, params->allowloops ? "" : "L",
673 params->difficulty);
674 } else {
675 sprintf(buf, "%dx%dm%d%s", params->w, params->h,
676 params->maxb, params->allowloops ? "" : "L");
678 return dupstr(buf);
681 static config_item *game_configure(game_params *params)
683 config_item *ret;
684 char buf[80];
686 ret = snewn(8, config_item);
688 ret[0].name = "Width";
689 ret[0].type = C_STRING;
690 sprintf(buf, "%d", params->w);
691 ret[0].sval = dupstr(buf);
692 ret[0].ival = 0;
694 ret[1].name = "Height";
695 ret[1].type = C_STRING;
696 sprintf(buf, "%d", params->h);
697 ret[1].sval = dupstr(buf);
698 ret[1].ival = 0;
700 ret[2].name = "Difficulty";
701 ret[2].type = C_CHOICES;
702 ret[2].sval = ":Easy:Medium:Hard";
703 ret[2].ival = params->difficulty;
705 ret[3].name = "Allow loops";
706 ret[3].type = C_BOOLEAN;
707 ret[3].sval = NULL;
708 ret[3].ival = params->allowloops;
710 ret[4].name = "Max. bridges per direction";
711 ret[4].type = C_CHOICES;
712 ret[4].sval = ":1:2:3:4"; /* keep up-to-date with MAX_BRIDGES */
713 ret[4].ival = params->maxb - 1;
715 ret[5].name = "%age of island squares";
716 ret[5].type = C_CHOICES;
717 ret[5].sval = ":5%:10%:15%:20%:25%:30%";
718 ret[5].ival = (params->islands / 5)-1;
720 ret[6].name = "Expansion factor (%age)";
721 ret[6].type = C_CHOICES;
722 ret[6].sval = ":0%:10%:20%:30%:40%:50%:60%:70%:80%:90%:100%";
723 ret[6].ival = params->expansion / 10;
725 ret[7].name = NULL;
726 ret[7].type = C_END;
727 ret[7].sval = NULL;
728 ret[7].ival = 0;
730 return ret;
733 static game_params *custom_params(config_item *cfg)
735 game_params *ret = snew(game_params);
737 ret->w = atoi(cfg[0].sval);
738 ret->h = atoi(cfg[1].sval);
739 ret->difficulty = cfg[2].ival;
740 ret->allowloops = cfg[3].ival;
741 ret->maxb = cfg[4].ival + 1;
742 ret->islands = (cfg[5].ival + 1) * 5;
743 ret->expansion = cfg[6].ival * 10;
745 return ret;
748 static char *validate_params(game_params *params, int full)
750 if (params->w < 3 || params->h < 3)
751 return "Width and height must be at least 3";
752 if (params->maxb < 1 || params->maxb > MAX_BRIDGES)
753 return "Too many bridges.";
754 if (full) {
755 if (params->islands <= 0 || params->islands > 30)
756 return "%age of island squares must be between 1% and 30%";
757 if (params->expansion < 0 || params->expansion > 100)
758 return "Expansion factor must be between 0 and 100";
760 return NULL;
763 /* --- Game encoding and differences --- */
765 static char *encode_game(game_state *state)
767 char *ret, *p;
768 int wh = state->w*state->h, run, x, y;
769 struct island *is;
771 ret = snewn(wh + 1, char);
772 p = ret;
773 run = 0;
774 for (y = 0; y < state->h; y++) {
775 for (x = 0; x < state->w; x++) {
776 is = INDEX(state, gridi, x, y);
777 if (is) {
778 if (run) {
779 *p++ = ('a'-1) + run;
780 run = 0;
782 if (is->count < 10)
783 *p++ = '0' + is->count;
784 else
785 *p++ = 'A' + (is->count - 10);
786 } else {
787 if (run == 26) {
788 *p++ = ('a'-1) + run;
789 run = 0;
791 run++;
795 if (run) {
796 *p++ = ('a'-1) + run;
797 run = 0;
799 *p = '\0';
800 assert(p - ret <= wh);
802 return ret;
805 static char *game_state_diff(game_state *src, game_state *dest)
807 int movesize = 256, movelen = 0;
808 char *move = snewn(movesize, char), buf[80];
809 int i, d, x, y, len;
810 grid_type gline, nline;
811 struct island *is_s, *is_d, *is_orth;
813 #define APPEND do { \
814 if (movelen + len >= movesize) { \
815 movesize = movelen + len + 256; \
816 move = sresize(move, movesize, char); \
818 strcpy(move + movelen, buf); \
819 movelen += len; \
820 } while(0)
822 move[movelen++] = 'S';
823 move[movelen] = '\0';
825 assert(src->n_islands == dest->n_islands);
827 for (i = 0; i < src->n_islands; i++) {
828 is_s = &src->islands[i];
829 is_d = &dest->islands[i];
830 assert(is_s->x == is_d->x);
831 assert(is_s->y == is_d->y);
832 assert(is_s->adj.npoints == is_d->adj.npoints); /* more paranoia */
834 for (d = 0; d < is_s->adj.npoints; d++) {
835 if (is_s->adj.points[d].dx == -1 ||
836 is_s->adj.points[d].dy == -1) continue;
838 x = is_s->adj.points[d].x;
839 y = is_s->adj.points[d].y;
840 gline = is_s->adj.points[d].dx ? G_LINEH : G_LINEV;
841 nline = is_s->adj.points[d].dx ? G_NOLINEH : G_NOLINEV;
842 is_orth = INDEX(dest, gridi,
843 ISLAND_ORTHX(is_d, d), ISLAND_ORTHY(is_d, d));
845 if (GRIDCOUNT(src, x, y, gline) != GRIDCOUNT(dest, x, y, gline)) {
846 assert(is_orth);
847 len = sprintf(buf, ";L%d,%d,%d,%d,%d",
848 is_s->x, is_s->y, is_orth->x, is_orth->y,
849 GRIDCOUNT(dest, x, y, gline));
850 APPEND;
852 if ((GRID(src,x,y) & nline) != (GRID(dest, x, y) & nline)) {
853 assert(is_orth);
854 len = sprintf(buf, ";N%d,%d,%d,%d",
855 is_s->x, is_s->y, is_orth->x, is_orth->y);
856 APPEND;
859 if ((GRID(src, is_s->x, is_s->y) & G_MARK) !=
860 (GRID(dest, is_d->x, is_d->y) & G_MARK)) {
861 len = sprintf(buf, ";M%d,%d", is_s->x, is_s->y);
862 APPEND;
865 return move;
868 /* --- Game setup and solving utilities --- */
870 /* This function is optimised; a Quantify showed that lots of grid-generation time
871 * (>50%) was spent in here. Hence the IDX() stuff. */
873 static void map_update_possibles(game_state *state)
875 int x, y, s, e, bl, i, np, maxb, w = state->w, idx;
876 struct island *is_s = NULL, *is_f = NULL;
878 /* Run down vertical stripes [un]setting possv... */
879 for (x = 0; x < state->w; x++) {
880 idx = x;
881 s = e = -1;
882 bl = 0;
883 /* Unset possible flags until we find an island. */
884 for (y = 0; y < state->h; y++) {
885 is_s = IDX(state, gridi, idx);
886 if (is_s) break;
888 IDX(state, possv, idx) = 0;
889 idx += w;
891 for (; y < state->h; y++) {
892 is_f = IDX(state, gridi, idx);
893 if (is_f) {
894 assert(is_s);
895 maxb = IDX(state, maxv, idx);
896 np = min(maxb, min(is_s->count, is_f->count));
898 if (s != -1) {
899 for (i = s; i <= e; i++) {
900 INDEX(state, possv, x, i) = bl ? 0 : np;
903 s = y+1;
904 bl = 0;
905 is_s = is_f;
906 } else {
907 e = y;
908 if (IDX(state,grid,idx) & (G_LINEH|G_NOLINEV)) bl = 1;
910 idx += w;
912 if (s != -1) {
913 for (i = s; i <= e; i++)
914 INDEX(state, possv, x, i) = 0;
918 /* ...and now do horizontal stripes [un]setting possh. */
919 /* can we lose this clone'n'hack? */
920 for (y = 0; y < state->h; y++) {
921 idx = y*w;
922 s = e = -1;
923 bl = 0;
924 for (x = 0; x < state->w; x++) {
925 is_s = IDX(state, gridi, idx);
926 if (is_s) break;
928 IDX(state, possh, idx) = 0;
929 idx += 1;
931 for (; x < state->w; x++) {
932 is_f = IDX(state, gridi, idx);
933 if (is_f) {
934 assert(is_s);
935 maxb = IDX(state, maxh, idx);
936 np = min(maxb, min(is_s->count, is_f->count));
938 if (s != -1) {
939 for (i = s; i <= e; i++) {
940 INDEX(state, possh, i, y) = bl ? 0 : np;
943 s = x+1;
944 bl = 0;
945 is_s = is_f;
946 } else {
947 e = x;
948 if (IDX(state,grid,idx) & (G_LINEV|G_NOLINEH)) bl = 1;
950 idx += 1;
952 if (s != -1) {
953 for (i = s; i <= e; i++)
954 INDEX(state, possh, i, y) = 0;
959 static void map_count(game_state *state)
961 int i, n, ax, ay;
962 grid_type flag, grid;
963 struct island *is;
965 for (i = 0; i < state->n_islands; i++) {
966 is = &state->islands[i];
967 is->count = 0;
968 for (n = 0; n < is->adj.npoints; n++) {
969 ax = is->adj.points[n].x;
970 ay = is->adj.points[n].y;
971 flag = (ax == is->x) ? G_LINEV : G_LINEH;
972 grid = GRID(state,ax,ay);
973 if (grid & flag) {
974 is->count += INDEX(state,lines,ax,ay);
980 static void map_find_orthogonal(game_state *state)
982 int i;
984 for (i = 0; i < state->n_islands; i++) {
985 island_find_orthogonal(&state->islands[i]);
989 static int grid_degree(game_state *state, int x, int y, int *nx_r, int *ny_r)
991 grid_type grid = SCRATCH(state, x, y), gline = grid & G_LINE;
992 struct island *is;
993 int x1, y1, x2, y2, c = 0, i, nx, ny;
995 nx = ny = -1; /* placate optimiser */
996 is = INDEX(state, gridi, x, y);
997 if (is) {
998 for (i = 0; i < is->adj.npoints; i++) {
999 gline = is->adj.points[i].dx ? G_LINEH : G_LINEV;
1000 if (SCRATCH(state,
1001 is->adj.points[i].x,
1002 is->adj.points[i].y) & gline) {
1003 nx = is->adj.points[i].x;
1004 ny = is->adj.points[i].y;
1005 c++;
1008 } else if (gline) {
1009 if (gline & G_LINEV) {
1010 x1 = x2 = x;
1011 y1 = y-1; y2 = y+1;
1012 } else {
1013 x1 = x-1; x2 = x+1;
1014 y1 = y2 = y;
1016 /* Non-island squares with edges in should never be pointing off the
1017 * edge of the grid. */
1018 assert(INGRID(state, x1, y1));
1019 assert(INGRID(state, x2, y2));
1020 if (SCRATCH(state, x1, y1) & (gline | G_ISLAND)) {
1021 nx = x1; ny = y1; c++;
1023 if (SCRATCH(state, x2, y2) & (gline | G_ISLAND)) {
1024 nx = x2; ny = y2; c++;
1027 if (c == 1) {
1028 assert(nx != -1 && ny != -1); /* paranoia */
1029 *nx_r = nx; *ny_r = ny;
1031 return c;
1034 static int map_hasloops(game_state *state, int mark)
1036 int x, y, ox, oy, nx = 0, ny = 0, loop = 0;
1038 memcpy(state->scratch, state->grid, GRIDSZ(state));
1040 /* This algorithm is actually broken; if there are two loops connected
1041 * by bridges this will also highlight bridges. The correct algorithm
1042 * uses a dsf and a two-pass edge-detection algorithm (see check_correct
1043 * in slant.c); this is BALGE for now, especially since disallow-loops
1044 * is not the default for this puzzle. If we want to fix this later then
1045 * copy the alg in slant.c to the empty statement in map_group. */
1047 /* Remove all 1-degree edges. */
1048 for (y = 0; y < state->h; y++) {
1049 for (x = 0; x < state->w; x++) {
1050 ox = x; oy = y;
1051 while (grid_degree(state, ox, oy, &nx, &ny) == 1) {
1052 /*debug(("hasloops: removing 1-degree at (%d,%d).\n", ox, oy));*/
1053 SCRATCH(state, ox, oy) &= ~(G_LINE|G_ISLAND);
1054 ox = nx; oy = ny;
1058 /* Mark any remaining edges as G_WARN, if required. */
1059 for (x = 0; x < state->w; x++) {
1060 for (y = 0; y < state->h; y++) {
1061 if (GRID(state,x,y) & G_ISLAND) continue;
1063 if (SCRATCH(state, x, y) & G_LINE) {
1064 if (mark) {
1065 /*debug(("hasloops: marking loop square at (%d,%d).\n",
1066 x, y));*/
1067 GRID(state,x,y) |= G_WARN;
1068 loop = 1;
1069 } else
1070 return 1; /* short-cut as soon as we find one */
1071 } else {
1072 if (mark)
1073 GRID(state,x,y) &= ~G_WARN;
1077 return loop;
1080 static void map_group(game_state *state)
1082 int i, wh = state->w*state->h, d1, d2;
1083 int x, y, x2, y2;
1084 int *dsf = state->solver->dsf;
1085 struct island *is, *is_join;
1087 /* Initialise dsf. */
1088 dsf_init(dsf, wh);
1090 /* For each island, find connected islands right or down
1091 * and merge the dsf for the island squares as well as the
1092 * bridge squares. */
1093 for (x = 0; x < state->w; x++) {
1094 for (y = 0; y < state->h; y++) {
1095 GRID(state,x,y) &= ~(G_SWEEP|G_WARN); /* for group_full. */
1097 is = INDEX(state, gridi, x, y);
1098 if (!is) continue;
1099 d1 = DINDEX(x,y);
1100 for (i = 0; i < is->adj.npoints; i++) {
1101 /* only want right/down */
1102 if (is->adj.points[i].dx == -1 ||
1103 is->adj.points[i].dy == -1) continue;
1105 is_join = island_find_connection(is, i);
1106 if (!is_join) continue;
1108 d2 = DINDEX(is_join->x, is_join->y);
1109 if (dsf_canonify(dsf,d1) == dsf_canonify(dsf,d2)) {
1110 ; /* we have a loop. See comment in map_hasloops. */
1111 /* However, we still want to merge all squares joining
1112 * this side-that-makes-a-loop. */
1114 /* merge all squares between island 1 and island 2. */
1115 for (x2 = x; x2 <= is_join->x; x2++) {
1116 for (y2 = y; y2 <= is_join->y; y2++) {
1117 d2 = DINDEX(x2,y2);
1118 if (d1 != d2) dsf_merge(dsf,d1,d2);
1126 static int map_group_check(game_state *state, int canon, int warn,
1127 int *nislands_r)
1129 int *dsf = state->solver->dsf, nislands = 0;
1130 int x, y, i, allfull = 1;
1131 struct island *is;
1133 for (i = 0; i < state->n_islands; i++) {
1134 is = &state->islands[i];
1135 if (dsf_canonify(dsf, DINDEX(is->x,is->y)) != canon) continue;
1137 GRID(state, is->x, is->y) |= G_SWEEP;
1138 nislands++;
1139 if (island_countbridges(is) != is->count)
1140 allfull = 0;
1142 if (warn && allfull && nislands != state->n_islands) {
1143 /* we're full and this island group isn't the whole set.
1144 * Mark all squares with this dsf canon as ERR. */
1145 for (x = 0; x < state->w; x++) {
1146 for (y = 0; y < state->h; y++) {
1147 if (dsf_canonify(dsf, DINDEX(x,y)) == canon) {
1148 GRID(state,x,y) |= G_WARN;
1154 if (nislands_r) *nislands_r = nislands;
1155 return allfull;
1158 static int map_group_full(game_state *state, int *ngroups_r)
1160 int *dsf = state->solver->dsf, ngroups = 0;
1161 int i, anyfull = 0;
1162 struct island *is;
1164 /* NB this assumes map_group (or sth else) has cleared G_SWEEP. */
1166 for (i = 0; i < state->n_islands; i++) {
1167 is = &state->islands[i];
1168 if (GRID(state,is->x,is->y) & G_SWEEP) continue;
1170 ngroups++;
1171 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1172 1, NULL))
1173 anyfull = 1;
1176 *ngroups_r = ngroups;
1177 return anyfull;
1180 static int map_check(game_state *state)
1182 int ngroups;
1184 /* Check for loops, if necessary. */
1185 if (!state->allowloops) {
1186 if (map_hasloops(state, 1))
1187 return 0;
1190 /* Place islands into island groups and check for early
1191 * satisfied-groups. */
1192 map_group(state); /* clears WARN and SWEEP */
1193 if (map_group_full(state, &ngroups)) {
1194 if (ngroups == 1) return 1;
1196 return 0;
1199 static void map_clear(game_state *state)
1201 int x, y;
1203 for (x = 0; x < state->w; x++) {
1204 for (y = 0; y < state->h; y++) {
1205 /* clear most flags; might want to be slightly more careful here. */
1206 GRID(state,x,y) &= G_ISLAND;
1211 static void solve_join(struct island *is, int direction, int n, int is_max)
1213 struct island *is_orth;
1214 int d1, d2, *dsf = is->state->solver->dsf;
1215 game_state *state = is->state; /* for DINDEX */
1217 is_orth = INDEX(is->state, gridi,
1218 ISLAND_ORTHX(is, direction),
1219 ISLAND_ORTHY(is, direction));
1220 assert(is_orth);
1221 /*debug(("...joining (%d,%d) to (%d,%d) with %d bridge(s).\n",
1222 is->x, is->y, is_orth->x, is_orth->y, n));*/
1223 island_join(is, is_orth, n, is_max);
1225 if (n > 0 && !is_max) {
1226 d1 = DINDEX(is->x, is->y);
1227 d2 = DINDEX(is_orth->x, is_orth->y);
1228 if (dsf_canonify(dsf, d1) != dsf_canonify(dsf, d2))
1229 dsf_merge(dsf, d1, d2);
1233 static int solve_fillone(struct island *is)
1235 int i, nadded = 0;
1237 debug(("solve_fillone for island (%d,%d).\n", is->x, is->y));
1239 for (i = 0; i < is->adj.npoints; i++) {
1240 if (island_isadj(is, i)) {
1241 if (island_hasbridge(is, i)) {
1242 /* already attached; do nothing. */;
1243 } else {
1244 solve_join(is, i, 1, 0);
1245 nadded++;
1249 return nadded;
1252 static int solve_fill(struct island *is)
1254 /* for each unmarked adjacent, make sure we convert every possible bridge
1255 * to a real one, and then work out the possibles afresh. */
1256 int i, nnew, ncurr, nadded = 0, missing;
1258 debug(("solve_fill for island (%d,%d).\n", is->x, is->y));
1260 missing = is->count - island_countbridges(is);
1261 if (missing < 0) return 0;
1263 /* very like island_countspaces. */
1264 for (i = 0; i < is->adj.npoints; i++) {
1265 nnew = island_adjspace(is, 1, missing, i);
1266 if (nnew) {
1267 ncurr = GRIDCOUNT(is->state,
1268 is->adj.points[i].x, is->adj.points[i].y,
1269 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1271 solve_join(is, i, nnew + ncurr, 0);
1272 nadded += nnew;
1275 return nadded;
1278 static int solve_island_stage1(struct island *is, int *didsth_r)
1280 int bridges = island_countbridges(is);
1281 int nspaces = island_countspaces(is, 1);
1282 int nadj = island_countadj(is);
1283 int didsth = 0;
1285 assert(didsth_r);
1287 /*debug(("island at (%d,%d) filled %d/%d (%d spc) nadj %d\n",
1288 is->x, is->y, bridges, is->count, nspaces, nadj));*/
1289 if (bridges > is->count) {
1290 /* We only ever add bridges when we're sure they fit, or that's
1291 * the only place they can go. If we've added bridges such that
1292 * another island has become wrong, the puzzle must not have had
1293 * a solution. */
1294 debug(("...island at (%d,%d) is overpopulated!\n", is->x, is->y));
1295 return 0;
1296 } else if (bridges == is->count) {
1297 /* This island is full. Make sure it's marked (and update
1298 * possibles if we did). */
1299 if (!(GRID(is->state, is->x, is->y) & G_MARK)) {
1300 debug(("...marking island (%d,%d) as full.\n", is->x, is->y));
1301 island_togglemark(is);
1302 didsth = 1;
1304 } else if (GRID(is->state, is->x, is->y) & G_MARK) {
1305 debug(("...island (%d,%d) is marked but unfinished!\n",
1306 is->x, is->y));
1307 return 0; /* island has been marked unfinished; no solution from here. */
1308 } else {
1309 /* This is the interesting bit; we try and fill in more information
1310 * about this island. */
1311 if (is->count == bridges + nspaces) {
1312 if (solve_fill(is) > 0) didsth = 1;
1313 } else if (is->count > ((nadj-1) * is->state->maxb)) {
1314 /* must have at least one bridge in each possible direction. */
1315 if (solve_fillone(is) > 0) didsth = 1;
1318 if (didsth) {
1319 map_update_possibles(is->state);
1320 *didsth_r = 1;
1322 return 1;
1325 /* returns non-zero if a new line here would cause a loop. */
1326 static int solve_island_checkloop(struct island *is, int direction)
1328 struct island *is_orth;
1329 int *dsf = is->state->solver->dsf, d1, d2;
1330 game_state *state = is->state;
1332 if (is->state->allowloops) return 0; /* don't care anyway */
1333 if (island_hasbridge(is, direction)) return 0; /* already has a bridge */
1334 if (island_isadj(is, direction) == 0) return 0; /* no adj island */
1336 is_orth = INDEX(is->state, gridi,
1337 ISLAND_ORTHX(is,direction),
1338 ISLAND_ORTHY(is,direction));
1339 if (!is_orth) return 0;
1341 d1 = DINDEX(is->x, is->y);
1342 d2 = DINDEX(is_orth->x, is_orth->y);
1343 if (dsf_canonify(dsf, d1) == dsf_canonify(dsf, d2)) {
1344 /* two islands are connected already; don't join them. */
1345 return 1;
1347 return 0;
1350 static int solve_island_stage2(struct island *is, int *didsth_r)
1352 int added = 0, removed = 0, navail = 0, nadj, i;
1354 assert(didsth_r);
1356 for (i = 0; i < is->adj.npoints; i++) {
1357 if (solve_island_checkloop(is, i)) {
1358 debug(("removing possible loop at (%d,%d) direction %d.\n",
1359 is->x, is->y, i));
1360 solve_join(is, i, -1, 0);
1361 map_update_possibles(is->state);
1362 removed = 1;
1363 } else {
1364 navail += island_isadj(is, i);
1365 /*debug(("stage2: navail for (%d,%d) direction (%d,%d) is %d.\n",
1366 is->x, is->y,
1367 is->adj.points[i].dx, is->adj.points[i].dy,
1368 island_isadj(is, i)));*/
1372 /*debug(("island at (%d,%d) navail %d: checking...\n", is->x, is->y, navail));*/
1374 for (i = 0; i < is->adj.npoints; i++) {
1375 if (!island_hasbridge(is, i)) {
1376 nadj = island_isadj(is, i);
1377 if (nadj > 0 && (navail - nadj) < is->count) {
1378 /* we couldn't now complete the island without at
1379 * least one bridge here; put it in. */
1380 /*debug(("nadj %d, navail %d, is->count %d.\n",
1381 nadj, navail, is->count));*/
1382 debug(("island at (%d,%d) direction (%d,%d) must have 1 bridge\n",
1383 is->x, is->y,
1384 is->adj.points[i].dx, is->adj.points[i].dy));
1385 solve_join(is, i, 1, 0);
1386 added = 1;
1387 /*debug_state(is->state);
1388 debug_possibles(is->state);*/
1392 if (added) map_update_possibles(is->state);
1393 if (added || removed) *didsth_r = 1;
1394 return 1;
1397 static int solve_island_subgroup(struct island *is, int direction, int n)
1399 struct island *is_join;
1400 int nislands, *dsf = is->state->solver->dsf;
1401 game_state *state = is->state;
1403 debug(("..checking subgroups.\n"));
1405 /* if is isn't full, return 0. */
1406 if (n < is->count) {
1407 debug(("...orig island (%d,%d) not full.\n", is->x, is->y));
1408 return 0;
1411 is_join = INDEX(state, gridi,
1412 ISLAND_ORTHX(is, direction),
1413 ISLAND_ORTHY(is, direction));
1414 assert(is_join);
1416 /* if is_join isn't full, return 0. */
1417 if (island_countbridges(is_join) < is_join->count) {
1418 debug(("...dest island (%d,%d) not full.\n", is_join->x, is_join->y));
1419 return 0;
1422 /* Check group membership for is->dsf; if it's full return 1. */
1423 if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1424 0, &nislands)) {
1425 if (nislands < state->n_islands) {
1426 /* we have a full subgroup that isn't the whole set.
1427 * This isn't allowed. */
1428 debug(("island at (%d,%d) makes full subgroup, disallowing.\n",
1429 is->x, is->y, n));
1430 return 1;
1431 } else {
1432 debug(("...has finished puzzle.\n"));
1435 return 0;
1438 static int solve_island_impossible(game_state *state)
1440 struct island *is;
1441 int i;
1443 /* If any islands are impossible, return 1. */
1444 for (i = 0; i < state->n_islands; i++) {
1445 is = &state->islands[i];
1446 if (island_impossible(is, 0)) {
1447 debug(("island at (%d,%d) has become impossible, disallowing.\n",
1448 is->x, is->y));
1449 return 1;
1452 return 0;
1455 /* Bear in mind that this function is really rather inefficient. */
1456 static int solve_island_stage3(struct island *is, int *didsth_r)
1458 int i, n, x, y, missing, spc, curr, maxb, didsth = 0;
1459 int wh = is->state->w * is->state->h;
1460 struct solver_state *ss = is->state->solver;
1462 assert(didsth_r);
1464 missing = is->count - island_countbridges(is);
1465 if (missing <= 0) return 1;
1467 for (i = 0; i < is->adj.npoints; i++) {
1468 /* We only do right- or down-pointing bridges. */
1469 if (is->adj.points[i].dx == -1 ||
1470 is->adj.points[i].dy == -1) continue;
1472 x = is->adj.points[i].x;
1473 y = is->adj.points[i].y;
1474 spc = island_adjspace(is, 1, missing, i);
1475 if (spc == 0) continue;
1477 curr = GRIDCOUNT(is->state, x, y,
1478 is->adj.points[i].dx ? G_LINEH : G_LINEV);
1479 debug(("island at (%d,%d) s3, trying %d - %d bridges.\n",
1480 is->x, is->y, curr+1, curr+spc));
1482 /* Now we know that this island could have more bridges,
1483 * to bring the total from curr+1 to curr+spc. */
1484 maxb = -1;
1485 /* We have to squirrel the dsf away and restore it afterwards;
1486 * it is additive only, and can't be removed from. */
1487 memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int));
1488 for (n = curr+1; n <= curr+spc; n++) {
1489 solve_join(is, i, n, 0);
1490 map_update_possibles(is->state);
1492 if (solve_island_subgroup(is, i, n) ||
1493 solve_island_impossible(is->state)) {
1494 maxb = n-1;
1495 debug(("island at (%d,%d) d(%d,%d) new max of %d bridges:\n",
1496 is->x, is->y,
1497 is->adj.points[i].dx, is->adj.points[i].dy,
1498 maxb));
1499 break;
1502 solve_join(is, i, curr, 0); /* put back to before. */
1503 memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int));
1505 if (maxb != -1) {
1506 /*debug_state(is->state);*/
1507 if (maxb == 0) {
1508 debug(("...adding NOLINE.\n"));
1509 solve_join(is, i, -1, 0); /* we can't have any bridges here. */
1510 didsth = 1;
1511 } else {
1512 debug(("...setting maximum\n"));
1513 solve_join(is, i, maxb, 1);
1516 map_update_possibles(is->state);
1518 if (didsth) *didsth_r = didsth;
1519 return 1;
1522 #define CONTINUE_IF_FULL do { \
1523 if (GRID(state, is->x, is->y) & G_MARK) { \
1524 /* island full, don't try fixing it */ \
1525 continue; \
1526 } } while(0)
1528 static int solve_sub(game_state *state, int difficulty, int depth)
1530 struct island *is;
1531 int i, didsth;
1533 while (1) {
1534 didsth = 0;
1536 /* First island iteration: things we can work out by looking at
1537 * properties of the island as a whole. */
1538 for (i = 0; i < state->n_islands; i++) {
1539 is = &state->islands[i];
1540 if (!solve_island_stage1(is, &didsth)) return 0;
1542 if (didsth) continue;
1543 else if (difficulty < 1) break;
1545 /* Second island iteration: thing we can work out by looking at
1546 * properties of individual island connections. */
1547 for (i = 0; i < state->n_islands; i++) {
1548 is = &state->islands[i];
1549 CONTINUE_IF_FULL;
1550 if (!solve_island_stage2(is, &didsth)) return 0;
1552 if (didsth) continue;
1553 else if (difficulty < 2) break;
1555 /* Third island iteration: things we can only work out by looking
1556 * at groups of islands. */
1557 for (i = 0; i < state->n_islands; i++) {
1558 is = &state->islands[i];
1559 if (!solve_island_stage3(is, &didsth)) return 0;
1561 if (didsth) continue;
1562 else if (difficulty < 3) break;
1564 /* If we can be bothered, write a recursive solver to finish here. */
1565 break;
1567 if (map_check(state)) return 1; /* solved it */
1568 return 0;
1571 static void solve_for_hint(game_state *state)
1573 map_group(state);
1574 solve_sub(state, 10, 0);
1577 static int solve_from_scratch(game_state *state, int difficulty)
1579 map_clear(state);
1580 map_group(state);
1581 map_update_possibles(state);
1582 return solve_sub(state, difficulty, 0);
1585 /* --- New game functions --- */
1587 static game_state *new_state(game_params *params)
1589 game_state *ret = snew(game_state);
1590 int wh = params->w * params->h, i;
1592 ret->w = params->w;
1593 ret->h = params->h;
1594 ret->allowloops = params->allowloops;
1595 ret->maxb = params->maxb;
1596 ret->params = *params;
1598 ret->grid = snewn(wh, grid_type);
1599 memset(ret->grid, 0, GRIDSZ(ret));
1600 ret->scratch = snewn(wh, grid_type);
1601 memset(ret->scratch, 0, GRIDSZ(ret));
1603 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1604 memset(ret->wha, 0, wh*N_WH_ARRAYS*sizeof(char));
1606 ret->possv = ret->wha;
1607 ret->possh = ret->wha + wh;
1608 ret->lines = ret->wha + wh*2;
1609 ret->maxv = ret->wha + wh*3;
1610 ret->maxh = ret->wha + wh*4;
1612 memset(ret->maxv, ret->maxb, wh*sizeof(char));
1613 memset(ret->maxh, ret->maxb, wh*sizeof(char));
1615 ret->islands = NULL;
1616 ret->n_islands = 0;
1617 ret->n_islands_alloc = 0;
1619 ret->gridi = snewn(wh, struct island *);
1620 for (i = 0; i < wh; i++) ret->gridi[i] = NULL;
1622 ret->solved = ret->completed = 0;
1624 ret->solver = snew(struct solver_state);
1625 ret->solver->dsf = snew_dsf(wh);
1626 ret->solver->tmpdsf = snewn(wh, int);
1628 ret->solver->refcount = 1;
1630 return ret;
1633 static game_state *dup_game(game_state *state)
1635 game_state *ret = snew(game_state);
1636 int wh = state->w*state->h;
1638 ret->w = state->w;
1639 ret->h = state->h;
1640 ret->allowloops = state->allowloops;
1641 ret->maxb = state->maxb;
1642 ret->params = state->params;
1644 ret->grid = snewn(wh, grid_type);
1645 memcpy(ret->grid, state->grid, GRIDSZ(ret));
1646 ret->scratch = snewn(wh, grid_type);
1647 memcpy(ret->scratch, state->scratch, GRIDSZ(ret));
1649 ret->wha = snewn(wh*N_WH_ARRAYS, char);
1650 memcpy(ret->wha, state->wha, wh*N_WH_ARRAYS*sizeof(char));
1652 ret->possv = ret->wha;
1653 ret->possh = ret->wha + wh;
1654 ret->lines = ret->wha + wh*2;
1655 ret->maxv = ret->wha + wh*3;
1656 ret->maxh = ret->wha + wh*4;
1658 ret->islands = snewn(state->n_islands, struct island);
1659 memcpy(ret->islands, state->islands, state->n_islands * sizeof(struct island));
1660 ret->n_islands = ret->n_islands_alloc = state->n_islands;
1662 ret->gridi = snewn(wh, struct island *);
1663 fixup_islands_for_realloc(ret);
1665 ret->solved = state->solved;
1666 ret->completed = state->completed;
1668 ret->solver = state->solver;
1669 ret->solver->refcount++;
1671 return ret;
1674 static void free_game(game_state *state)
1676 if (--state->solver->refcount <= 0) {
1677 sfree(state->solver->dsf);
1678 sfree(state->solver->tmpdsf);
1679 sfree(state->solver);
1682 sfree(state->islands);
1683 sfree(state->gridi);
1685 sfree(state->wha);
1687 sfree(state->scratch);
1688 sfree(state->grid);
1689 sfree(state);
1692 #define MAX_NEWISLAND_TRIES 50
1693 #define MIN_SENSIBLE_ISLANDS 3
1695 #define ORDER(a,b) do { if (a < b) { int tmp=a; int a=b; int b=tmp; } } while(0)
1697 static char *new_game_desc(game_params *params, random_state *rs,
1698 char **aux, int interactive)
1700 game_state *tobuild = NULL;
1701 int i, j, wh = params->w * params->h, x, y, dx, dy;
1702 int minx, miny, maxx, maxy, joinx, joiny, newx, newy, diffx, diffy;
1703 int ni_req = max((params->islands * wh) / 100, MIN_SENSIBLE_ISLANDS), ni_curr, ni_bad;
1704 struct island *is, *is2;
1705 char *ret;
1706 unsigned int echeck;
1708 /* pick a first island position randomly. */
1709 generate:
1710 if (tobuild) free_game(tobuild);
1711 tobuild = new_state(params);
1713 x = random_upto(rs, params->w);
1714 y = random_upto(rs, params->h);
1715 island_add(tobuild, x, y, 0);
1716 ni_curr = 1;
1717 ni_bad = 0;
1718 debug(("Created initial island at (%d,%d).\n", x, y));
1720 while (ni_curr < ni_req) {
1721 /* Pick a random island to try and extend from. */
1722 i = random_upto(rs, tobuild->n_islands);
1723 is = &tobuild->islands[i];
1725 /* Pick a random direction to extend in. */
1726 j = random_upto(rs, is->adj.npoints);
1727 dx = is->adj.points[j].x - is->x;
1728 dy = is->adj.points[j].y - is->y;
1730 /* Find out limits of where we could put a new island. */
1731 joinx = joiny = -1;
1732 minx = is->x + 2*dx; miny = is->y + 2*dy; /* closest is 2 units away. */
1733 x = is->x+dx; y = is->y+dy;
1734 if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1735 /* already a line next to the island, continue. */
1736 goto bad;
1738 while (1) {
1739 if (x < 0 || x >= params->w || y < 0 || y >= params->h) {
1740 /* got past the edge; put a possible at the island
1741 * and exit. */
1742 maxx = x-dx; maxy = y-dy;
1743 goto foundmax;
1745 if (GRID(tobuild,x,y) & G_ISLAND) {
1746 /* could join up to an existing island... */
1747 joinx = x; joiny = y;
1748 /* ... or make a new one 2 spaces away. */
1749 maxx = x - 2*dx; maxy = y - 2*dy;
1750 goto foundmax;
1751 } else if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1752 /* could make a new one 1 space away from the line. */
1753 maxx = x - dx; maxy = y - dy;
1754 goto foundmax;
1756 x += dx; y += dy;
1759 foundmax:
1760 debug(("Island at (%d,%d) with d(%d,%d) has new positions "
1761 "(%d,%d) -> (%d,%d), join (%d,%d).\n",
1762 is->x, is->y, dx, dy, minx, miny, maxx, maxy, joinx, joiny));
1763 /* Now we know where we could either put a new island
1764 * (between min and max), or (if loops are allowed) could join on
1765 * to an existing island (at join). */
1766 if (params->allowloops && joinx != -1 && joiny != -1) {
1767 if (random_upto(rs, 100) < (unsigned long)params->expansion) {
1768 is2 = INDEX(tobuild, gridi, joinx, joiny);
1769 debug(("Joining island at (%d,%d) to (%d,%d).\n",
1770 is->x, is->y, is2->x, is2->y));
1771 goto join;
1774 diffx = (maxx - minx) * dx;
1775 diffy = (maxy - miny) * dy;
1776 if (diffx < 0 || diffy < 0) goto bad;
1777 if (random_upto(rs,100) < (unsigned long)params->expansion) {
1778 newx = maxx; newy = maxy;
1779 debug(("Creating new island at (%d,%d) (expanded).\n", newx, newy));
1780 } else {
1781 newx = minx + random_upto(rs,diffx+1)*dx;
1782 newy = miny + random_upto(rs,diffy+1)*dy;
1783 debug(("Creating new island at (%d,%d).\n", newx, newy));
1785 /* check we're not next to island in the other orthogonal direction. */
1786 if ((INGRID(tobuild,newx+dy,newy+dx) && (GRID(tobuild,newx+dy,newy+dx) & G_ISLAND)) ||
1787 (INGRID(tobuild,newx-dy,newy-dx) && (GRID(tobuild,newx-dy,newy-dx) & G_ISLAND))) {
1788 debug(("New location is adjacent to island, skipping.\n"));
1789 goto bad;
1791 is2 = island_add(tobuild, newx, newy, 0);
1792 /* Must get is again at this point; the array might have
1793 * been realloced by island_add... */
1794 is = &tobuild->islands[i]; /* ...but order will not change. */
1796 ni_curr++; ni_bad = 0;
1797 join:
1798 island_join(is, is2, random_upto(rs, tobuild->maxb)+1, 0);
1799 debug_state(tobuild);
1800 continue;
1802 bad:
1803 ni_bad++;
1804 if (ni_bad > MAX_NEWISLAND_TRIES) {
1805 debug(("Unable to create any new islands after %d tries; "
1806 "created %d [%d%%] (instead of %d [%d%%] requested).\n",
1807 MAX_NEWISLAND_TRIES,
1808 ni_curr, ni_curr * 100 / wh,
1809 ni_req, ni_req * 100 / wh));
1810 goto generated;
1814 generated:
1815 if (ni_curr == 1) {
1816 debug(("Only generated one island (!), retrying.\n"));
1817 goto generate;
1819 /* Check we have at least one island on each extremity of the grid. */
1820 echeck = 0;
1821 for (x = 0; x < params->w; x++) {
1822 if (INDEX(tobuild, gridi, x, 0)) echeck |= 1;
1823 if (INDEX(tobuild, gridi, x, params->h-1)) echeck |= 2;
1825 for (y = 0; y < params->h; y++) {
1826 if (INDEX(tobuild, gridi, 0, y)) echeck |= 4;
1827 if (INDEX(tobuild, gridi, params->w-1, y)) echeck |= 8;
1829 if (echeck != 15) {
1830 debug(("Generated grid doesn't fill to sides, retrying.\n"));
1831 goto generate;
1834 map_count(tobuild);
1835 map_find_orthogonal(tobuild);
1837 if (params->difficulty > 0) {
1838 if ((ni_curr > MIN_SENSIBLE_ISLANDS) &&
1839 (solve_from_scratch(tobuild, params->difficulty-1) > 0)) {
1840 debug(("Grid is solvable at difficulty %d (too easy); retrying.\n",
1841 params->difficulty-1));
1842 goto generate;
1846 if (solve_from_scratch(tobuild, params->difficulty) == 0) {
1847 debug(("Grid not solvable at difficulty %d, (too hard); retrying.\n",
1848 params->difficulty));
1849 goto generate;
1852 /* ... tobuild is now solved. We rely on this making the diff for aux. */
1853 debug_state(tobuild);
1854 ret = encode_game(tobuild);
1856 game_state *clean = dup_game(tobuild);
1857 map_clear(clean);
1858 map_update_possibles(clean);
1859 *aux = game_state_diff(clean, tobuild);
1860 free_game(clean);
1862 free_game(tobuild);
1864 return ret;
1867 static char *validate_desc(game_params *params, char *desc)
1869 int i, wh = params->w * params->h;
1871 for (i = 0; i < wh; i++) {
1872 if (*desc >= '1' && *desc <= '9')
1873 /* OK */;
1874 else if (*desc >= 'a' && *desc <= 'z')
1875 i += *desc - 'a'; /* plus the i++ */
1876 else if (*desc >= 'A' && *desc <= 'G')
1877 /* OK */;
1878 else if (*desc == 'V' || *desc == 'W' ||
1879 *desc == 'X' || *desc == 'Y' ||
1880 *desc == 'H' || *desc == 'I' ||
1881 *desc == 'J' || *desc == 'K')
1882 /* OK */;
1883 else if (!*desc)
1884 return "Game description shorter than expected";
1885 else
1886 return "Game description containers unexpected character";
1887 desc++;
1889 if (*desc || i > wh)
1890 return "Game description longer than expected";
1892 return NULL;
1895 static game_state *new_game_sub(game_params *params, char *desc)
1897 game_state *state = new_state(params);
1898 int x, y, run = 0;
1900 debug(("new_game[_sub]: desc = '%s'.\n", desc));
1902 for (y = 0; y < params->h; y++) {
1903 for (x = 0; x < params->w; x++) {
1904 char c = '\0';
1906 if (run == 0) {
1907 c = *desc++;
1908 assert(c != 'S');
1909 if (c >= 'a' && c <= 'z')
1910 run = c - 'a' + 1;
1913 if (run > 0) {
1914 c = 'S';
1915 run--;
1918 switch (c) {
1919 case '1': case '2': case '3': case '4':
1920 case '5': case '6': case '7': case '8': case '9':
1921 island_add(state, x, y, (c - '0'));
1922 break;
1924 case 'A': case 'B': case 'C': case 'D':
1925 case 'E': case 'F': case 'G':
1926 island_add(state, x, y, (c - 'A') + 10);
1927 break;
1929 case 'S':
1930 /* empty square */
1931 break;
1933 default:
1934 assert(!"Malformed desc.");
1935 break;
1939 if (*desc) assert(!"Over-long desc.");
1941 map_find_orthogonal(state);
1942 map_update_possibles(state);
1944 return state;
1947 static game_state *new_game(midend *me, game_params *params, char *desc)
1949 return new_game_sub(params, desc);
1952 struct game_ui {
1953 int dragx_src, dragy_src; /* source; -1 means no drag */
1954 int dragx_dst, dragy_dst; /* src's closest orth island. */
1955 grid_type todraw;
1956 int dragging, drag_is_noline, nlines;
1958 int cur_x, cur_y, cur_visible; /* cursor position */
1959 int show_hints;
1962 static char *ui_cancel_drag(game_ui *ui)
1964 ui->dragx_src = ui->dragy_src = -1;
1965 ui->dragx_dst = ui->dragy_dst = -1;
1966 ui->dragging = 0;
1967 return "";
1970 static game_ui *new_ui(game_state *state)
1972 game_ui *ui = snew(game_ui);
1973 ui_cancel_drag(ui);
1974 ui->cur_x = state->islands[0].x;
1975 ui->cur_y = state->islands[0].y;
1976 ui->cur_visible = 0;
1977 ui->show_hints = 0;
1978 return ui;
1981 static void free_ui(game_ui *ui)
1983 sfree(ui);
1986 static char *encode_ui(game_ui *ui)
1988 return NULL;
1991 static void decode_ui(game_ui *ui, char *encoding)
1995 static void game_changed_state(game_ui *ui, game_state *oldstate,
1996 game_state *newstate)
2000 struct game_drawstate {
2001 int tilesize;
2002 int w, h;
2003 grid_type *grid;
2004 int *lv, *lh;
2005 int started, dragging;
2006 int show_hints;
2009 static char *update_drag_dst(game_state *state, game_ui *ui, game_drawstate *ds,
2010 int nx, int ny)
2012 int ox, oy, dx, dy, i, currl, maxb;
2013 struct island *is;
2014 grid_type gtype, ntype, mtype, curr;
2016 if (ui->dragx_src == -1 || ui->dragy_src == -1) return NULL;
2018 ui->dragx_dst = -1;
2019 ui->dragy_dst = -1;
2021 /* work out which of the four directions we're closest to... */
2022 ox = COORD(ui->dragx_src) + TILE_SIZE/2;
2023 oy = COORD(ui->dragy_src) + TILE_SIZE/2;
2025 if (abs(nx-ox) < abs(ny-oy)) {
2026 dx = 0;
2027 dy = (ny-oy) < 0 ? -1 : 1;
2028 gtype = G_LINEV; ntype = G_NOLINEV; mtype = G_MARKV;
2029 maxb = INDEX(state, maxv, ui->dragx_src+dx, ui->dragy_src+dy);
2030 } else {
2031 dy = 0;
2032 dx = (nx-ox) < 0 ? -1 : 1;
2033 gtype = G_LINEH; ntype = G_NOLINEH; mtype = G_MARKH;
2034 maxb = INDEX(state, maxh, ui->dragx_src+dx, ui->dragy_src+dy);
2036 if (ui->drag_is_noline) {
2037 ui->todraw = ntype;
2038 } else {
2039 curr = GRID(state, ui->dragx_src+dx, ui->dragy_src+dy);
2040 currl = INDEX(state, lines, ui->dragx_src+dx, ui->dragy_src+dy);
2042 if (curr & gtype) {
2043 if (currl == maxb) {
2044 ui->todraw = 0;
2045 ui->nlines = 0;
2046 } else {
2047 ui->todraw = gtype;
2048 ui->nlines = currl + 1;
2050 } else {
2051 ui->todraw = gtype;
2052 ui->nlines = 1;
2056 /* ... and see if there's an island off in that direction. */
2057 is = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2058 for (i = 0; i < is->adj.npoints; i++) {
2059 if (is->adj.points[i].off == 0) continue;
2060 curr = GRID(state, is->x+dx, is->y+dy);
2061 if (curr & mtype) continue; /* don't allow changes to marked lines. */
2062 if (ui->drag_is_noline) {
2063 if (curr & gtype) continue; /* no no-line where already a line */
2064 } else {
2065 if (POSSIBLES(state, dx, is->x+dx, is->y+dy) == 0) continue; /* no line if !possible. */
2066 if (curr & ntype) continue; /* can't have a bridge where there's a no-line. */
2069 if (is->adj.points[i].dx == dx &&
2070 is->adj.points[i].dy == dy) {
2071 ui->dragx_dst = ISLAND_ORTHX(is,i);
2072 ui->dragy_dst = ISLAND_ORTHY(is,i);
2075 /*debug(("update_drag src (%d,%d) d(%d,%d) dst (%d,%d)\n",
2076 ui->dragx_src, ui->dragy_src, dx, dy,
2077 ui->dragx_dst, ui->dragy_dst));*/
2078 return "";
2081 static char *finish_drag(game_state *state, game_ui *ui)
2083 char buf[80];
2085 if (ui->dragx_src == -1 || ui->dragy_src == -1)
2086 return NULL;
2087 if (ui->dragx_dst == -1 || ui->dragy_dst == -1)
2088 return ui_cancel_drag(ui);
2090 if (ui->drag_is_noline) {
2091 sprintf(buf, "N%d,%d,%d,%d",
2092 ui->dragx_src, ui->dragy_src,
2093 ui->dragx_dst, ui->dragy_dst);
2094 } else {
2095 sprintf(buf, "L%d,%d,%d,%d,%d",
2096 ui->dragx_src, ui->dragy_src,
2097 ui->dragx_dst, ui->dragy_dst, ui->nlines);
2100 ui_cancel_drag(ui);
2102 return dupstr(buf);
2105 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2106 int x, int y, int button)
2108 int gx = FROMCOORD(x), gy = FROMCOORD(y);
2109 char buf[80], *ret;
2110 grid_type ggrid = INGRID(state,gx,gy) ? GRID(state,gx,gy) : 0;
2112 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2113 if (!INGRID(state, gx, gy)) return NULL;
2114 ui->cur_visible = 0;
2115 if ((ggrid & G_ISLAND) && !(ggrid & G_MARK)) {
2116 ui->dragx_src = gx;
2117 ui->dragy_src = gy;
2118 return "";
2119 } else
2120 return ui_cancel_drag(ui);
2121 } else if (button == LEFT_DRAG || button == RIGHT_DRAG) {
2122 if (gx != ui->dragx_src || gy != ui->dragy_src) {
2123 ui->dragging = 1;
2124 ui->drag_is_noline = (button == RIGHT_DRAG) ? 1 : 0;
2125 return update_drag_dst(state, ui, ds, x, y);
2126 } else {
2127 /* cancel a drag when we go back to the starting point */
2128 ui->dragx_dst = -1;
2129 ui->dragy_dst = -1;
2130 return "";
2132 } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) {
2133 if (ui->dragging) {
2134 return finish_drag(state, ui);
2135 } else {
2136 ui_cancel_drag(ui);
2137 if (!INGRID(state, gx, gy)) return NULL;
2138 if (!(GRID(state, gx, gy) & G_ISLAND)) return NULL;
2139 sprintf(buf, "M%d,%d", gx, gy);
2140 return dupstr(buf);
2142 } else if (button == 'h' || button == 'H') {
2143 game_state *solved = dup_game(state);
2144 solve_for_hint(solved);
2145 ret = game_state_diff(state, solved);
2146 free_game(solved);
2147 return ret;
2148 } else if (IS_CURSOR_MOVE(button)) {
2149 ui->cur_visible = 1;
2150 if (ui->dragging) {
2151 int nx = ui->cur_x, ny = ui->cur_y;
2153 move_cursor(button, &nx, &ny, state->w, state->h, 0);
2154 update_drag_dst(state, ui, ds,
2155 COORD(nx)+TILE_SIZE/2,
2156 COORD(ny)+TILE_SIZE/2);
2157 return finish_drag(state, ui);
2158 } else {
2159 int dx = (button == CURSOR_RIGHT) ? +1 : (button == CURSOR_LEFT) ? -1 : 0;
2160 int dy = (button == CURSOR_DOWN) ? +1 : (button == CURSOR_UP) ? -1 : 0;
2161 int dorthx = 1 - abs(dx), dorthy = 1 - abs(dy);
2162 int dir, orth, nx = x, ny = y;
2164 /* 'orthorder' is a tweak to ensure that if you press RIGHT and
2165 * happen to move upwards, when you press LEFT you then tend
2166 * downwards (rather than upwards again). */
2167 int orthorder = (button == CURSOR_LEFT || button == CURSOR_UP) ? 1 : -1;
2169 /* This attempts to find an island in the direction you're
2170 * asking for, broadly speaking. If you ask to go right, for
2171 * example, it'll look for islands to the right and slightly
2172 * above or below your current horiz. position, allowing
2173 * further above/below the further away it searches. */
2175 assert(GRID(state, ui->cur_x, ui->cur_y) & G_ISLAND);
2176 /* currently this is depth-first (so orthogonally-adjacent
2177 * islands across the other side of the grid will be moved to
2178 * before closer islands slightly offset). Swap the order of
2179 * these two loops to change to breadth-first search. */
2180 for (orth = 0; ; orth++) {
2181 int oingrid = 0;
2182 for (dir = 1; ; dir++) {
2183 int dingrid = 0;
2185 if (orth > dir) continue; /* only search in cone outwards. */
2187 nx = ui->cur_x + dir*dx + orth*dorthx*orthorder;
2188 ny = ui->cur_y + dir*dy + orth*dorthy*orthorder;
2189 if (INGRID(state, nx, ny)) {
2190 dingrid = oingrid = 1;
2191 if (GRID(state, nx, ny) & G_ISLAND) goto found;
2194 nx = ui->cur_x + dir*dx - orth*dorthx*orthorder;
2195 ny = ui->cur_y + dir*dy - orth*dorthy*orthorder;
2196 if (INGRID(state, nx, ny)) {
2197 dingrid = oingrid = 1;
2198 if (GRID(state, nx, ny) & G_ISLAND) goto found;
2201 if (!dingrid) break;
2203 if (!oingrid) return "";
2205 /* not reached */
2207 found:
2208 ui->cur_x = nx;
2209 ui->cur_y = ny;
2210 return "";
2212 } else if (IS_CURSOR_SELECT(button)) {
2213 if (!ui->cur_visible) {
2214 ui->cur_visible = 1;
2215 return "";
2217 if (ui->dragging) {
2218 ui_cancel_drag(ui);
2219 if (ui->dragx_dst == -1 && ui->dragy_dst == -1) {
2220 sprintf(buf, "M%d,%d", ui->cur_x, ui->cur_y);
2221 return dupstr(buf);
2222 } else
2223 return "";
2224 } else {
2225 grid_type v = GRID(state, ui->cur_x, ui->cur_y);
2226 if (v & G_ISLAND) {
2227 ui->dragging = 1;
2228 ui->dragx_src = ui->cur_x;
2229 ui->dragy_src = ui->cur_y;
2230 ui->dragx_dst = ui->dragy_dst = -1;
2231 ui->drag_is_noline = (button == CURSOR_SELECT2) ? 1 : 0;
2232 return "";
2235 } else if (button == 'g' || button == 'G') {
2236 ui->show_hints = 1 - ui->show_hints;
2237 return "";
2240 return NULL;
2243 static game_state *execute_move(game_state *state, char *move)
2245 game_state *ret = dup_game(state);
2246 int x1, y1, x2, y2, nl, n;
2247 struct island *is1, *is2;
2248 char c;
2250 debug(("execute_move: %s\n", move));
2252 if (!*move) goto badmove;
2253 while (*move) {
2254 c = *move++;
2255 if (c == 'S') {
2256 ret->solved = TRUE;
2257 n = 0;
2258 } else if (c == 'L') {
2259 if (sscanf(move, "%d,%d,%d,%d,%d%n",
2260 &x1, &y1, &x2, &y2, &nl, &n) != 5)
2261 goto badmove;
2262 if (!INGRID(ret, x1, y1) || !INGRID(ret, x2, y2))
2263 goto badmove;
2264 is1 = INDEX(ret, gridi, x1, y1);
2265 is2 = INDEX(ret, gridi, x2, y2);
2266 if (!is1 || !is2) goto badmove;
2267 if (nl < 0 || nl > state->maxb) goto badmove;
2268 island_join(is1, is2, nl, 0);
2269 } else if (c == 'N') {
2270 if (sscanf(move, "%d,%d,%d,%d%n",
2271 &x1, &y1, &x2, &y2, &n) != 4)
2272 goto badmove;
2273 if (!INGRID(ret, x1, y1) || !INGRID(ret, x2, y2))
2274 goto badmove;
2275 is1 = INDEX(ret, gridi, x1, y1);
2276 is2 = INDEX(ret, gridi, x2, y2);
2277 if (!is1 || !is2) goto badmove;
2278 island_join(is1, is2, -1, 0);
2279 } else if (c == 'M') {
2280 if (sscanf(move, "%d,%d%n",
2281 &x1, &y1, &n) != 2)
2282 goto badmove;
2283 if (!INGRID(ret, x1, y1))
2284 goto badmove;
2285 is1 = INDEX(ret, gridi, x1, y1);
2286 if (!is1) goto badmove;
2287 island_togglemark(is1);
2288 } else
2289 goto badmove;
2291 move += n;
2292 if (*move == ';')
2293 move++;
2294 else if (*move) goto badmove;
2297 map_update_possibles(ret);
2298 if (map_check(ret)) {
2299 debug(("Game completed.\n"));
2300 ret->completed = 1;
2302 return ret;
2304 badmove:
2305 debug(("%s: unrecognised move.\n", move));
2306 free_game(ret);
2307 return NULL;
2310 static char *solve_game(game_state *state, game_state *currstate,
2311 char *aux, char **error)
2313 char *ret;
2314 game_state *solved;
2316 if (aux) {
2317 debug(("solve_game: aux = %s\n", aux));
2318 solved = execute_move(state, aux);
2319 if (!solved) {
2320 *error = "Generated aux string is not a valid move (!).";
2321 return NULL;
2323 } else {
2324 solved = dup_game(state);
2325 /* solve with max strength... */
2326 if (solve_from_scratch(solved, 10) == 0) {
2327 free_game(solved);
2328 *error = "Game does not have a (non-recursive) solution.";
2329 return NULL;
2332 ret = game_state_diff(currstate, solved);
2333 free_game(solved);
2334 debug(("solve_game: ret = %s\n", ret));
2335 return ret;
2338 /* ----------------------------------------------------------------------
2339 * Drawing routines.
2342 static void game_compute_size(game_params *params, int tilesize,
2343 int *x, int *y)
2345 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2346 struct { int tilesize; } ads, *ds = &ads;
2347 ads.tilesize = tilesize;
2349 *x = TILE_SIZE * params->w + 2 * BORDER;
2350 *y = TILE_SIZE * params->h + 2 * BORDER;
2353 static void game_set_size(drawing *dr, game_drawstate *ds,
2354 game_params *params, int tilesize)
2356 ds->tilesize = tilesize;
2359 static float *game_colours(frontend *fe, int *ncolours)
2361 float *ret = snewn(3 * NCOLOURS, float);
2362 int i;
2364 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
2366 for (i = 0; i < 3; i++) {
2367 ret[COL_FOREGROUND * 3 + i] = 0.0F;
2368 ret[COL_HINT * 3 + i] = ret[COL_LOWLIGHT * 3 + i];
2369 ret[COL_GRID * 3 + i] =
2370 (ret[COL_HINT * 3 + i] + ret[COL_BACKGROUND * 3 + i]) * 0.5F;
2371 ret[COL_MARK * 3 + i] = ret[COL_HIGHLIGHT * 3 + i];
2373 ret[COL_WARNING * 3 + 0] = 1.0F;
2374 ret[COL_WARNING * 3 + 1] = 0.25F;
2375 ret[COL_WARNING * 3 + 2] = 0.25F;
2377 ret[COL_SELECTED * 3 + 0] = 0.25F;
2378 ret[COL_SELECTED * 3 + 1] = 1.00F;
2379 ret[COL_SELECTED * 3 + 2] = 0.25F;
2381 ret[COL_CURSOR * 3 + 0] = min(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
2382 ret[COL_CURSOR * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
2383 ret[COL_CURSOR * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
2385 *ncolours = NCOLOURS;
2386 return ret;
2389 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2391 struct game_drawstate *ds = snew(struct game_drawstate);
2392 int wh = state->w*state->h;
2394 ds->tilesize = 0;
2395 ds->w = state->w;
2396 ds->h = state->h;
2397 ds->started = 0;
2398 ds->grid = snewn(wh, grid_type);
2399 memset(ds->grid, -1, wh*sizeof(grid_type));
2400 ds->lv = snewn(wh, int);
2401 ds->lh = snewn(wh, int);
2402 memset(ds->lv, 0, wh*sizeof(int));
2403 memset(ds->lh, 0, wh*sizeof(int));
2404 ds->show_hints = 0;
2406 return ds;
2409 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2411 sfree(ds->lv);
2412 sfree(ds->lh);
2413 sfree(ds->grid);
2414 sfree(ds);
2417 #define LINE_WIDTH (TILE_SIZE/8)
2418 #define TS8(x) (((x)*TILE_SIZE)/8)
2420 #define OFFSET(thing) ((TILE_SIZE/2) - ((thing)/2))
2422 static void lines_vert(drawing *dr, game_drawstate *ds,
2423 int ox, int oy, int lv, int col, grid_type v)
2425 int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2426 while ((bw = lw * lv + gw * (lv+1)) > TILE_SIZE)
2427 gw--;
2428 loff = OFFSET(bw);
2429 if (v & G_MARKV)
2430 draw_rect(dr, ox + loff, oy, bw, TILE_SIZE, COL_MARK);
2431 for (i = 0; i < lv; i++, loff += lw + gw)
2432 draw_rect(dr, ox + loff + gw, oy, lw, TILE_SIZE, col);
2435 static void lines_horiz(drawing *dr, game_drawstate *ds,
2436 int ox, int oy, int lh, int col, grid_type v)
2438 int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2439 while ((bw = lw * lh + gw * (lh+1)) > TILE_SIZE)
2440 gw--;
2441 loff = OFFSET(bw);
2442 if (v & G_MARKH)
2443 draw_rect(dr, ox, oy + loff, TILE_SIZE, bw, COL_MARK);
2444 for (i = 0; i < lh; i++, loff += lw + gw)
2445 draw_rect(dr, ox, oy + loff + gw, TILE_SIZE, lw, col);
2448 static void line_cross(drawing *dr, game_drawstate *ds,
2449 int ox, int oy, int col, grid_type v)
2451 int off = TS8(2);
2452 draw_line(dr, ox, oy, ox+off, oy+off, col);
2453 draw_line(dr, ox+off, oy, ox, oy+off, col);
2456 static int between_island(game_state *state, int sx, int sy, int dx, int dy)
2458 int x = sx - dx, y = sy - dy;
2460 while (INGRID(state, x, y)) {
2461 if (GRID(state, x, y) & G_ISLAND) goto found;
2462 x -= dx; y -= dy;
2464 return 0;
2465 found:
2466 x = sx + dx, y = sy + dy;
2467 while (INGRID(state, x, y)) {
2468 if (GRID(state, x, y) & G_ISLAND) return 1;
2469 x += dx; y += dy;
2471 return 0;
2474 static void lines_lvlh(game_state *state, game_ui *ui, int x, int y, grid_type v,
2475 int *lv_r, int *lh_r)
2477 int lh = 0, lv = 0;
2479 if (v & G_LINEV) lv = INDEX(state,lines,x,y);
2480 if (v & G_LINEH) lh = INDEX(state,lines,x,y);
2482 if (ui->show_hints) {
2483 if (between_island(state, x, y, 0, 1) && !lv) lv = 1;
2484 if (between_island(state, x, y, 1, 0) && !lh) lh = 1;
2486 /*debug(("lvlh: (%d,%d) v 0x%x lv %d lh %d.\n", x, y, v, lv, lh));*/
2487 *lv_r = lv; *lh_r = lh;
2490 static void dsf_debug_draw(drawing *dr,
2491 game_state *state, game_drawstate *ds,
2492 int x, int y)
2494 #ifdef DRAW_DSF
2495 int ts = TILE_SIZE/2;
2496 int ox = COORD(x) + ts/2, oy = COORD(y) + ts/2;
2497 char str[32];
2499 sprintf(str, "%d", dsf_canonify(state->solver->dsf, DINDEX(x,y)));
2500 draw_text(dr, ox, oy, FONT_VARIABLE, ts,
2501 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_WARNING, str);
2502 #endif
2505 static void lines_redraw(drawing *dr,
2506 game_state *state, game_drawstate *ds, game_ui *ui,
2507 int x, int y, grid_type v, int lv, int lh)
2509 int ox = COORD(x), oy = COORD(y);
2510 int vcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2511 (v & G_WARN) ? COL_WARNING : COL_FOREGROUND, hcol = vcol;
2512 grid_type todraw = v & G_NOLINE;
2514 if (v & G_ISSEL) {
2515 if (ui->todraw & G_FLAGSH) hcol = COL_SELECTED;
2516 if (ui->todraw & G_FLAGSV) vcol = COL_SELECTED;
2517 todraw |= ui->todraw;
2520 draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2521 /*if (v & G_CURSOR)
2522 draw_rect(dr, ox+TILE_SIZE/4, oy+TILE_SIZE/4,
2523 TILE_SIZE/2, TILE_SIZE/2, COL_CURSOR);*/
2526 if (ui->show_hints) {
2527 if (between_island(state, x, y, 0, 1) && !(v & G_LINEV))
2528 vcol = COL_HINT;
2529 if (between_island(state, x, y, 1, 0) && !(v & G_LINEH))
2530 hcol = COL_HINT;
2532 #ifdef DRAW_GRID
2533 draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_GRID);
2534 #endif
2536 if (todraw & G_NOLINEV) {
2537 line_cross(dr, ds, ox + TS8(3), oy + TS8(1), vcol, todraw);
2538 line_cross(dr, ds, ox + TS8(3), oy + TS8(5), vcol, todraw);
2540 if (todraw & G_NOLINEH) {
2541 line_cross(dr, ds, ox + TS8(1), oy + TS8(3), hcol, todraw);
2542 line_cross(dr, ds, ox + TS8(5), oy + TS8(3), hcol, todraw);
2544 /* if we're drawing a real line and a hint, make sure we draw the real
2545 * line on top. */
2546 if (lv && vcol == COL_HINT) lines_vert(dr, ds, ox, oy, lv, vcol, v);
2547 if (lh) lines_horiz(dr, ds, ox, oy, lh, hcol, v);
2548 if (lv && vcol != COL_HINT) lines_vert(dr, ds, ox, oy, lv, vcol, v);
2550 dsf_debug_draw(dr, state, ds, x, y);
2551 draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2554 #define ISLAND_RADIUS ((TILE_SIZE*12)/20)
2555 #define ISLAND_NUMSIZE(is) \
2556 (((is)->count < 10) ? (TILE_SIZE*7)/10 : (TILE_SIZE*5)/10)
2558 static void island_redraw(drawing *dr,
2559 game_state *state, game_drawstate *ds,
2560 struct island *is, grid_type v)
2562 /* These overlap the edges of their squares, which is why they're drawn later.
2563 * We know they can't overlap each other because they're not allowed within 2
2564 * squares of each other. */
2565 int half = TILE_SIZE/2;
2566 int ox = COORD(is->x) + half, oy = COORD(is->y) + half;
2567 int orad = ISLAND_RADIUS, irad = orad - LINE_WIDTH;
2568 int updatesz = orad*2+1;
2569 int tcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2570 (v & G_WARN) ? COL_WARNING : COL_FOREGROUND;
2571 int col = (v & G_ISSEL) ? COL_SELECTED : tcol;
2572 int bg = (v & G_CURSOR) ? COL_CURSOR :
2573 (v & G_MARK) ? COL_MARK : COL_BACKGROUND;
2574 char str[32];
2576 #ifdef DRAW_GRID
2577 draw_rect_outline(dr, COORD(is->x), COORD(is->y),
2578 TILE_SIZE, TILE_SIZE, COL_GRID);
2579 #endif
2581 /* draw a thick circle */
2582 draw_circle(dr, ox, oy, orad, col, col);
2583 draw_circle(dr, ox, oy, irad, bg, bg);
2585 sprintf(str, "%d", is->count);
2586 draw_text(dr, ox, oy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2587 ALIGN_VCENTRE | ALIGN_HCENTRE, tcol, str);
2589 dsf_debug_draw(dr, state, ds, is->x, is->y);
2590 draw_update(dr, ox - orad, oy - orad, updatesz, updatesz);
2593 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2594 game_state *state, int dir, game_ui *ui,
2595 float animtime, float flashtime)
2597 int x, y, force = 0, i, j, redraw, lv, lh;
2598 grid_type v, dsv, flash = 0;
2599 struct island *is, *is_drag_src = NULL, *is_drag_dst = NULL;
2601 if (flashtime) {
2602 int f = (int)(flashtime * 5 / FLASH_TIME);
2603 if (f == 1 || f == 3) flash = G_FLASH;
2606 /* Clear screen, if required. */
2607 if (!ds->started) {
2608 draw_rect(dr, 0, 0,
2609 TILE_SIZE * ds->w + 2 * BORDER,
2610 TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND);
2611 #ifdef DRAW_GRID
2612 draw_rect_outline(dr,
2613 COORD(0)-1, COORD(0)-1,
2614 TILE_SIZE * ds->w + 2, TILE_SIZE * ds->h + 2,
2615 COL_GRID);
2616 #endif
2617 draw_update(dr, 0, 0,
2618 TILE_SIZE * ds->w + 2 * BORDER,
2619 TILE_SIZE * ds->h + 2 * BORDER);
2620 ds->started = 1;
2621 force = 1;
2624 if (ui->dragx_src != -1 && ui->dragy_src != -1) {
2625 ds->dragging = 1;
2626 is_drag_src = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2627 assert(is_drag_src);
2628 if (ui->dragx_dst != -1 && ui->dragy_dst != -1) {
2629 is_drag_dst = INDEX(state, gridi, ui->dragx_dst, ui->dragy_dst);
2630 assert(is_drag_dst);
2632 } else
2633 ds->dragging = 0;
2635 if (ui->show_hints != ds->show_hints) {
2636 force = 1;
2637 ds->show_hints = ui->show_hints;
2640 /* Draw all lines (and hints, if we want), but *not* islands. */
2641 for (x = 0; x < ds->w; x++) {
2642 for (y = 0; y < ds->h; y++) {
2643 v = GRID(state, x, y) | flash;
2644 dsv = GRID(ds,x,y) & ~G_REDRAW;
2646 if (v & G_ISLAND) continue;
2648 if (is_drag_dst) {
2649 if (WITHIN(x,is_drag_src->x, is_drag_dst->x) &&
2650 WITHIN(y,is_drag_src->y, is_drag_dst->y))
2651 v |= G_ISSEL;
2653 lines_lvlh(state, ui, x, y, v, &lv, &lh);
2655 /*if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
2656 v |= G_CURSOR;*/
2658 if (v != dsv ||
2659 lv != INDEX(ds,lv,x,y) ||
2660 lh != INDEX(ds,lh,x,y) ||
2661 force) {
2662 GRID(ds, x, y) = v | G_REDRAW;
2663 INDEX(ds,lv,x,y) = lv;
2664 INDEX(ds,lh,x,y) = lh;
2665 lines_redraw(dr, state, ds, ui, x, y, v, lv, lh);
2666 } else
2667 GRID(ds,x,y) &= ~G_REDRAW;
2671 /* Draw islands. */
2672 for (i = 0; i < state->n_islands; i++) {
2673 is = &state->islands[i];
2674 v = GRID(state, is->x, is->y) | flash;
2676 redraw = 0;
2677 for (j = 0; j < is->adj.npoints; j++) {
2678 if (GRID(ds,is->adj.points[j].x,is->adj.points[j].y) & G_REDRAW) {
2679 redraw = 1;
2683 if (is_drag_src) {
2684 if (is == is_drag_src)
2685 v |= G_ISSEL;
2686 else if (is_drag_dst && is == is_drag_dst)
2687 v |= G_ISSEL;
2690 if (island_impossible(is, v & G_MARK)) v |= G_WARN;
2692 if (ui->cur_visible && ui->cur_x == is->x && ui->cur_y == is->y)
2693 v |= G_CURSOR;
2695 if ((v != GRID(ds, is->x, is->y)) || force || redraw) {
2696 GRID(ds,is->x,is->y) = v;
2697 island_redraw(dr, state, ds, is, v);
2702 static float game_anim_length(game_state *oldstate, game_state *newstate,
2703 int dir, game_ui *ui)
2705 return 0.0F;
2708 static float game_flash_length(game_state *oldstate, game_state *newstate,
2709 int dir, game_ui *ui)
2711 if (!oldstate->completed && newstate->completed &&
2712 !oldstate->solved && !newstate->solved)
2713 return FLASH_TIME;
2715 return 0.0F;
2718 static int game_timing_state(game_state *state, game_ui *ui)
2720 return TRUE;
2723 static void game_print_size(game_params *params, float *x, float *y)
2725 int pw, ph;
2727 /* 10mm squares by default. */
2728 game_compute_size(params, 1000, &pw, &ph);
2729 *x = pw / 100.0F;
2730 *y = ph / 100.0F;
2733 static void game_print(drawing *dr, game_state *state, int ts)
2735 int ink = print_mono_colour(dr, 0);
2736 int paper = print_mono_colour(dr, 1);
2737 int x, y, cx, cy, i, nl;
2738 int loff = ts/8;
2739 grid_type grid;
2741 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2742 game_drawstate ads, *ds = &ads;
2743 ads.tilesize = ts;
2745 /* I don't think this wants a border. */
2747 /* Bridges */
2748 print_line_width(dr, ts / 12);
2749 for (x = 0; x < state->w; x++) {
2750 for (y = 0; y < state->h; y++) {
2751 cx = COORD(x); cy = COORD(y);
2752 grid = GRID(state,x,y);
2753 nl = INDEX(state,lines,x,y);
2755 if (grid & G_ISLAND) continue;
2756 if (grid & G_LINEV) {
2757 if (nl > 1) {
2758 draw_line(dr, cx+ts/2-loff, cy, cx+ts/2-loff, cy+ts, ink);
2759 draw_line(dr, cx+ts/2+loff, cy, cx+ts/2+loff, cy+ts, ink);
2760 } else {
2761 draw_line(dr, cx+ts/2, cy, cx+ts/2, cy+ts, ink);
2764 if (grid & G_LINEH) {
2765 if (nl > 1) {
2766 draw_line(dr, cx, cy+ts/2-loff, cx+ts, cy+ts/2-loff, ink);
2767 draw_line(dr, cx, cy+ts/2+loff, cx+ts, cy+ts/2+loff, ink);
2768 } else {
2769 draw_line(dr, cx, cy+ts/2, cx+ts, cy+ts/2, ink);
2775 /* Islands */
2776 for (i = 0; i < state->n_islands; i++) {
2777 char str[32];
2778 struct island *is = &state->islands[i];
2779 grid = GRID(state, is->x, is->y);
2780 cx = COORD(is->x) + ts/2;
2781 cy = COORD(is->y) + ts/2;
2783 draw_circle(dr, cx, cy, ISLAND_RADIUS, paper, ink);
2785 sprintf(str, "%d", is->count);
2786 draw_text(dr, cx, cy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2787 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
2791 #ifdef COMBINED
2792 #define thegame bridges
2793 #endif
2795 const struct game thegame = {
2796 "Bridges", "games.bridges", "bridges",
2797 default_params,
2798 game_fetch_preset,
2799 decode_params,
2800 encode_params,
2801 free_params,
2802 dup_params,
2803 TRUE, game_configure, custom_params,
2804 validate_params,
2805 new_game_desc,
2806 validate_desc,
2807 new_game,
2808 dup_game,
2809 free_game,
2810 TRUE, solve_game,
2811 TRUE, game_can_format_as_text_now, game_text_format,
2812 new_ui,
2813 free_ui,
2814 encode_ui,
2815 decode_ui,
2816 game_changed_state,
2817 interpret_move,
2818 execute_move,
2819 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2820 game_colours,
2821 game_new_drawstate,
2822 game_free_drawstate,
2823 game_redraw,
2824 game_anim_length,
2825 game_flash_length,
2826 TRUE, FALSE, game_print_size, game_print,
2827 FALSE, /* wants_statusbar */
2828 FALSE, game_timing_state,
2829 REQUIRE_RBUTTON, /* flags */
2832 /* vim: set shiftwidth=4 tabstop=8: */