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