chiark / gitweb /
Apply a missing bit of r9164, which only broke the build with
[sgt-puzzles.git] / magnets.c
1 /*
2  * magnets.c: implementation of janko.at 'magnets puzzle' game.
3  *
4  * http://64.233.179.104/translate_c?hl=en&u=http://www.janko.at/Raetsel/Magnete/Beispiel.htm
5  *
6  * Puzzle definition is just the size, and then the list of + (across then
7  * down) and - (across then down) present, then domino edges.
8  *
9  * An example:
10  *
11  *  + 2 0 1
12  *   +-----+
13  *  1|+ -| |1
14  *   |-+-+ |
15  *  0|-|#| |1
16  *   | +-+-|
17  *  2|+|- +|1
18  *   +-----+
19  *    1 2 0 -
20  *
21  * 3x3:201,102,120,111,LRTT*BBLR
22  *
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**
26  *
27  * Janko 6x6 with solution:
28  * 6x6:322223,323132,232223,232223,LRTLRTTTBLRBBBTTLRLRBBLRTTLRTTBBLRBB
29  *
30  * janko 8x8:
31  * 8x8:34131323,23131334,43122323,21332243,LRTLRLRT,LRBTTTTB,LRTBBBBT,TTBTLRTB,BBTBTTBT,TTBTBBTB,BBTBLRBT,LRBLRLRB
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38 #include <ctype.h>
39 #include <math.h>
40
41 #include "puzzles.h"
42
43 #ifdef STANDALONE_SOLVER
44 int verbose = 0;
45 #endif
46
47 enum {
48     COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT,
49     COL_TEXT, COL_ERROR, COL_CURSOR,
50     COL_NEUTRAL, COL_NEGATIVE, COL_POSITIVE, COL_NOT,
51     NCOLOURS
52 };
53
54 /* Cell states. */
55 enum { EMPTY = 0, NEUTRAL = EMPTY, POSITIVE = 1, NEGATIVE = 2 };
56
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)] )
60 #endif
61
62 #define GRID2CHAR(g) ( ((g) >= 0 && (g) <= 2) ? ".+-"[(g)] : '?' )
63 #define CHAR2GRID(c) ( (c) == '+' ? POSITIVE : (c) == '-' ? NEGATIVE : NEUTRAL )
64
65 #define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
66
67 #define OPPOSITE(x) ( ((x)*2) % 3 ) /* 0 --> 0,
68                                        1 --> 2,
69                                        2 --> 4 --> 1 */
70
71 #define FLASH_TIME 0.7F
72
73 /* Macro ickery copied from slant.c */
74 #define DIFFLIST(A) \
75     A(EASY,Easy,e) \
76     A(TRICKY,Tricky,t)
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)
85
86
87 /* --------------------------------------------------------------- */
88 /* Game parameter functions. */
89
90 struct game_params {
91     int w, h, diff, stripclues;
92 };
93
94 #define DEFAULT_PRESET 2
95
96 static const struct game_params magnets_presets[] = {
97     {6, 5, DIFF_EASY, 0},
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}
105 };
106
107 static game_params *default_params(void)
108 {
109     game_params *ret = snew(game_params);
110
111     *ret = magnets_presets[DEFAULT_PRESET];
112
113     return ret;
114 }
115
116 static int game_fetch_preset(int i, char **name, game_params **params)
117 {
118     game_params *ret;
119     char buf[64];
120
121     if (i < 0 || i >= lenof(magnets_presets)) return FALSE;
122
123     ret = default_params();
124     *ret = magnets_presets[i]; /* struct copy */
125     *params = ret;
126
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" : "");
131     *name = dupstr(buf);
132
133     return TRUE;
134 }
135
136 static void free_params(game_params *params)
137 {
138     sfree(params);
139 }
140
141 static game_params *dup_params(game_params *params)
142 {
143     game_params *ret = snew(game_params);
144     *ret = *params;       /* structure copy */
145     return ret;
146 }
147
148 static void decode_params(game_params *ret, char const *string)
149 {
150     ret->w = ret->h = atoi(string);
151     while (*string && isdigit((unsigned char) *string)) ++string;
152     if (*string == 'x') {
153         string++;
154         ret->h = atoi(string);
155         while (*string && isdigit((unsigned char)*string)) string++;
156     }
157
158     ret->diff = DIFF_EASY;
159     if (*string == 'd') {
160         int i;
161         string++;
162         for (i = 0; i < DIFFCOUNT; i++)
163             if (*string == magnets_diffchars[i])
164                 ret->diff = i;
165         if (*string) string++;
166     }
167
168     ret->stripclues = 0;
169     if (*string == 'S') {
170         string++;
171         ret->stripclues = 1;
172     }
173 }
174
175 static char *encode_params(game_params *params, int full)
176 {
177     char buf[256];
178     sprintf(buf, "%dx%d", params->w, params->h);
179     if (full)
180         sprintf(buf + strlen(buf), "d%c%s",
181                 magnets_diffchars[params->diff],
182                 params->stripclues ? "S" : "");
183     return dupstr(buf);
184 }
185
186 static config_item *game_configure(game_params *params)
187 {
188     config_item *ret;
189     char buf[64];
190
191     ret = snewn(5, config_item);
192
193     ret[0].name = "Width";
194     ret[0].type = C_STRING;
195     sprintf(buf, "%d", params->w);
196     ret[0].sval = dupstr(buf);
197     ret[0].ival = 0;
198
199     ret[1].name = "Height";
200     ret[1].type = C_STRING;
201     sprintf(buf, "%d", params->h);
202     ret[1].sval = dupstr(buf);
203     ret[1].ival = 0;
204
205     ret[2].name = "Difficulty";
206     ret[2].type = C_CHOICES;
207     ret[2].sval = DIFFCONFIG;
208     ret[2].ival = params->diff;
209
210     ret[3].name = "Strip clues";
211     ret[3].type = C_BOOLEAN;
212     ret[3].sval = NULL;
213     ret[3].ival = params->stripclues;
214
215     ret[4].name = NULL;
216     ret[4].type = C_END;
217     ret[4].sval = NULL;
218     ret[4].ival = 0;
219
220     return ret;
221 }
222
223 static game_params *custom_params(config_item *cfg)
224 {
225     game_params *ret = snew(game_params);
226
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;
231
232     return ret;
233 }
234
235 static char *validate_params(game_params *params, int full)
236 {
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";
241
242     return NULL;
243 }
244
245 /* --------------------------------------------------------------- */
246 /* Game state allocation, deallocation. */
247
248 struct game_common {
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 */
252     int refcount;
253 };
254
255 #define GS_ERROR        1
256 #define GS_SET          2
257 #define GS_NOTPOSITIVE  4
258 #define GS_NOTNEGATIVE  8
259 #define GS_NOTNEUTRAL  16
260 #define GS_MARK        32
261
262 #define GS_NOTMASK (GS_NOTPOSITIVE|GS_NOTNEGATIVE|GS_NOTNEUTRAL)
263
264 #define NOTFLAG(w) ( (w) == NEUTRAL ? GS_NOTNEUTRAL : \
265                      (w) == POSITIVE ? GS_NOTPOSITIVE : \
266                      (w) == NEGATIVE ? GS_NOTNEGATIVE : \
267                      0 )
268
269 #define POSSIBLE(f,w) (!(state->flags[(f)] & NOTFLAG(w)))
270
271 struct game_state {
272     int w, h, wh;
273     int *grid;                  /* size w*h, for cell state (pos/neg) */
274     unsigned int *flags;        /* size w*h */
275     int solved, completed, numbered;
276
277     struct game_common *common; /* domino layout never changes. */
278 };
279
280 static void clear_state(game_state *ret)
281 {
282     int i;
283
284     ret->solved = ret->completed = ret->numbered = 0;
285
286     memset(ret->common->rowcount, 0, ret->h*3*sizeof(int));
287     memset(ret->common->colcount, 0, ret->w*3*sizeof(int));
288
289     for (i = 0; i < ret->wh; i++) {
290         ret->grid[i] = EMPTY;
291         ret->flags[i] = 0;
292         ret->common->dominoes[i] = i;
293     }
294 }
295
296 static game_state *new_state(int w, int h)
297 {
298     game_state *ret = snew(game_state);
299
300     memset(ret, 0, sizeof(game_state));
301     ret->w = w;
302     ret->h = h;
303     ret->wh = w*h;
304
305     ret->grid = snewn(ret->wh, int);
306     ret->flags = snewn(ret->wh, unsigned int);
307
308     ret->common = snew(struct game_common);
309     ret->common->refcount = 1;
310
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);
314
315     clear_state(ret);
316
317     return ret;
318 }
319
320 static game_state *dup_game(game_state *src)
321 {
322     game_state *dest = snew(game_state);
323
324     dest->w = src->w;
325     dest->h = src->h;
326     dest->wh = src->wh;
327
328     dest->solved = src->solved;
329     dest->completed = src->completed;
330     dest->numbered = src->numbered;
331
332     dest->common = src->common;
333     dest->common->refcount++;
334
335     dest->grid = snewn(dest->wh, int);
336     memcpy(dest->grid, src->grid, dest->wh*sizeof(int));
337
338     dest->flags = snewn(dest->wh, unsigned int);
339     memcpy(dest->flags, src->flags, dest->wh*sizeof(unsigned int));
340
341     return dest;
342 }
343
344 static void free_game(game_state *state)
345 {
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);
352     }
353     sfree(state->flags);
354     sfree(state->grid);
355     sfree(state);
356 }
357
358 /* --------------------------------------------------------------- */
359 /* Game generation and reading. */
360
361 /* For a game of size w*h the game description is:
362  * w-sized string of column + numbers (L-R), or '.' for none
363  * semicolon
364  * h-sized string of row + numbers (T-B), or '.'
365  * semicolon
366  * w-sized string of column - numbers (L-R), or '.'
367  * semicolon
368  * h-sized string of row - numbers (T-B), or '.'
369  * semicolon
370  * w*h-sized string of 'L', 'R', 'U', 'D' for domino associations,
371  *   or '*' for a black singleton square.
372  *
373  * for a total length of 2w + 2h + wh + 4.
374  */
375
376 static char n2c(int num) { /* XXX cloned from singles.c */
377     if (num == -1)
378         return '.';
379     if (num < 10)
380         return '0' + num;
381     else if (num < 10+26)
382         return 'a' + num - 10;
383     else
384         return 'A' + num - 10 - 26;
385     return '?';
386 }
387
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);
395     return -1;
396 }
397
398 static char *readrow(char *desc, int n, int *array, int off, const char **prob)
399 {
400     int i, num;
401     char c;
402
403     for (i = 0; i < n; i++) {
404         c = *desc++;
405         if (c == 0) goto badchar;
406         if (c == '.')
407             num = -1;
408         else {
409             num = c2n(c);
410             if (num < 0) goto badchar;
411         }
412         array[i*3+off] = num;
413     }
414     c = *desc++;
415     if (c != ',') goto badchar;
416     return desc;
417
418 badchar:
419     *prob = (c == 0) ?
420                 "Game description too short" :
421                 "Game description contained unexpected characters";
422     return NULL;
423 }
424
425 static game_state *new_game_int(game_params *params, char *desc, const char **prob)
426 {
427     game_state *state = new_state(params->w, params->h);
428     int x, y, idx, *count;
429     char c;
430
431     *prob = NULL;
432
433     /* top row, left-to-right */
434     desc = readrow(desc, state->w, state->common->colcount, POSITIVE, prob);
435     if (*prob) goto done;
436
437     /* left column, top-to-bottom */
438     desc = readrow(desc, state->h, state->common->rowcount, POSITIVE, prob);
439     if (*prob) goto done;
440
441     /* bottom row, left-to-right */
442     desc = readrow(desc, state->w, state->common->colcount, NEGATIVE, prob);
443     if (*prob) goto done;
444
445     /* right column, top-to-bottom */
446     desc = readrow(desc, state->h, state->common->rowcount, NEGATIVE, prob);
447     if (*prob) goto done;
448
449     /* Add neutral counts (== size - pos - neg) to columns and rows.
450      * Any singleton cells will just be treated as permanently neutral. */
451     count = state->common->colcount;
452     for (x = 0; x < state->w; x++) {
453         if (count[x*3+POSITIVE] < 0 || count[x*3+NEGATIVE] < 0)
454             count[x*3+NEUTRAL] = -1;
455         else {
456             count[x*3+NEUTRAL] =
457                 state->h - count[x*3+POSITIVE] - count[x*3+NEGATIVE];
458             if (count[x*3+NEUTRAL] < 0) {
459                 *prob = "Column counts inconsistent";
460                 goto done;
461             }
462         }
463     }
464     count = state->common->rowcount;
465     for (y = 0; y < state->h; y++) {
466         if (count[y*3+POSITIVE] < 0 || count[y*3+NEGATIVE] < 0)
467             count[y*3+NEUTRAL] = -1;
468         else {
469             count[y*3+NEUTRAL] =
470                 state->w - count[y*3+POSITIVE] - count[y*3+NEGATIVE];
471             if (count[y*3+NEUTRAL] < 0) {
472                 *prob = "Row counts inconsistent";
473                 goto done;
474             }
475         }
476     }
477
478
479     for (y = 0; y < state->h; y++) {
480         for (x = 0; x < state->w; x++) {
481             idx = y*state->w + x;
482 nextchar:
483             c = *desc++;
484
485             if (c == 'L') /* this square is LHS of a domino */
486                 state->common->dominoes[idx] = idx+1;
487             else if (c == 'R') /* ... RHS of a domino */
488                 state->common->dominoes[idx] = idx-1;
489             else if (c == 'T') /* ... top of a domino */
490                 state->common->dominoes[idx] = idx+state->w;
491             else if (c == 'B') /* ... bottom of a domino */
492                 state->common->dominoes[idx] = idx-state->w;
493             else if (c == '*') /* singleton */
494                 state->common->dominoes[idx] = idx;
495             else if (c == ',') /* spacer, ignore */
496                 goto nextchar;
497             else goto badchar;
498         }
499     }
500
501     /* Check dominoes as input are sensibly consistent
502      * (i.e. each end points to the other) */
503     for (idx = 0; idx < state->wh; idx++) {
504         if (state->common->dominoes[idx] < 0 ||
505             state->common->dominoes[idx] > state->wh ||
506             state->common->dominoes[state->common->dominoes[idx]] != idx) {
507             *prob = "Domino descriptions inconsistent";
508             goto done;
509         }
510         if (state->common->dominoes[idx] == idx) {
511             state->grid[idx] = NEUTRAL;
512             state->flags[idx] |= GS_SET;
513         }
514     }
515     /* Success. */
516     state->numbered = 1;
517     goto done;
518
519 badchar:
520     *prob = (c == 0) ?
521                 "Game description too short" :
522                 "Game description contained unexpected characters";
523
524 done:
525     if (*prob) {
526         free_game(state);
527         return NULL;
528     }
529     return state;
530 }
531
532 static char *validate_desc(game_params *params, char *desc)
533 {
534     const char *prob;
535     game_state *st = new_game_int(params, desc, &prob);
536     if (!st) return (char*)prob;
537     free_game(st);
538     return NULL;
539 }
540
541 static game_state *new_game(midend *me, game_params *params, char *desc)
542 {
543     const char *prob;
544     game_state *st = new_game_int(params, desc, &prob);
545     assert(st);
546     return st;
547 }
548
549 static char *generate_desc(game_state *new)
550 {
551     int x, y, idx, other, w = new->w, h = new->h;
552     char *desc = snewn(new->wh + 2*(w + h) + 5, char), *p = desc;
553
554     for (x = 0; x < w; x++) *p++ = n2c(new->common->colcount[x*3+POSITIVE]);
555     *p++ = ',';
556     for (y = 0; y < h; y++) *p++ = n2c(new->common->rowcount[y*3+POSITIVE]);
557     *p++ = ',';
558
559     for (x = 0; x < w; x++) *p++ = n2c(new->common->colcount[x*3+NEGATIVE]);
560     *p++ = ',';
561     for (y = 0; y < h; y++) *p++ = n2c(new->common->rowcount[y*3+NEGATIVE]);
562     *p++ = ',';
563
564     for (y = 0; y < h; y++) {
565         for (x = 0; x < w; x++) {
566             idx = y*w + x;
567             other = new->common->dominoes[idx];
568
569             if (other == idx) *p++ = '*';
570             else if (other == idx+1) *p++ = 'L';
571             else if (other == idx-1) *p++ = 'R';
572             else if (other == idx+w) *p++ = 'T';
573             else if (other == idx-w) *p++ = 'B';
574             else assert(!"mad domino orientation");
575         }
576     }
577     *p = '\0';
578
579     return desc;
580 }
581
582 static void game_text_hborder(game_state *state, char **p_r)
583 {
584     char *p = *p_r;
585     int x;
586
587     *p++ = ' ';
588     *p++ = '+';
589     for (x = 0; x < state->w*2-1; x++) *p++ = '-';
590     *p++ = '+';
591     *p++ = '\n';
592
593     *p_r = p;
594 }
595
596 static int game_can_format_as_text_now(game_params *params)
597 {
598     return TRUE;
599 }
600
601 static char *game_text_format(game_state *state)
602 {
603     int len, x, y, i;
604     char *ret, *p;
605
606     len = ((state->w*2)+4) * ((state->h*2)+4) + 2;
607     p = ret = snewn(len, char);
608
609     /* top row: '+' then column totals for plus. */
610     *p++ = '+';
611     for (x = 0; x < state->w; x++) {
612         *p++ = ' ';
613         *p++ = n2c(state->common->colcount[x*3+POSITIVE]);
614     }
615     *p++ = '\n';
616
617     /* top border. */
618     game_text_hborder(state, &p);
619
620     for (y = 0; y < state->h; y++) {
621         *p++ = n2c(state->common->rowcount[y*3+POSITIVE]);
622         *p++ = '|';
623         for (x = 0; x < state->w; x++) {
624             i = y*state->w+x;
625             *p++ = state->common->dominoes[i] == i ? '#' :
626                 state->grid[i] == POSITIVE ? '+' :
627                 state->grid[i] == NEGATIVE ? '-' :
628                 state->flags[i] & GS_SET ? '*' : ' ';
629             if (x < (state->w-1))
630                 *p++ = state->common->dominoes[i] == i+1 ? ' ' : '|';
631         }
632         *p++ = '|';
633         *p++ = n2c(state->common->rowcount[y*3+NEGATIVE]);
634         *p++ = '\n';
635
636         if (y < (state->h-1)) {
637             *p++ = ' ';
638             *p++ = '|';
639             for (x = 0; x < state->w; x++) {
640                 i = y*state->w+x;
641                 *p++ = state->common->dominoes[i] == i+state->w ? ' ' : '-';
642                 if (x < (state->w-1))
643                     *p++ = '+';
644             }
645             *p++ = '|';
646             *p++ = '\n';
647         }
648     }
649
650     /* bottom border. */
651     game_text_hborder(state, &p);
652
653     /* bottom row: column totals for minus then '-'. */
654     *p++ = ' ';
655     for (x = 0; x < state->w; x++) {
656         *p++ = ' ';
657         *p++ = n2c(state->common->colcount[x*3+NEGATIVE]);
658     }
659     *p++ = ' ';
660     *p++ = '-';
661     *p++ = '\n';
662     *p++ = '\0';
663
664     return ret;
665 }
666
667 static void game_debug(game_state *state, const char *desc)
668 {
669     char *fmt = game_text_format(state);
670     debug(("%s:\n%s\n", desc, fmt));
671     sfree(fmt);
672 }
673
674 enum { ROW, COLUMN };
675
676 typedef struct rowcol {
677     int i, di, n, roworcol, num;
678     int *targets;
679     const char *name;
680 } rowcol;
681
682 static rowcol mkrowcol(game_state *state, int num, int roworcol)
683 {
684     rowcol rc;
685
686     rc.roworcol = roworcol;
687     rc.num = num;
688
689     if (roworcol == ROW) {
690         rc.i = num * state->w;
691         rc.di = 1;
692         rc.n = state->w;
693         rc.targets = &(state->common->rowcount[num*3]);
694         rc.name = "row";
695     } else if (roworcol == COLUMN) {
696         rc.i = num;
697         rc.di = state->w;
698         rc.n = state->h;
699         rc.targets = &(state->common->colcount[num*3]);
700         rc.name = "column";
701     } else {
702         assert(!"unknown roworcol");
703     }
704     return rc;
705 }
706
707 static int count_rowcol(game_state *state, int num, int roworcol, int which)
708 {
709     int i, count = 0;
710     rowcol rc = mkrowcol(state, num, roworcol);
711
712     for (i = 0; i < rc.n; i++, rc.i += rc.di) {
713         if (which < 0) {
714             if (state->grid[rc.i] == EMPTY &&
715                 !(state->flags[rc.i] & GS_SET))
716                 count++;
717         } else if (state->grid[rc.i] == which)
718             count++;
719     }
720     return count;
721 }
722
723 static void check_rowcol(game_state *state, int num, int roworcol, int which,
724                         int *wrong, int *incomplete)
725 {
726     int count, target = mkrowcol(state, num, roworcol).targets[which];
727
728     if (target == -1) return; /* no number to check against. */
729
730     count = count_rowcol(state, num, roworcol, which);
731     if (count < target) *incomplete = 1;
732     if (count > target) *wrong = 1;
733 }
734
735 static int check_completion(game_state *state)
736 {
737     int i, j, x, y, idx, w = state->w, h = state->h;
738     int which = POSITIVE, wrong = 0, incomplete = 0;
739
740     /* Check row and column counts for magnets. */
741     for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
742         for (i = 0; i < w; i++)
743             check_rowcol(state, i, COLUMN, which, &wrong, &incomplete);
744
745         for (i = 0; i < h; i++)
746             check_rowcol(state, i, ROW, which, &wrong, &incomplete);
747     }
748     /* Check each domino has been filled, and that we don't have
749      * touching identical terminals. */
750     for (i = 0; i < state->wh; i++) state->flags[i] &= ~GS_ERROR;
751     for (x = 0; x < w; x++) {
752         for (y = 0; y < h; y++) {
753             idx = y*w + x;
754             if (state->common->dominoes[idx] == idx)
755                 continue; /* no domino here */
756
757             if (!(state->flags[idx] & GS_SET))
758                 incomplete = 1;
759
760             which = state->grid[idx];
761             if (which != NEUTRAL) {
762 #define CHECK(xx,yy) do { \
763     if (INGRID(state,xx,yy) && \
764         (state->grid[(yy)*w+(xx)] == which)) { \
765         wrong = 1; \
766         state->flags[(yy)*w+(xx)] |= GS_ERROR; \
767         state->flags[y*w+x] |= GS_ERROR; \
768     } \
769 } while(0)
770                 CHECK(x,y-1);
771                 CHECK(x,y+1);
772                 CHECK(x-1,y);
773                 CHECK(x+1,y);
774 #undef CHECK
775             }
776         }
777     }
778     return wrong ? -1 : incomplete ? 0 : 1;
779 }
780
781 static const int dx[4] = {-1, 1, 0, 0};
782 static const int dy[4] = {0, 0, -1, 1};
783
784 static void solve_clearflags(game_state *state)
785 {
786     int i;
787
788     for (i = 0; i < state->wh; i++) {
789         state->flags[i] &= ~GS_NOTMASK;
790         if (state->common->dominoes[i] != i)
791             state->flags[i] &= ~GS_SET;
792     }
793 }
794
795 /* Knowing a given cell cannot be a certain colour also tells us
796  * something about the other cell in that domino. */
797 static int solve_unflag(game_state *state, int i, int which,
798                         const char *why, rowcol *rc)
799 {
800     int ii, ret = 0;
801 #if defined DEBUGGING || defined STANDALONE_SOLVER
802     int w = state->w;
803 #endif
804
805     assert(i >= 0 && i < state->wh);
806     ii = state->common->dominoes[i];
807     if (ii == i) return 0;
808
809     if (rc)
810         debug(("solve_unflag: (%d,%d) for %s %d", i%w, i/w, rc->name, rc->num));
811
812     if ((state->flags[i] & GS_SET) && (state->grid[i] == which)) {
813         debug(("solve_unflag: (%d,%d) already %s, cannot unflag (for %s).",
814                i%w, i/w, NAME(which), why));
815         return -1;
816     }
817     if ((state->flags[ii] & GS_SET) && (state->grid[ii] == OPPOSITE(which))) {
818         debug(("solve_unflag: (%d,%d) opposite already %s, cannot unflag (for %s).",
819                ii%w, ii/w, NAME(OPPOSITE(which)), why));
820         return -1;
821     }
822     if (POSSIBLE(i, which)) {
823         state->flags[i] |= NOTFLAG(which);
824         ret++;
825         debug(("solve_unflag: (%d,%d) CANNOT be %s (%s)",
826                i%w, i/w, NAME(which), why));
827     }
828     if (POSSIBLE(ii, OPPOSITE(which))) {
829         state->flags[ii] |= NOTFLAG(OPPOSITE(which));
830         ret++;
831         debug(("solve_unflag: (%d,%d) CANNOT be %s (%s, other half)",
832                ii%w, ii/w, NAME(OPPOSITE(which)), why));
833     }
834 #ifdef STANDALONE_SOLVER
835     if (verbose && ret) {
836         printf("(%d,%d)", i%w, i/w);
837         if (rc) printf(" in %s %d", rc->name, rc->num);
838         printf(" cannot be %s (%s); opposite (%d,%d) not %s.\n",
839                NAME(which), why, ii%w, ii/w, NAME(OPPOSITE(which)));
840     }
841 #endif
842     return ret;
843 }
844
845 static int solve_unflag_surrounds(game_state *state, int i, int which)
846 {
847     int x = i%state->w, y = i/state->w, xx, yy, j, ii;
848
849     assert(INGRID(state, x, y));
850
851     for (j = 0; j < 4; j++) {
852         xx = x+dx[j]; yy = y+dy[j];
853         if (!INGRID(state, xx, yy)) continue;
854
855         ii = yy*state->w+xx;
856         if (solve_unflag(state, ii, which, "adjacent to set cell", NULL) < 0)
857             return -1;
858     }
859     return 0;
860 }
861
862 /* Sets a cell to a particular colour, and also perform other
863  * housekeeping around that. */
864 static int solve_set(game_state *state, int i, int which,
865                      const char *why, rowcol *rc)
866 {
867     int ii;
868 #if defined DEBUGGING || defined STANDALONE_SOLVER
869     int w = state->w;
870 #endif
871
872     ii = state->common->dominoes[i];
873
874     if (state->flags[i] & GS_SET) {
875         if (state->grid[i] == which) {
876             return 0; /* was already set and held, do nothing. */
877         } else {
878             debug(("solve_set: (%d,%d) is held and %s, cannot set to %s",
879                    i%w, i/w, NAME(state->grid[i]), NAME(which)));
880             return -1;
881         }
882     }
883     if ((state->flags[ii] & GS_SET) && state->grid[ii] != OPPOSITE(which)) {
884         debug(("solve_set: (%d,%d) opposite is held and %s, cannot set to %s",
885                 ii%w, ii/w, NAME(state->grid[ii]), NAME(OPPOSITE(which))));
886         return -1;
887     }
888     if (!POSSIBLE(i, which)) {
889         debug(("solve_set: (%d,%d) NOT %s, cannot set.", i%w, i/w, NAME(which)));
890         return -1;
891     }
892     if (!POSSIBLE(ii, OPPOSITE(which))) {
893         debug(("solve_set: (%d,%d) NOT %s, cannot set (%d,%d).",
894                ii%w, ii/w, NAME(OPPOSITE(which)), i%w, i/w));
895         return -1;
896     }
897
898 #ifdef STANDALONE_SOLVER
899     if (verbose) {
900         printf("(%d,%d)", i%w, i/w);
901         if (rc) printf(" in %s %d", rc->name, rc->num);
902         printf(" set to %s (%s), opposite (%d,%d) set to %s.\n",
903                NAME(which), why, ii%w, ii/w, NAME(OPPOSITE(which)));
904     }
905 #endif
906     if (rc)
907         debug(("solve_set: (%d,%d) for %s %d", i%w, i/w, rc->name, rc->num));
908     debug(("solve_set: (%d,%d) setting to %s (%s), surrounds first:",
909            i%w, i/w, NAME(which), why));
910
911     if (which != NEUTRAL) {
912         if (solve_unflag_surrounds(state, i, which) < 0)
913             return -1;
914         if (solve_unflag_surrounds(state, ii, OPPOSITE(which)) < 0)
915             return -1;
916     }
917
918     state->grid[i] = which;
919     state->grid[ii] = OPPOSITE(which);
920
921     state->flags[i] |= GS_SET;
922     state->flags[ii] |= GS_SET;
923
924     debug(("solve_set: (%d,%d) set to %s (%s)", i%w, i/w, NAME(which), why));
925
926     return 1;
927 }
928
929 /* counts should be int[4]. */
930 static void solve_counts(game_state *state, rowcol rc, int *counts, int *unset)
931 {
932     int i, j, which;
933
934     assert(counts);
935     for (i = 0; i < 4; i++) {
936         counts[i] = 0;
937         if (unset) unset[i] = 0;
938     }
939
940     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
941         if (state->flags[i] & GS_SET) {
942             assert(state->grid[i] < 3);
943             counts[state->grid[i]]++;
944         } else if (unset) {
945             for (which = 0; which <= 2; which++) {
946                 if (POSSIBLE(i, which))
947                     unset[which]++;
948             }
949         }
950     }
951 }
952
953 static int solve_checkfull(game_state *state, rowcol rc, int *counts)
954 {
955     int starti = rc.i, j, which, didsth = 0, target;
956     int unset[4];
957
958     assert(state->numbered); /* only useful (should only be called) if numbered. */
959
960     solve_counts(state, rc, counts, unset);
961
962     for (which = 0; which <= 2; which++) {
963         target = rc.targets[which];
964         if (target == -1) continue;
965
966         /*debug(("%s %d for %s: target %d, count %d, unset %d",
967                rc.name, rc.num, NAME(which),
968                target, counts[which], unset[which]));*/
969
970         if (target < counts[which]) {
971             debug(("%s %d has too many (%d) %s squares (target %d), impossible!",
972                    rc.name, rc.num, counts[which], NAME(which), target));
973             return -1;
974         }
975         if (target == counts[which]) {
976             /* We have the correct no. of the colour in this row/column
977              * already; unflag all the rest. */
978             for (rc.i = starti, j = 0; j < rc.n; rc.i += rc.di, j++) {
979                 if (state->flags[rc.i] & GS_SET) continue;
980                 if (!POSSIBLE(rc.i, which)) continue;
981
982                 if (solve_unflag(state, rc.i, which, "row/col full", &rc) < 0)
983                     return -1;
984                 didsth = 1;
985             }
986         } else if ((target - counts[which]) == unset[which]) {
987             /* We need all the remaining unset squares for this colour;
988              * set them all. */
989             for (rc.i = starti, j = 0; j < rc.n; rc.i += rc.di, j++) {
990                 if (state->flags[rc.i] & GS_SET) continue;
991                 if (!POSSIBLE(rc.i, which)) continue;
992
993                 if (solve_set(state, rc.i, which, "row/col needs all unset", &rc) < 0)
994                     return -1;
995                 didsth = 1;
996             }
997         }
998     }
999     return didsth;
1000 }
1001
1002 static int solve_startflags(game_state *state)
1003 {
1004     int x, y, i;
1005
1006     for (x = 0; x < state->w; x++) {
1007         for (y = 0; y < state->h; y++) {
1008             i = y*state->w+x;
1009             if (state->common->dominoes[i] == i) continue;
1010             if (state->grid[i] != NEUTRAL ||
1011                 state->flags[i] & GS_SET) {
1012                 if (solve_set(state, i, state->grid[i], "initial set-and-hold", NULL) < 0)
1013                     return -1;
1014             }
1015         }
1016     }
1017     return 0;
1018 }
1019
1020 typedef int (*rowcolfn)(game_state *state, rowcol rc, int *counts);
1021
1022 static int solve_rowcols(game_state *state, rowcolfn fn)
1023 {
1024     int x, y, didsth = 0, ret;
1025     rowcol rc;
1026     int counts[4];
1027
1028     for (x = 0; x < state->w; x++) {
1029         rc = mkrowcol(state, x, COLUMN);
1030         solve_counts(state, rc, counts, NULL);
1031
1032         ret = fn(state, rc, counts);
1033         if (ret < 0) return ret;
1034         didsth += ret;
1035     }
1036     for (y = 0; y < state->h; y++) {
1037         rc = mkrowcol(state, y, ROW);
1038         solve_counts(state, rc, counts, NULL);
1039
1040         ret = fn(state, rc, counts);
1041         if (ret < 0) return ret;
1042         didsth += ret;
1043     }
1044     return didsth;
1045 }
1046
1047 static int solve_force(game_state *state)
1048 {
1049     int i, which, didsth = 0;
1050     unsigned long f;
1051
1052     for (i = 0; i < state->wh; i++) {
1053         if (state->flags[i] & GS_SET) continue;
1054         if (state->common->dominoes[i] == i) continue;
1055
1056         f = state->flags[i] & GS_NOTMASK;
1057         which = -1;
1058         if (f == (GS_NOTPOSITIVE|GS_NOTNEGATIVE))
1059             which = NEUTRAL;
1060         if (f == (GS_NOTPOSITIVE|GS_NOTNEUTRAL))
1061             which = NEGATIVE;
1062         if (f == (GS_NOTNEGATIVE|GS_NOTNEUTRAL))
1063             which = POSITIVE;
1064         if (which != -1) {
1065             if (solve_set(state, i, which, "forced by flags", NULL) < 0)
1066                 return -1;
1067             didsth = 1;
1068         }
1069     }
1070     return didsth;
1071 }
1072
1073 static int solve_neither(game_state *state)
1074 {
1075     int i, j, didsth = 0;
1076
1077     for (i = 0; i < state->wh; i++) {
1078         if (state->flags[i] & GS_SET) continue;
1079         j = state->common->dominoes[i];
1080         if (i == j) continue;
1081
1082         if (((state->flags[i] & GS_NOTPOSITIVE) &&
1083              (state->flags[j] & GS_NOTPOSITIVE)) ||
1084             ((state->flags[i] & GS_NOTNEGATIVE) &&
1085              (state->flags[j] & GS_NOTNEGATIVE))) {
1086             if (solve_set(state, i, NEUTRAL, "neither tile magnet", NULL) < 0)
1087                 return -1;
1088             didsth = 1;
1089         }
1090     }
1091     return didsth;
1092 }
1093
1094 static int solve_advancedfull(game_state *state, rowcol rc, int *counts)
1095 {
1096     int i, j, nfound = 0, clearpos = 0, clearneg = 0, ret = 0;
1097
1098     /* For this row/col, look for a domino entirely within the row where
1099      * both ends can only be + or - (but isn't held).
1100      * The +/- counts can thus be decremented by 1 each, and the 'unset'
1101      * count by 2.
1102      *
1103      * Once that's done for all such dominoes (and they're marked), try
1104      * and made usual deductions about rest of the row based on new totals. */
1105
1106     if (rc.targets[POSITIVE] == -1 && rc.targets[NEGATIVE] == -1)
1107         return 0; /* don't have a target for either colour, nothing to do. */
1108     if ((rc.targets[POSITIVE] >= 0 && counts[POSITIVE] == rc.targets[POSITIVE]) &&
1109         (rc.targets[NEGATIVE] >= 0 && counts[NEGATIVE] == rc.targets[NEGATIVE]))
1110         return 0; /* both colours are full up already, nothing to do. */
1111
1112     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++)
1113         state->flags[i] &= ~GS_MARK;
1114
1115     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1116         if (state->flags[i] & GS_SET) continue;
1117
1118         /* We're looking for a domino in our row/col, thus if
1119          * dominoes[i] -> i+di we've found one. */
1120         if (state->common->dominoes[i] != i+rc.di) continue;
1121
1122         /* We need both squares of this domino to be either + or -
1123          * (i.e. both NOTNEUTRAL only). */
1124         if (((state->flags[i] & GS_NOTMASK) != GS_NOTNEUTRAL) ||
1125             ((state->flags[i+rc.di] & GS_NOTMASK) != GS_NOTNEUTRAL))
1126             continue;
1127
1128         debug(("Domino in %s %d at (%d,%d) must be polarised.",
1129                rc.name, rc.num, i%state->w, i/state->w));
1130         state->flags[i] |= GS_MARK;
1131         state->flags[i+rc.di] |= GS_MARK;
1132         nfound++;
1133     }
1134     if (nfound == 0) return 0;
1135
1136     /* nfound is #dominoes we matched, which will all be marked. */
1137     counts[POSITIVE] += nfound;
1138     counts[NEGATIVE] += nfound;
1139
1140     if (rc.targets[POSITIVE] >= 0 && counts[POSITIVE] == rc.targets[POSITIVE]) {
1141         debug(("%s %d has now filled POSITIVE:", rc.name, rc.num));
1142         clearpos = 1;
1143     }
1144     if (rc.targets[NEGATIVE] >= 0 && counts[NEGATIVE] == rc.targets[NEGATIVE]) {
1145         debug(("%s %d has now filled NEGATIVE:", rc.name, rc.num));
1146         clearneg = 1;
1147     }
1148
1149     if (!clearpos && !clearneg) return 0;
1150
1151     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1152         if (state->flags[i] & GS_SET) continue;
1153         if (state->flags[i] & GS_MARK) continue;
1154
1155         if (clearpos && !(state->flags[i] & GS_NOTPOSITIVE)) {
1156             if (solve_unflag(state, i, POSITIVE, "row/col full (+ve) [tricky]", &rc) < 0)
1157                 return -1;
1158             ret++;
1159         }
1160         if (clearneg && !(state->flags[i] & GS_NOTNEGATIVE)) {
1161             if (solve_unflag(state, i, NEGATIVE, "row/col full (-ve) [tricky]", &rc) < 0)
1162                 return -1;
1163             ret++;
1164         }
1165     }
1166
1167     return ret;
1168 }
1169
1170 /* If we only have one neutral still to place on a row/column then no
1171    dominoes entirely in that row/column can be neutral. */
1172 static int solve_nonneutral(game_state *state, rowcol rc, int *counts)
1173 {
1174     int i, j, ret = 0;
1175
1176     if (rc.targets[NEUTRAL] != counts[NEUTRAL]+1)
1177         return 0;
1178
1179     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1180         if (state->flags[i] & GS_SET) continue;
1181         if (state->common->dominoes[i] != i+rc.di) continue;
1182
1183         if (!(state->flags[i] & GS_NOTNEUTRAL)) {
1184             if (solve_unflag(state, i, NEUTRAL, "single neutral in row/col [tricky]", &rc) < 0)
1185                 return -1;
1186             ret++;
1187         }
1188     }
1189     return ret;
1190 }
1191
1192 /* If we need to fill all unfilled cells with +-, and we need 1 more of
1193  * one than the other, and we have a single odd-numbered region of unfilled
1194  * cells, that odd-numbered region must start and end with the extra number. */
1195 static int solve_oddlength(game_state *state, rowcol rc, int *counts)
1196 {
1197     int i, j, ret = 0, extra, tpos, tneg;
1198     int start = -1, length = 0, inempty = 0, startodd = -1;
1199
1200     /* need zero neutral cells still to find... */
1201     if (rc.targets[NEUTRAL] != counts[NEUTRAL])
1202         return 0;
1203
1204     /* ...and #positive and #negative to differ by one. */
1205     tpos = rc.targets[POSITIVE] - counts[POSITIVE];
1206     tneg = rc.targets[NEGATIVE] - counts[NEGATIVE];
1207     if (tpos == tneg+1)
1208         extra = POSITIVE;
1209     else if (tneg == tpos+1)
1210         extra = NEGATIVE;
1211     else return 0;
1212
1213     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1214         if (state->flags[i] & GS_SET) {
1215             if (inempty) {
1216                 if (length % 2) {
1217                     /* we've just finished an odd-length section. */
1218                     if (startodd != -1) goto twoodd;
1219                     startodd = start;
1220                 }
1221                 inempty = 0;
1222             }
1223         } else {
1224             if (inempty)
1225                 length++;
1226             else {
1227                 start = i;
1228                 length = 1;
1229                 inempty = 1;
1230             }
1231         }
1232     }
1233     if (inempty && (length % 2)) {
1234         if (startodd != -1) goto twoodd;
1235         startodd = start;
1236     }
1237     if (startodd != -1)
1238         ret = solve_set(state, startodd, extra, "odd-length section start", &rc);
1239
1240     return ret;
1241
1242 twoodd:
1243     debug(("%s %d has >1 odd-length sections, starting at %d,%d and %d,%d.",
1244            rc.name, rc.num,
1245            startodd%state->w, startodd/state->w,
1246            start%state->w, start/state->w));
1247     return 0;
1248 }
1249
1250 /* Count the number of remaining empty dominoes in any row/col.
1251  * If that number is equal to the #remaining positive,
1252  * or to the #remaining negative, no empty cells can be neutral. */
1253 static int solve_countdominoes_neutral(game_state *state, rowcol rc, int *counts)
1254 {
1255     int i, j, ndom = 0, nonn = 0, ret = 0;
1256
1257     if ((rc.targets[POSITIVE] == -1) && (rc.targets[NEGATIVE] == -1))
1258         return 0; /* need at least one target to compare. */
1259
1260     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1261         if (state->flags[i] & GS_SET) continue;
1262         assert(state->grid[i] == EMPTY);
1263
1264         /* Skip solo cells, or second cell in domino. */
1265         if ((state->common->dominoes[i] == i) ||
1266             (state->common->dominoes[i] == i-rc.di))
1267             continue;
1268
1269         ndom++;
1270     }
1271
1272     if ((rc.targets[POSITIVE] != -1) &&
1273         (rc.targets[POSITIVE]-counts[POSITIVE] == ndom))
1274         nonn = 1;
1275     if ((rc.targets[NEGATIVE] != -1) &&
1276         (rc.targets[NEGATIVE]-counts[NEGATIVE] == ndom))
1277         nonn = 1;
1278
1279     if (!nonn) return 0;
1280
1281     for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1282         if (state->flags[i] & GS_SET) continue;
1283
1284         if (!(state->flags[i] & GS_NOTNEUTRAL)) {
1285             if (solve_unflag(state, i, NEUTRAL, "all dominoes +/- [tricky]", &rc) < 0)
1286                 return -1;
1287             ret++;
1288         }
1289     }
1290     return ret;
1291 }
1292
1293 static int solve_domino_count(game_state *state, rowcol rc, int i, int which)
1294 {
1295     int nposs = 0;
1296
1297     /* Skip solo cells or 2nd in domino. */
1298     if ((state->common->dominoes[i] == i) ||
1299         (state->common->dominoes[i] == i-rc.di))
1300         return 0;
1301
1302     if (state->flags[i] & GS_SET)
1303         return 0;
1304
1305     if (POSSIBLE(i, which))
1306         nposs++;
1307
1308     if (state->common->dominoes[i] == i+rc.di) {
1309         /* second cell of domino is on our row: test that too. */
1310         if (POSSIBLE(i+rc.di, which))
1311             nposs++;
1312     }
1313     return nposs;
1314 }
1315
1316 /* Count number of dominoes we could put each of + and - into. If it is equal
1317  * to the #left, any domino we can only put + or - in one cell of must have it. */
1318 static int solve_countdominoes_nonneutral(game_state *state, rowcol rc, int *counts)
1319 {
1320     int which, w, i, j, ndom = 0, didsth = 0, toset;
1321
1322     for (which = POSITIVE, w = 0; w < 2; which = OPPOSITE(which), w++) {
1323         if (rc.targets[which] == -1) continue;
1324
1325         for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1326             if (solve_domino_count(state, rc, i, which) > 0)
1327                 ndom++;
1328         }
1329
1330         if ((rc.targets[which] - counts[which]) != ndom)
1331             continue;
1332
1333         for (i = rc.i, j = 0; j < rc.n; i += rc.di, j++) {
1334             if (solve_domino_count(state, rc, i, which) == 1) {
1335                 if (POSSIBLE(i, which))
1336                     toset = i;
1337                 else {
1338                     /* paranoia, should have been checked by solve_domino_count. */
1339                     assert(state->common->dominoes[i] == i+rc.di);
1340                     assert(POSSIBLE(i+rc.di, which));
1341                     toset = i+rc.di;
1342                 }
1343                 if (solve_set(state, toset, which, "all empty dominoes need +/- [tricky]", &rc) < 0)
1344                     return -1;
1345                 didsth++;
1346             }
1347         }
1348     }
1349     return didsth;
1350 }
1351
1352 /* danger, evil macro. can't use the do { ... } while(0) trick because
1353  * the continue breaks. */
1354 #define SOLVE_FOR_ROWCOLS(fn) \
1355     ret = solve_rowcols(state, fn); \
1356     if (ret < 0) { debug(("%s said impossible, cannot solve", #fn)); return -1; } \
1357     if (ret > 0) continue
1358
1359 static int solve_state(game_state *state, int diff)
1360 {
1361     int ret;
1362
1363     debug(("solve_state, difficulty %s", magnets_diffnames[diff]));
1364
1365     solve_clearflags(state);
1366     if (solve_startflags(state) < 0) return -1;
1367
1368     while (1) {
1369         ret = solve_force(state);
1370         if (ret > 0) continue;
1371         if (ret < 0) return -1;
1372
1373         ret = solve_neither(state);
1374         if (ret > 0) continue;
1375         if (ret < 0) return -1;
1376
1377         SOLVE_FOR_ROWCOLS(solve_checkfull);
1378         SOLVE_FOR_ROWCOLS(solve_oddlength);
1379
1380         if (diff < DIFF_TRICKY) break;
1381
1382         SOLVE_FOR_ROWCOLS(solve_advancedfull);
1383         SOLVE_FOR_ROWCOLS(solve_nonneutral);
1384         SOLVE_FOR_ROWCOLS(solve_countdominoes_neutral);
1385         SOLVE_FOR_ROWCOLS(solve_countdominoes_nonneutral);
1386
1387         /* more ... */
1388
1389         break;
1390     }
1391     return check_completion(state);
1392 }
1393
1394
1395 static char *game_state_diff(game_state *src, game_state *dst, int issolve)
1396 {
1397     char *ret = NULL, buf[80], c;
1398     int retlen = 0, x, y, i, k;
1399
1400     assert(src->w == dst->w && src->h == dst->h);
1401
1402     if (issolve) {
1403         ret = sresize(ret, 3, char);
1404         ret[0] = 'S'; ret[1] = ';'; ret[2] = '\0';
1405         retlen += 2;
1406     }
1407     for (x = 0; x < dst->w; x++) {
1408         for (y = 0; y < dst->h; y++) {
1409             i = y*dst->w+x;
1410
1411             if (src->common->dominoes[i] == i) continue;
1412
1413 #define APPEND do { \
1414     ret = sresize(ret, retlen + k + 1, char); \
1415     strcpy(ret + retlen, buf); \
1416     retlen += k; \
1417 } while(0)
1418
1419             if ((src->grid[i] != dst->grid[i]) ||
1420                 ((src->flags[i] & GS_SET) != (dst->flags[i] & GS_SET))) {
1421                 if (dst->grid[i] == EMPTY && !(dst->flags[i] & GS_SET))
1422                     c = ' ';
1423                 else
1424                     c = GRID2CHAR(dst->grid[i]);
1425                 k = sprintf(buf, "%c%d,%d;", (int)c, x, y);
1426                 APPEND;
1427             }
1428         }
1429     }
1430     debug(("game_state_diff returns %s", ret));
1431     return ret;
1432 }
1433
1434 static void solve_from_aux(game_state *state, char *aux)
1435 {
1436     int i;
1437     assert(strlen(aux) == state->wh);
1438     for (i = 0; i < state->wh; i++) {
1439         state->grid[i] = CHAR2GRID(aux[i]);
1440         state->flags[i] |= GS_SET;
1441     }
1442 }
1443
1444 static char *solve_game(game_state *state, game_state *currstate,
1445                         char *aux, char **error)
1446 {
1447     game_state *solved = dup_game(currstate);
1448     char *move = NULL;
1449     int ret;
1450
1451     if (aux && strlen(aux) == state->wh) {
1452         solve_from_aux(solved, aux);
1453         goto solved;
1454     }
1455
1456     if (solve_state(solved, DIFFCOUNT) > 0) goto solved;
1457     free_game(solved);
1458
1459     solved = dup_game(state);
1460     ret = solve_state(solved, DIFFCOUNT);
1461     if (ret > 0) goto solved;
1462     free_game(solved);
1463
1464     *error = (ret < 0) ? "Puzzle is impossible." : "Unable to solve puzzle.";
1465     return NULL;
1466
1467 solved:
1468     move = game_state_diff(currstate, solved, 1);
1469     free_game(solved);
1470     return move;
1471 }
1472
1473 static int solve_unnumbered(game_state *state)
1474 {
1475     int i, ret;
1476     while (1) {
1477         ret = solve_force(state);
1478         if (ret > 0) continue;
1479         if (ret < 0) return -1;
1480
1481         ret = solve_neither(state);
1482         if (ret > 0) continue;
1483         if (ret < 0) return -1;
1484
1485         break;
1486     }
1487     for (i = 0; i < state->wh; i++) {
1488         if (!(state->flags[i] & GS_SET)) return 0;
1489     }
1490     return 1;
1491 }
1492
1493 static int lay_dominoes(game_state *state, random_state *rs, int *scratch)
1494 {
1495     int n, i, ret = 0, nlaid = 0, n_initial_neutral;
1496
1497     for (i = 0; i < state->wh; i++) {
1498         scratch[i] = i;
1499         state->grid[i] = EMPTY;
1500         state->flags[i] = (state->common->dominoes[i] == i) ? GS_SET : 0;
1501     }
1502     shuffle(scratch, state->wh, sizeof(int), rs);
1503
1504     n_initial_neutral = (state->wh > 100) ? 5 : (state->wh / 10);
1505
1506     for (n = 0; n < state->wh; n++) {
1507         /* Find a space ... */
1508
1509         i = scratch[n];
1510         if (state->flags[i] & GS_SET) continue; /* already laid here. */
1511
1512         /* ...and lay a domino if we can. */
1513
1514         debug(("Laying domino at i:%d, (%d,%d)\n", i, i%state->w, i/state->w));
1515
1516         /* The choice of which type of domino to lay here leads to subtle differences
1517          * in the sorts of boards that get produced. Too much bias towards magnets
1518          * leads to games that are too easy.
1519          *
1520          * Currently, it lays a small set of dominoes at random as neutral, and
1521          * then lays the rest preferring to be magnets -- however, if the
1522          * current layout is such that a magnet won't go there, then it lays
1523          * another neutral.
1524          *
1525          * The number of initially neutral dominoes is limited as grids get bigger:
1526          * too many neutral dominoes invariably ends up with insoluble puzzle at
1527          * this size, and the positioning process means it'll always end up laying
1528          * more than the initial 5 anyway.
1529          */
1530
1531         /* We should always be able to lay a neutral anywhere. */
1532         assert(!(state->flags[i] & GS_NOTNEUTRAL));
1533
1534         if (n < n_initial_neutral) {
1535             debug(("  ...laying neutral\n"));
1536             ret = solve_set(state, i, NEUTRAL, "layout initial neutral", NULL);
1537         } else {
1538             debug(("  ... preferring magnet\n"));
1539             if (!(state->flags[i] & GS_NOTPOSITIVE))
1540                 ret = solve_set(state, i, POSITIVE, "layout", NULL);
1541             else if (!(state->flags[i] & GS_NOTNEGATIVE))
1542                 ret = solve_set(state, i, NEGATIVE, "layout", NULL);
1543             else
1544                 ret = solve_set(state, i, NEUTRAL, "layout", NULL);
1545         }
1546         if (!ret) {
1547             debug(("Unable to lay anything at (%d,%d), giving up.",
1548                    i%state->w, i/state->w));
1549             ret = -1;
1550             break;
1551         }
1552
1553         nlaid++;
1554         ret = solve_unnumbered(state);
1555         if (ret == -1)
1556             debug(("solve_unnumbered decided impossible.\n"));
1557         if (ret != 0)
1558             break;
1559     }
1560
1561     debug(("Laid %d dominoes, total %d dominoes.\n", nlaid, state->wh/2));
1562     game_debug(state, "Final layout");
1563     return ret;
1564 }
1565
1566 static void gen_game(game_state *new, random_state *rs)
1567 {
1568     int ret, x, y, val;
1569     int *scratch = snewn(new->wh, int);
1570
1571 #ifdef STANDALONE_SOLVER
1572     if (verbose) printf("Generating new game...\n");
1573 #endif
1574
1575     clear_state(new);
1576     sfree(new->common->dominoes); /* bit grotty. */
1577     new->common->dominoes = domino_layout(new->w, new->h, rs);
1578
1579     do {
1580         ret = lay_dominoes(new, rs, scratch);
1581     } while(ret == -1);
1582
1583     /* for each cell, update colcount/rowcount as appropriate. */
1584     memset(new->common->colcount, 0, new->w*3*sizeof(int));
1585     memset(new->common->rowcount, 0, new->h*3*sizeof(int));
1586     for (x = 0; x < new->w; x++) {
1587         for (y = 0; y < new->h; y++) {
1588             val = new->grid[y*new->w+x];
1589             new->common->colcount[x*3+val]++;
1590             new->common->rowcount[y*3+val]++;
1591         }
1592     }
1593     new->numbered = 1;
1594
1595     sfree(scratch);
1596 }
1597
1598 static void generate_aux(game_state *new, char *aux)
1599 {
1600     int i;
1601     for (i = 0; i < new->wh; i++)
1602         aux[i] = GRID2CHAR(new->grid[i]);
1603     aux[new->wh] = '\0';
1604 }
1605
1606 static int check_difficulty(game_params *params, game_state *new,
1607                             random_state *rs)
1608 {
1609     int *scratch, *grid_correct, slen, i;
1610
1611     memset(new->grid, EMPTY, new->wh*sizeof(int));
1612
1613     if (params->diff > DIFF_EASY) {
1614         /* If this is too easy, return. */
1615         if (solve_state(new, params->diff-1) > 0) {
1616             debug(("Puzzle is too easy."));
1617             return -1;
1618         }
1619     }
1620     if (solve_state(new, params->diff) <= 0) {
1621         debug(("Puzzle is not soluble at requested difficulty."));
1622         return -1;
1623     }
1624     if (!params->stripclues) return 0;
1625
1626     /* Copy the correct grid away. */
1627     grid_correct = snewn(new->wh, int);
1628     memcpy(grid_correct, new->grid, new->wh*sizeof(int));
1629
1630     /* Create shuffled array of side-clue locations. */
1631     slen = new->w*2 + new->h*2;
1632     scratch = snewn(slen, int);
1633     for (i = 0; i < slen; i++) scratch[i] = i;
1634     shuffle(scratch, slen, sizeof(int), rs);
1635
1636     /* For each clue, check whether removing it makes the puzzle unsoluble;
1637      * put it back if so. */
1638     for (i = 0; i < slen; i++) {
1639         int num = scratch[i], which, roworcol, target, targetn, ret;
1640         rowcol rc;
1641
1642         /* work out which clue we meant. */
1643         if (num < new->w+new->h) { which = POSITIVE; }
1644         else { which = NEGATIVE; num -= new->w+new->h; }
1645
1646         if (num < new->w) { roworcol = COLUMN; }
1647         else { roworcol = ROW; num -= new->w; }
1648
1649         /* num is now the row/column index in question. */
1650         rc = mkrowcol(new, num, roworcol);
1651
1652         /* Remove clue, storing original... */
1653         target = rc.targets[which];
1654         targetn = rc.targets[NEUTRAL];
1655         rc.targets[which] = -1;
1656         rc.targets[NEUTRAL] = -1;
1657
1658         /* ...and see if we can still solve it. */
1659         game_debug(new, "removed clue, new board:");
1660         memset(new->grid, EMPTY, new->wh * sizeof(int));
1661         ret = solve_state(new, params->diff);
1662         assert(ret != -1);
1663
1664         if (ret == 0 ||
1665             memcmp(new->grid, grid_correct, new->wh*sizeof(int)) != 0) {
1666             /* We made it ambiguous: put clue back. */
1667             debug(("...now impossible/different, put clue back."));
1668             rc.targets[which] = target;
1669             rc.targets[NEUTRAL] = targetn;
1670         }
1671     }
1672     sfree(scratch);
1673     sfree(grid_correct);
1674
1675     return 0;
1676 }
1677
1678 static char *new_game_desc(game_params *params, random_state *rs,
1679                            char **aux_r, int interactive)
1680 {
1681     game_state *new = new_state(params->w, params->h);
1682     char *desc, *aux = snewn(new->wh+1, char);
1683
1684     do {
1685         gen_game(new, rs);
1686         generate_aux(new, aux);
1687     } while (check_difficulty(params, new, rs) < 0);
1688
1689     /* now we're complete, generate the description string
1690      * and an aux_info for the completed game. */
1691     desc = generate_desc(new);
1692
1693     free_game(new);
1694
1695     *aux_r = aux;
1696     return desc;
1697 }
1698
1699 struct game_ui {
1700     int cur_x, cur_y, cur_visible;
1701 };
1702
1703 static game_ui *new_ui(game_state *state)
1704 {
1705     game_ui *ui = snew(game_ui);
1706     ui->cur_x = ui->cur_y = 0;
1707     ui->cur_visible = 0;
1708     return ui;
1709 }
1710
1711 static void free_ui(game_ui *ui)
1712 {
1713     sfree(ui);
1714 }
1715
1716 static char *encode_ui(game_ui *ui)
1717 {
1718     return NULL;
1719 }
1720
1721 static void decode_ui(game_ui *ui, char *encoding)
1722 {
1723 }
1724
1725 static void game_changed_state(game_ui *ui, game_state *oldstate,
1726                                game_state *newstate)
1727 {
1728     if (!oldstate->completed && newstate->completed)
1729         ui->cur_visible = 0;
1730 }
1731
1732 struct game_drawstate {
1733     int tilesize, started, solved;
1734     int w, h;
1735     unsigned long *what;                /* size w*h */
1736     unsigned long *colwhat, *rowwhat;   /* size 3*w, 3*h */
1737 };
1738
1739 #define DS_WHICH_MASK 0xf
1740
1741 #define DS_ERROR    0x10
1742 #define DS_CURSOR   0x20
1743 #define DS_SET      0x40
1744 #define DS_FULL     0x80
1745 #define DS_NOTPOS   0x100
1746 #define DS_NOTNEG   0x200
1747 #define DS_NOTNEU   0x400
1748 #define DS_FLASH    0x800
1749
1750 #define PREFERRED_TILE_SIZE 32
1751 #define TILE_SIZE (ds->tilesize)
1752 #define BORDER    (TILE_SIZE / 8)
1753
1754 #define COORD(x) ( (x+1) * TILE_SIZE + BORDER )
1755 #define FROMCOORD(x) ( (x - BORDER) / TILE_SIZE - 1 )
1756
1757 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
1758                             int x, int y, int button)
1759 {
1760     int gx = FROMCOORD(x), gy = FROMCOORD(y), idx, curr;
1761     char *nullret = NULL, buf[80], movech;
1762     enum { CYCLE_MAGNET, CYCLE_NEUTRAL } action;
1763
1764     if (IS_CURSOR_MOVE(button)) {
1765         move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0);
1766         ui->cur_visible = 1;
1767         return "";
1768     } else if (IS_CURSOR_SELECT(button)) {
1769         if (!ui->cur_visible) {
1770             ui->cur_visible = 1;
1771             return "";
1772         }
1773         action = (button == CURSOR_SELECT) ? CYCLE_MAGNET : CYCLE_NEUTRAL;
1774         gx = ui->cur_x;
1775         gy = ui->cur_y;
1776     } else if (INGRID(state, gx, gy) &&
1777                (button == LEFT_BUTTON || button == RIGHT_BUTTON)) {
1778         if (ui->cur_visible) {
1779             ui->cur_visible = 0;
1780             nullret = "";
1781         }
1782         action = (button == LEFT_BUTTON) ? CYCLE_MAGNET : CYCLE_NEUTRAL;
1783     } else
1784         return NULL;
1785
1786     idx = gy * state->w + gx;
1787     if (state->common->dominoes[idx] == idx) return nullret;
1788     curr = state->grid[idx];
1789
1790     if (action == CYCLE_MAGNET) {
1791         /* ... empty --> positive --> negative --> empty ... */
1792
1793         if (state->grid[idx] == NEUTRAL && state->flags[idx] & GS_SET)
1794             return nullret; /* can't cycle a magnet from a neutral. */
1795         movech = (curr == EMPTY) ? '+' : (curr == POSITIVE) ? '-' : ' ';
1796     } else if (action == CYCLE_NEUTRAL) {
1797         /* ... empty -> neutral -> !neutral --> empty ... */
1798
1799         if (state->grid[idx] != NEUTRAL)
1800             return nullret; /* can't cycle through neutral from a magnet. */
1801
1802         /* All of these are grid == EMPTY == NEUTRAL; it twiddles
1803          * combinations of flags. */
1804         if (state->flags[idx] & GS_SET) /* neutral */
1805             movech = '?';
1806         else if (state->flags[idx] & GS_NOTNEUTRAL) /* !neutral */
1807             movech = ' ';
1808         else
1809             movech = '.';
1810     } else {
1811         assert(!"unknown action");
1812         movech = 0;                    /* placate optimiser */
1813     }
1814
1815     sprintf(buf, "%c%d,%d", movech, gx, gy);
1816
1817     return dupstr(buf);
1818 }
1819
1820 static game_state *execute_move(game_state *state, char *move)
1821 {
1822     game_state *ret = dup_game(state);
1823     int x, y, n, idx, idx2;
1824     char c;
1825
1826     if (!*move) goto badmove;
1827     while (*move) {
1828         c = *move++;
1829         if (c == 'S') {
1830             ret->solved = TRUE;
1831             n = 0;
1832         } else if (c == '+' || c == '-' ||
1833                    c == '.' || c == ' ' || c == '?') {
1834             if ((sscanf(move, "%d,%d%n", &x, &y, &n) != 2) ||
1835                 !INGRID(state, x, y)) goto badmove;
1836
1837             idx = y*state->w + x;
1838             idx2 = state->common->dominoes[idx];
1839             if (idx == idx2) goto badmove;
1840
1841             ret->flags[idx] &= ~GS_NOTMASK;
1842             ret->flags[idx2] &= ~GS_NOTMASK;
1843
1844             if (c == ' ' || c == '?') {
1845                 ret->grid[idx] = EMPTY;
1846                 ret->grid[idx2] = EMPTY;
1847                 ret->flags[idx] &= ~GS_SET;
1848                 ret->flags[idx2] &= ~GS_SET;
1849                 if (c == '?') {
1850                     ret->flags[idx] |= GS_NOTNEUTRAL;
1851                     ret->flags[idx2] |= GS_NOTNEUTRAL;
1852                 }
1853             } else {
1854                 ret->grid[idx] = CHAR2GRID(c);
1855                 ret->grid[idx2] = OPPOSITE(CHAR2GRID(c));
1856                 ret->flags[idx] |= GS_SET;
1857                 ret->flags[idx2] |= GS_SET;
1858             }
1859         } else
1860             goto badmove;
1861
1862         move += n;
1863         if (*move == ';') move++;
1864         else if (*move) goto badmove;
1865     }
1866     if (check_completion(ret) == 1)
1867         ret->completed = 1;
1868
1869     return ret;
1870
1871 badmove:
1872     free_game(ret);
1873     return NULL;
1874 }
1875
1876 /* ----------------------------------------------------------------------
1877  * Drawing routines.
1878  */
1879
1880 static void game_compute_size(game_params *params, int tilesize,
1881                               int *x, int *y)
1882 {
1883     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1884     struct { int tilesize; } ads, *ds = &ads;
1885     ads.tilesize = tilesize;
1886
1887     *x = TILE_SIZE * (params->w+2) + 2 * BORDER;
1888     *y = TILE_SIZE * (params->h+2) + 2 * BORDER;
1889 }
1890
1891 static void game_set_size(drawing *dr, game_drawstate *ds,
1892                           game_params *params, int tilesize)
1893 {
1894     ds->tilesize = tilesize;
1895 }
1896
1897 static float *game_colours(frontend *fe, int *ncolours)
1898 {
1899     float *ret = snewn(3 * NCOLOURS, float);
1900     int i;
1901
1902     game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
1903
1904     for (i = 0; i < 3; i++) {
1905         ret[COL_TEXT * 3 + i] = 0.0F;
1906         ret[COL_NEGATIVE * 3 + i] = 0.0F;
1907         ret[COL_CURSOR * 3 + i] = 0.9F;
1908     }
1909
1910     ret[COL_POSITIVE * 3 + 0] = 0.8F;
1911     ret[COL_POSITIVE * 3 + 1] = 0.0F;
1912     ret[COL_POSITIVE * 3 + 2] = 0.0F;
1913
1914     ret[COL_NEUTRAL * 3 + 0] = 0.10F;
1915     ret[COL_NEUTRAL * 3 + 1] = 0.60F;
1916     ret[COL_NEUTRAL * 3 + 2] = 0.10F;
1917
1918     ret[COL_ERROR * 3 + 0] = 1.0F;
1919     ret[COL_ERROR * 3 + 1] = 0.0F;
1920     ret[COL_ERROR * 3 + 2] = 0.0F;
1921
1922     ret[COL_NOT * 3 + 0] = 0.2F;
1923     ret[COL_NOT * 3 + 1] = 0.2F;
1924     ret[COL_NOT * 3 + 2] = 1.0F;
1925
1926     *ncolours = NCOLOURS;
1927     return ret;
1928 }
1929
1930 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1931 {
1932     struct game_drawstate *ds = snew(struct game_drawstate);
1933
1934     ds->tilesize = ds->started = ds->solved = 0;
1935     ds->w = state->w;
1936     ds->h = state->h;
1937
1938     ds->what = snewn(state->wh, unsigned long);
1939     memset(ds->what, 0, state->wh*sizeof(unsigned long));
1940
1941     ds->colwhat = snewn(state->w*3, unsigned long);
1942     memset(ds->colwhat, 0, state->w*3*sizeof(unsigned long));
1943     ds->rowwhat = snewn(state->h*3, unsigned long);
1944     memset(ds->rowwhat, 0, state->h*3*sizeof(unsigned long));
1945
1946     return ds;
1947 }
1948
1949 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1950 {
1951     sfree(ds->colwhat);
1952     sfree(ds->rowwhat);
1953     sfree(ds->what);
1954     sfree(ds);
1955 }
1956
1957 static void draw_num_col(drawing *dr, game_drawstate *ds, int rowcol, int which,
1958                          int idx, int colbg, int col, int num)
1959 {
1960     char buf[32];
1961     int cx, cy, tsz;
1962
1963     if (num < 0) return;
1964
1965     sprintf(buf, "%d", num);
1966     tsz = (strlen(buf) == 1) ? (7*TILE_SIZE/10) : (9*TILE_SIZE/10)/strlen(buf);
1967
1968     if (rowcol == ROW) {
1969         cx = BORDER;
1970         if (which == NEGATIVE) cx += TILE_SIZE * (ds->w+1);
1971         cy = BORDER + TILE_SIZE * (idx+1);
1972     } else {
1973         cx = BORDER + TILE_SIZE * (idx+1);
1974         cy = BORDER;
1975         if (which == NEGATIVE) cy += TILE_SIZE * (ds->h+1);
1976     }
1977
1978     draw_rect(dr, cx, cy, TILE_SIZE, TILE_SIZE, colbg);
1979     draw_text(dr, cx + TILE_SIZE/2, cy + TILE_SIZE/2, FONT_VARIABLE, tsz,
1980               ALIGN_VCENTRE | ALIGN_HCENTRE, col, buf);
1981
1982     draw_update(dr, cx, cy, TILE_SIZE, TILE_SIZE);
1983 }
1984
1985 static void draw_num(drawing *dr, game_drawstate *ds, int rowcol, int which,
1986                      int idx, unsigned long c, int num)
1987 {
1988     draw_num_col(dr, ds, rowcol, which, idx, COL_BACKGROUND,
1989                  (c & DS_ERROR) ? COL_ERROR : COL_TEXT, num);
1990 }
1991
1992 static void draw_sym(drawing *dr, game_drawstate *ds, int x, int y, int which, int col)
1993 {
1994     int cx = COORD(x), cy = COORD(y);
1995     int ccx = cx + TILE_SIZE/2, ccy = cy + TILE_SIZE/2;
1996     int roff = TILE_SIZE/4, rsz = 2*roff+1;
1997     int soff = TILE_SIZE/16, ssz = 2*soff+1;
1998
1999     if (which == POSITIVE || which == NEGATIVE) {
2000         draw_rect(dr, ccx - roff, ccy - soff, rsz, ssz, col);
2001         if (which == POSITIVE)
2002             draw_rect(dr, ccx - soff, ccy - roff, ssz, rsz, col);
2003     } else if (col == COL_NOT) {
2004         /* not-a-neutral is a blue question mark. */
2005         char qu[2] = { '?', 0 };
2006         draw_text(dr, ccx, ccy, FONT_VARIABLE, 7*TILE_SIZE/10,
2007                   ALIGN_VCENTRE | ALIGN_HCENTRE, col, qu);
2008     } else {
2009         draw_line(dr, ccx - roff, ccy - roff, ccx + roff, ccy + roff, col);
2010         draw_line(dr, ccx + roff, ccy - roff, ccx - roff, ccy + roff, col);
2011     }
2012 }
2013
2014 enum {
2015     TYPE_L,
2016     TYPE_R,
2017     TYPE_T,
2018     TYPE_B,
2019     TYPE_BLANK
2020 };
2021
2022 /* NOT responsible for redrawing background or updating. */
2023 static void draw_tile_col(drawing *dr, game_drawstate *ds, int *dominoes,
2024                           int x, int y, int which, int bg, int fg, int perc)
2025 {
2026     int cx = COORD(x), cy = COORD(y), i, other, type = TYPE_BLANK;
2027     int gutter, radius, coffset;
2028
2029     /* gutter is TSZ/16 for 100%, 8*TSZ/16 (TSZ/2) for 0% */
2030     gutter = (TILE_SIZE / 16) + ((100 - perc) * (7*TILE_SIZE / 16))/100;
2031     radius = (perc * (TILE_SIZE / 8)) / 100;
2032     coffset = gutter + radius;
2033
2034     i = y*ds->w + x;
2035     other = dominoes[i];
2036
2037     if (other == i) return;
2038     else if (other == i+1) type = TYPE_L;
2039     else if (other == i-1) type = TYPE_R;
2040     else if (other == i+ds->w) type = TYPE_T;
2041     else if (other == i-ds->w) type = TYPE_B;
2042     else assert(!"mad domino orientation");
2043
2044     /* domino drawing shamelessly stolen from dominosa.c. */
2045     if (type == TYPE_L || type == TYPE_T)
2046         draw_circle(dr, cx+coffset, cy+coffset,
2047                     radius, bg, bg);
2048     if (type == TYPE_R || type == TYPE_T)
2049         draw_circle(dr, cx+TILE_SIZE-1-coffset, cy+coffset,
2050                     radius, bg, bg);
2051     if (type == TYPE_L || type == TYPE_B)
2052         draw_circle(dr, cx+coffset, cy+TILE_SIZE-1-coffset,
2053                     radius, bg, bg);
2054     if (type == TYPE_R || type == TYPE_B)
2055         draw_circle(dr, cx+TILE_SIZE-1-coffset,
2056                     cy+TILE_SIZE-1-coffset,
2057                     radius, bg, bg);
2058
2059     for (i = 0; i < 2; i++) {
2060         int x1, y1, x2, y2;
2061
2062         x1 = cx + (i ? gutter : coffset);
2063         y1 = cy + (i ? coffset : gutter);
2064         x2 = cx + TILE_SIZE-1 - (i ? gutter : coffset);
2065         y2 = cy + TILE_SIZE-1 - (i ? coffset : gutter);
2066         if (type == TYPE_L)
2067             x2 = cx + TILE_SIZE;
2068         else if (type == TYPE_R)
2069             x1 = cx;
2070         else if (type == TYPE_T)
2071             y2 = cy + TILE_SIZE ;
2072         else if (type == TYPE_B)
2073             y1 = cy;
2074
2075         draw_rect(dr, x1, y1, x2-x1+1, y2-y1+1, bg);
2076     }
2077
2078     if (fg != -1) draw_sym(dr, ds, x, y, which, fg);
2079 }
2080
2081 static void draw_tile(drawing *dr, game_drawstate *ds, int *dominoes,
2082                       int x, int y, unsigned long flags)
2083 {
2084     int cx = COORD(x), cy = COORD(y), bg, fg, perc = 100;
2085     int which = flags & DS_WHICH_MASK;
2086
2087     flags &= ~DS_WHICH_MASK;
2088
2089     draw_rect(dr, cx, cy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2090
2091     if (flags & DS_CURSOR)
2092         bg = COL_CURSOR;        /* off-white white for cursor */
2093     else if (which == POSITIVE)
2094         bg = COL_POSITIVE;
2095     else if (which == NEGATIVE)
2096         bg = COL_NEGATIVE;
2097     else if (flags & DS_SET)
2098         bg = COL_NEUTRAL;       /* green inner for neutral cells */
2099     else
2100         bg = COL_LOWLIGHT;      /* light grey for empty cells. */
2101
2102     if (which == EMPTY && !(flags & DS_SET)) {
2103         int notwhich = -1;
2104         fg = -1; /* don't draw cross unless actually set as neutral. */
2105
2106         if (flags & DS_NOTPOS) notwhich = POSITIVE;
2107         if (flags & DS_NOTNEG) notwhich = NEGATIVE;
2108         if (flags & DS_NOTNEU) notwhich = NEUTRAL;
2109         if (notwhich != -1) {
2110             which = notwhich;
2111             fg = COL_NOT;
2112         }
2113     } else
2114         fg = (flags & DS_ERROR) ? COL_ERROR :
2115              (flags & DS_CURSOR) ? COL_TEXT : COL_BACKGROUND;
2116
2117     draw_rect(dr, cx, cy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2118
2119     if (flags & DS_FLASH) {
2120         int bordercol = COL_HIGHLIGHT;
2121         draw_tile_col(dr, ds, dominoes, x, y, which, bordercol, -1, perc);
2122         perc = 3*perc/4;
2123     }
2124     draw_tile_col(dr, ds, dominoes, x, y, which, bg, fg, perc);
2125
2126     draw_update(dr, cx, cy, TILE_SIZE, TILE_SIZE);
2127 }
2128
2129
2130 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2131                         game_state *state, int dir, game_ui *ui,
2132                         float animtime, float flashtime)
2133 {
2134     int x, y, w = state->w, h = state->h, which, i, j, flash;
2135     unsigned long c = 0;
2136
2137     flash = (int)(flashtime * 5 / FLASH_TIME) % 2;
2138
2139     if (!ds->started) {
2140         /* draw background, corner +-. */
2141         draw_rect(dr, 0, 0,
2142                   TILE_SIZE * (w+2) + 2 * BORDER,
2143                   TILE_SIZE * (h+2) + 2 * BORDER,
2144                   COL_BACKGROUND);
2145
2146         draw_sym(dr, ds, -1, -1, POSITIVE, COL_TEXT);
2147         draw_sym(dr, ds, state->w, state->h, NEGATIVE, COL_TEXT);
2148
2149         draw_update(dr, 0, 0,
2150                     TILE_SIZE * (ds->w+2) + 2 * BORDER,
2151                     TILE_SIZE * (ds->h+2) + 2 * BORDER);
2152     }
2153
2154     /* Draw grid */
2155     for (y = 0; y < h; y++) {
2156         for (x = 0; x < w; x++) {
2157             int idx = y*w+x;
2158
2159             c = state->grid[idx];
2160
2161             if (state->flags[idx] & GS_ERROR)
2162                 c |= DS_ERROR;
2163             if (state->flags[idx] & GS_SET)
2164                 c |= DS_SET;
2165
2166             if (x == ui->cur_x && y == ui->cur_y && ui->cur_visible)
2167                 c |= DS_CURSOR;
2168
2169             if (flash)
2170                 c |= DS_FLASH;
2171
2172             if (state->flags[idx] & GS_NOTPOSITIVE)
2173                 c |= DS_NOTPOS;
2174             if (state->flags[idx] & GS_NOTNEGATIVE)
2175                 c |= DS_NOTNEG;
2176             if (state->flags[idx] & GS_NOTNEUTRAL)
2177                 c |= DS_NOTNEU;
2178
2179             if (ds->what[idx] != c || !ds->started) {
2180                 draw_tile(dr, ds, state->common->dominoes, x, y, c);
2181                 ds->what[idx] = c;
2182             }
2183         }
2184     }
2185     /* Draw counts around side */
2186     for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
2187         int target, count;
2188         for (i = 0; i < w; i++) {
2189             target = state->common->colcount[i*3+which];
2190             count = count_rowcol(state, i, COLUMN, which);
2191             c = 0;
2192             if ((count > target) ||
2193                 (count < target && !count_rowcol(state, i, COLUMN, -1)))
2194                 c |= DS_ERROR;
2195             if (count == target) c |= DS_FULL;
2196             if (c != ds->colwhat[i*3+which] || !ds->started) {
2197                 draw_num(dr, ds, COLUMN, which, i, c,
2198                          state->common->colcount[i*3+which]);
2199                 ds->colwhat[i*3+which] = c;
2200             }
2201         }
2202         for (i = 0; i < h; i++) {
2203             target = state->common->rowcount[i*3+which];
2204             count = count_rowcol(state, i, ROW, which);
2205             c = 0;
2206             if ((count > target) ||
2207                 (count < target && !count_rowcol(state, i, ROW, -1)))
2208                 c |= DS_ERROR;
2209             if (count == target) c |= DS_FULL;
2210             if (c != ds->rowwhat[i*3+which] || !ds->started) {
2211                 draw_num(dr, ds, ROW, which, i, c,
2212                          state->common->rowcount[i*3+which]);
2213                 ds->rowwhat[i*3+which] = c;
2214             }
2215         }
2216     }
2217
2218     ds->started = 1;
2219 }
2220
2221 static float game_anim_length(game_state *oldstate, game_state *newstate,
2222                               int dir, game_ui *ui)
2223 {
2224     return 0.0F;
2225 }
2226
2227 static float game_flash_length(game_state *oldstate, game_state *newstate,
2228                                int dir, game_ui *ui)
2229 {
2230     if (!oldstate->completed && newstate->completed &&
2231         !oldstate->solved && !newstate->solved)
2232         return FLASH_TIME;
2233     return 0.0F;
2234 }
2235
2236 static int game_is_solved(game_state *state)
2237 {
2238     return state->completed;
2239 }
2240
2241 static int game_timing_state(game_state *state, game_ui *ui)
2242 {
2243     return TRUE;
2244 }
2245
2246 static void game_print_size(game_params *params, float *x, float *y)
2247 {
2248     int pw, ph;
2249
2250     /*
2251      * I'll use 6mm squares by default.
2252      */
2253     game_compute_size(params, 600, &pw, &ph);
2254     *x = pw / 100.0F;
2255     *y = ph / 100.0F;
2256 }
2257
2258 static void game_print(drawing *dr, game_state *state, int tilesize)
2259 {
2260     int w = state->w, h = state->h;
2261     int ink = print_mono_colour(dr, 0);
2262     int paper = print_mono_colour(dr, 1);
2263     int x, y, which, i, j;
2264
2265     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2266     game_drawstate ads, *ds = &ads;
2267     game_set_size(dr, ds, NULL, tilesize);
2268     ds->w = w; ds->h = h;
2269
2270     /* Border. */
2271     print_line_width(dr, TILE_SIZE/12);
2272
2273     /* Numbers and +/- for corners. */
2274     draw_sym(dr, ds, -1, -1, POSITIVE, ink);
2275     draw_sym(dr, ds, state->w, state->h, NEGATIVE, ink);
2276     for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
2277         for (i = 0; i < w; i++) {
2278             draw_num_col(dr, ds, COLUMN, which, i, paper, ink,
2279                          state->common->colcount[i*3+which]);
2280         }
2281         for (i = 0; i < h; i++) {
2282             draw_num_col(dr, ds, ROW, which, i, paper, ink,
2283                          state->common->rowcount[i*3+which]);
2284         }
2285     }
2286
2287     /* Dominoes. */
2288     for (x = 0; x < w; x++) {
2289         for (y = 0; y < h; y++) {
2290             i = y*state->w + x;
2291             if (state->common->dominoes[i] == i+1 ||
2292                 state->common->dominoes[i] == i+w) {
2293                 int dx = state->common->dominoes[i] == i+1 ? 2 : 1;
2294                 int dy = 3 - dx;
2295                 int xx, yy;
2296                 int cx = COORD(x), cy = COORD(y);
2297
2298                 print_line_width(dr, 0);
2299
2300                 /* Ink the domino */
2301                 for (yy = 0; yy < 2; yy++)
2302                     for (xx = 0; xx < 2; xx++)
2303                         draw_circle(dr,
2304                                     cx+xx*dx*TILE_SIZE+(1-2*xx)*3*TILE_SIZE/16,
2305                                     cy+yy*dy*TILE_SIZE+(1-2*yy)*3*TILE_SIZE/16,
2306                                     TILE_SIZE/8, ink, ink);
2307                 draw_rect(dr, cx + TILE_SIZE/16, cy + 3*TILE_SIZE/16,
2308                           dx*TILE_SIZE - 2*(TILE_SIZE/16),
2309                           dy*TILE_SIZE - 6*(TILE_SIZE/16), ink);
2310                 draw_rect(dr, cx + 3*TILE_SIZE/16, cy + TILE_SIZE/16,
2311                           dx*TILE_SIZE - 6*(TILE_SIZE/16),
2312                           dy*TILE_SIZE - 2*(TILE_SIZE/16), ink);
2313
2314                 /* Un-ink the domino interior */
2315                 for (yy = 0; yy < 2; yy++)
2316                     for (xx = 0; xx < 2; xx++)
2317                         draw_circle(dr,
2318                                     cx+xx*dx*TILE_SIZE+(1-2*xx)*3*TILE_SIZE/16,
2319                                     cy+yy*dy*TILE_SIZE+(1-2*yy)*3*TILE_SIZE/16,
2320                                     3*TILE_SIZE/32, paper, paper);
2321                 draw_rect(dr, cx + 3*TILE_SIZE/32, cy + 3*TILE_SIZE/16,
2322                           dx*TILE_SIZE - 2*(3*TILE_SIZE/32),
2323                           dy*TILE_SIZE - 6*(TILE_SIZE/16), paper);
2324                 draw_rect(dr, cx + 3*TILE_SIZE/16, cy + 3*TILE_SIZE/32,
2325                           dx*TILE_SIZE - 6*(TILE_SIZE/16),
2326                           dy*TILE_SIZE - 2*(3*TILE_SIZE/32), paper);
2327             }
2328         }
2329     }
2330
2331     /* Grid symbols (solution). */
2332     for (x = 0; x < w; x++) {
2333         for (y = 0; y < h; y++) {
2334             i = y*state->w + x;
2335             if ((state->grid[i] != NEUTRAL) || (state->flags[i] & GS_SET))
2336                 draw_sym(dr, ds, x, y, state->grid[i], ink);
2337         }
2338     }
2339 }
2340
2341 #ifdef COMBINED
2342 #define thegame magnets
2343 #endif
2344
2345 const struct game thegame = {
2346     "Magnets", "games.magnets", "magnets",
2347     default_params,
2348     game_fetch_preset,
2349     decode_params,
2350     encode_params,
2351     free_params,
2352     dup_params,
2353     TRUE, game_configure, custom_params,
2354     validate_params,
2355     new_game_desc,
2356     validate_desc,
2357     new_game,
2358     dup_game,
2359     free_game,
2360     TRUE, solve_game,
2361     TRUE, game_can_format_as_text_now, game_text_format,
2362     new_ui,
2363     free_ui,
2364     encode_ui,
2365     decode_ui,
2366     game_changed_state,
2367     interpret_move,
2368     execute_move,
2369     PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2370     game_colours,
2371     game_new_drawstate,
2372     game_free_drawstate,
2373     game_redraw,
2374     game_anim_length,
2375     game_flash_length,
2376     game_is_solved,
2377     TRUE, FALSE, game_print_size, game_print,
2378     FALSE,                             /* wants_statusbar */
2379     FALSE, game_timing_state,
2380     REQUIRE_RBUTTON,                   /* flags */
2381 };
2382
2383 #ifdef STANDALONE_SOLVER
2384
2385 #include <time.h>
2386 #include <stdarg.h>
2387
2388 const char *quis = NULL;
2389 int csv = 0;
2390
2391 void usage(FILE *out) {
2392     fprintf(out, "usage: %s [-v] [--print] <params>|<game id>\n", quis);
2393 }
2394
2395 void doprint(game_state *state)
2396 {
2397     char *fmt = game_text_format(state);
2398     printf("%s", fmt);
2399     sfree(fmt);
2400 }
2401
2402 static void pnum(int n, int ntot, const char *desc)
2403 {
2404     printf("%2.1f%% (%d) %s", (double)n*100.0 / (double)ntot, n, desc);
2405 }
2406
2407 static void start_soak(game_params *p, random_state *rs)
2408 {
2409     time_t tt_start, tt_now, tt_last;
2410     char *aux;
2411     game_state *s, *s2;
2412     int n = 0, nsolved = 0, nimpossible = 0, ntricky = 0, ret, i;
2413     long nn, nn_total = 0, nn_solved = 0, nn_tricky = 0;
2414
2415     tt_start = tt_now = time(NULL);
2416
2417     if (csv)
2418         printf("time, w, h,  #generated, #solved, #tricky, #impossible,  "
2419                "#neutral, #neutral/solved, #neutral/tricky\n");
2420     else
2421         printf("Soak-testing a %dx%d grid.\n", p->w, p->h);
2422
2423     s = new_state(p->w, p->h);
2424     aux = snewn(s->wh+1, char);
2425
2426     while (1) {
2427         gen_game(s, rs);
2428
2429         nn = 0;
2430         for (i = 0; i < s->wh; i++) {
2431             if (s->grid[i] == NEUTRAL) nn++;
2432         }
2433
2434         generate_aux(s, aux);
2435         memset(s->grid, EMPTY, s->wh * sizeof(int));
2436         s2 = dup_game(s);
2437
2438         ret = solve_state(s, DIFFCOUNT);
2439
2440         n++;
2441         nn_total += nn;
2442         if (ret > 0) {
2443             nsolved++;
2444             nn_solved += nn;
2445             if (solve_state(s2, DIFF_EASY) <= 0) {
2446                 ntricky++;
2447                 nn_tricky += nn;
2448             }
2449         } else if (ret < 0) {
2450             char *desc = generate_desc(s);
2451             solve_from_aux(s, aux);
2452             printf("Game considered impossible:\n  %dx%d:%s\n",
2453                     p->w, p->h, desc);
2454             sfree(desc);
2455             doprint(s);
2456             nimpossible++;
2457         }
2458
2459         free_game(s2);
2460
2461         tt_last = time(NULL);
2462         if (tt_last > tt_now) {
2463             tt_now = tt_last;
2464             if (csv) {
2465                 printf("%d,%d,%d, %d,%d,%d,%d, %ld,%ld,%ld\n",
2466                        (int)(tt_now - tt_start), p->w, p->h,
2467                        n, nsolved, ntricky, nimpossible,
2468                        nn_total, nn_solved, nn_tricky);
2469             } else {
2470                 printf("%d total, %3.1f/s, ",
2471                        n, (double)n / ((double)tt_now - tt_start));
2472                 pnum(nsolved, n, "solved"); printf(", ");
2473                 pnum(ntricky, n, "tricky");
2474                 if (nimpossible > 0)
2475                     pnum(nimpossible, n, "impossible");
2476                 printf("\n");
2477
2478                 printf("  overall %3.1f%% neutral (%3.1f%% for solved, %3.1f%% for tricky)\n",
2479                        (double)(nn_total * 100) / (double)(p->w * p->h * n),
2480                        (double)(nn_solved * 100) / (double)(p->w * p->h * nsolved),
2481                        (double)(nn_tricky * 100) / (double)(p->w * p->h * ntricky));
2482             }
2483         }
2484     }
2485     free_game(s);
2486     sfree(aux);
2487 }
2488
2489 int main(int argc, const char *argv[])
2490 {
2491     int print = 0, soak = 0, solved = 0, ret;
2492     char *id = NULL, *desc, *desc_gen = NULL, *err, *aux = NULL;
2493     game_state *s = NULL;
2494     game_params *p = NULL;
2495     random_state *rs = NULL;
2496     time_t seed = time(NULL);
2497
2498     setvbuf(stdout, NULL, _IONBF, 0);
2499
2500     quis = argv[0];
2501     while (--argc > 0) {
2502         char *p = (char*)(*++argv);
2503         if (!strcmp(p, "-v") || !strcmp(p, "--verbose")) {
2504             verbose = 1;
2505         } else if (!strcmp(p, "--csv")) {
2506             csv = 1;
2507         } else if (!strcmp(p, "-e") || !strcmp(p, "--seed")) {
2508             seed = atoi(*++argv);
2509             argc--;
2510         } else if (!strcmp(p, "-p") || !strcmp(p, "--print")) {
2511             print = 1;
2512         } else if (!strcmp(p, "-s") || !strcmp(p, "--soak")) {
2513             soak = 1;
2514         } else if (*p == '-') {
2515             fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2516             usage(stderr);
2517             exit(1);
2518         } else {
2519             id = p;
2520         }
2521     }
2522
2523     rs = random_new((void*)&seed, sizeof(time_t));
2524
2525     if (!id) {
2526         fprintf(stderr, "usage: %s [-v] [--soak] <params> | <game_id>\n", argv[0]);
2527         goto done;
2528     }
2529     desc = strchr(id, ':');
2530     if (desc) *desc++ = '\0';
2531
2532     p = default_params();
2533     decode_params(p, id);
2534     err = validate_params(p, 1);
2535     if (err) {
2536         fprintf(stderr, "%s: %s", argv[0], err);
2537         goto done;
2538     }
2539
2540     if (soak) {
2541         if (desc) {
2542             fprintf(stderr, "%s: --soak needs parameters, not description.\n", quis);
2543             goto done;
2544         }
2545         start_soak(p, rs);
2546         goto done;
2547     }
2548
2549     if (!desc)
2550         desc = desc_gen = new_game_desc(p, rs, &aux, 0);
2551
2552     err = validate_desc(p, desc);
2553     if (err) {
2554         fprintf(stderr, "%s: %s\nDescription: %s\n", quis, err, desc);
2555         goto done;
2556     }
2557     s = new_game(NULL, p, desc);
2558     printf("%s:%s (seed %ld)\n", id, desc, seed);
2559     if (aux) {
2560         /* We just generated this ourself. */
2561         if (verbose || print) {
2562             doprint(s);
2563             solve_from_aux(s, aux);
2564             solved = 1;
2565         }
2566     } else {
2567         doprint(s);
2568         verbose = 1;
2569         ret = solve_state(s, DIFFCOUNT);
2570         if (ret < 0) printf("Puzzle is impossible.\n");
2571         else if (ret == 0) printf("Puzzle is ambiguous.\n");
2572         else printf("Puzzle was solved.\n");
2573         verbose = 0;
2574         solved = 1;
2575     }
2576     if (solved) doprint(s);
2577
2578 done:
2579     if (desc_gen) sfree(desc_gen);
2580     if (p) free_params(p);
2581     if (s) free_game(s);
2582     if (rs) random_free(rs);
2583     if (aux) sfree(aux);
2584
2585     return 0;
2586 }
2587
2588 #endif
2589
2590 /* vim: set shiftwidth=4 tabstop=8: */