2 * magnets.c: implementation of janko.at 'magnets puzzle' game.
4 * http://64.233.179.104/translate_c?hl=en&u=http://www.janko.at/Raetsel/Magnete/Beispiel.htm
6 * Puzzle definition is just the size, and then the list of + (across then
7 * down) and - (across then down) present, then domino edges.
21 * 3x3:201,102,120,111,LRTT*BBLR
23 * 'Zotmeister' examples:
24 * 5x5:.2..1,3..1.,.2..2,2..2.,LRLRTTLRTBBT*BTTBLRBBLRLR
25 * 9x9:3.51...33,.2..23.13,..33.33.2,12...5.3.,**TLRTLR*,*TBLRBTLR,TBLRLRBTT,BLRTLRTBB,LRTB*TBLR,LRBLRBLRT,TTTLRLRTB,BBBTLRTB*,*LRBLRB**
27 * Janko 6x6 with solution:
28 * 6x6:322223,323132,232223,232223,LRTLRTTTBLRBBBTTLRLRBBLRTTLRTTBBLRBB
31 * 8x8:34131323,23131334,43122323,21332243,LRTLRLRT,LRBTTTTB,LRTBBBBT,TTBTLRTB,BBTBTTBT,TTBTBBTB,BBTBLRBT,LRBLRLRB
43 #ifdef STANDALONE_SOLVER
48 COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT,
49 COL_TEXT, COL_ERROR, COL_CURSOR,
50 COL_NEUTRAL, COL_NEGATIVE, COL_POSITIVE, COL_NOT,
55 enum { EMPTY = 0, NEUTRAL = EMPTY, POSITIVE = 1, NEGATIVE = 2 };
57 #if defined DEBUGGING || defined STANDALONE_SOLVER
58 static const char *cellnames[3] = { "neutral", "positive", "negative" };
59 #define NAME(w) ( ((w) < 0 || (w) > 2) ? "(out of range)" : cellnames[(w)] )
62 #define GRID2CHAR(g) ( ((g) >= 0 && (g) <= 2) ? ".+-"[(g)] : '?' )
63 #define CHAR2GRID(c) ( (c) == '+' ? POSITIVE : (c) == '-' ? NEGATIVE : NEUTRAL )
65 #define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
67 #define OPPOSITE(x) ( ((x)*2) % 3 ) /* 0 --> 0,
71 #define FLASH_TIME 0.7F
73 /* Macro ickery copied from slant.c */
77 #define ENUM(upper,title,lower) DIFF_ ## upper,
78 #define TITLE(upper,title,lower) #title,
79 #define ENCODE(upper,title,lower) #lower
80 #define CONFIG(upper,title,lower) ":" #title
81 enum { DIFFLIST(ENUM) DIFFCOUNT };
82 static char const *const magnets_diffnames[] = { DIFFLIST(TITLE) "(count)" };
83 static char const magnets_diffchars[] = DIFFLIST(ENCODE);
84 #define DIFFCONFIG DIFFLIST(CONFIG)
87 /* --------------------------------------------------------------- */
88 /* Game parameter functions. */
91 int w, h, diff, stripclues;
94 #define DEFAULT_PRESET 2
96 static const struct game_params magnets_presets[] = {
98 {6, 5, DIFF_TRICKY, 0},
99 {6, 5, DIFF_TRICKY, 1},
100 {8, 7, DIFF_EASY, 0},
101 {8, 7, DIFF_TRICKY, 0},
102 {8, 7, DIFF_TRICKY, 1},
103 {10, 9, DIFF_TRICKY, 0},
104 {10, 9, DIFF_TRICKY, 1}
107 static game_params *default_params(void)
109 game_params *ret = snew(game_params);
111 *ret = magnets_presets[DEFAULT_PRESET];
116 static int game_fetch_preset(int i, char **name, game_params **params)
121 if (i < 0 || i >= lenof(magnets_presets)) return FALSE;
123 ret = default_params();
124 *ret = magnets_presets[i]; /* struct copy */
127 sprintf(buf, "%dx%d %s%s",
128 magnets_presets[i].w, magnets_presets[i].h,
129 magnets_diffnames[magnets_presets[i].diff],
130 magnets_presets[i].stripclues ? ", strip clues" : "");
136 static void free_params(game_params *params)
141 static game_params *dup_params(const game_params *params)
143 game_params *ret = snew(game_params);
144 *ret = *params; /* structure copy */
148 static void decode_params(game_params *ret, char const *string)
150 ret->w = ret->h = atoi(string);
151 while (*string && isdigit((unsigned char) *string)) ++string;
152 if (*string == 'x') {
154 ret->h = atoi(string);
155 while (*string && isdigit((unsigned char)*string)) string++;
158 ret->diff = DIFF_EASY;
159 if (*string == 'd') {
162 for (i = 0; i < DIFFCOUNT; i++)
163 if (*string == magnets_diffchars[i])
165 if (*string) string++;
169 if (*string == 'S') {
175 static char *encode_params(const game_params *params, int full)
178 sprintf(buf, "%dx%d", params->w, params->h);
180 sprintf(buf + strlen(buf), "d%c%s",
181 magnets_diffchars[params->diff],
182 params->stripclues ? "S" : "");
186 static config_item *game_configure(const game_params *params)
191 ret = snewn(5, config_item);
193 ret[0].name = "Width";
194 ret[0].type = C_STRING;
195 sprintf(buf, "%d", params->w);
196 ret[0].sval = dupstr(buf);
199 ret[1].name = "Height";
200 ret[1].type = C_STRING;
201 sprintf(buf, "%d", params->h);
202 ret[1].sval = dupstr(buf);
205 ret[2].name = "Difficulty";
206 ret[2].type = C_CHOICES;
207 ret[2].sval = DIFFCONFIG;
208 ret[2].ival = params->diff;
210 ret[3].name = "Strip clues";
211 ret[3].type = C_BOOLEAN;
213 ret[3].ival = params->stripclues;
223 static game_params *custom_params(const config_item *cfg)
225 game_params *ret = snew(game_params);
227 ret->w = atoi(cfg[0].sval);
228 ret->h = atoi(cfg[1].sval);
229 ret->diff = cfg[2].ival;
230 ret->stripclues = cfg[3].ival;
235 static char *validate_params(const game_params *params, int full)
237 if (params->w < 2) return "Width must be at least one";
238 if (params->h < 2) return "Height must be at least one";
239 if (params->diff < 0 || params->diff >= DIFFCOUNT)
240 return "Unknown difficulty level";
245 /* --------------------------------------------------------------- */
246 /* Game state allocation, deallocation. */
249 int *dominoes; /* size w*h, dominoes[i] points to other end of domino. */
250 int *rowcount; /* size 3*h, array of [plus, minus, neutral] counts */
251 int *colcount; /* size 3*w, ditto */
257 #define GS_NOTPOSITIVE 4
258 #define GS_NOTNEGATIVE 8
259 #define GS_NOTNEUTRAL 16
262 #define GS_NOTMASK (GS_NOTPOSITIVE|GS_NOTNEGATIVE|GS_NOTNEUTRAL)
264 #define NOTFLAG(w) ( (w) == NEUTRAL ? GS_NOTNEUTRAL : \
265 (w) == POSITIVE ? GS_NOTPOSITIVE : \
266 (w) == NEGATIVE ? GS_NOTNEGATIVE : \
269 #define POSSIBLE(f,w) (!(state->flags[(f)] & NOTFLAG(w)))
273 int *grid; /* size w*h, for cell state (pos/neg) */
274 unsigned int *flags; /* size w*h */
275 int solved, completed, numbered;
277 struct game_common *common; /* domino layout never changes. */
280 static void clear_state(game_state *ret)
284 ret->solved = ret->completed = ret->numbered = 0;
286 memset(ret->common->rowcount, 0, ret->h*3*sizeof(int));
287 memset(ret->common->colcount, 0, ret->w*3*sizeof(int));
289 for (i = 0; i < ret->wh; i++) {
290 ret->grid[i] = EMPTY;
292 ret->common->dominoes[i] = i;
296 static game_state *new_state(int w, int h)
298 game_state *ret = snew(game_state);
300 memset(ret, 0, sizeof(game_state));
305 ret->grid = snewn(ret->wh, int);
306 ret->flags = snewn(ret->wh, unsigned int);
308 ret->common = snew(struct game_common);
309 ret->common->refcount = 1;
311 ret->common->dominoes = snewn(ret->wh, int);
312 ret->common->rowcount = snewn(ret->h*3, int);
313 ret->common->colcount = snewn(ret->w*3, int);
320 static game_state *dup_game(const game_state *src)
322 game_state *dest = snew(game_state);
328 dest->solved = src->solved;
329 dest->completed = src->completed;
330 dest->numbered = src->numbered;
332 dest->common = src->common;
333 dest->common->refcount++;
335 dest->grid = snewn(dest->wh, int);
336 memcpy(dest->grid, src->grid, dest->wh*sizeof(int));
338 dest->flags = snewn(dest->wh, unsigned int);
339 memcpy(dest->flags, src->flags, dest->wh*sizeof(unsigned int));
344 static void free_game(game_state *state)
346 state->common->refcount--;
347 if (state->common->refcount == 0) {
348 sfree(state->common->dominoes);
349 sfree(state->common->rowcount);
350 sfree(state->common->colcount);
351 sfree(state->common);
358 /* --------------------------------------------------------------- */
359 /* Game generation and reading. */
361 /* For a game of size w*h the game description is:
362 * w-sized string of column + numbers (L-R), or '.' for none
364 * h-sized string of row + numbers (T-B), or '.'
366 * w-sized string of column - numbers (L-R), or '.'
368 * h-sized string of row - numbers (T-B), or '.'
370 * w*h-sized string of 'L', 'R', 'U', 'D' for domino associations,
371 * or '*' for a black singleton square.
373 * for a total length of 2w + 2h + wh + 4.
376 static char n2c(int num) { /* XXX cloned from singles.c */
381 else if (num < 10+26)
382 return 'a' + num - 10;
384 return 'A' + num - 10 - 26;
388 static int c2n(char c) { /* XXX cloned from singles.c */
389 if (isdigit((unsigned char)c))
390 return (int)(c - '0');
391 else if (c >= 'a' && c <= 'z')
392 return (int)(c - 'a' + 10);
393 else if (c >= 'A' && c <= 'Z')
394 return (int)(c - 'A' + 10 + 26);
398 static const char *readrow(const char *desc, int n, int *array, int off,
404 for (i = 0; i < n; i++) {
406 if (c == 0) goto badchar;
411 if (num < 0) goto badchar;
413 array[i*3+off] = num;
416 if (c != ',') goto badchar;
421 "Game description too short" :
422 "Game description contained unexpected characters";
426 static game_state *new_game_int(const game_params *params, const char *desc,
429 game_state *state = new_state(params->w, params->h);
430 int x, y, idx, *count;
435 /* top row, left-to-right */
436 desc = readrow(desc, state->w, state->common->colcount, POSITIVE, prob);
437 if (*prob) goto done;
439 /* left column, top-to-bottom */
440 desc = readrow(desc, state->h, state->common->rowcount, POSITIVE, prob);
441 if (*prob) goto done;
443 /* bottom row, left-to-right */
444 desc = readrow(desc, state->w, state->common->colcount, NEGATIVE, prob);
445 if (*prob) goto done;
447 /* right column, top-to-bottom */
448 desc = readrow(desc, state->h, state->common->rowcount, NEGATIVE, prob);
449 if (*prob) goto done;
451 /* Add neutral counts (== size - pos - neg) to columns and rows.
452 * Any singleton cells will just be treated as permanently neutral. */
453 count = state->common->colcount;
454 for (x = 0; x < state->w; x++) {
455 if (count[x*3+POSITIVE] < 0 || count[x*3+NEGATIVE] < 0)
456 count[x*3+NEUTRAL] = -1;
459 state->h - count[x*3+POSITIVE] - count[x*3+NEGATIVE];
460 if (count[x*3+NEUTRAL] < 0) {
461 *prob = "Column counts inconsistent";
466 count = state->common->rowcount;
467 for (y = 0; y < state->h; y++) {
468 if (count[y*3+POSITIVE] < 0 || count[y*3+NEGATIVE] < 0)
469 count[y*3+NEUTRAL] = -1;
472 state->w - count[y*3+POSITIVE] - count[y*3+NEGATIVE];
473 if (count[y*3+NEUTRAL] < 0) {
474 *prob = "Row counts inconsistent";
481 for (y = 0; y < state->h; y++) {
482 for (x = 0; x < state->w; x++) {
483 idx = y*state->w + x;
487 if (c == 'L') /* this square is LHS of a domino */
488 state->common->dominoes[idx] = idx+1;
489 else if (c == 'R') /* ... RHS of a domino */
490 state->common->dominoes[idx] = idx-1;
491 else if (c == 'T') /* ... top of a domino */
492 state->common->dominoes[idx] = idx+state->w;
493 else if (c == 'B') /* ... bottom of a domino */
494 state->common->dominoes[idx] = idx-state->w;
495 else if (c == '*') /* singleton */
496 state->common->dominoes[idx] = idx;
497 else if (c == ',') /* spacer, ignore */
503 /* Check dominoes as input are sensibly consistent
504 * (i.e. each end points to the other) */
505 for (idx = 0; idx < state->wh; idx++) {
506 if (state->common->dominoes[idx] < 0 ||
507 state->common->dominoes[idx] > state->wh ||
508 state->common->dominoes[state->common->dominoes[idx]] != idx) {
509 *prob = "Domino descriptions inconsistent";
512 if (state->common->dominoes[idx] == idx) {
513 state->grid[idx] = NEUTRAL;
514 state->flags[idx] |= GS_SET;
523 "Game description too short" :
524 "Game description contained unexpected characters";
534 static char *validate_desc(const game_params *params, const char *desc)
537 game_state *st = new_game_int(params, desc, &prob);
538 if (!st) return (char*)prob;
543 static game_state *new_game(midend *me, const game_params *params,
547 game_state *st = new_game_int(params, desc, &prob);
552 static char *generate_desc(game_state *new)
554 int x, y, idx, other, w = new->w, h = new->h;
555 char *desc = snewn(new->wh + 2*(w + h) + 5, char), *p = desc;
557 for (x = 0; x < w; x++) *p++ = n2c(new->common->colcount[x*3+POSITIVE]);
559 for (y = 0; y < h; y++) *p++ = n2c(new->common->rowcount[y*3+POSITIVE]);
562 for (x = 0; x < w; x++) *p++ = n2c(new->common->colcount[x*3+NEGATIVE]);
564 for (y = 0; y < h; y++) *p++ = n2c(new->common->rowcount[y*3+NEGATIVE]);
567 for (y = 0; y < h; y++) {
568 for (x = 0; x < w; x++) {
570 other = new->common->dominoes[idx];
572 if (other == idx) *p++ = '*';
573 else if (other == idx+1) *p++ = 'L';
574 else if (other == idx-1) *p++ = 'R';
575 else if (other == idx+w) *p++ = 'T';
576 else if (other == idx-w) *p++ = 'B';
577 else assert(!"mad domino orientation");
585 static void game_text_hborder(const game_state *state, char **p_r)
592 for (x = 0; x < state->w*2-1; x++) *p++ = '-';
599 static int game_can_format_as_text_now(const game_params *params)
604 static char *game_text_format(const game_state *state)
609 len = ((state->w*2)+4) * ((state->h*2)+4) + 2;
610 p = ret = snewn(len, char);
612 /* top row: '+' then column totals for plus. */
614 for (x = 0; x < state->w; x++) {
616 *p++ = n2c(state->common->colcount[x*3+POSITIVE]);
621 game_text_hborder(state, &p);
623 for (y = 0; y < state->h; y++) {
624 *p++ = n2c(state->common->rowcount[y*3+POSITIVE]);
626 for (x = 0; x < state->w; x++) {
628 *p++ = state->common->dominoes[i] == i ? '#' :
629 state->grid[i] == POSITIVE ? '+' :
630 state->grid[i] == NEGATIVE ? '-' :
631 state->flags[i] & GS_SET ? '*' : ' ';
632 if (x < (state->w-1))
633 *p++ = state->common->dominoes[i] == i+1 ? ' ' : '|';
636 *p++ = n2c(state->common->rowcount[y*3+NEGATIVE]);
639 if (y < (state->h-1)) {
642 for (x = 0; x < state->w; x++) {
644 *p++ = state->common->dominoes[i] == i+state->w ? ' ' : '-';
645 if (x < (state->w-1))
654 game_text_hborder(state, &p);
656 /* bottom row: column totals for minus then '-'. */
658 for (x = 0; x < state->w; x++) {
660 *p++ = n2c(state->common->colcount[x*3+NEGATIVE]);
670 static void game_debug(game_state *state, const char *desc)
672 char *fmt = game_text_format(state);
673 debug(("%s:\n%s\n", desc, fmt));
677 enum { ROW, COLUMN };
679 typedef struct rowcol {
680 int i, di, n, roworcol, num;
685 static rowcol mkrowcol(const game_state *state, int num, int roworcol)
689 rc.roworcol = roworcol;
692 if (roworcol == ROW) {
693 rc.i = num * state->w;
696 rc.targets = &(state->common->rowcount[num*3]);
698 } else if (roworcol == COLUMN) {
702 rc.targets = &(state->common->colcount[num*3]);
705 assert(!"unknown roworcol");
710 static int count_rowcol(const game_state *state, int num, int roworcol,
714 rowcol rc = mkrowcol(state, num, roworcol);
716 for (i = 0; i < rc.n; i++, rc.i += rc.di) {
718 if (state->grid[rc.i] == EMPTY &&
719 !(state->flags[rc.i] & GS_SET))
721 } else if (state->grid[rc.i] == which)
727 static void check_rowcol(game_state *state, int num, int roworcol, int which,
728 int *wrong, int *incomplete)
730 int count, target = mkrowcol(state, num, roworcol).targets[which];
732 if (target == -1) return; /* no number to check against. */
734 count = count_rowcol(state, num, roworcol, which);
735 if (count < target) *incomplete = 1;
736 if (count > target) *wrong = 1;
739 static int check_completion(game_state *state)
741 int i, j, x, y, idx, w = state->w, h = state->h;
742 int which = POSITIVE, wrong = 0, incomplete = 0;
744 /* Check row and column counts for magnets. */
745 for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
746 for (i = 0; i < w; i++)
747 check_rowcol(state, i, COLUMN, which, &wrong, &incomplete);
749 for (i = 0; i < h; i++)
750 check_rowcol(state, i, ROW, which, &wrong, &incomplete);
752 /* Check each domino has been filled, and that we don't have
753 * touching identical terminals. */
754 for (i = 0; i < state->wh; i++) state->flags[i] &= ~GS_ERROR;
755 for (x = 0; x < w; x++) {
756 for (y = 0; y < h; y++) {
758 if (state->common->dominoes[idx] == idx)
759 continue; /* no domino here */
761 if (!(state->flags[idx] & GS_SET))
764 which = state->grid[idx];
765 if (which != NEUTRAL) {
766 #define CHECK(xx,yy) do { \
767 if (INGRID(state,xx,yy) && \
768 (state->grid[(yy)*w+(xx)] == which)) { \
770 state->flags[(yy)*w+(xx)] |= GS_ERROR; \
771 state->flags[y*w+x] |= GS_ERROR; \
782 return wrong ? -1 : incomplete ? 0 : 1;
785 static const int dx[4] = {-1, 1, 0, 0};
786 static const int dy[4] = {0, 0, -1, 1};
788 static void solve_clearflags(game_state *state)
792 for (i = 0; i < state->wh; i++) {
793 state->flags[i] &= ~GS_NOTMASK;
794 if (state->common->dominoes[i] != i)
795 state->flags[i] &= ~GS_SET;
799 /* Knowing a given cell cannot be a certain colour also tells us
800 * something about the other cell in that domino. */
801 static int solve_unflag(game_state *state, int i, int which,
802 const char *why, rowcol *rc)
805 #if defined DEBUGGING || defined STANDALONE_SOLVER
809 assert(i >= 0 && i < state->wh);
810 ii = state->common->dominoes[i];
811 if (ii == i) return 0;
814 debug(("solve_unflag: (%d,%d) for %s %d", i%w, i/w, rc->name, rc->num));
816 if ((state->flags[i] & GS_SET) && (state->grid[i] == which)) {
817 debug(("solve_unflag: (%d,%d) already %s, cannot unflag (for %s).",
818 i%w, i/w, NAME(which), why));
821 if ((state->flags[ii] & GS_SET) && (state->grid[ii] == OPPOSITE(which))) {
822 debug(("solve_unflag: (%d,%d) opposite already %s, cannot unflag (for %s).",
823 ii%w, ii/w, NAME(OPPOSITE(which)), why));
826 if (POSSIBLE(i, which)) {
827 state->flags[i] |= NOTFLAG(which);
829 debug(("solve_unflag: (%d,%d) CANNOT be %s (%s)",
830 i%w, i/w, NAME(which), why));
832 if (POSSIBLE(ii, OPPOSITE(which))) {
833 state->flags[ii] |= NOTFLAG(OPPOSITE(which));
835 debug(("solve_unflag: (%d,%d) CANNOT be %s (%s, other half)",
836 ii%w, ii/w, NAME(OPPOSITE(which)), why));
838 #ifdef STANDALONE_SOLVER
839 if (verbose && ret) {
840 printf("(%d,%d)", i%w, i/w);
841 if (rc) printf(" in %s %d", rc->name, rc->num);
842 printf(" cannot be %s (%s); opposite (%d,%d) not %s.\n",
843 NAME(which), why, ii%w, ii/w, NAME(OPPOSITE(which)));
849 static int solve_unflag_surrounds(game_state *state, int i, int which)
851 int x = i%state->w, y = i/state->w, xx, yy, j, ii;
853 assert(INGRID(state, x, y));
855 for (j = 0; j < 4; j++) {
856 xx = x+dx[j]; yy = y+dy[j];
857 if (!INGRID(state, xx, yy)) continue;
860 if (solve_unflag(state, ii, which, "adjacent to set cell", NULL) < 0)
866 /* Sets a cell to a particular colour, and also perform other
867 * housekeeping around that. */
868 static int solve_set(game_state *state, int i, int which,
869 const char *why, rowcol *rc)
872 #if defined DEBUGGING || defined STANDALONE_SOLVER
876 ii = state->common->dominoes[i];
878 if (state->flags[i] & GS_SET) {
879 if (state->grid[i] == which) {
880 return 0; /* was already set and held, do nothing. */
882 debug(("solve_set: (%d,%d) is held and %s, cannot set to %s",
883 i%w, i/w, NAME(state->grid[i]), NAME(which)));
887 if ((state->flags[ii] & GS_SET) && state->grid[ii] != OPPOSITE(which)) {
888 debug(("solve_set: (%d,%d) opposite is held and %s, cannot set to %s",
889 ii%w, ii/w, NAME(state->grid[ii]), NAME(OPPOSITE(which))));
892 if (!POSSIBLE(i, which)) {
893 debug(("solve_set: (%d,%d) NOT %s, cannot set.", i%w, i/w, NAME(which)));
896 if (!POSSIBLE(ii, OPPOSITE(which))) {
897 debug(("solve_set: (%d,%d) NOT %s, cannot set (%d,%d).",
898 ii%w, ii/w, NAME(OPPOSITE(which)), i%w, i/w));
902 #ifdef STANDALONE_SOLVER
904 printf("(%d,%d)", i%w, i/w);
905 if (rc) printf(" in %s %d", rc->name, rc->num);
906 printf(" set to %s (%s), opposite (%d,%d) set to %s.\n",
907 NAME(which), why, ii%w, ii/w, NAME(OPPOSITE(which)));
911 debug(("solve_set: (%d,%d) for %s %d", i%w, i/w, rc->name, rc->num));
912 debug(("solve_set: (%d,%d) setting to %s (%s), surrounds first:",
913 i%w, i/w, NAME(which), why));
915 if (which != NEUTRAL) {
916 if (solve_unflag_surrounds(state, i, which) < 0)
918 if (solve_unflag_surrounds(state, ii, OPPOSITE(which)) < 0)
922 state->grid[i] = which;
923 state->grid[ii] = OPPOSITE(which);
925 state->flags[i] |= GS_SET;
926 state->flags[ii] |= GS_SET;
928 debug(("solve_set: (%d,%d) set to %s (%s)", i%w, i/w, NAME(which), why));
933 /* counts should be int[4]. */
934 static void solve_counts(game_state *state, rowcol rc, int *counts, int *unset)
939 for (i = 0; i < 4; i++) {
941 if (unset) unset[i] = 0;
944 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
945 if (state->flags[i] & GS_SET) {
946 assert(state->grid[i] < 3);
947 counts[state->grid[i]]++;
949 for (which = 0; which <= 2; which++) {
950 if (POSSIBLE(i, which))
957 static int solve_checkfull(game_state *state, rowcol rc, int *counts)
959 int starti = rc.i, j, which, didsth = 0, target;
962 assert(state->numbered); /* only useful (should only be called) if numbered. */
964 solve_counts(state, rc, counts, unset);
966 for (which = 0; which <= 2; which++) {
967 target = rc.targets[which];
968 if (target == -1) continue;
970 /*debug(("%s %d for %s: target %d, count %d, unset %d",
971 rc.name, rc.num, NAME(which),
972 target, counts[which], unset[which]));*/
974 if (target < counts[which]) {
975 debug(("%s %d has too many (%d) %s squares (target %d), impossible!",
976 rc.name, rc.num, counts[which], NAME(which), target));
979 if (target == counts[which]) {
980 /* We have the correct no. of the colour in this row/column
981 * already; unflag all the rest. */
982 for (rc.i = starti, j = 0; j < rc.n; rc.i += rc.di, j++) {
983 if (state->flags[rc.i] & GS_SET) continue;
984 if (!POSSIBLE(rc.i, which)) continue;
986 if (solve_unflag(state, rc.i, which, "row/col full", &rc) < 0)
990 } else if ((target - counts[which]) == unset[which]) {
991 /* We need all the remaining unset squares for this colour;
993 for (rc.i = starti, j = 0; j < rc.n; rc.i += rc.di, j++) {
994 if (state->flags[rc.i] & GS_SET) continue;
995 if (!POSSIBLE(rc.i, which)) continue;
997 if (solve_set(state, rc.i, which, "row/col needs all unset", &rc) < 0)
1006 static int solve_startflags(game_state *state)
1010 for (x = 0; x < state->w; x++) {
1011 for (y = 0; y < state->h; y++) {
1013 if (state->common->dominoes[i] == i) continue;
1014 if (state->grid[i] != NEUTRAL ||
1015 state->flags[i] & GS_SET) {
1016 if (solve_set(state, i, state->grid[i], "initial set-and-hold", NULL) < 0)
1024 typedef int (*rowcolfn)(game_state *state, rowcol rc, int *counts);
1026 static int solve_rowcols(game_state *state, rowcolfn fn)
1028 int x, y, didsth = 0, ret;
1032 for (x = 0; x < state->w; x++) {
1033 rc = mkrowcol(state, x, COLUMN);
1034 solve_counts(state, rc, counts, NULL);
1036 ret = fn(state, rc, counts);
1037 if (ret < 0) return ret;
1040 for (y = 0; y < state->h; y++) {
1041 rc = mkrowcol(state, y, ROW);
1042 solve_counts(state, rc, counts, NULL);
1044 ret = fn(state, rc, counts);
1045 if (ret < 0) return ret;
1051 static int solve_force(game_state *state)
1053 int i, which, didsth = 0;
1056 for (i = 0; i < state->wh; i++) {
1057 if (state->flags[i] & GS_SET) continue;
1058 if (state->common->dominoes[i] == i) continue;
1060 f = state->flags[i] & GS_NOTMASK;
1062 if (f == (GS_NOTPOSITIVE|GS_NOTNEGATIVE))
1064 if (f == (GS_NOTPOSITIVE|GS_NOTNEUTRAL))
1066 if (f == (GS_NOTNEGATIVE|GS_NOTNEUTRAL))
1069 if (solve_set(state, i, which, "forced by flags", NULL) < 0)
1077 static int solve_neither(game_state *state)
1079 int i, j, didsth = 0;
1081 for (i = 0; i < state->wh; i++) {
1082 if (state->flags[i] & GS_SET) continue;
1083 j = state->common->dominoes[i];
1084 if (i == j) continue;
1086 if (((state->flags[i] & GS_NOTPOSITIVE) &&
1087 (state->flags[j] & GS_NOTPOSITIVE)) ||
1088 ((state->flags[i] & GS_NOTNEGATIVE) &&
1089 (state->flags[j] & GS_NOTNEGATIVE))) {
1090 if (solve_set(state, i, NEUTRAL, "neither tile magnet", NULL) < 0)
1098 static int solve_advancedfull(game_state *state, rowcol rc, int *counts)
1100 int i, j, nfound = 0, clearpos = 0, clearneg = 0, ret = 0;
1102 /* For this row/col, look for a domino entirely within the row where
1103 * both ends can only be + or - (but isn't held).
1104 * The +/- counts can thus be decremented by 1 each, and the 'unset'
1107 * Once that's done for all such dominoes (and they're marked), try
1108 * and made usual deductions about rest of the row based on new totals. */
1110 if (rc.targets[POSITIVE] == -1 && rc.targets[NEGATIVE] == -1)
1111 return 0; /* don't have a target for either colour, nothing to do. */
1112 if ((rc.targets[POSITIVE] >= 0 && counts[POSITIVE] == rc.targets[POSITIVE]) &&
1113 (rc.targets[NEGATIVE] >= 0 && counts[NEGATIVE] == rc.targets[NEGATIVE]))
1114 return 0; /* both colours are full up already, nothing to do. */
1116 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++)
1117 state->flags[i] &= ~GS_MARK;
1119 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1120 if (state->flags[i] & GS_SET) continue;
1122 /* We're looking for a domino in our row/col, thus if
1123 * dominoes[i] -> i+di we've found one. */
1124 if (state->common->dominoes[i] != i+rc.di) continue;
1126 /* We need both squares of this domino to be either + or -
1127 * (i.e. both NOTNEUTRAL only). */
1128 if (((state->flags[i] & GS_NOTMASK) != GS_NOTNEUTRAL) ||
1129 ((state->flags[i+rc.di] & GS_NOTMASK) != GS_NOTNEUTRAL))
1132 debug(("Domino in %s %d at (%d,%d) must be polarised.",
1133 rc.name, rc.num, i%state->w, i/state->w));
1134 state->flags[i] |= GS_MARK;
1135 state->flags[i+rc.di] |= GS_MARK;
1138 if (nfound == 0) return 0;
1140 /* nfound is #dominoes we matched, which will all be marked. */
1141 counts[POSITIVE] += nfound;
1142 counts[NEGATIVE] += nfound;
1144 if (rc.targets[POSITIVE] >= 0 && counts[POSITIVE] == rc.targets[POSITIVE]) {
1145 debug(("%s %d has now filled POSITIVE:", rc.name, rc.num));
1148 if (rc.targets[NEGATIVE] >= 0 && counts[NEGATIVE] == rc.targets[NEGATIVE]) {
1149 debug(("%s %d has now filled NEGATIVE:", rc.name, rc.num));
1153 if (!clearpos && !clearneg) return 0;
1155 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1156 if (state->flags[i] & GS_SET) continue;
1157 if (state->flags[i] & GS_MARK) continue;
1159 if (clearpos && !(state->flags[i] & GS_NOTPOSITIVE)) {
1160 if (solve_unflag(state, i, POSITIVE, "row/col full (+ve) [tricky]", &rc) < 0)
1164 if (clearneg && !(state->flags[i] & GS_NOTNEGATIVE)) {
1165 if (solve_unflag(state, i, NEGATIVE, "row/col full (-ve) [tricky]", &rc) < 0)
1174 /* If we only have one neutral still to place on a row/column then no
1175 dominoes entirely in that row/column can be neutral. */
1176 static int solve_nonneutral(game_state *state, rowcol rc, int *counts)
1180 if (rc.targets[NEUTRAL] != counts[NEUTRAL]+1)
1183 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1184 if (state->flags[i] & GS_SET) continue;
1185 if (state->common->dominoes[i] != i+rc.di) continue;
1187 if (!(state->flags[i] & GS_NOTNEUTRAL)) {
1188 if (solve_unflag(state, i, NEUTRAL, "single neutral in row/col [tricky]", &rc) < 0)
1196 /* If we need to fill all unfilled cells with +-, and we need 1 more of
1197 * one than the other, and we have a single odd-numbered region of unfilled
1198 * cells, that odd-numbered region must start and end with the extra number. */
1199 static int solve_oddlength(game_state *state, rowcol rc, int *counts)
1201 int i, j, ret = 0, extra, tpos, tneg;
1202 int start = -1, length = 0, inempty = 0, startodd = -1;
1204 /* need zero neutral cells still to find... */
1205 if (rc.targets[NEUTRAL] != counts[NEUTRAL])
1208 /* ...and #positive and #negative to differ by one. */
1209 tpos = rc.targets[POSITIVE] - counts[POSITIVE];
1210 tneg = rc.targets[NEGATIVE] - counts[NEGATIVE];
1213 else if (tneg == tpos+1)
1217 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1218 if (state->flags[i] & GS_SET) {
1221 /* we've just finished an odd-length section. */
1222 if (startodd != -1) goto twoodd;
1237 if (inempty && (length % 2)) {
1238 if (startodd != -1) goto twoodd;
1242 ret = solve_set(state, startodd, extra, "odd-length section start", &rc);
1247 debug(("%s %d has >1 odd-length sections, starting at %d,%d and %d,%d.",
1249 startodd%state->w, startodd/state->w,
1250 start%state->w, start/state->w));
1254 /* Count the number of remaining empty dominoes in any row/col.
1255 * If that number is equal to the #remaining positive,
1256 * or to the #remaining negative, no empty cells can be neutral. */
1257 static int solve_countdominoes_neutral(game_state *state, rowcol rc, int *counts)
1259 int i, j, ndom = 0, nonn = 0, ret = 0;
1261 if ((rc.targets[POSITIVE] == -1) && (rc.targets[NEGATIVE] == -1))
1262 return 0; /* need at least one target to compare. */
1264 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1265 if (state->flags[i] & GS_SET) continue;
1266 assert(state->grid[i] == EMPTY);
1268 /* Skip solo cells, or second cell in domino. */
1269 if ((state->common->dominoes[i] == i) ||
1270 (state->common->dominoes[i] == i-rc.di))
1276 if ((rc.targets[POSITIVE] != -1) &&
1277 (rc.targets[POSITIVE]-counts[POSITIVE] == ndom))
1279 if ((rc.targets[NEGATIVE] != -1) &&
1280 (rc.targets[NEGATIVE]-counts[NEGATIVE] == ndom))
1283 if (!nonn) return 0;
1285 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1286 if (state->flags[i] & GS_SET) continue;
1288 if (!(state->flags[i] & GS_NOTNEUTRAL)) {
1289 if (solve_unflag(state, i, NEUTRAL, "all dominoes +/- [tricky]", &rc) < 0)
1297 static int solve_domino_count(game_state *state, rowcol rc, int i, int which)
1301 /* Skip solo cells or 2nd in domino. */
1302 if ((state->common->dominoes[i] == i) ||
1303 (state->common->dominoes[i] == i-rc.di))
1306 if (state->flags[i] & GS_SET)
1309 if (POSSIBLE(i, which))
1312 if (state->common->dominoes[i] == i+rc.di) {
1313 /* second cell of domino is on our row: test that too. */
1314 if (POSSIBLE(i+rc.di, which))
1320 /* Count number of dominoes we could put each of + and - into. If it is equal
1321 * to the #left, any domino we can only put + or - in one cell of must have it. */
1322 static int solve_countdominoes_nonneutral(game_state *state, rowcol rc, int *counts)
1324 int which, w, i, j, ndom = 0, didsth = 0, toset;
1326 for (which = POSITIVE, w = 0; w < 2; which = OPPOSITE(which), w++) {
1327 if (rc.targets[which] == -1) continue;
1329 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1330 if (solve_domino_count(state, rc, i, which) > 0)
1334 if ((rc.targets[which] - counts[which]) != ndom)
1337 for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1338 if (solve_domino_count(state, rc, i, which) == 1) {
1339 if (POSSIBLE(i, which))
1342 /* paranoia, should have been checked by solve_domino_count. */
1343 assert(state->common->dominoes[i] == i+rc.di);
1344 assert(POSSIBLE(i+rc.di, which));
1347 if (solve_set(state, toset, which, "all empty dominoes need +/- [tricky]", &rc) < 0)
1356 /* danger, evil macro. can't use the do { ... } while(0) trick because
1357 * the continue breaks. */
1358 #define SOLVE_FOR_ROWCOLS(fn) \
1359 ret = solve_rowcols(state, fn); \
1360 if (ret < 0) { debug(("%s said impossible, cannot solve", #fn)); return -1; } \
1361 if (ret > 0) continue
1363 static int solve_state(game_state *state, int diff)
1367 debug(("solve_state, difficulty %s", magnets_diffnames[diff]));
1369 solve_clearflags(state);
1370 if (solve_startflags(state) < 0) return -1;
1373 ret = solve_force(state);
1374 if (ret > 0) continue;
1375 if (ret < 0) return -1;
1377 ret = solve_neither(state);
1378 if (ret > 0) continue;
1379 if (ret < 0) return -1;
1381 SOLVE_FOR_ROWCOLS(solve_checkfull);
1382 SOLVE_FOR_ROWCOLS(solve_oddlength);
1384 if (diff < DIFF_TRICKY) break;
1386 SOLVE_FOR_ROWCOLS(solve_advancedfull);
1387 SOLVE_FOR_ROWCOLS(solve_nonneutral);
1388 SOLVE_FOR_ROWCOLS(solve_countdominoes_neutral);
1389 SOLVE_FOR_ROWCOLS(solve_countdominoes_nonneutral);
1395 return check_completion(state);
1399 static char *game_state_diff(const game_state *src, const game_state *dst,
1402 char *ret = NULL, buf[80], c;
1403 int retlen = 0, x, y, i, k;
1405 assert(src->w == dst->w && src->h == dst->h);
1408 ret = sresize(ret, 3, char);
1409 ret[0] = 'S'; ret[1] = ';'; ret[2] = '\0';
1412 for (x = 0; x < dst->w; x++) {
1413 for (y = 0; y < dst->h; y++) {
1416 if (src->common->dominoes[i] == i) continue;
1418 #define APPEND do { \
1419 ret = sresize(ret, retlen + k + 1, char); \
1420 strcpy(ret + retlen, buf); \
1424 if ((src->grid[i] != dst->grid[i]) ||
1425 ((src->flags[i] & GS_SET) != (dst->flags[i] & GS_SET))) {
1426 if (dst->grid[i] == EMPTY && !(dst->flags[i] & GS_SET))
1429 c = GRID2CHAR(dst->grid[i]);
1430 k = sprintf(buf, "%c%d,%d;", (int)c, x, y);
1435 debug(("game_state_diff returns %s", ret));
1439 static void solve_from_aux(const game_state *state, const char *aux)
1442 assert(strlen(aux) == state->wh);
1443 for (i = 0; i < state->wh; i++) {
1444 state->grid[i] = CHAR2GRID(aux[i]);
1445 state->flags[i] |= GS_SET;
1449 static char *solve_game(const game_state *state, const game_state *currstate,
1450 const char *aux, char **error)
1452 game_state *solved = dup_game(currstate);
1456 if (aux && strlen(aux) == state->wh) {
1457 solve_from_aux(solved, aux);
1461 if (solve_state(solved, DIFFCOUNT) > 0) goto solved;
1464 solved = dup_game(state);
1465 ret = solve_state(solved, DIFFCOUNT);
1466 if (ret > 0) goto solved;
1469 *error = (ret < 0) ? "Puzzle is impossible." : "Unable to solve puzzle.";
1473 move = game_state_diff(currstate, solved, 1);
1478 static int solve_unnumbered(game_state *state)
1482 ret = solve_force(state);
1483 if (ret > 0) continue;
1484 if (ret < 0) return -1;
1486 ret = solve_neither(state);
1487 if (ret > 0) continue;
1488 if (ret < 0) return -1;
1492 for (i = 0; i < state->wh; i++) {
1493 if (!(state->flags[i] & GS_SET)) return 0;
1498 static int lay_dominoes(game_state *state, random_state *rs, int *scratch)
1500 int n, i, ret = 0, nlaid = 0, n_initial_neutral;
1502 for (i = 0; i < state->wh; i++) {
1504 state->grid[i] = EMPTY;
1505 state->flags[i] = (state->common->dominoes[i] == i) ? GS_SET : 0;
1507 shuffle(scratch, state->wh, sizeof(int), rs);
1509 n_initial_neutral = (state->wh > 100) ? 5 : (state->wh / 10);
1511 for (n = 0; n < state->wh; n++) {
1512 /* Find a space ... */
1515 if (state->flags[i] & GS_SET) continue; /* already laid here. */
1517 /* ...and lay a domino if we can. */
1519 debug(("Laying domino at i:%d, (%d,%d)\n", i, i%state->w, i/state->w));
1521 /* The choice of which type of domino to lay here leads to subtle differences
1522 * in the sorts of boards that get produced. Too much bias towards magnets
1523 * leads to games that are too easy.
1525 * Currently, it lays a small set of dominoes at random as neutral, and
1526 * then lays the rest preferring to be magnets -- however, if the
1527 * current layout is such that a magnet won't go there, then it lays
1530 * The number of initially neutral dominoes is limited as grids get bigger:
1531 * too many neutral dominoes invariably ends up with insoluble puzzle at
1532 * this size, and the positioning process means it'll always end up laying
1533 * more than the initial 5 anyway.
1536 /* We should always be able to lay a neutral anywhere. */
1537 assert(!(state->flags[i] & GS_NOTNEUTRAL));
1539 if (n < n_initial_neutral) {
1540 debug((" ...laying neutral\n"));
1541 ret = solve_set(state, i, NEUTRAL, "layout initial neutral", NULL);
1543 debug((" ... preferring magnet\n"));
1544 if (!(state->flags[i] & GS_NOTPOSITIVE))
1545 ret = solve_set(state, i, POSITIVE, "layout", NULL);
1546 else if (!(state->flags[i] & GS_NOTNEGATIVE))
1547 ret = solve_set(state, i, NEGATIVE, "layout", NULL);
1549 ret = solve_set(state, i, NEUTRAL, "layout", NULL);
1552 debug(("Unable to lay anything at (%d,%d), giving up.",
1553 i%state->w, i/state->w));
1559 ret = solve_unnumbered(state);
1561 debug(("solve_unnumbered decided impossible.\n"));
1566 debug(("Laid %d dominoes, total %d dominoes.\n", nlaid, state->wh/2));
1567 game_debug(state, "Final layout");
1571 static void gen_game(game_state *new, random_state *rs)
1574 int *scratch = snewn(new->wh, int);
1576 #ifdef STANDALONE_SOLVER
1577 if (verbose) printf("Generating new game...\n");
1581 sfree(new->common->dominoes); /* bit grotty. */
1582 new->common->dominoes = domino_layout(new->w, new->h, rs);
1585 ret = lay_dominoes(new, rs, scratch);
1588 /* for each cell, update colcount/rowcount as appropriate. */
1589 memset(new->common->colcount, 0, new->w*3*sizeof(int));
1590 memset(new->common->rowcount, 0, new->h*3*sizeof(int));
1591 for (x = 0; x < new->w; x++) {
1592 for (y = 0; y < new->h; y++) {
1593 val = new->grid[y*new->w+x];
1594 new->common->colcount[x*3+val]++;
1595 new->common->rowcount[y*3+val]++;
1603 static void generate_aux(game_state *new, char *aux)
1606 for (i = 0; i < new->wh; i++)
1607 aux[i] = GRID2CHAR(new->grid[i]);
1608 aux[new->wh] = '\0';
1611 static int check_difficulty(const game_params *params, game_state *new,
1614 int *scratch, *grid_correct, slen, i;
1616 memset(new->grid, EMPTY, new->wh*sizeof(int));
1618 if (params->diff > DIFF_EASY) {
1619 /* If this is too easy, return. */
1620 if (solve_state(new, params->diff-1) > 0) {
1621 debug(("Puzzle is too easy."));
1625 if (solve_state(new, params->diff) <= 0) {
1626 debug(("Puzzle is not soluble at requested difficulty."));
1629 if (!params->stripclues) return 0;
1631 /* Copy the correct grid away. */
1632 grid_correct = snewn(new->wh, int);
1633 memcpy(grid_correct, new->grid, new->wh*sizeof(int));
1635 /* Create shuffled array of side-clue locations. */
1636 slen = new->w*2 + new->h*2;
1637 scratch = snewn(slen, int);
1638 for (i = 0; i < slen; i++) scratch[i] = i;
1639 shuffle(scratch, slen, sizeof(int), rs);
1641 /* For each clue, check whether removing it makes the puzzle unsoluble;
1642 * put it back if so. */
1643 for (i = 0; i < slen; i++) {
1644 int num = scratch[i], which, roworcol, target, targetn, ret;
1647 /* work out which clue we meant. */
1648 if (num < new->w+new->h) { which = POSITIVE; }
1649 else { which = NEGATIVE; num -= new->w+new->h; }
1651 if (num < new->w) { roworcol = COLUMN; }
1652 else { roworcol = ROW; num -= new->w; }
1654 /* num is now the row/column index in question. */
1655 rc = mkrowcol(new, num, roworcol);
1657 /* Remove clue, storing original... */
1658 target = rc.targets[which];
1659 targetn = rc.targets[NEUTRAL];
1660 rc.targets[which] = -1;
1661 rc.targets[NEUTRAL] = -1;
1663 /* ...and see if we can still solve it. */
1664 game_debug(new, "removed clue, new board:");
1665 memset(new->grid, EMPTY, new->wh * sizeof(int));
1666 ret = solve_state(new, params->diff);
1670 memcmp(new->grid, grid_correct, new->wh*sizeof(int)) != 0) {
1671 /* We made it ambiguous: put clue back. */
1672 debug(("...now impossible/different, put clue back."));
1673 rc.targets[which] = target;
1674 rc.targets[NEUTRAL] = targetn;
1678 sfree(grid_correct);
1683 static char *new_game_desc(const game_params *params, random_state *rs,
1684 char **aux_r, int interactive)
1686 game_state *new = new_state(params->w, params->h);
1687 char *desc, *aux = snewn(new->wh+1, char);
1691 generate_aux(new, aux);
1692 } while (check_difficulty(params, new, rs) < 0);
1694 /* now we're complete, generate the description string
1695 * and an aux_info for the completed game. */
1696 desc = generate_desc(new);
1705 int cur_x, cur_y, cur_visible;
1708 static game_ui *new_ui(const game_state *state)
1710 game_ui *ui = snew(game_ui);
1711 ui->cur_x = ui->cur_y = 0;
1712 ui->cur_visible = 0;
1716 static void free_ui(game_ui *ui)
1721 static char *encode_ui(const game_ui *ui)
1726 static void decode_ui(game_ui *ui, const char *encoding)
1730 static void game_changed_state(game_ui *ui, const game_state *oldstate,
1731 const game_state *newstate)
1733 if (!oldstate->completed && newstate->completed)
1734 ui->cur_visible = 0;
1737 struct game_drawstate {
1738 int tilesize, started, solved;
1740 unsigned long *what; /* size w*h */
1741 unsigned long *colwhat, *rowwhat; /* size 3*w, 3*h */
1744 #define DS_WHICH_MASK 0xf
1746 #define DS_ERROR 0x10
1747 #define DS_CURSOR 0x20
1749 #define DS_FULL 0x80
1750 #define DS_NOTPOS 0x100
1751 #define DS_NOTNEG 0x200
1752 #define DS_NOTNEU 0x400
1753 #define DS_FLASH 0x800
1755 #define PREFERRED_TILE_SIZE 32
1756 #define TILE_SIZE (ds->tilesize)
1757 #define BORDER (TILE_SIZE / 8)
1759 #define COORD(x) ( (x+1) * TILE_SIZE + BORDER )
1760 #define FROMCOORD(x) ( (x - BORDER) / TILE_SIZE - 1 )
1762 static char *interpret_move(const game_state *state, game_ui *ui,
1763 const game_drawstate *ds,
1764 int x, int y, int button)
1766 int gx = FROMCOORD(x), gy = FROMCOORD(y), idx, curr;
1767 char *nullret = NULL, buf[80], movech;
1768 enum { CYCLE_MAGNET, CYCLE_NEUTRAL } action;
1770 if (IS_CURSOR_MOVE(button)) {
1771 move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0);
1772 ui->cur_visible = 1;
1774 } else if (IS_CURSOR_SELECT(button)) {
1775 if (!ui->cur_visible) {
1776 ui->cur_visible = 1;
1779 action = (button == CURSOR_SELECT) ? CYCLE_MAGNET : CYCLE_NEUTRAL;
1782 } else if (INGRID(state, gx, gy) &&
1783 (button == LEFT_BUTTON || button == RIGHT_BUTTON)) {
1784 if (ui->cur_visible) {
1785 ui->cur_visible = 0;
1788 action = (button == LEFT_BUTTON) ? CYCLE_MAGNET : CYCLE_NEUTRAL;
1792 idx = gy * state->w + gx;
1793 if (state->common->dominoes[idx] == idx) return nullret;
1794 curr = state->grid[idx];
1796 if (action == CYCLE_MAGNET) {
1797 /* ... empty --> positive --> negative --> empty ... */
1799 if (state->grid[idx] == NEUTRAL && state->flags[idx] & GS_SET)
1800 return nullret; /* can't cycle a magnet from a neutral. */
1801 movech = (curr == EMPTY) ? '+' : (curr == POSITIVE) ? '-' : ' ';
1802 } else if (action == CYCLE_NEUTRAL) {
1803 /* ... empty -> neutral -> !neutral --> empty ... */
1805 if (state->grid[idx] != NEUTRAL)
1806 return nullret; /* can't cycle through neutral from a magnet. */
1808 /* All of these are grid == EMPTY == NEUTRAL; it twiddles
1809 * combinations of flags. */
1810 if (state->flags[idx] & GS_SET) /* neutral */
1812 else if (state->flags[idx] & GS_NOTNEUTRAL) /* !neutral */
1817 assert(!"unknown action");
1818 movech = 0; /* placate optimiser */
1821 sprintf(buf, "%c%d,%d", movech, gx, gy);
1826 static game_state *execute_move(const game_state *state, const char *move)
1828 game_state *ret = dup_game(state);
1829 int x, y, n, idx, idx2;
1832 if (!*move) goto badmove;
1838 } else if (c == '+' || c == '-' ||
1839 c == '.' || c == ' ' || c == '?') {
1840 if ((sscanf(move, "%d,%d%n", &x, &y, &n) != 2) ||
1841 !INGRID(state, x, y)) goto badmove;
1843 idx = y*state->w + x;
1844 idx2 = state->common->dominoes[idx];
1845 if (idx == idx2) goto badmove;
1847 ret->flags[idx] &= ~GS_NOTMASK;
1848 ret->flags[idx2] &= ~GS_NOTMASK;
1850 if (c == ' ' || c == '?') {
1851 ret->grid[idx] = EMPTY;
1852 ret->grid[idx2] = EMPTY;
1853 ret->flags[idx] &= ~GS_SET;
1854 ret->flags[idx2] &= ~GS_SET;
1856 ret->flags[idx] |= GS_NOTNEUTRAL;
1857 ret->flags[idx2] |= GS_NOTNEUTRAL;
1860 ret->grid[idx] = CHAR2GRID(c);
1861 ret->grid[idx2] = OPPOSITE(CHAR2GRID(c));
1862 ret->flags[idx] |= GS_SET;
1863 ret->flags[idx2] |= GS_SET;
1869 if (*move == ';') move++;
1870 else if (*move) goto badmove;
1872 if (check_completion(ret) == 1)
1882 /* ----------------------------------------------------------------------
1886 static void game_compute_size(const game_params *params, int tilesize,
1889 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1890 struct { int tilesize; } ads, *ds = &ads;
1891 ads.tilesize = tilesize;
1893 *x = TILE_SIZE * (params->w+2) + 2 * BORDER;
1894 *y = TILE_SIZE * (params->h+2) + 2 * BORDER;
1897 static void game_set_size(drawing *dr, game_drawstate *ds,
1898 const game_params *params, int tilesize)
1900 ds->tilesize = tilesize;
1903 static float *game_colours(frontend *fe, int *ncolours)
1905 float *ret = snewn(3 * NCOLOURS, float);
1908 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
1910 for (i = 0; i < 3; i++) {
1911 ret[COL_TEXT * 3 + i] = 0.0F;
1912 ret[COL_NEGATIVE * 3 + i] = 0.0F;
1913 ret[COL_CURSOR * 3 + i] = 0.9F;
1916 ret[COL_POSITIVE * 3 + 0] = 0.8F;
1917 ret[COL_POSITIVE * 3 + 1] = 0.0F;
1918 ret[COL_POSITIVE * 3 + 2] = 0.0F;
1920 ret[COL_NEUTRAL * 3 + 0] = 0.10F;
1921 ret[COL_NEUTRAL * 3 + 1] = 0.60F;
1922 ret[COL_NEUTRAL * 3 + 2] = 0.10F;
1924 ret[COL_ERROR * 3 + 0] = 1.0F;
1925 ret[COL_ERROR * 3 + 1] = 0.0F;
1926 ret[COL_ERROR * 3 + 2] = 0.0F;
1928 ret[COL_NOT * 3 + 0] = 0.2F;
1929 ret[COL_NOT * 3 + 1] = 0.2F;
1930 ret[COL_NOT * 3 + 2] = 1.0F;
1932 *ncolours = NCOLOURS;
1936 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1938 struct game_drawstate *ds = snew(struct game_drawstate);
1940 ds->tilesize = ds->started = ds->solved = 0;
1944 ds->what = snewn(state->wh, unsigned long);
1945 memset(ds->what, 0, state->wh*sizeof(unsigned long));
1947 ds->colwhat = snewn(state->w*3, unsigned long);
1948 memset(ds->colwhat, 0, state->w*3*sizeof(unsigned long));
1949 ds->rowwhat = snewn(state->h*3, unsigned long);
1950 memset(ds->rowwhat, 0, state->h*3*sizeof(unsigned long));
1955 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1963 static void draw_num_col(drawing *dr, game_drawstate *ds, int rowcol, int which,
1964 int idx, int colbg, int col, int num)
1969 if (num < 0) return;
1971 sprintf(buf, "%d", num);
1972 tsz = (strlen(buf) == 1) ? (7*TILE_SIZE/10) : (9*TILE_SIZE/10)/strlen(buf);
1974 if (rowcol == ROW) {
1976 if (which == NEGATIVE) cx += TILE_SIZE * (ds->w+1);
1977 cy = BORDER + TILE_SIZE * (idx+1);
1979 cx = BORDER + TILE_SIZE * (idx+1);
1981 if (which == NEGATIVE) cy += TILE_SIZE * (ds->h+1);
1984 draw_rect(dr, cx, cy, TILE_SIZE, TILE_SIZE, colbg);
1985 draw_text(dr, cx + TILE_SIZE/2, cy + TILE_SIZE/2, FONT_VARIABLE, tsz,
1986 ALIGN_VCENTRE | ALIGN_HCENTRE, col, buf);
1988 draw_update(dr, cx, cy, TILE_SIZE, TILE_SIZE);
1991 static void draw_num(drawing *dr, game_drawstate *ds, int rowcol, int which,
1992 int idx, unsigned long c, int num)
1994 draw_num_col(dr, ds, rowcol, which, idx, COL_BACKGROUND,
1995 (c & DS_ERROR) ? COL_ERROR : COL_TEXT, num);
1998 static void draw_sym(drawing *dr, game_drawstate *ds, int x, int y, int which, int col)
2000 int cx = COORD(x), cy = COORD(y);
2001 int ccx = cx + TILE_SIZE/2, ccy = cy + TILE_SIZE/2;
2002 int roff = TILE_SIZE/4, rsz = 2*roff+1;
2003 int soff = TILE_SIZE/16, ssz = 2*soff+1;
2005 if (which == POSITIVE || which == NEGATIVE) {
2006 draw_rect(dr, ccx - roff, ccy - soff, rsz, ssz, col);
2007 if (which == POSITIVE)
2008 draw_rect(dr, ccx - soff, ccy - roff, ssz, rsz, col);
2009 } else if (col == COL_NOT) {
2010 /* not-a-neutral is a blue question mark. */
2011 char qu[2] = { '?', 0 };
2012 draw_text(dr, ccx, ccy, FONT_VARIABLE, 7*TILE_SIZE/10,
2013 ALIGN_VCENTRE | ALIGN_HCENTRE, col, qu);
2015 draw_line(dr, ccx - roff, ccy - roff, ccx + roff, ccy + roff, col);
2016 draw_line(dr, ccx + roff, ccy - roff, ccx - roff, ccy + roff, col);
2028 /* NOT responsible for redrawing background or updating. */
2029 static void draw_tile_col(drawing *dr, game_drawstate *ds, int *dominoes,
2030 int x, int y, int which, int bg, int fg, int perc)
2032 int cx = COORD(x), cy = COORD(y), i, other, type = TYPE_BLANK;
2033 int gutter, radius, coffset;
2035 /* gutter is TSZ/16 for 100%, 8*TSZ/16 (TSZ/2) for 0% */
2036 gutter = (TILE_SIZE / 16) + ((100 - perc) * (7*TILE_SIZE / 16))/100;
2037 radius = (perc * (TILE_SIZE / 8)) / 100;
2038 coffset = gutter + radius;
2041 other = dominoes[i];
2043 if (other == i) return;
2044 else if (other == i+1) type = TYPE_L;
2045 else if (other == i-1) type = TYPE_R;
2046 else if (other == i+ds->w) type = TYPE_T;
2047 else if (other == i-ds->w) type = TYPE_B;
2048 else assert(!"mad domino orientation");
2050 /* domino drawing shamelessly stolen from dominosa.c. */
2051 if (type == TYPE_L || type == TYPE_T)
2052 draw_circle(dr, cx+coffset, cy+coffset,
2054 if (type == TYPE_R || type == TYPE_T)
2055 draw_circle(dr, cx+TILE_SIZE-1-coffset, cy+coffset,
2057 if (type == TYPE_L || type == TYPE_B)
2058 draw_circle(dr, cx+coffset, cy+TILE_SIZE-1-coffset,
2060 if (type == TYPE_R || type == TYPE_B)
2061 draw_circle(dr, cx+TILE_SIZE-1-coffset,
2062 cy+TILE_SIZE-1-coffset,
2065 for (i = 0; i < 2; i++) {
2068 x1 = cx + (i ? gutter : coffset);
2069 y1 = cy + (i ? coffset : gutter);
2070 x2 = cx + TILE_SIZE-1 - (i ? gutter : coffset);
2071 y2 = cy + TILE_SIZE-1 - (i ? coffset : gutter);
2073 x2 = cx + TILE_SIZE;
2074 else if (type == TYPE_R)
2076 else if (type == TYPE_T)
2077 y2 = cy + TILE_SIZE ;
2078 else if (type == TYPE_B)
2081 draw_rect(dr, x1, y1, x2-x1+1, y2-y1+1, bg);
2084 if (fg != -1) draw_sym(dr, ds, x, y, which, fg);
2087 static void draw_tile(drawing *dr, game_drawstate *ds, int *dominoes,
2088 int x, int y, unsigned long flags)
2090 int cx = COORD(x), cy = COORD(y), bg, fg, perc = 100;
2091 int which = flags & DS_WHICH_MASK;
2093 flags &= ~DS_WHICH_MASK;
2095 draw_rect(dr, cx, cy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2097 if (flags & DS_CURSOR)
2098 bg = COL_CURSOR; /* off-white white for cursor */
2099 else if (which == POSITIVE)
2101 else if (which == NEGATIVE)
2103 else if (flags & DS_SET)
2104 bg = COL_NEUTRAL; /* green inner for neutral cells */
2106 bg = COL_LOWLIGHT; /* light grey for empty cells. */
2108 if (which == EMPTY && !(flags & DS_SET)) {
2110 fg = -1; /* don't draw cross unless actually set as neutral. */
2112 if (flags & DS_NOTPOS) notwhich = POSITIVE;
2113 if (flags & DS_NOTNEG) notwhich = NEGATIVE;
2114 if (flags & DS_NOTNEU) notwhich = NEUTRAL;
2115 if (notwhich != -1) {
2120 fg = (flags & DS_ERROR) ? COL_ERROR :
2121 (flags & DS_CURSOR) ? COL_TEXT : COL_BACKGROUND;
2123 draw_rect(dr, cx, cy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2125 if (flags & DS_FLASH) {
2126 int bordercol = COL_HIGHLIGHT;
2127 draw_tile_col(dr, ds, dominoes, x, y, which, bordercol, -1, perc);
2130 draw_tile_col(dr, ds, dominoes, x, y, which, bg, fg, perc);
2132 draw_update(dr, cx, cy, TILE_SIZE, TILE_SIZE);
2136 static void game_redraw(drawing *dr, game_drawstate *ds,
2137 const game_state *oldstate, const game_state *state,
2138 int dir, const game_ui *ui,
2139 float animtime, float flashtime)
2141 int x, y, w = state->w, h = state->h, which, i, j, flash;
2142 unsigned long c = 0;
2144 flash = (int)(flashtime * 5 / FLASH_TIME) % 2;
2147 /* draw background, corner +-. */
2149 TILE_SIZE * (w+2) + 2 * BORDER,
2150 TILE_SIZE * (h+2) + 2 * BORDER,
2153 draw_sym(dr, ds, -1, -1, POSITIVE, COL_TEXT);
2154 draw_sym(dr, ds, state->w, state->h, NEGATIVE, COL_TEXT);
2156 draw_update(dr, 0, 0,
2157 TILE_SIZE * (ds->w+2) + 2 * BORDER,
2158 TILE_SIZE * (ds->h+2) + 2 * BORDER);
2162 for (y = 0; y < h; y++) {
2163 for (x = 0; x < w; x++) {
2166 c = state->grid[idx];
2168 if (state->flags[idx] & GS_ERROR)
2170 if (state->flags[idx] & GS_SET)
2173 if (x == ui->cur_x && y == ui->cur_y && ui->cur_visible)
2179 if (state->flags[idx] & GS_NOTPOSITIVE)
2181 if (state->flags[idx] & GS_NOTNEGATIVE)
2183 if (state->flags[idx] & GS_NOTNEUTRAL)
2186 if (ds->what[idx] != c || !ds->started) {
2187 draw_tile(dr, ds, state->common->dominoes, x, y, c);
2192 /* Draw counts around side */
2193 for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
2195 for (i = 0; i < w; i++) {
2196 target = state->common->colcount[i*3+which];
2197 count = count_rowcol(state, i, COLUMN, which);
2199 if ((count > target) ||
2200 (count < target && !count_rowcol(state, i, COLUMN, -1)))
2202 if (count == target) c |= DS_FULL;
2203 if (c != ds->colwhat[i*3+which] || !ds->started) {
2204 draw_num(dr, ds, COLUMN, which, i, c,
2205 state->common->colcount[i*3+which]);
2206 ds->colwhat[i*3+which] = c;
2209 for (i = 0; i < h; i++) {
2210 target = state->common->rowcount[i*3+which];
2211 count = count_rowcol(state, i, ROW, which);
2213 if ((count > target) ||
2214 (count < target && !count_rowcol(state, i, ROW, -1)))
2216 if (count == target) c |= DS_FULL;
2217 if (c != ds->rowwhat[i*3+which] || !ds->started) {
2218 draw_num(dr, ds, ROW, which, i, c,
2219 state->common->rowcount[i*3+which]);
2220 ds->rowwhat[i*3+which] = c;
2228 static float game_anim_length(const game_state *oldstate,
2229 const game_state *newstate, int dir, game_ui *ui)
2234 static float game_flash_length(const game_state *oldstate,
2235 const game_state *newstate, int dir, game_ui *ui)
2237 if (!oldstate->completed && newstate->completed &&
2238 !oldstate->solved && !newstate->solved)
2243 static int game_status(const game_state *state)
2245 return state->completed ? +1 : 0;
2248 static int game_timing_state(const game_state *state, game_ui *ui)
2253 static void game_print_size(const game_params *params, float *x, float *y)
2258 * I'll use 6mm squares by default.
2260 game_compute_size(params, 600, &pw, &ph);
2265 static void game_print(drawing *dr, const game_state *state, int tilesize)
2267 int w = state->w, h = state->h;
2268 int ink = print_mono_colour(dr, 0);
2269 int paper = print_mono_colour(dr, 1);
2270 int x, y, which, i, j;
2272 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2273 game_drawstate ads, *ds = &ads;
2274 game_set_size(dr, ds, NULL, tilesize);
2275 ds->w = w; ds->h = h;
2278 print_line_width(dr, TILE_SIZE/12);
2280 /* Numbers and +/- for corners. */
2281 draw_sym(dr, ds, -1, -1, POSITIVE, ink);
2282 draw_sym(dr, ds, state->w, state->h, NEGATIVE, ink);
2283 for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
2284 for (i = 0; i < w; i++) {
2285 draw_num_col(dr, ds, COLUMN, which, i, paper, ink,
2286 state->common->colcount[i*3+which]);
2288 for (i = 0; i < h; i++) {
2289 draw_num_col(dr, ds, ROW, which, i, paper, ink,
2290 state->common->rowcount[i*3+which]);
2295 for (x = 0; x < w; x++) {
2296 for (y = 0; y < h; y++) {
2298 if (state->common->dominoes[i] == i+1 ||
2299 state->common->dominoes[i] == i+w) {
2300 int dx = state->common->dominoes[i] == i+1 ? 2 : 1;
2303 int cx = COORD(x), cy = COORD(y);
2305 print_line_width(dr, 0);
2307 /* Ink the domino */
2308 for (yy = 0; yy < 2; yy++)
2309 for (xx = 0; xx < 2; xx++)
2311 cx+xx*dx*TILE_SIZE+(1-2*xx)*3*TILE_SIZE/16,
2312 cy+yy*dy*TILE_SIZE+(1-2*yy)*3*TILE_SIZE/16,
2313 TILE_SIZE/8, ink, ink);
2314 draw_rect(dr, cx + TILE_SIZE/16, cy + 3*TILE_SIZE/16,
2315 dx*TILE_SIZE - 2*(TILE_SIZE/16),
2316 dy*TILE_SIZE - 6*(TILE_SIZE/16), ink);
2317 draw_rect(dr, cx + 3*TILE_SIZE/16, cy + TILE_SIZE/16,
2318 dx*TILE_SIZE - 6*(TILE_SIZE/16),
2319 dy*TILE_SIZE - 2*(TILE_SIZE/16), ink);
2321 /* Un-ink the domino interior */
2322 for (yy = 0; yy < 2; yy++)
2323 for (xx = 0; xx < 2; xx++)
2325 cx+xx*dx*TILE_SIZE+(1-2*xx)*3*TILE_SIZE/16,
2326 cy+yy*dy*TILE_SIZE+(1-2*yy)*3*TILE_SIZE/16,
2327 3*TILE_SIZE/32, paper, paper);
2328 draw_rect(dr, cx + 3*TILE_SIZE/32, cy + 3*TILE_SIZE/16,
2329 dx*TILE_SIZE - 2*(3*TILE_SIZE/32),
2330 dy*TILE_SIZE - 6*(TILE_SIZE/16), paper);
2331 draw_rect(dr, cx + 3*TILE_SIZE/16, cy + 3*TILE_SIZE/32,
2332 dx*TILE_SIZE - 6*(TILE_SIZE/16),
2333 dy*TILE_SIZE - 2*(3*TILE_SIZE/32), paper);
2338 /* Grid symbols (solution). */
2339 for (x = 0; x < w; x++) {
2340 for (y = 0; y < h; y++) {
2342 if ((state->grid[i] != NEUTRAL) || (state->flags[i] & GS_SET))
2343 draw_sym(dr, ds, x, y, state->grid[i], ink);
2349 #define thegame magnets
2352 const struct game thegame = {
2353 "Magnets", "games.magnets", "magnets",
2360 TRUE, game_configure, custom_params,
2368 TRUE, game_can_format_as_text_now, game_text_format,
2376 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2379 game_free_drawstate,
2384 TRUE, FALSE, game_print_size, game_print,
2385 FALSE, /* wants_statusbar */
2386 FALSE, game_timing_state,
2387 REQUIRE_RBUTTON, /* flags */
2390 #ifdef STANDALONE_SOLVER
2395 const char *quis = NULL;
2398 void usage(FILE *out) {
2399 fprintf(out, "usage: %s [-v] [--print] <params>|<game id>\n", quis);
2402 void doprint(game_state *state)
2404 char *fmt = game_text_format(state);
2409 static void pnum(int n, int ntot, const char *desc)
2411 printf("%2.1f%% (%d) %s", (double)n*100.0 / (double)ntot, n, desc);
2414 static void start_soak(game_params *p, random_state *rs)
2416 time_t tt_start, tt_now, tt_last;
2419 int n = 0, nsolved = 0, nimpossible = 0, ntricky = 0, ret, i;
2420 long nn, nn_total = 0, nn_solved = 0, nn_tricky = 0;
2422 tt_start = tt_now = time(NULL);
2425 printf("time, w, h, #generated, #solved, #tricky, #impossible, "
2426 "#neutral, #neutral/solved, #neutral/tricky\n");
2428 printf("Soak-testing a %dx%d grid.\n", p->w, p->h);
2430 s = new_state(p->w, p->h);
2431 aux = snewn(s->wh+1, char);
2437 for (i = 0; i < s->wh; i++) {
2438 if (s->grid[i] == NEUTRAL) nn++;
2441 generate_aux(s, aux);
2442 memset(s->grid, EMPTY, s->wh * sizeof(int));
2445 ret = solve_state(s, DIFFCOUNT);
2452 if (solve_state(s2, DIFF_EASY) <= 0) {
2456 } else if (ret < 0) {
2457 char *desc = generate_desc(s);
2458 solve_from_aux(s, aux);
2459 printf("Game considered impossible:\n %dx%d:%s\n",
2468 tt_last = time(NULL);
2469 if (tt_last > tt_now) {
2472 printf("%d,%d,%d, %d,%d,%d,%d, %ld,%ld,%ld\n",
2473 (int)(tt_now - tt_start), p->w, p->h,
2474 n, nsolved, ntricky, nimpossible,
2475 nn_total, nn_solved, nn_tricky);
2477 printf("%d total, %3.1f/s, ",
2478 n, (double)n / ((double)tt_now - tt_start));
2479 pnum(nsolved, n, "solved"); printf(", ");
2480 pnum(ntricky, n, "tricky");
2481 if (nimpossible > 0)
2482 pnum(nimpossible, n, "impossible");
2485 printf(" overall %3.1f%% neutral (%3.1f%% for solved, %3.1f%% for tricky)\n",
2486 (double)(nn_total * 100) / (double)(p->w * p->h * n),
2487 (double)(nn_solved * 100) / (double)(p->w * p->h * nsolved),
2488 (double)(nn_tricky * 100) / (double)(p->w * p->h * ntricky));
2496 int main(int argc, const char *argv[])
2498 int print = 0, soak = 0, solved = 0, ret;
2499 char *id = NULL, *desc, *desc_gen = NULL, *err, *aux = NULL;
2500 game_state *s = NULL;
2501 game_params *p = NULL;
2502 random_state *rs = NULL;
2503 time_t seed = time(NULL);
2505 setvbuf(stdout, NULL, _IONBF, 0);
2508 while (--argc > 0) {
2509 char *p = (char*)(*++argv);
2510 if (!strcmp(p, "-v") || !strcmp(p, "--verbose")) {
2512 } else if (!strcmp(p, "--csv")) {
2514 } else if (!strcmp(p, "-e") || !strcmp(p, "--seed")) {
2515 seed = atoi(*++argv);
2517 } else if (!strcmp(p, "-p") || !strcmp(p, "--print")) {
2519 } else if (!strcmp(p, "-s") || !strcmp(p, "--soak")) {
2521 } else if (*p == '-') {
2522 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2530 rs = random_new((void*)&seed, sizeof(time_t));
2533 fprintf(stderr, "usage: %s [-v] [--soak] <params> | <game_id>\n", argv[0]);
2536 desc = strchr(id, ':');
2537 if (desc) *desc++ = '\0';
2539 p = default_params();
2540 decode_params(p, id);
2541 err = validate_params(p, 1);
2543 fprintf(stderr, "%s: %s", argv[0], err);
2549 fprintf(stderr, "%s: --soak needs parameters, not description.\n", quis);
2557 desc = desc_gen = new_game_desc(p, rs, &aux, 0);
2559 err = validate_desc(p, desc);
2561 fprintf(stderr, "%s: %s\nDescription: %s\n", quis, err, desc);
2564 s = new_game(NULL, p, desc);
2565 printf("%s:%s (seed %ld)\n", id, desc, seed);
2567 /* We just generated this ourself. */
2568 if (verbose || print) {
2570 solve_from_aux(s, aux);
2576 ret = solve_state(s, DIFFCOUNT);
2577 if (ret < 0) printf("Puzzle is impossible.\n");
2578 else if (ret == 0) printf("Puzzle is ambiguous.\n");
2579 else printf("Puzzle was solved.\n");
2583 if (solved) doprint(s);
2586 if (desc_gen) sfree(desc_gen);
2587 if (p) free_params(p);
2588 if (s) free_game(s);
2589 if (rs) random_free(rs);
2590 if (aux) sfree(aux);
2597 /* vim: set shiftwidth=4 tabstop=8: */