chiark / gitweb /
Mines now follows the conventional approach of offering a completely
[sgt-puzzles.git] / mines.c
1 /*
2  * mines.c: Minesweeper clone with sophisticated grid generation.
3  * 
4  * Still TODO:
5  * 
6  *  - possibly disable undo? Or alternatively mark game states as
7  *    `cheated', although that's horrid.
8  *     + OK. Rather than _disabling_ undo, we have a hook callable
9  *       in the game backend which is called before we do an undo.
10  *       That hook can talk to the game_ui and set the cheated flag,
11  *       and then make_move can avoid setting the `won' flag after that.
12  *
13  *  - delay game description generation until first click
14  *     + do we actually _need_ to do this? Hmm.
15  *     + it's a perfectly good puzzle game without
16  *     + but it might be useful when we start timing, since it
17  *       ensures the user is really paying attention.
18  * 
19  *  - timer
20  * 
21  *  - question marks (arrgh, preferences?)
22  * 
23  *  - sensible parameter constraints
24  *     + 30x16: 191 mines just about works if rather slowly, 192 is
25  *       just about doom (the latter corresponding to a density of
26  *       exactly 1 in 2.5)
27  *     + 9x9: 45 mines works - over 1 in 2! 50 seems a bit slow.
28  *     + it might not be feasible to work out the exact limit
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <ctype.h>
36 #include <math.h>
37
38 #include "tree234.h"
39 #include "puzzles.h"
40
41 enum {
42     COL_BACKGROUND,
43     COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8,
44     COL_MINE, COL_BANG, COL_CROSS, COL_FLAG, COL_FLAGBASE, COL_QUERY,
45     COL_HIGHLIGHT, COL_LOWLIGHT,
46     NCOLOURS
47 };
48
49 #define TILE_SIZE 20
50 #define BORDER (TILE_SIZE * 3 / 2)
51 #define HIGHLIGHT_WIDTH 2
52 #define OUTER_HIGHLIGHT_WIDTH 3
53 #define COORD(x)  ( (x) * TILE_SIZE + BORDER )
54 #define FROMCOORD(x)  ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
55
56 #define FLASH_FRAME 0.13F
57
58 struct game_params {
59     int w, h, n;
60     int unique;
61 };
62
63 struct mine_layout {
64     /*
65      * This structure is shared between all the game_states for a
66      * given instance of the puzzle, so we reference-count it.
67      */
68     int refcount;
69     char *mines;
70     /*
71      * If we haven't yet actually generated the mine layout, here's
72      * all the data we will need to do so.
73      */
74     int n, unique;
75     random_state *rs;
76     midend_data *me;                   /* to give back the new game desc */
77 };
78
79 struct game_state {
80     int w, h, n, dead, won;
81     struct mine_layout *layout;        /* real mine positions */
82     char *grid;                        /* player knowledge */
83     /*
84      * Each item in the `grid' array is one of the following values:
85      * 
86      *  - 0 to 8 mean the square is open and has a surrounding mine
87      *    count.
88      * 
89      *  - -1 means the square is marked as a mine.
90      * 
91      *  - -2 means the square is unknown.
92      * 
93      *  - -3 means the square is marked with a question mark
94      *    (FIXME: do we even want to bother with this?).
95      * 
96      *  - 64 means the square has had a mine revealed when the game
97      *    was lost.
98      * 
99      *  - 65 means the square had a mine revealed and this was the
100      *    one the player hits.
101      * 
102      *  - 66 means the square has a crossed-out mine because the
103      *    player had incorrectly marked it.
104      */
105 };
106
107 static game_params *default_params(void)
108 {
109     game_params *ret = snew(game_params);
110
111     ret->w = ret->h = 9;
112     ret->n = 10;
113     ret->unique = TRUE;
114
115     return ret;
116 }
117
118 static int game_fetch_preset(int i, char **name, game_params **params)
119 {
120     game_params *ret;
121     char str[80];
122     static const struct { int w, h, n; } values[] = {
123         {9, 9, 10},
124         {16, 16, 40},
125         {30, 16, 99},
126     };
127
128     if (i < 0 || i >= lenof(values))
129         return FALSE;
130
131     ret = snew(game_params);
132     ret->w = values[i].w;
133     ret->h = values[i].h;
134     ret->n = values[i].n;
135     ret->unique = TRUE;
136
137     sprintf(str, "%dx%d, %d mines", ret->w, ret->h, ret->n);
138
139     *name = dupstr(str);
140     *params = ret;
141     return TRUE;
142 }
143
144 static void free_params(game_params *params)
145 {
146     sfree(params);
147 }
148
149 static game_params *dup_params(game_params *params)
150 {
151     game_params *ret = snew(game_params);
152     *ret = *params;                    /* structure copy */
153     return ret;
154 }
155
156 static void decode_params(game_params *params, char const *string)
157 {
158     char const *p = string;
159
160     params->w = atoi(p);
161     while (*p && isdigit((unsigned char)*p)) p++;
162     if (*p == 'x') {
163         p++;
164         params->h = atoi(p);
165         while (*p && isdigit((unsigned char)*p)) p++;
166     } else {
167         params->h = params->w;
168     }
169     if (*p == 'n') {
170         p++;
171         params->n = atoi(p);
172         while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
173     } else {
174         params->n = params->w * params->h / 10;
175     }
176
177     while (*p) {
178         if (*p == 'a') {
179             p++;
180             params->unique = FALSE;
181         } else
182             p++;                       /* skip any other gunk */
183     }
184 }
185
186 static char *encode_params(game_params *params, int full)
187 {
188     char ret[400];
189     int len;
190
191     len = sprintf(ret, "%dx%d", params->w, params->h);
192     /*
193      * Mine count is a generation-time parameter, since it can be
194      * deduced from the mine bitmap!
195      */
196     if (full)
197         len += sprintf(ret+len, "n%d", params->n);
198     if (full && !params->unique)
199         ret[len++] = 'a';
200     assert(len < lenof(ret));
201     ret[len] = '\0';
202
203     return dupstr(ret);
204 }
205
206 static config_item *game_configure(game_params *params)
207 {
208     config_item *ret;
209     char buf[80];
210
211     ret = snewn(5, config_item);
212
213     ret[0].name = "Width";
214     ret[0].type = C_STRING;
215     sprintf(buf, "%d", params->w);
216     ret[0].sval = dupstr(buf);
217     ret[0].ival = 0;
218
219     ret[1].name = "Height";
220     ret[1].type = C_STRING;
221     sprintf(buf, "%d", params->h);
222     ret[1].sval = dupstr(buf);
223     ret[1].ival = 0;
224
225     ret[2].name = "Mines";
226     ret[2].type = C_STRING;
227     sprintf(buf, "%d", params->n);
228     ret[2].sval = dupstr(buf);
229     ret[2].ival = 0;
230
231     ret[3].name = "Ensure solubility";
232     ret[3].type = C_BOOLEAN;
233     ret[3].sval = NULL;
234     ret[3].ival = params->unique;
235
236     ret[4].name = NULL;
237     ret[4].type = C_END;
238     ret[4].sval = NULL;
239     ret[4].ival = 0;
240
241     return ret;
242 }
243
244 static game_params *custom_params(config_item *cfg)
245 {
246     game_params *ret = snew(game_params);
247
248     ret->w = atoi(cfg[0].sval);
249     ret->h = atoi(cfg[1].sval);
250     ret->n = atoi(cfg[2].sval);
251     if (strchr(cfg[2].sval, '%'))
252         ret->n = ret->n * (ret->w * ret->h) / 100;
253     ret->unique = cfg[3].ival;
254
255     return ret;
256 }
257
258 static char *validate_params(game_params *params)
259 {
260     if (params->w <= 0 && params->h <= 0)
261         return "Width and height must both be greater than zero";
262     if (params->w <= 0)
263         return "Width must be greater than zero";
264     if (params->h <= 0)
265         return "Height must be greater than zero";
266
267     /*
268      * FIXME: Need more constraints here. Not sure what the
269      * sensible limits for Minesweeper actually are. The limits
270      * probably ought to change, however, depending on uniqueness.
271      */
272
273     return NULL;
274 }
275
276 /* ----------------------------------------------------------------------
277  * Minesweeper solver, used to ensure the generated grids are
278  * solvable without having to take risks.
279  */
280
281 /*
282  * Count the bits in a word. Only needs to cope with 16 bits.
283  */
284 static int bitcount16(int word)
285 {
286     word = ((word & 0xAAAA) >> 1) + (word & 0x5555);
287     word = ((word & 0xCCCC) >> 2) + (word & 0x3333);
288     word = ((word & 0xF0F0) >> 4) + (word & 0x0F0F);
289     word = ((word & 0xFF00) >> 8) + (word & 0x00FF);
290
291     return word;
292 }
293
294 /*
295  * We use a tree234 to store a large number of small localised
296  * sets, each with a mine count. We also keep some of those sets
297  * linked together into a to-do list.
298  */
299 struct set {
300     short x, y, mask, mines;
301     int todo;
302     struct set *prev, *next;
303 };
304
305 static int setcmp(void *av, void *bv)
306 {
307     struct set *a = (struct set *)av;
308     struct set *b = (struct set *)bv;
309
310     if (a->y < b->y)
311         return -1;
312     else if (a->y > b->y)
313         return +1;
314     else if (a->x < b->x)
315         return -1;
316     else if (a->x > b->x)
317         return +1;
318     else if (a->mask < b->mask)
319         return -1;
320     else if (a->mask > b->mask)
321         return +1;
322     else
323         return 0;
324 }
325
326 struct setstore {
327     tree234 *sets;
328     struct set *todo_head, *todo_tail;
329 };
330
331 static struct setstore *ss_new(void)
332 {
333     struct setstore *ss = snew(struct setstore);
334     ss->sets = newtree234(setcmp);
335     ss->todo_head = ss->todo_tail = NULL;
336     return ss;
337 }
338
339 /*
340  * Take two input sets, in the form (x,y,mask). Munge the first by
341  * taking either its intersection with the second or its difference
342  * with the second. Return the new mask part of the first set.
343  */
344 static int setmunge(int x1, int y1, int mask1, int x2, int y2, int mask2,
345                     int diff)
346 {
347     /*
348      * Adjust the second set so that it has the same x,y
349      * coordinates as the first.
350      */
351     if (abs(x2-x1) >= 3 || abs(y2-y1) >= 3) {
352         mask2 = 0;
353     } else {
354         while (x2 > x1) {
355             mask2 &= ~(4|32|256);
356             mask2 <<= 1;
357             x2--;
358         }
359         while (x2 < x1) {
360             mask2 &= ~(1|8|64);
361             mask2 >>= 1;
362             x2++;
363         }
364         while (y2 > y1) {
365             mask2 &= ~(64|128|256);
366             mask2 <<= 3;
367             y2--;
368         }
369         while (y2 < y1) {
370             mask2 &= ~(1|2|4);
371             mask2 >>= 3;
372             y2++;
373         }
374     }
375
376     /*
377      * Invert the second set if `diff' is set (we're after A &~ B
378      * rather than A & B).
379      */
380     if (diff)
381         mask2 ^= 511;
382
383     /*
384      * Now all that's left is a logical AND.
385      */
386     return mask1 & mask2;
387 }
388
389 static void ss_add_todo(struct setstore *ss, struct set *s)
390 {
391     if (s->todo)
392         return;                        /* already on it */
393
394 #ifdef SOLVER_DIAGNOSTICS
395     printf("adding set on todo list: %d,%d %03x %d\n",
396            s->x, s->y, s->mask, s->mines);
397 #endif
398
399     s->prev = ss->todo_tail;
400     if (s->prev)
401         s->prev->next = s;
402     else
403         ss->todo_head = s;
404     ss->todo_tail = s;
405     s->next = NULL;
406     s->todo = TRUE;
407 }
408
409 static void ss_add(struct setstore *ss, int x, int y, int mask, int mines)
410 {
411     struct set *s;
412
413     assert(mask != 0);
414
415     /*
416      * Normalise so that x and y are genuinely the bounding
417      * rectangle.
418      */
419     while (!(mask & (1|8|64)))
420         mask >>= 1, x++;
421     while (!(mask & (1|2|4)))
422         mask >>= 3, y++;
423
424     /*
425      * Create a set structure and add it to the tree.
426      */
427     s = snew(struct set);
428     s->x = x;
429     s->y = y;
430     s->mask = mask;
431     s->mines = mines;
432     s->todo = FALSE;
433     if (add234(ss->sets, s) != s) {
434         /*
435          * This set already existed! Free it and return.
436          */
437         sfree(s);
438         return;
439     }
440
441     /*
442      * We've added a new set to the tree, so put it on the todo
443      * list.
444      */
445     ss_add_todo(ss, s);
446 }
447
448 static void ss_remove(struct setstore *ss, struct set *s)
449 {
450     struct set *next = s->next, *prev = s->prev;
451
452 #ifdef SOLVER_DIAGNOSTICS
453     printf("removing set %d,%d %03x\n", s->x, s->y, s->mask);
454 #endif
455     /*
456      * Remove s from the todo list.
457      */
458     if (prev)
459         prev->next = next;
460     else if (s == ss->todo_head)
461         ss->todo_head = next;
462
463     if (next)
464         next->prev = prev;
465     else if (s == ss->todo_tail)
466         ss->todo_tail = prev;
467
468     s->todo = FALSE;
469
470     /*
471      * Remove s from the tree.
472      */
473     del234(ss->sets, s);
474
475     /*
476      * Destroy the actual set structure.
477      */
478     sfree(s);
479 }
480
481 /*
482  * Return a dynamically allocated list of all the sets which
483  * overlap a provided input set.
484  */
485 static struct set **ss_overlap(struct setstore *ss, int x, int y, int mask)
486 {
487     struct set **ret = NULL;
488     int nret = 0, retsize = 0;
489     int xx, yy;
490
491     for (xx = x-3; xx < x+3; xx++)
492         for (yy = y-3; yy < y+3; yy++) {
493             struct set stmp, *s;
494             int pos;
495
496             /*
497              * Find the first set with these top left coordinates.
498              */
499             stmp.x = xx;
500             stmp.y = yy;
501             stmp.mask = 0;
502
503             if (findrelpos234(ss->sets, &stmp, NULL, REL234_GE, &pos)) {
504                 while ((s = index234(ss->sets, pos)) != NULL &&
505                        s->x == xx && s->y == yy) {
506                     /*
507                      * This set potentially overlaps the input one.
508                      * Compute the intersection to see if they
509                      * really overlap, and add it to the list if
510                      * so.
511                      */
512                     if (setmunge(x, y, mask, s->x, s->y, s->mask, FALSE)) {
513                         /*
514                          * There's an overlap.
515                          */
516                         if (nret >= retsize) {
517                             retsize = nret + 32;
518                             ret = sresize(ret, retsize, struct set *);
519                         }
520                         ret[nret++] = s;
521                     }
522
523                     pos++;
524                 }
525             }
526         }
527
528     ret = sresize(ret, nret+1, struct set *);
529     ret[nret] = NULL;
530
531     return ret;
532 }
533
534 /*
535  * Get an element from the head of the set todo list.
536  */
537 static struct set *ss_todo(struct setstore *ss)
538 {
539     if (ss->todo_head) {
540         struct set *ret = ss->todo_head;
541         ss->todo_head = ret->next;
542         if (ss->todo_head)
543             ss->todo_head->prev = NULL;
544         else
545             ss->todo_tail = NULL;
546         ret->next = ret->prev = NULL;
547         ret->todo = FALSE;
548         return ret;
549     } else {
550         return NULL;
551     }
552 }
553
554 struct squaretodo {
555     int *next;
556     int head, tail;
557 };
558
559 static void std_add(struct squaretodo *std, int i)
560 {
561     if (std->tail >= 0)
562         std->next[std->tail] = i;
563     else
564         std->head = i;
565     std->tail = i;
566     std->next[i] = -1;
567 }
568
569 static void known_squares(int w, int h, struct squaretodo *std, char *grid,
570                           int (*open)(void *ctx, int x, int y), void *openctx,
571                           int x, int y, int mask, int mine)
572 {
573     int xx, yy, bit;
574
575     bit = 1;
576
577     for (yy = 0; yy < 3; yy++)
578         for (xx = 0; xx < 3; xx++) {
579             if (mask & bit) {
580                 int i = (y + yy) * w + (x + xx);
581
582                 /*
583                  * It's possible that this square is _already_
584                  * known, in which case we don't try to add it to
585                  * the list twice.
586                  */
587                 if (grid[i] == -2) {
588
589                     if (mine) {
590                         grid[i] = -1;   /* and don't open it! */
591                     } else {
592                         grid[i] = open(openctx, x + xx, y + yy);
593                         assert(grid[i] != -1);   /* *bang* */
594                     }
595                     std_add(std, i);
596
597                 }
598             }
599             bit <<= 1;
600         }
601 }
602
603 /*
604  * This is data returned from the `perturb' function. It details
605  * which squares have become mines and which have become clear. The
606  * solver is (of course) expected to honourably not use that
607  * knowledge directly, but to efficently adjust its internal data
608  * structures and proceed based on only the information it
609  * legitimately has.
610  */
611 struct perturbation {
612     int x, y;
613     int delta;                         /* +1 == become a mine; -1 == cleared */
614 };
615 struct perturbations {
616     int n;
617     struct perturbation *changes;
618 };
619
620 /*
621  * Main solver entry point. You give it a grid of existing
622  * knowledge (-1 for a square known to be a mine, 0-8 for empty
623  * squares with a given number of neighbours, -2 for completely
624  * unknown), plus a function which you can call to open new squares
625  * once you're confident of them. It fills in as much more of the
626  * grid as it can.
627  * 
628  * Return value is:
629  * 
630  *  - -1 means deduction stalled and nothing could be done
631  *  - 0 means deduction succeeded fully
632  *  - >0 means deduction succeeded but some number of perturbation
633  *    steps were required; the exact return value is the number of
634  *    perturb calls.
635  */
636 static int minesolve(int w, int h, int n, char *grid,
637                      int (*open)(void *ctx, int x, int y),
638                      struct perturbations *(*perturb)(void *ctx, char *grid,
639                                                       int x, int y, int mask),
640                      void *ctx, random_state *rs)
641 {
642     struct setstore *ss = ss_new();
643     struct set **list;
644     struct squaretodo astd, *std = &astd;
645     int x, y, i, j;
646     int nperturbs = 0;
647
648     /*
649      * Set up a linked list of squares with known contents, so that
650      * we can process them one by one.
651      */
652     std->next = snewn(w*h, int);
653     std->head = std->tail = -1;
654
655     /*
656      * Initialise that list with all known squares in the input
657      * grid.
658      */
659     for (y = 0; y < h; y++) {
660         for (x = 0; x < w; x++) {
661             i = y*w+x;
662             if (grid[i] != -2)
663                 std_add(std, i);
664         }
665     }
666
667     /*
668      * Main deductive loop.
669      */
670     while (1) {
671         int done_something = FALSE;
672         struct set *s;
673
674         /*
675          * If there are any known squares on the todo list, process
676          * them and construct a set for each.
677          */
678         while (std->head != -1) {
679             i = std->head;
680 #ifdef SOLVER_DIAGNOSTICS
681             printf("known square at %d,%d [%d]\n", i%w, i/w, grid[i]);
682 #endif
683             std->head = std->next[i];
684             if (std->head == -1)
685                 std->tail = -1;
686
687             x = i % w;
688             y = i / w;
689
690             if (grid[i] >= 0) {
691                 int dx, dy, mines, bit, val;
692 #ifdef SOLVER_DIAGNOSTICS
693                 printf("creating set around this square\n");
694 #endif
695                 /*
696                  * Empty square. Construct the set of non-known squares
697                  * around this one, and determine its mine count.
698                  */
699                 mines = grid[i];
700                 bit = 1;
701                 val = 0;
702                 for (dy = -1; dy <= +1; dy++) {
703                     for (dx = -1; dx <= +1; dx++) {
704 #ifdef SOLVER_DIAGNOSTICS
705                         printf("grid %d,%d = %d\n", x+dx, y+dy, grid[i+dy*w+dx]);
706 #endif
707                         if (x+dx < 0 || x+dx >= w || y+dy < 0 || y+dy >= h)
708                             /* ignore this one */;
709                         else if (grid[i+dy*w+dx] == -1)
710                             mines--;
711                         else if (grid[i+dy*w+dx] == -2)
712                             val |= bit;
713                         bit <<= 1;
714                     }
715                 }
716                 if (val)
717                     ss_add(ss, x-1, y-1, val, mines);
718             }
719
720             /*
721              * Now, whether the square is empty or full, we must
722              * find any set which contains it and replace it with
723              * one which does not.
724              */
725             {
726 #ifdef SOLVER_DIAGNOSTICS
727                 printf("finding sets containing known square %d,%d\n", x, y);
728 #endif
729                 list = ss_overlap(ss, x, y, 1);
730
731                 for (j = 0; list[j]; j++) {
732                     int newmask, newmines;
733
734                     s = list[j];
735
736                     /*
737                      * Compute the mask for this set minus the
738                      * newly known square.
739                      */
740                     newmask = setmunge(s->x, s->y, s->mask, x, y, 1, TRUE);
741
742                     /*
743                      * Compute the new mine count.
744                      */
745                     newmines = s->mines - (grid[i] == -1);
746
747                     /*
748                      * Insert the new set into the collection,
749                      * unless it's been whittled right down to
750                      * nothing.
751                      */
752                     if (newmask)
753                         ss_add(ss, s->x, s->y, newmask, newmines);
754
755                     /*
756                      * Destroy the old one; it is actually obsolete.
757                      */
758                     ss_remove(ss, s);
759                 }
760
761                 sfree(list);
762             }
763
764             /*
765              * Marking a fresh square as known certainly counts as
766              * doing something.
767              */
768             done_something = TRUE;
769         }
770
771         /*
772          * Now pick a set off the to-do list and attempt deductions
773          * based on it.
774          */
775         if ((s = ss_todo(ss)) != NULL) {
776
777 #ifdef SOLVER_DIAGNOSTICS
778             printf("set to do: %d,%d %03x %d\n", s->x, s->y, s->mask, s->mines);
779 #endif
780             /*
781              * Firstly, see if this set has a mine count of zero or
782              * of its own cardinality.
783              */
784             if (s->mines == 0 || s->mines == bitcount16(s->mask)) {
785                 /*
786                  * If so, we can immediately mark all the squares
787                  * in the set as known.
788                  */
789 #ifdef SOLVER_DIAGNOSTICS
790                 printf("easy\n");
791 #endif
792                 known_squares(w, h, std, grid, open, ctx,
793                               s->x, s->y, s->mask, (s->mines != 0));
794
795                 /*
796                  * Having done that, we need do nothing further
797                  * with this set; marking all the squares in it as
798                  * known will eventually eliminate it, and will
799                  * also permit further deductions about anything
800                  * that overlaps it.
801                  */
802                 continue;
803             }
804
805             /*
806              * Failing that, we now search through all the sets
807              * which overlap this one.
808              */
809             list = ss_overlap(ss, s->x, s->y, s->mask);
810
811             for (j = 0; list[j]; j++) {
812                 struct set *s2 = list[j];
813                 int swing, s2wing, swc, s2wc;
814
815                 /*
816                  * Find the non-overlapping parts s2-s and s-s2,
817                  * and their cardinalities.
818                  * 
819                  * I'm going to refer to these parts as `wings'
820                  * surrounding the central part common to both
821                  * sets. The `s wing' is s-s2; the `s2 wing' is
822                  * s2-s.
823                  */
824                 swing = setmunge(s->x, s->y, s->mask, s2->x, s2->y, s2->mask,
825                                  TRUE);
826                 s2wing = setmunge(s2->x, s2->y, s2->mask, s->x, s->y, s->mask,
827                                  TRUE);
828                 swc = bitcount16(swing);
829                 s2wc = bitcount16(s2wing);
830
831                 /*
832                  * If one set has more mines than the other, and
833                  * the number of extra mines is equal to the
834                  * cardinality of that set's wing, then we can mark
835                  * every square in the wing as a known mine, and
836                  * every square in the other wing as known clear.
837                  */
838                 if (swc == s->mines - s2->mines ||
839                     s2wc == s2->mines - s->mines) {
840                     known_squares(w, h, std, grid, open, ctx,
841                                   s->x, s->y, swing,
842                                   (swc == s->mines - s2->mines));
843                     known_squares(w, h, std, grid, open, ctx,
844                                   s2->x, s2->y, s2wing,
845                                   (s2wc == s2->mines - s->mines));
846                     continue;
847                 }
848
849                 /*
850                  * Failing that, see if one set is a subset of the
851                  * other. If so, we can divide up the mine count of
852                  * the larger set between the smaller set and its
853                  * complement, even if neither smaller set ends up
854                  * being immediately clearable.
855                  */
856                 if (swc == 0 && s2wc != 0) {
857                     /* s is a subset of s2. */
858                     assert(s2->mines > s->mines);
859                     ss_add(ss, s2->x, s2->y, s2wing, s2->mines - s->mines);
860                 } else if (s2wc == 0 && swc != 0) {
861                     /* s2 is a subset of s. */
862                     assert(s->mines > s2->mines);
863                     ss_add(ss, s->x, s->y, swing, s->mines - s2->mines);
864                 }
865             }
866
867             sfree(list);
868
869             /*
870              * In this situation we have definitely done
871              * _something_, even if it's only reducing the size of
872              * our to-do list.
873              */
874             done_something = TRUE;
875         } else if (n >= 0) {
876             /*
877              * We have nothing left on our todo list, which means
878              * all localised deductions have failed. Our next step
879              * is to resort to global deduction based on the total
880              * mine count. This is computationally expensive
881              * compared to any of the above deductions, which is
882              * why we only ever do it when all else fails, so that
883              * hopefully it won't have to happen too often.
884              * 
885              * If you pass n<0 into this solver, that informs it
886              * that you do not know the total mine count, so it
887              * won't even attempt these deductions.
888              */
889
890             int minesleft, squaresleft;
891             int nsets, setused[10], cursor;
892
893             /*
894              * Start by scanning the current grid state to work out
895              * how many unknown squares we still have, and how many
896              * mines are to be placed in them.
897              */
898             squaresleft = 0;
899             minesleft = n;
900             for (i = 0; i < w*h; i++) {
901                 if (grid[i] == -1)
902                     minesleft--;
903                 else if (grid[i] == -2)
904                     squaresleft++;
905             }
906
907 #ifdef SOLVER_DIAGNOSTICS
908             printf("global deduction time: squaresleft=%d minesleft=%d\n",
909                    squaresleft, minesleft);
910             for (y = 0; y < h; y++) {
911                 for (x = 0; x < w; x++) {
912                     int v = grid[y*w+x];
913                     if (v == -1)
914                         putchar('*');
915                     else if (v == -2)
916                         putchar('?');
917                     else if (v == 0)
918                         putchar('-');
919                     else
920                         putchar('0' + v);
921                 }
922                 putchar('\n');
923             }
924 #endif
925
926             /*
927              * If there _are_ no unknown squares, we have actually
928              * finished.
929              */
930             if (squaresleft == 0) {
931                 assert(minesleft == 0);
932                 break;
933             }
934
935             /*
936              * First really simple case: if there are no more mines
937              * left, or if there are exactly as many mines left as
938              * squares to play them in, then it's all easy.
939              */
940             if (minesleft == 0 || minesleft == squaresleft) {
941                 for (i = 0; i < w*h; i++)
942                     if (grid[i] == -2)
943                         known_squares(w, h, std, grid, open, ctx,
944                                       i % w, i / w, 1, minesleft != 0);
945                 continue;              /* now go back to main deductive loop */
946             }
947
948             /*
949              * Failing that, we have to do some _real_ work.
950              * Ideally what we do here is to try every single
951              * combination of the currently available sets, in an
952              * attempt to find a disjoint union (i.e. a set of
953              * squares with a known mine count between them) such
954              * that the remaining unknown squares _not_ contained
955              * in that union either contain no mines or are all
956              * mines.
957              * 
958              * Actually enumerating all 2^n possibilities will get
959              * a bit slow for large n, so I artificially cap this
960              * recursion at n=10 to avoid too much pain.
961              */
962             nsets = count234(ss->sets);
963             if (nsets <= lenof(setused)) {
964                 /*
965                  * Doing this with actual recursive function calls
966                  * would get fiddly because a load of local
967                  * variables from this function would have to be
968                  * passed down through the recursion. So instead
969                  * I'm going to use a virtual recursion within this
970                  * function. The way this works is:
971                  * 
972                  *  - we have an array `setused', such that
973                  *    setused[n] is 0 or 1 depending on whether set
974                  *    n is currently in the union we are
975                  *    considering.
976                  * 
977                  *  - we have a value `cursor' which indicates how
978                  *    much of `setused' we have so far filled in.
979                  *    It's conceptually the recursion depth.
980                  * 
981                  * We begin by setting `cursor' to zero. Then:
982                  * 
983                  *  - if cursor can advance, we advance it by one.
984                  *    We set the value in `setused' that it went
985                  *    past to 1 if that set is disjoint from
986                  *    anything else currently in `setused', or to 0
987                  *    otherwise.
988                  * 
989                  *  - If cursor cannot advance because it has
990                  *    reached the end of the setused list, then we
991                  *    have a maximal disjoint union. Check to see
992                  *    whether its mine count has any useful
993                  *    properties. If so, mark all the squares not
994                  *    in the union as known and terminate.
995                  * 
996                  *  - If cursor has reached the end of setused and
997                  *    the algorithm _hasn't_ terminated, back
998                  *    cursor up to the nearest 1, turn it into a 0
999                  *    and advance cursor just past it.
1000                  * 
1001                  *  - If we attempt to back up to the nearest 1 and
1002                  *    there isn't one at all, then we have gone
1003                  *    through all disjoint unions of sets in the
1004                  *    list and none of them has been helpful, so we
1005                  *    give up.
1006                  */
1007                 struct set *sets[lenof(setused)];
1008                 for (i = 0; i < nsets; i++)
1009                     sets[i] = index234(ss->sets, i);
1010
1011                 cursor = 0;
1012                 while (1) {
1013
1014                     if (cursor < nsets) {
1015                         int ok = TRUE;
1016
1017                         /* See if any existing set overlaps this one. */
1018                         for (i = 0; i < cursor; i++)
1019                             if (setused[i] &&
1020                                 setmunge(sets[cursor]->x,
1021                                          sets[cursor]->y,
1022                                          sets[cursor]->mask,
1023                                          sets[i]->x, sets[i]->y, sets[i]->mask,
1024                                          FALSE)) {
1025                                 ok = FALSE;
1026                                 break;
1027                             }
1028
1029                         if (ok) {
1030                             /*
1031                              * We're adding this set to our union,
1032                              * so adjust minesleft and squaresleft
1033                              * appropriately.
1034                              */
1035                             minesleft -= sets[cursor]->mines;
1036                             squaresleft -= bitcount16(sets[cursor]->mask);
1037                         }
1038
1039                         setused[cursor++] = ok;
1040                     } else {
1041 #ifdef SOLVER_DIAGNOSTICS
1042                         printf("trying a set combination with %d %d\n",
1043                                squaresleft, minesleft);
1044 #endif /* SOLVER_DIAGNOSTICS */
1045
1046                         /*
1047                          * We've reached the end. See if we've got
1048                          * anything interesting.
1049                          */
1050                         if (squaresleft > 0 &&
1051                             (minesleft == 0 || minesleft == squaresleft)) {
1052                             /*
1053                              * We have! There is at least one
1054                              * square not contained within the set
1055                              * union we've just found, and we can
1056                              * deduce that either all such squares
1057                              * are mines or all are not (depending
1058                              * on whether minesleft==0). So now all
1059                              * we have to do is actually go through
1060                              * the grid, find those squares, and
1061                              * mark them.
1062                              */
1063                             for (i = 0; i < w*h; i++)
1064                                 if (grid[i] == -2) {
1065                                     int outside = TRUE;
1066                                     y = i / w;
1067                                     x = i % w;
1068                                     for (j = 0; j < nsets; j++)
1069                                         if (setused[j] &&
1070                                             setmunge(sets[j]->x, sets[j]->y,
1071                                                      sets[j]->mask, x, y, 1,
1072                                                      FALSE)) {
1073                                             outside = FALSE;
1074                                             break;
1075                                         }
1076                                     if (outside)
1077                                         known_squares(w, h, std, grid,
1078                                                       open, ctx,
1079                                                       x, y, 1, minesleft != 0);
1080                                 }
1081
1082                             done_something = TRUE;
1083                             break;     /* return to main deductive loop */
1084                         }
1085
1086                         /*
1087                          * If we reach here, then this union hasn't
1088                          * done us any good, so move on to the
1089                          * next. Backtrack cursor to the nearest 1,
1090                          * change it to a 0 and continue.
1091                          */
1092                         while (cursor-- >= 0 && !setused[cursor]);
1093                         if (cursor >= 0) {
1094                             assert(setused[cursor]);
1095
1096                             /*
1097                              * We're removing this set from our
1098                              * union, so re-increment minesleft and
1099                              * squaresleft.
1100                              */
1101                             minesleft += sets[cursor]->mines;
1102                             squaresleft += bitcount16(sets[cursor]->mask);
1103
1104                             setused[cursor++] = 0;
1105                         } else {
1106                             /*
1107                              * We've backtracked all the way to the
1108                              * start without finding a single 1,
1109                              * which means that our virtual
1110                              * recursion is complete and nothing
1111                              * helped.
1112                              */
1113                             break;
1114                         }
1115                     }
1116
1117                 }
1118
1119             }
1120         }
1121
1122         if (done_something)
1123             continue;
1124
1125 #ifdef SOLVER_DIAGNOSTICS
1126         /*
1127          * Dump the current known state of the grid.
1128          */
1129         printf("solver ran out of steam, ret=%d, grid:\n", nperturbs);
1130         for (y = 0; y < h; y++) {
1131             for (x = 0; x < w; x++) {
1132                 int v = grid[y*w+x];
1133                 if (v == -1)
1134                     putchar('*');
1135                 else if (v == -2)
1136                     putchar('?');
1137                 else if (v == 0)
1138                     putchar('-');
1139                 else
1140                     putchar('0' + v);
1141             }
1142             putchar('\n');
1143         }
1144
1145         {
1146             struct set *s;
1147
1148             for (i = 0; (s = index234(ss->sets, i)) != NULL; i++)
1149                 printf("remaining set: %d,%d %03x %d\n", s->x, s->y, s->mask, s->mines);
1150         }
1151 #endif
1152
1153         /*
1154          * Now we really are at our wits' end as far as solving
1155          * this grid goes. Our only remaining option is to call
1156          * a perturb function and ask it to modify the grid to
1157          * make it easier.
1158          */
1159         if (perturb) {
1160             struct perturbations *ret;
1161             struct set *s;
1162
1163             nperturbs++;
1164
1165             /*
1166              * Choose a set at random from the current selection,
1167              * and ask the perturb function to either fill or empty
1168              * it.
1169              * 
1170              * If we have no sets at all, we must give up.
1171              */
1172             if (count234(ss->sets) == 0)
1173                 break;
1174             s = index234(ss->sets, random_upto(rs, count234(ss->sets)));
1175 #ifdef SOLVER_DIAGNOSTICS
1176             printf("perturbing on set %d,%d %03x\n", s->x, s->y, s->mask);
1177 #endif
1178             ret = perturb(ctx, grid, s->x, s->y, s->mask);
1179
1180             if (ret) {
1181                 assert(ret->n > 0);    /* otherwise should have been NULL */
1182
1183                 /*
1184                  * A number of squares have been fiddled with, and
1185                  * the returned structure tells us which. Adjust
1186                  * the mine count in any set which overlaps one of
1187                  * those squares, and put them back on the to-do
1188                  * list.
1189                  */
1190                 for (i = 0; i < ret->n; i++) {
1191 #ifdef SOLVER_DIAGNOSTICS
1192                     printf("perturbation %s mine at %d,%d\n",
1193                            ret->changes[i].delta > 0 ? "added" : "removed",
1194                            ret->changes[i].x, ret->changes[i].y);
1195 #endif
1196
1197                     list = ss_overlap(ss,
1198                                       ret->changes[i].x, ret->changes[i].y, 1);
1199
1200                     for (j = 0; list[j]; j++) {
1201                         list[j]->mines += ret->changes[i].delta;
1202                         ss_add_todo(ss, list[j]);
1203                     }
1204
1205                     sfree(list);
1206                 }
1207
1208                 /*
1209                  * Now free the returned data.
1210                  */
1211                 sfree(ret->changes);
1212                 sfree(ret);
1213
1214 #ifdef SOLVER_DIAGNOSTICS
1215                 /*
1216                  * Dump the current known state of the grid.
1217                  */
1218                 printf("state after perturbation:\n", nperturbs);
1219                 for (y = 0; y < h; y++) {
1220                     for (x = 0; x < w; x++) {
1221                         int v = grid[y*w+x];
1222                         if (v == -1)
1223                             putchar('*');
1224                         else if (v == -2)
1225                             putchar('?');
1226                         else if (v == 0)
1227                             putchar('-');
1228                         else
1229                             putchar('0' + v);
1230                     }
1231                     putchar('\n');
1232                 }
1233
1234                 {
1235                     struct set *s;
1236
1237                     for (i = 0; (s = index234(ss->sets, i)) != NULL; i++)
1238                         printf("remaining set: %d,%d %03x %d\n", s->x, s->y, s->mask, s->mines);
1239                 }
1240 #endif
1241
1242                 /*
1243                  * And now we can go back round the deductive loop.
1244                  */
1245                 continue;
1246             }
1247         }
1248
1249         /*
1250          * If we get here, even that didn't work (either we didn't
1251          * have a perturb function or it returned failure), so we
1252          * give up entirely.
1253          */
1254         break;
1255     }
1256
1257     /*
1258      * See if we've got any unknown squares left.
1259      */
1260     for (y = 0; y < h; y++)
1261         for (x = 0; x < w; x++)
1262             if (grid[y*w+x] == -2) {
1263                 nperturbs = -1;        /* failed to complete */
1264                 break;
1265             }
1266
1267     /*
1268      * Free the set list and square-todo list.
1269      */
1270     {
1271         struct set *s;
1272         while ((s = delpos234(ss->sets, 0)) != NULL)
1273             sfree(s);
1274         freetree234(ss->sets);
1275         sfree(ss);
1276         sfree(std->next);
1277     }
1278
1279     return nperturbs;
1280 }
1281
1282 /* ----------------------------------------------------------------------
1283  * Grid generator which uses the above solver.
1284  */
1285
1286 struct minectx {
1287     char *grid;
1288     int w, h;
1289     int sx, sy;
1290     random_state *rs;
1291 };
1292
1293 static int mineopen(void *vctx, int x, int y)
1294 {
1295     struct minectx *ctx = (struct minectx *)vctx;
1296     int i, j, n;
1297
1298     assert(x >= 0 && x < ctx->w && y >= 0 && y < ctx->h);
1299     if (ctx->grid[y * ctx->w + x])
1300         return -1;                     /* *bang* */
1301
1302     n = 0;
1303     for (i = -1; i <= +1; i++) {
1304         if (x + i < 0 || x + i >= ctx->w)
1305             continue;
1306         for (j = -1; j <= +1; j++) {
1307             if (y + j < 0 || y + j >= ctx->h)
1308                 continue;
1309             if (i == 0 && j == 0)
1310                 continue;
1311             if (ctx->grid[(y+j) * ctx->w + (x+i)])
1312                 n++;
1313         }
1314     }
1315
1316     return n;
1317 }
1318
1319 /* Structure used internally to mineperturb(). */
1320 struct square {
1321     int x, y, type, random;
1322 };
1323 static int squarecmp(const void *av, const void *bv)
1324 {
1325     const struct square *a = (const struct square *)av;
1326     const struct square *b = (const struct square *)bv;
1327     if (a->type < b->type)
1328         return -1;
1329     else if (a->type > b->type)
1330         return +1;
1331     else if (a->random < b->random)
1332         return -1;
1333     else if (a->random > b->random)
1334         return +1;
1335     else if (a->y < b->y)
1336         return -1;
1337     else if (a->y > b->y)
1338         return +1;
1339     else if (a->x < b->x)
1340         return -1;
1341     else if (a->x > b->x)
1342         return +1;
1343     return 0;
1344 }
1345
1346 static struct perturbations *mineperturb(void *vctx, char *grid,
1347                                          int setx, int sety, int mask)
1348 {
1349     struct minectx *ctx = (struct minectx *)vctx;
1350     struct square *sqlist;
1351     int x, y, dx, dy, i, n, nfull, nempty;
1352     struct square *tofill[9], *toempty[9], **todo;
1353     int ntofill, ntoempty, ntodo, dtodo, dset;
1354     struct perturbations *ret;
1355
1356     /*
1357      * Make a list of all the squares in the grid which we can
1358      * possibly use. This list should be in preference order, which
1359      * means
1360      * 
1361      *  - first, unknown squares on the boundary of known space
1362      *  - next, unknown squares beyond that boundary
1363      *  - as a very last resort, known squares, but not within one
1364      *    square of the starting position.
1365      * 
1366      * Each of these sections needs to be shuffled independently.
1367      * We do this by preparing list of all squares and then sorting
1368      * it with a random secondary key.
1369      */
1370     sqlist = snewn(ctx->w * ctx->h, struct square);
1371     n = 0;
1372     for (y = 0; y < ctx->h; y++)
1373         for (x = 0; x < ctx->w; x++) {
1374             /*
1375              * If this square is too near the starting position,
1376              * don't put it on the list at all.
1377              */
1378             if (abs(y - ctx->sy) <= 1 && abs(x - ctx->sx) <= 1)
1379                 continue;
1380
1381             /*
1382              * If this square is in the input set, also don't put
1383              * it on the list!
1384              */
1385             if (x >= setx && x < setx + 3 &&
1386                 y >= sety && y < sety + 3 &&
1387                 mask & (1 << ((y-sety)*3+(x-setx))))
1388                 continue;
1389
1390             sqlist[n].x = x;
1391             sqlist[n].y = y;
1392
1393             if (grid[y*ctx->w+x] != -2) {
1394                 sqlist[n].type = 3;    /* known square */
1395             } else {
1396                 /*
1397                  * Unknown square. Examine everything around it and
1398                  * see if it borders on any known squares. If it
1399                  * does, it's class 1, otherwise it's 2.
1400                  */
1401
1402                 sqlist[n].type = 2;
1403
1404                 for (dy = -1; dy <= +1; dy++)
1405                     for (dx = -1; dx <= +1; dx++)
1406                         if (x+dx >= 0 && x+dx < ctx->w &&
1407                             y+dy >= 0 && y+dy < ctx->h &&
1408                             grid[(y+dy)*ctx->w+(x+dx)] != -2) {
1409                             sqlist[n].type = 1;
1410                             break;
1411                         }
1412             }
1413
1414             /*
1415              * Finally, a random number to cause qsort to
1416              * shuffle within each group.
1417              */
1418             sqlist[n].random = random_bits(ctx->rs, 31);
1419
1420             n++;
1421         }
1422
1423     qsort(sqlist, n, sizeof(struct square), squarecmp);
1424
1425     /*
1426      * Now count up the number of full and empty squares in the set
1427      * we've been provided.
1428      */
1429     nfull = nempty = 0;
1430     for (dy = 0; dy < 3; dy++)
1431         for (dx = 0; dx < 3; dx++)
1432             if (mask & (1 << (dy*3+dx))) {
1433                 assert(setx+dx <= ctx->w);
1434                 assert(sety+dy <= ctx->h);
1435                 if (ctx->grid[(sety+dy)*ctx->w+(setx+dx)])
1436                     nfull++;
1437                 else
1438                     nempty++;
1439             }
1440
1441     /*
1442      * Now go through our sorted list until we find either `nfull'
1443      * empty squares, or `nempty' full squares; these will be
1444      * swapped with the appropriate squares in the set to either
1445      * fill or empty the set while keeping the same number of mines
1446      * overall.
1447      */
1448     ntofill = ntoempty = 0;
1449     for (i = 0; i < n; i++) {
1450         struct square *sq = &sqlist[i];
1451         if (ctx->grid[sq->y * ctx->w + sq->x])
1452             toempty[ntoempty++] = sq;
1453         else
1454             tofill[ntofill++] = sq;
1455         if (ntofill == nfull || ntoempty == nempty)
1456             break;
1457     }
1458
1459     /*
1460      * If this didn't work at all, I think we just give up.
1461      */
1462     if (ntofill != nfull && ntoempty != nempty) {
1463         sfree(sqlist);
1464         return NULL;
1465     }
1466
1467     /*
1468      * Now we're pretty much there. We need to either
1469      *  (a) put a mine in each of the empty squares in the set, and
1470      *      take one out of each square in `toempty'
1471      *  (b) take a mine out of each of the full squares in the set,
1472      *      and put one in each square in `tofill'
1473      * depending on which one we've found enough squares to do.
1474      * 
1475      * So we start by constructing our list of changes to return to
1476      * the solver, so that it can update its data structures
1477      * efficiently rather than having to rescan the whole grid.
1478      */
1479     ret = snew(struct perturbations);
1480     if (ntofill == nfull) {
1481         todo = tofill;
1482         ntodo = ntofill;
1483         dtodo = +1;
1484         dset = -1;
1485     } else {
1486         todo = toempty;
1487         ntodo = ntoempty;
1488         dtodo = -1;
1489         dset = +1;
1490     }
1491     ret->n = 2 * ntodo;
1492     ret->changes = snewn(ret->n, struct perturbation);
1493     for (i = 0; i < ntodo; i++) {
1494         ret->changes[i].x = todo[i]->x;
1495         ret->changes[i].y = todo[i]->y;
1496         ret->changes[i].delta = dtodo;
1497     }
1498     /* now i == ntodo */
1499     for (dy = 0; dy < 3; dy++)
1500         for (dx = 0; dx < 3; dx++)
1501             if (mask & (1 << (dy*3+dx))) {
1502                 int currval = (ctx->grid[(sety+dy)*ctx->w+(setx+dx)] ? +1 : -1);
1503                 if (dset == -currval) {
1504                     ret->changes[i].x = setx + dx;
1505                     ret->changes[i].y = sety + dy;
1506                     ret->changes[i].delta = dset;
1507                     i++;
1508                 }
1509             }
1510     assert(i == ret->n);
1511
1512     sfree(sqlist);
1513
1514     /*
1515      * Having set up the precise list of changes we're going to
1516      * make, we now simply make them and return.
1517      */
1518     for (i = 0; i < ret->n; i++) {
1519         int delta;
1520
1521         x = ret->changes[i].x;
1522         y = ret->changes[i].y;
1523         delta = ret->changes[i].delta;
1524
1525         /*
1526          * Check we're not trying to add an existing mine or remove
1527          * an absent one.
1528          */
1529         assert((delta < 0) ^ (ctx->grid[y*ctx->w+x] == 0));
1530
1531         /*
1532          * Actually make the change.
1533          */
1534         ctx->grid[y*ctx->w+x] = (delta > 0);
1535
1536         /*
1537          * Update any numbers already present in the grid.
1538          */
1539         for (dy = -1; dy <= +1; dy++)
1540             for (dx = -1; dx <= +1; dx++)
1541                 if (x+dx >= 0 && x+dx < ctx->w &&
1542                     y+dy >= 0 && y+dy < ctx->h &&
1543                     grid[(y+dy)*ctx->w+(x+dx)] != -2) {
1544                     if (dx == 0 && dy == 0) {
1545                         /*
1546                          * The square itself is marked as known in
1547                          * the grid. Mark it as a mine if it's a
1548                          * mine, or else work out its number.
1549                          */
1550                         if (delta > 0) {
1551                             grid[y*ctx->w+x] = -1;
1552                         } else {
1553                             int dx2, dy2, minecount = 0;
1554                             for (dy2 = -1; dy2 <= +1; dy2++)
1555                                 for (dx2 = -1; dx2 <= +1; dx2++)
1556                                     if (x+dx2 >= 0 && x+dx2 < ctx->w &&
1557                                         y+dy2 >= 0 && y+dy2 < ctx->h &&
1558                                         ctx->grid[(y+dy2)*ctx->w+(x+dx2)])
1559                                         minecount++;
1560                             grid[y*ctx->w+x] = minecount;
1561                         }
1562                     } else {
1563                         if (grid[(y+dy)*ctx->w+(x+dx)] >= 0)
1564                             grid[(y+dy)*ctx->w+(x+dx)] += delta;
1565                     }
1566                 }
1567     }
1568
1569 #ifdef GENERATION_DIAGNOSTICS
1570     {
1571         int yy, xx;
1572         printf("grid after perturbing:\n");
1573         for (yy = 0; yy < ctx->h; yy++) {
1574             for (xx = 0; xx < ctx->w; xx++) {
1575                 int v = ctx->grid[yy*ctx->w+xx];
1576                 if (yy == ctx->sy && xx == ctx->sx) {
1577                     assert(!v);
1578                     putchar('S');
1579                 } else if (v) {
1580                     putchar('*');
1581                 } else {
1582                     putchar('-');
1583                 }
1584             }
1585             putchar('\n');
1586         }
1587         printf("\n");
1588     }
1589 #endif
1590
1591     return ret;
1592 }
1593
1594 static char *minegen(int w, int h, int n, int x, int y, int unique,
1595                      random_state *rs)
1596 {
1597     char *ret = snewn(w*h, char);
1598     int success;
1599
1600     do {
1601         success = FALSE;
1602
1603         memset(ret, 0, w*h);
1604
1605         /*
1606          * Start by placing n mines, none of which is at x,y or within
1607          * one square of it.
1608          */
1609         {
1610             int *tmp = snewn(w*h, int);
1611             int i, j, k, nn;
1612
1613             /*
1614              * Write down the list of possible mine locations.
1615              */
1616             k = 0;
1617             for (i = 0; i < h; i++)
1618                 for (j = 0; j < w; j++)
1619                     if (abs(i - y) > 1 || abs(j - x) > 1)
1620                         tmp[k++] = i*w+j;
1621
1622             /*
1623              * Now pick n off the list at random.
1624              */
1625             nn = n;
1626             while (nn-- > 0) {
1627                 i = random_upto(rs, k);
1628                 ret[tmp[i]] = 1;
1629                 tmp[i] = tmp[--k];
1630             }
1631
1632             sfree(tmp);
1633         }
1634
1635 #ifdef GENERATION_DIAGNOSTICS
1636         {
1637             int yy, xx;
1638             printf("grid after initial generation:\n");
1639             for (yy = 0; yy < h; yy++) {
1640                 for (xx = 0; xx < w; xx++) {
1641                     int v = ret[yy*w+xx];
1642                     if (yy == y && xx == x) {
1643                         assert(!v);
1644                         putchar('S');
1645                     } else if (v) {
1646                         putchar('*');
1647                     } else {
1648                         putchar('-');
1649                     }
1650                 }
1651                 putchar('\n');
1652             }
1653             printf("\n");
1654         }
1655 #endif
1656
1657         /*
1658          * Now set up a results grid to run the solver in, and a
1659          * context for the solver to open squares. Then run the solver
1660          * repeatedly; if the number of perturb steps ever goes up or
1661          * it ever returns -1, give up completely.
1662          *
1663          * We bypass this bit if we're not after a unique grid.
1664          */
1665         if (unique) {
1666             char *solvegrid = snewn(w*h, char);
1667             struct minectx actx, *ctx = &actx;
1668             int solveret, prevret = -2;
1669
1670             ctx->grid = ret;
1671             ctx->w = w;
1672             ctx->h = h;
1673             ctx->sx = x;
1674             ctx->sy = y;
1675             ctx->rs = rs;
1676
1677             while (1) {
1678                 memset(solvegrid, -2, w*h);
1679                 solvegrid[y*w+x] = mineopen(ctx, x, y);
1680                 assert(solvegrid[y*w+x] == 0); /* by deliberate arrangement */
1681
1682                 solveret =
1683                     minesolve(w, h, n, solvegrid, mineopen, mineperturb, ctx, rs);
1684                 if (solveret < 0 || (prevret >= 0 && solveret >= prevret)) {
1685                     success = FALSE;
1686                     break;
1687                 } else if (solveret == 0) {
1688                     success = TRUE;
1689                     break;
1690                 }
1691             }
1692
1693             sfree(solvegrid);
1694         } else {
1695             success = TRUE;
1696         }
1697
1698     } while (!success);
1699
1700     return ret;
1701 }
1702
1703 /*
1704  * The Mines game descriptions contain the location of every mine,
1705  * and can therefore be used to cheat.
1706  * 
1707  * It would be pointless to attempt to _prevent_ this form of
1708  * cheating by encrypting the description, since Mines is
1709  * open-source so anyone can find out the encryption key. However,
1710  * I think it is worth doing a bit of gentle obfuscation to prevent
1711  * _accidental_ spoilers: if you happened to note that the game ID
1712  * starts with an F, for example, you might be unable to put the
1713  * knowledge of those mines out of your mind while playing. So,
1714  * just as discussions of film endings are rot13ed to avoid
1715  * spoiling it for people who don't want to be told, we apply a
1716  * keyless, reversible, but visually completely obfuscatory masking
1717  * function to the mine bitmap.
1718  */
1719 static void obfuscate_bitmap(unsigned char *bmp, int bits, int decode)
1720 {
1721     int bytes, firsthalf, secondhalf;
1722     struct step {
1723         unsigned char *seedstart;
1724         int seedlen;
1725         unsigned char *targetstart;
1726         int targetlen;
1727     } steps[2];
1728     int i, j;
1729
1730     /*
1731      * My obfuscation algorithm is similar in concept to the OAEP
1732      * encoding used in some forms of RSA. Here's a specification
1733      * of it:
1734      * 
1735      *  + We have a `masking function' which constructs a stream of
1736      *    pseudorandom bytes from a seed of some number of input
1737      *    bytes.
1738      * 
1739      *  + We pad out our input bit stream to a whole number of
1740      *    bytes by adding up to 7 zero bits on the end. (In fact
1741      *    the bitmap passed as input to this function will already
1742      *    have had this done in practice.)
1743      * 
1744      *  + We divide the _byte_ stream exactly in half, rounding the
1745      *    half-way position _down_. So an 81-bit input string, for
1746      *    example, rounds up to 88 bits or 11 bytes, and then
1747      *    dividing by two gives 5 bytes in the first half and 6 in
1748      *    the second half.
1749      * 
1750      *  + We generate a mask from the second half of the bytes, and
1751      *    XOR it over the first half.
1752      * 
1753      *  + We generate a mask from the (encoded) first half of the
1754      *    bytes, and XOR it over the second half. Any null bits at
1755      *    the end which were added as padding are cleared back to
1756      *    zero even if this operation would have made them nonzero.
1757      * 
1758      * To de-obfuscate, the steps are precisely the same except
1759      * that the final two are reversed.
1760      * 
1761      * Finally, our masking function. Given an input seed string of
1762      * bytes, the output mask consists of concatenating the SHA-1
1763      * hashes of the seed string and successive decimal integers,
1764      * starting from 0.
1765      */
1766
1767     bytes = (bits + 7) / 8;
1768     firsthalf = bytes / 2;
1769     secondhalf = bytes - firsthalf;
1770
1771     steps[decode ? 1 : 0].seedstart = bmp + firsthalf;
1772     steps[decode ? 1 : 0].seedlen = secondhalf;
1773     steps[decode ? 1 : 0].targetstart = bmp;
1774     steps[decode ? 1 : 0].targetlen = firsthalf;
1775
1776     steps[decode ? 0 : 1].seedstart = bmp;
1777     steps[decode ? 0 : 1].seedlen = firsthalf;
1778     steps[decode ? 0 : 1].targetstart = bmp + firsthalf;
1779     steps[decode ? 0 : 1].targetlen = secondhalf;
1780
1781     for (i = 0; i < 2; i++) {
1782         SHA_State base, final;
1783         unsigned char digest[20];
1784         char numberbuf[80];
1785         int digestpos = 20, counter = 0;
1786
1787         SHA_Init(&base);
1788         SHA_Bytes(&base, steps[i].seedstart, steps[i].seedlen);
1789
1790         for (j = 0; j < steps[i].targetlen; j++) {
1791             if (digestpos >= 20) {
1792                 sprintf(numberbuf, "%d", counter++);
1793                 final = base;
1794                 SHA_Bytes(&final, numberbuf, strlen(numberbuf));
1795                 SHA_Final(&final, digest);
1796                 digestpos = 0;
1797             }
1798             steps[i].targetstart[j] ^= digest[digestpos]++;
1799         }
1800
1801         /*
1802          * Mask off the pad bits in the final byte after both steps.
1803          */
1804         if (bits % 8)
1805             bmp[bits / 8] &= 0xFF & (0xFF00 >> (bits % 8));
1806     }
1807 }
1808
1809 static char *new_mine_layout(int w, int h, int n, int x, int y, int unique,
1810                              random_state *rs, char **game_desc)
1811 {
1812     char *grid, *ret, *p;
1813     unsigned char *bmp;
1814     int i, area;
1815
1816     grid = minegen(w, h, n, x, y, unique, rs);
1817
1818     if (game_desc) {
1819         /*
1820          * Set up the mine bitmap and obfuscate it.
1821          */
1822         area = w * h;
1823         bmp = snewn((area + 7) / 8, unsigned char);
1824         memset(bmp, 0, (area + 7) / 8);
1825         for (i = 0; i < area; i++) {
1826             if (grid[i])
1827                 bmp[i / 8] |= 0x80 >> (i % 8);
1828         }
1829         obfuscate_bitmap(bmp, area, FALSE);
1830
1831         /*
1832          * Now encode the resulting bitmap in hex. We can work to
1833          * nibble rather than byte granularity, since the obfuscation
1834          * function guarantees to return a bit string of the same
1835          * length as its input.
1836          */
1837         ret = snewn((area+3)/4 + 100, char);
1838         p = ret + sprintf(ret, "%d,%d,m", x, y);   /* 'm' == masked */
1839         for (i = 0; i < (area+3)/4; i++) {
1840             int v = bmp[i/2];
1841             if (i % 2 == 0)
1842                 v >>= 4;
1843             *p++ = "0123456789abcdef"[v & 0xF];
1844         }
1845         *p = '\0';
1846
1847         sfree(bmp);
1848
1849         *game_desc = ret;
1850     }   
1851
1852     return grid;
1853 }
1854
1855 static char *new_game_desc(game_params *params, random_state *rs,
1856                            game_aux_info **aux)
1857 {
1858 #ifdef PREOPENED
1859     int x = random_upto(rs, params->w);
1860     int y = random_upto(rs, params->h);
1861     char *grid, *desc;
1862
1863     grid = new_mine_layout(params->w, params->h, params->n,
1864                            x, y, params->unique, rs);
1865 #else
1866     char *rsdesc, *desc;
1867
1868     rsdesc = random_state_encode(rs);
1869     desc = snewn(strlen(rsdesc) + 100, char);
1870     sprintf(desc, "r%d,%c,%s", params->n, params->unique ? 'u' : 'a', rsdesc);
1871     sfree(rsdesc);
1872     return desc;
1873 #endif
1874 }
1875
1876 static void game_free_aux_info(game_aux_info *aux)
1877 {
1878     assert(!"Shouldn't happen");
1879 }
1880
1881 static char *validate_desc(game_params *params, char *desc)
1882 {
1883     int wh = params->w * params->h;
1884     int x, y;
1885
1886     if (*desc == 'r') {
1887         if (!*desc || !isdigit((unsigned char)*desc))
1888             return "No initial mine count in game description";
1889         while (*desc && isdigit((unsigned char)*desc))
1890             desc++;                    /* skip over mine count */
1891         if (*desc != ',')
1892             return "No ',' after initial x-coordinate in game description";
1893         desc++;
1894         if (*desc != 'u' && *desc != 'a')
1895             return "No uniqueness specifier in game description";
1896         desc++;
1897         if (*desc != ',')
1898             return "No ',' after uniqueness specifier in game description";
1899         /* now ignore the rest */
1900     } else {
1901         if (!*desc || !isdigit((unsigned char)*desc))
1902             return "No initial x-coordinate in game description";
1903         x = atoi(desc);
1904         if (x < 0 || x >= params->w)
1905             return "Initial x-coordinate was out of range";
1906         while (*desc && isdigit((unsigned char)*desc))
1907             desc++;                    /* skip over x coordinate */
1908         if (*desc != ',')
1909             return "No ',' after initial x-coordinate in game description";
1910         desc++;                        /* eat comma */
1911         if (!*desc || !isdigit((unsigned char)*desc))
1912             return "No initial y-coordinate in game description";
1913         y = atoi(desc);
1914         if (y < 0 || y >= params->h)
1915             return "Initial y-coordinate was out of range";
1916         while (*desc && isdigit((unsigned char)*desc))
1917             desc++;                    /* skip over y coordinate */
1918         if (*desc != ',')
1919             return "No ',' after initial y-coordinate in game description";
1920         desc++;                        /* eat comma */
1921         /* eat `m', meaning `masked', if present */
1922         if (*desc == 'm')
1923             desc++;
1924         /* now just check length of remainder */
1925         if (strlen(desc) != (wh+3)/4)
1926             return "Game description is wrong length";
1927     }
1928
1929     return NULL;
1930 }
1931
1932 static int open_square(game_state *state, int x, int y)
1933 {
1934     int w = state->w, h = state->h;
1935     int xx, yy, nmines, ncovered;
1936
1937     if (!state->layout->mines) {
1938         /*
1939          * We have a preliminary game in which the mine layout
1940          * hasn't been generated yet. Generate it based on the
1941          * initial click location.
1942          */
1943         char *desc;
1944         state->layout->mines = new_mine_layout(w, h, state->layout->n,
1945                                                x, y, state->layout->unique,
1946                                                state->layout->rs,
1947                                                &desc);
1948         midend_supersede_game_desc(state->layout->me, desc);
1949         sfree(desc);
1950         random_free(state->layout->rs);
1951         state->layout->rs = NULL;
1952     }
1953
1954     if (state->layout->mines[y*w+x]) {
1955         /*
1956          * The player has landed on a mine. Bad luck. Expose all
1957          * the mines.
1958          */
1959         state->dead = TRUE;
1960         for (yy = 0; yy < h; yy++)
1961             for (xx = 0; xx < w; xx++) {
1962                 if (state->layout->mines[yy*w+xx] &&
1963                     (state->grid[yy*w+xx] == -2 ||
1964                      state->grid[yy*w+xx] == -3)) {
1965                     state->grid[yy*w+xx] = 64;
1966                 }
1967                 if (!state->layout->mines[yy*w+xx] &&
1968                     state->grid[yy*w+xx] == -1) {
1969                     state->grid[yy*w+xx] = 66;
1970                 }
1971             }
1972         state->grid[y*w+x] = 65;
1973         return -1;
1974     }
1975
1976     /*
1977      * Otherwise, the player has opened a safe square. Mark it to-do.
1978      */
1979     state->grid[y*w+x] = -10;          /* `todo' value internal to this func */
1980
1981     /*
1982      * Now go through the grid finding all `todo' values and
1983      * opening them. Every time one of them turns out to have no
1984      * neighbouring mines, we add all its unopened neighbours to
1985      * the list as well.
1986      * 
1987      * FIXME: We really ought to be able to do this better than
1988      * using repeated N^2 scans of the grid.
1989      */
1990     while (1) {
1991         int done_something = FALSE;
1992
1993         for (yy = 0; yy < h; yy++)
1994             for (xx = 0; xx < w; xx++)
1995                 if (state->grid[yy*w+xx] == -10) {
1996                     int dx, dy, v;
1997
1998                     assert(!state->layout->mines[yy*w+xx]);
1999
2000                     v = 0;
2001
2002                     for (dx = -1; dx <= +1; dx++)
2003                         for (dy = -1; dy <= +1; dy++)
2004                             if (xx+dx >= 0 && xx+dx < state->w &&
2005                                 yy+dy >= 0 && yy+dy < state->h &&
2006                                 state->layout->mines[(yy+dy)*w+(xx+dx)])
2007                                 v++;
2008
2009                     state->grid[yy*w+xx] = v;
2010
2011                     if (v == 0) {
2012                         for (dx = -1; dx <= +1; dx++)
2013                             for (dy = -1; dy <= +1; dy++)
2014                                 if (xx+dx >= 0 && xx+dx < state->w &&
2015                                     yy+dy >= 0 && yy+dy < state->h &&
2016                                     state->grid[(yy+dy)*w+(xx+dx)] == -2)
2017                                     state->grid[(yy+dy)*w+(xx+dx)] = -10;
2018                     }
2019
2020                     done_something = TRUE;
2021                 }
2022
2023         if (!done_something)
2024             break;
2025     }
2026
2027     /*
2028      * Finally, scan the grid and see if exactly as many squares
2029      * are still covered as there are mines. If so, set the `won'
2030      * flag and fill in mine markers on all covered squares.
2031      */
2032     nmines = ncovered = 0;
2033     for (yy = 0; yy < h; yy++)
2034         for (xx = 0; xx < w; xx++) {
2035             if (state->grid[yy*w+xx] < 0)
2036                 ncovered++;
2037             if (state->layout->mines[yy*w+xx])
2038                 nmines++;
2039         }
2040     assert(ncovered >= nmines);
2041     if (ncovered == nmines) {
2042         for (yy = 0; yy < h; yy++)
2043             for (xx = 0; xx < w; xx++) {
2044                 if (state->grid[yy*w+xx] < 0)
2045                     state->grid[yy*w+xx] = -1;
2046         }
2047         state->won = TRUE;
2048     }
2049
2050     return 0;
2051 }
2052
2053 static game_state *new_game(midend_data *me, game_params *params, char *desc)
2054 {
2055     game_state *state = snew(game_state);
2056     int i, wh, x, y, ret, masked;
2057     unsigned char *bmp;
2058
2059     state->w = params->w;
2060     state->h = params->h;
2061     state->n = params->n;
2062     state->dead = state->won = FALSE;
2063
2064     wh = state->w * state->h;
2065
2066     state->layout = snew(struct mine_layout);
2067     state->layout->refcount = 1;
2068
2069     state->grid = snewn(wh, char);
2070     memset(state->grid, -2, wh);
2071
2072     if (*desc == 'r') {
2073         desc++;
2074         state->layout->n = atoi(desc);
2075         while (*desc && isdigit((unsigned char)*desc))
2076             desc++;                    /* skip over mine count */
2077         if (*desc) desc++;             /* eat comma */
2078         if (*desc == 'a')
2079             state->layout->unique = FALSE;
2080         else
2081             state->layout->unique = TRUE;
2082         desc++;
2083         if (*desc) desc++;             /* eat comma */
2084
2085         state->layout->mines = NULL;
2086         state->layout->rs = random_state_decode(desc);
2087         state->layout->me = me;
2088
2089     } else {
2090
2091         state->layout->mines = snewn(wh, char);
2092         x = atoi(desc);
2093         while (*desc && isdigit((unsigned char)*desc))
2094             desc++;                    /* skip over x coordinate */
2095         if (*desc) desc++;             /* eat comma */
2096         y = atoi(desc);
2097         while (*desc && isdigit((unsigned char)*desc))
2098             desc++;                    /* skip over y coordinate */
2099         if (*desc) desc++;             /* eat comma */
2100
2101         if (*desc == 'm') {
2102             masked = TRUE;
2103             desc++;
2104         } else {
2105             /*
2106              * We permit game IDs to be entered by hand without the
2107              * masking transformation.
2108              */
2109             masked = FALSE;
2110         }
2111
2112         bmp = snewn((wh + 7) / 8, unsigned char);
2113         memset(bmp, 0, (wh + 7) / 8);
2114         for (i = 0; i < (wh+3)/4; i++) {
2115             int c = desc[i];
2116             int v;
2117
2118             assert(c != 0);            /* validate_desc should have caught */
2119             if (c >= '0' && c <= '9')
2120                 v = c - '0';
2121             else if (c >= 'a' && c <= 'f')
2122                 v = c - 'a' + 10;
2123             else if (c >= 'A' && c <= 'F')
2124                 v = c - 'A' + 10;
2125             else
2126                 v = 0;
2127
2128             bmp[i / 2] |= v << (4 * (1 - (i % 2)));
2129         }
2130
2131         if (masked)
2132             obfuscate_bitmap(bmp, wh, TRUE);
2133
2134         memset(state->layout->mines, 0, wh);
2135         for (i = 0; i < wh; i++) {
2136             if (bmp[i / 8] & (0x80 >> (i % 8)))
2137                 state->layout->mines[i] = 1;
2138         }
2139
2140         ret = open_square(state, x, y);
2141     }
2142
2143     return state;
2144 }
2145
2146 static game_state *dup_game(game_state *state)
2147 {
2148     game_state *ret = snew(game_state);
2149
2150     ret->w = state->w;
2151     ret->h = state->h;
2152     ret->n = state->n;
2153     ret->dead = state->dead;
2154     ret->won = state->won;
2155     ret->layout = state->layout;
2156     ret->layout->refcount++;
2157     ret->grid = snewn(ret->w * ret->h, char);
2158     memcpy(ret->grid, state->grid, ret->w * ret->h);
2159
2160     return ret;
2161 }
2162
2163 static void free_game(game_state *state)
2164 {
2165     if (--state->layout->refcount <= 0) {
2166         sfree(state->layout->mines);
2167         if (state->layout->rs)
2168             random_free(state->layout->rs);
2169         sfree(state->layout);
2170     }
2171     sfree(state->grid);
2172     sfree(state);
2173 }
2174
2175 static game_state *solve_game(game_state *state, game_aux_info *aux,
2176                               char **error)
2177 {
2178     return NULL;
2179 }
2180
2181 static char *game_text_format(game_state *state)
2182 {
2183     return NULL;
2184 }
2185
2186 struct game_ui {
2187     int hx, hy, hradius;               /* for mouse-down highlights */
2188     int flash_is_death;
2189 };
2190
2191 static game_ui *new_ui(game_state *state)
2192 {
2193     game_ui *ui = snew(game_ui);
2194     ui->hx = ui->hy = -1;
2195     ui->hradius = 0;
2196     ui->flash_is_death = FALSE;        /* *shrug* */
2197     return ui;
2198 }
2199
2200 static void free_ui(game_ui *ui)
2201 {
2202     sfree(ui);
2203 }
2204
2205 static game_state *make_move(game_state *from, game_ui *ui, int x, int y,
2206                              int button)
2207 {
2208     game_state *ret;
2209     int cx, cy;
2210
2211     if (from->dead || from->won)
2212         return NULL;                   /* no further moves permitted */
2213
2214     if (!IS_MOUSE_DOWN(button) && !IS_MOUSE_DRAG(button) &&
2215         !IS_MOUSE_RELEASE(button))
2216         return NULL;
2217
2218     cx = FROMCOORD(x);
2219     cy = FROMCOORD(y);
2220     if (cx < 0 || cx >= from->w || cy < 0 || cy > from->h)
2221         return NULL;
2222
2223     if (button == LEFT_BUTTON || button == LEFT_DRAG) {
2224         /*
2225          * Mouse-downs and mouse-drags just cause highlighting
2226          * updates.
2227          */
2228         ui->hx = cx;
2229         ui->hy = cy;
2230         ui->hradius = (from->grid[cy*from->w+cx] >= 0 ? 1 : 0);
2231         return from;
2232     }
2233
2234     if (button == RIGHT_BUTTON) {
2235         /*
2236          * Right-clicking only works on a covered square, and it
2237          * toggles between -1 (marked as mine) and -2 (not marked
2238          * as mine).
2239          *
2240          * FIXME: question marks.
2241          */
2242         if (from->grid[cy * from->w + cx] != -2 &&
2243             from->grid[cy * from->w + cx] != -1)
2244             return NULL;
2245
2246         ret = dup_game(from);
2247         ret->grid[cy * from->w + cx] ^= (-2 ^ -1);
2248
2249         return ret;
2250     }
2251
2252     if (button == LEFT_RELEASE) {
2253         ui->hx = ui->hy = -1;
2254         ui->hradius = 0;
2255
2256         /*
2257          * At this stage we must never return NULL: we have adjusted
2258          * the ui, so at worst we return `from'.
2259          */
2260
2261         /*
2262          * Left-clicking on a covered square opens a tile. Not
2263          * permitted if the tile is marked as a mine, for safety.
2264          * (Unmark it and _then_ open it.)
2265          */
2266         if (from->grid[cy * from->w + cx] == -2 ||
2267             from->grid[cy * from->w + cx] == -3) {
2268             ret = dup_game(from);
2269             open_square(ret, cx, cy);
2270             return ret;
2271         }
2272
2273         /*
2274          * Left-clicking on an uncovered tile: first we check to see if
2275          * the number of mine markers surrounding the tile is equal to
2276          * its mine count, and if so then we open all other surrounding
2277          * squares.
2278          */
2279         if (from->grid[cy * from->w + cx] > 0) {
2280             int dy, dx, n;
2281
2282             /* Count mine markers. */
2283             n = 0;
2284             for (dy = -1; dy <= +1; dy++)
2285                 for (dx = -1; dx <= +1; dx++)
2286                     if (cx+dx >= 0 && cx+dx < from->w &&
2287                         cy+dy >= 0 && cy+dy < from->h) {
2288                         if (from->grid[(cy+dy)*from->w+(cx+dx)] == -1)
2289                             n++;
2290                     }
2291
2292             if (n == from->grid[cy * from->w + cx]) {
2293                 ret = dup_game(from);
2294                 for (dy = -1; dy <= +1; dy++)
2295                     for (dx = -1; dx <= +1; dx++)
2296                         if (cx+dx >= 0 && cx+dx < ret->w &&
2297                             cy+dy >= 0 && cy+dy < ret->h &&
2298                             (ret->grid[(cy+dy)*ret->w+(cx+dx)] == -2 ||
2299                              ret->grid[(cy+dy)*ret->w+(cx+dx)] == -3))
2300                             open_square(ret, cx+dx, cy+dy);
2301                 return ret;
2302             }
2303         }
2304
2305         return from;
2306     }
2307
2308     return NULL;
2309 }
2310
2311 /* ----------------------------------------------------------------------
2312  * Drawing routines.
2313  */
2314
2315 struct game_drawstate {
2316     int w, h, started;
2317     char *grid;
2318     /*
2319      * Items in this `grid' array have all the same values as in
2320      * the game_state grid, and in addition:
2321      * 
2322      *  - -10 means the tile was drawn `specially' as a result of a
2323      *    flash, so it will always need redrawing.
2324      * 
2325      *  - -22 and -23 mean the tile is highlighted for a possible
2326      *    click.
2327      */
2328 };
2329
2330 static void game_size(game_params *params, int *x, int *y)
2331 {
2332     *x = BORDER * 2 + TILE_SIZE * params->w;
2333     *y = BORDER * 2 + TILE_SIZE * params->h;
2334 }
2335
2336 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
2337 {
2338     float *ret = snewn(3 * NCOLOURS, float);
2339
2340     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2341
2342     ret[COL_1 * 3 + 0] = 0.0F;
2343     ret[COL_1 * 3 + 1] = 0.0F;
2344     ret[COL_1 * 3 + 2] = 1.0F;
2345
2346     ret[COL_2 * 3 + 0] = 0.0F;
2347     ret[COL_2 * 3 + 1] = 0.5F;
2348     ret[COL_2 * 3 + 2] = 0.0F;
2349
2350     ret[COL_3 * 3 + 0] = 1.0F;
2351     ret[COL_3 * 3 + 1] = 0.0F;
2352     ret[COL_3 * 3 + 2] = 0.0F;
2353
2354     ret[COL_4 * 3 + 0] = 0.0F;
2355     ret[COL_4 * 3 + 1] = 0.0F;
2356     ret[COL_4 * 3 + 2] = 0.5F;
2357
2358     ret[COL_5 * 3 + 0] = 0.5F;
2359     ret[COL_5 * 3 + 1] = 0.0F;
2360     ret[COL_5 * 3 + 2] = 0.0F;
2361
2362     ret[COL_6 * 3 + 0] = 0.0F;
2363     ret[COL_6 * 3 + 1] = 0.5F;
2364     ret[COL_6 * 3 + 2] = 0.5F;
2365
2366     ret[COL_7 * 3 + 0] = 0.0F;
2367     ret[COL_7 * 3 + 1] = 0.0F;
2368     ret[COL_7 * 3 + 2] = 0.0F;
2369
2370     ret[COL_8 * 3 + 0] = 0.5F;
2371     ret[COL_8 * 3 + 1] = 0.5F;
2372     ret[COL_8 * 3 + 2] = 0.5F;
2373
2374     ret[COL_MINE * 3 + 0] = 0.0F;
2375     ret[COL_MINE * 3 + 1] = 0.0F;
2376     ret[COL_MINE * 3 + 2] = 0.0F;
2377
2378     ret[COL_BANG * 3 + 0] = 1.0F;
2379     ret[COL_BANG * 3 + 1] = 0.0F;
2380     ret[COL_BANG * 3 + 2] = 0.0F;
2381
2382     ret[COL_CROSS * 3 + 0] = 1.0F;
2383     ret[COL_CROSS * 3 + 1] = 0.0F;
2384     ret[COL_CROSS * 3 + 2] = 0.0F;
2385
2386     ret[COL_FLAG * 3 + 0] = 1.0F;
2387     ret[COL_FLAG * 3 + 1] = 0.0F;
2388     ret[COL_FLAG * 3 + 2] = 0.0F;
2389
2390     ret[COL_FLAGBASE * 3 + 0] = 0.0F;
2391     ret[COL_FLAGBASE * 3 + 1] = 0.0F;
2392     ret[COL_FLAGBASE * 3 + 2] = 0.0F;
2393
2394     ret[COL_QUERY * 3 + 0] = 0.0F;
2395     ret[COL_QUERY * 3 + 1] = 0.0F;
2396     ret[COL_QUERY * 3 + 2] = 0.0F;
2397
2398     ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
2399     ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
2400     ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
2401
2402     ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
2403     ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
2404     ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
2405
2406     *ncolours = NCOLOURS;
2407     return ret;
2408 }
2409
2410 static game_drawstate *game_new_drawstate(game_state *state)
2411 {
2412     struct game_drawstate *ds = snew(struct game_drawstate);
2413
2414     ds->w = state->w;
2415     ds->h = state->h;
2416     ds->started = FALSE;
2417     ds->grid = snewn(ds->w * ds->h, char);
2418
2419     memset(ds->grid, -99, ds->w * ds->h);
2420
2421     return ds;
2422 }
2423
2424 static void game_free_drawstate(game_drawstate *ds)
2425 {
2426     sfree(ds->grid);
2427     sfree(ds);
2428 }
2429
2430 static void draw_tile(frontend *fe, int x, int y, int v, int bg)
2431 {
2432     if (v < 0) {
2433         int coords[12];
2434         int hl = 0;
2435
2436         if (v == -22 || v == -23) {
2437             v += 20;
2438
2439             /*
2440              * Omit the highlights in this case.
2441              */
2442             draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, bg);
2443             draw_line(fe, x, y, x + TILE_SIZE - 1, y, COL_LOWLIGHT);
2444             draw_line(fe, x, y, x, y + TILE_SIZE - 1, COL_LOWLIGHT);
2445         } else {
2446             /*
2447              * Draw highlights to indicate the square is covered.
2448              */
2449             coords[0] = x + TILE_SIZE - 1;
2450             coords[1] = y + TILE_SIZE - 1;
2451             coords[2] = x + TILE_SIZE - 1;
2452             coords[3] = y;
2453             coords[4] = x;
2454             coords[5] = y + TILE_SIZE - 1;
2455             draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT ^ hl);
2456             draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT ^ hl);
2457
2458             coords[0] = x;
2459             coords[1] = y;
2460             draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT ^ hl);
2461             draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT ^ hl);
2462
2463             draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
2464                       TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
2465                       bg);
2466         }
2467
2468         if (v == -1) {
2469             /*
2470              * Draw a flag.
2471              */
2472 #define SETCOORD(n, dx, dy) do { \
2473     coords[(n)*2+0] = x + TILE_SIZE * (dx); \
2474     coords[(n)*2+1] = y + TILE_SIZE * (dy); \
2475 } while (0)
2476             SETCOORD(0, 0.6, 0.35);
2477             SETCOORD(1, 0.6, 0.7);
2478             SETCOORD(2, 0.8, 0.8);
2479             SETCOORD(3, 0.25, 0.8);
2480             SETCOORD(4, 0.55, 0.7);
2481             SETCOORD(5, 0.55, 0.35);
2482             draw_polygon(fe, coords, 6, TRUE, COL_FLAGBASE);
2483             draw_polygon(fe, coords, 6, FALSE, COL_FLAGBASE);
2484
2485             SETCOORD(0, 0.6, 0.2);
2486             SETCOORD(1, 0.6, 0.5);
2487             SETCOORD(2, 0.2, 0.35);
2488             draw_polygon(fe, coords, 3, TRUE, COL_FLAG);
2489             draw_polygon(fe, coords, 3, FALSE, COL_FLAG);
2490 #undef SETCOORD
2491
2492         } else if (v == -3) {
2493             /*
2494              * Draw a question mark.
2495              */
2496             draw_text(fe, x + TILE_SIZE / 2, y + TILE_SIZE / 2,
2497                       FONT_VARIABLE, TILE_SIZE * 6 / 8,
2498                       ALIGN_VCENTRE | ALIGN_HCENTRE,
2499                       COL_QUERY, "?");
2500         }
2501     } else {
2502         /*
2503          * Clear the square to the background colour, and draw thin
2504          * grid lines along the top and left.
2505          * 
2506          * Exception is that for value 65 (mine we've just trodden
2507          * on), we clear the square to COL_BANG.
2508          */
2509         draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
2510                   (v == 65 ? COL_BANG : bg));
2511         draw_line(fe, x, y, x + TILE_SIZE - 1, y, COL_LOWLIGHT);
2512         draw_line(fe, x, y, x, y + TILE_SIZE - 1, COL_LOWLIGHT);
2513
2514         if (v > 0 && v <= 8) {
2515             /*
2516              * Mark a number.
2517              */
2518             char str[2];
2519             str[0] = v + '0';
2520             str[1] = '\0';
2521             draw_text(fe, x + TILE_SIZE / 2, y + TILE_SIZE / 2,
2522                       FONT_VARIABLE, TILE_SIZE * 7 / 8,
2523                       ALIGN_VCENTRE | ALIGN_HCENTRE,
2524                       (COL_1 - 1) + v, str);
2525
2526         } else if (v >= 64) {
2527             /*
2528              * Mark a mine.
2529              * 
2530              * FIXME: this could be done better!
2531              */
2532 #if 0
2533             draw_text(fe, x + TILE_SIZE / 2, y + TILE_SIZE / 2,
2534                       FONT_VARIABLE, TILE_SIZE * 7 / 8,
2535                       ALIGN_VCENTRE | ALIGN_HCENTRE,
2536                       COL_MINE, "*");
2537 #else
2538             {
2539                 int cx = x + TILE_SIZE / 2;
2540                 int cy = y + TILE_SIZE / 2;
2541                 int r = TILE_SIZE / 2 - 3;
2542                 int coords[4*5*2];
2543                 int xdx = 1, xdy = 0, ydx = 0, ydy = 1;
2544                 int tdx, tdy, i;
2545
2546                 for (i = 0; i < 4*5*2; i += 5*2) {
2547                     coords[i+2*0+0] = cx - r/6*xdx + r*4/5*ydx;
2548                     coords[i+2*0+1] = cy - r/6*xdy + r*4/5*ydy;
2549                     coords[i+2*1+0] = cx - r/6*xdx + r*ydx;
2550                     coords[i+2*1+1] = cy - r/6*xdy + r*ydy;
2551                     coords[i+2*2+0] = cx + r/6*xdx + r*ydx;
2552                     coords[i+2*2+1] = cy + r/6*xdy + r*ydy;
2553                     coords[i+2*3+0] = cx + r/6*xdx + r*4/5*ydx;
2554                     coords[i+2*3+1] = cy + r/6*xdy + r*4/5*ydy;
2555                     coords[i+2*4+0] = cx + r*3/5*xdx + r*3/5*ydx;
2556                     coords[i+2*4+1] = cy + r*3/5*xdy + r*3/5*ydy;
2557
2558                     tdx = ydx;
2559                     tdy = ydy;
2560                     ydx = xdx;
2561                     ydy = xdy;
2562                     xdx = -tdx;
2563                     xdy = -tdy;
2564                 }
2565
2566                 draw_polygon(fe, coords, 5*4, TRUE, COL_MINE);
2567                 draw_polygon(fe, coords, 5*4, FALSE, COL_MINE);
2568
2569                 draw_rect(fe, cx-r/3, cy-r/3, r/3, r/4, COL_HIGHLIGHT);
2570             }
2571 #endif
2572
2573             if (v == 66) {
2574                 /*
2575                  * Cross through the mine.
2576                  */
2577                 int dx;
2578                 for (dx = -1; dx <= +1; dx++) {
2579                     draw_line(fe, x + 3 + dx, y + 2,
2580                               x + TILE_SIZE - 3 + dx,
2581                               y + TILE_SIZE - 2, COL_CROSS);
2582                     draw_line(fe, x + TILE_SIZE - 3 + dx, y + 2,
2583                               x + 3 + dx, y + TILE_SIZE - 2,
2584                               COL_CROSS);
2585                 }
2586             }
2587         }
2588     }
2589
2590     draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
2591 }
2592
2593 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
2594                         game_state *state, int dir, game_ui *ui,
2595                         float animtime, float flashtime)
2596 {
2597     int x, y;
2598     int mines, markers, bg;
2599
2600     if (flashtime) {
2601         int frame = (flashtime / FLASH_FRAME);
2602         if (frame % 2)
2603             bg = (ui->flash_is_death ? COL_BACKGROUND : COL_LOWLIGHT);
2604         else
2605             bg = (ui->flash_is_death ? COL_BANG : COL_HIGHLIGHT);
2606     } else
2607         bg = COL_BACKGROUND;
2608
2609     if (!ds->started) {
2610         int coords[6];
2611
2612         draw_rect(fe, 0, 0,
2613                   TILE_SIZE * state->w + 2 * BORDER,
2614                   TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
2615         draw_update(fe, 0, 0,
2616                     TILE_SIZE * state->w + 2 * BORDER,
2617                     TILE_SIZE * state->h + 2 * BORDER);
2618
2619         /*
2620          * Recessed area containing the whole puzzle.
2621          */
2622         coords[0] = COORD(state->w) + OUTER_HIGHLIGHT_WIDTH - 1;
2623         coords[1] = COORD(state->h) + OUTER_HIGHLIGHT_WIDTH - 1;
2624         coords[2] = COORD(state->w) + OUTER_HIGHLIGHT_WIDTH - 1;
2625         coords[3] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2626         coords[4] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2627         coords[5] = COORD(state->h) + OUTER_HIGHLIGHT_WIDTH - 1;
2628         draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
2629         draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
2630
2631         coords[1] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2632         coords[0] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2633         draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
2634         draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
2635
2636         ds->started = TRUE;
2637     }
2638
2639     /*
2640      * Now draw the tiles. Also in this loop, count up the number
2641      * of mines and mine markers.
2642      */
2643     mines = markers = 0;
2644     for (y = 0; y < ds->h; y++)
2645         for (x = 0; x < ds->w; x++) {
2646             int v = state->grid[y*ds->w+x];
2647
2648             if (v == -1)
2649                 markers++;
2650             if (state->layout->mines && state->layout->mines[y*ds->w+x])
2651                 mines++;
2652
2653             if ((v == -2 || v == -3) &&
2654                 (abs(x-ui->hx) <= ui->hradius && abs(y-ui->hy) <= ui->hradius))
2655                 v -= 20;
2656
2657             if (ds->grid[y*ds->w+x] != v || bg != COL_BACKGROUND) {
2658                 draw_tile(fe, COORD(x), COORD(y), v, bg);
2659                 ds->grid[y*ds->w+x] = (bg == COL_BACKGROUND ? v : -10);
2660             }
2661         }
2662
2663     if (!state->layout->mines)
2664         mines = state->layout->n;
2665
2666     /*
2667      * Update the status bar.
2668      */
2669     {
2670         char statusbar[512];
2671         if (state->dead) {
2672             sprintf(statusbar, "GAME OVER!");
2673         } else if (state->won) {
2674             sprintf(statusbar, "COMPLETED!");
2675         } else {
2676             sprintf(statusbar, "Mines marked: %d / %d", markers, mines);
2677         }
2678         status_bar(fe, statusbar);
2679     }
2680 }
2681
2682 static float game_anim_length(game_state *oldstate, game_state *newstate,
2683                               int dir, game_ui *ui)
2684 {
2685     return 0.0F;
2686 }
2687
2688 static float game_flash_length(game_state *oldstate, game_state *newstate,
2689                                int dir, game_ui *ui)
2690 {
2691     if (dir > 0 && !oldstate->dead && !oldstate->won) {
2692         if (newstate->dead) {
2693             ui->flash_is_death = TRUE;
2694             return 3 * FLASH_FRAME;
2695         }
2696         if (newstate->won) {
2697             ui->flash_is_death = FALSE;
2698             return 2 * FLASH_FRAME;
2699         }
2700     }
2701     return 0.0F;
2702 }
2703
2704 static int game_wants_statusbar(void)
2705 {
2706     return TRUE;
2707 }
2708
2709 #ifdef COMBINED
2710 #define thegame mines
2711 #endif
2712
2713 const struct game thegame = {
2714     "Mines", "games.mines",
2715     default_params,
2716     game_fetch_preset,
2717     decode_params,
2718     encode_params,
2719     free_params,
2720     dup_params,
2721     TRUE, game_configure, custom_params,
2722     validate_params,
2723     new_game_desc,
2724     game_free_aux_info,
2725     validate_desc,
2726     new_game,
2727     dup_game,
2728     free_game,
2729     FALSE, solve_game,
2730     FALSE, game_text_format,
2731     new_ui,
2732     free_ui,
2733     make_move,
2734     game_size,
2735     game_colours,
2736     game_new_drawstate,
2737     game_free_drawstate,
2738     game_redraw,
2739     game_anim_length,
2740     game_flash_length,
2741     game_wants_statusbar,
2742 };