2 * mines.c: Minesweeper clone with sophisticated grid generation.
6 * - think about configurably supporting question marks. Once,
7 * that is, we've thought about configurability in general!
21 COL_BACKGROUND, COL_BACKGROUND2,
22 COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8,
23 COL_MINE, COL_BANG, COL_CROSS, COL_FLAG, COL_FLAGBASE, COL_QUERY,
24 COL_HIGHLIGHT, COL_LOWLIGHT,
29 #define BORDER (TILE_SIZE * 3 / 2)
30 #define HIGHLIGHT_WIDTH 2
31 #define OUTER_HIGHLIGHT_WIDTH 3
32 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
33 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
35 #define FLASH_FRAME 0.13F
44 * This structure is shared between all the game_states for a
45 * given instance of the puzzle, so we reference-count it.
50 * If we haven't yet actually generated the mine layout, here's
51 * all the data we will need to do so.
55 midend_data *me; /* to give back the new game desc */
59 int w, h, n, dead, won;
60 int used_solve, just_used_solve;
61 struct mine_layout *layout; /* real mine positions */
62 signed char *grid; /* player knowledge */
64 * Each item in the `grid' array is one of the following values:
66 * - 0 to 8 mean the square is open and has a surrounding mine
69 * - -1 means the square is marked as a mine.
71 * - -2 means the square is unknown.
73 * - -3 means the square is marked with a question mark
74 * (FIXME: do we even want to bother with this?).
76 * - 64 means the square has had a mine revealed when the game
79 * - 65 means the square had a mine revealed and this was the
80 * one the player hits.
82 * - 66 means the square has a crossed-out mine because the
83 * player had incorrectly marked it.
87 static game_params *default_params(void)
89 game_params *ret = snew(game_params);
98 static int game_fetch_preset(int i, char **name, game_params **params)
102 static const struct { int w, h, n; } values[] = {
108 if (i < 0 || i >= lenof(values))
111 ret = snew(game_params);
112 ret->w = values[i].w;
113 ret->h = values[i].h;
114 ret->n = values[i].n;
117 sprintf(str, "%dx%d, %d mines", ret->w, ret->h, ret->n);
124 static void free_params(game_params *params)
129 static game_params *dup_params(game_params *params)
131 game_params *ret = snew(game_params);
132 *ret = *params; /* structure copy */
136 static void decode_params(game_params *params, char const *string)
138 char const *p = string;
141 while (*p && isdigit((unsigned char)*p)) p++;
145 while (*p && isdigit((unsigned char)*p)) p++;
147 params->h = params->w;
152 while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
154 params->n = params->w * params->h / 10;
160 params->unique = FALSE;
162 p++; /* skip any other gunk */
166 static char *encode_params(game_params *params, int full)
171 len = sprintf(ret, "%dx%d", params->w, params->h);
173 * Mine count is a generation-time parameter, since it can be
174 * deduced from the mine bitmap!
177 len += sprintf(ret+len, "n%d", params->n);
178 if (full && !params->unique)
180 assert(len < lenof(ret));
186 static config_item *game_configure(game_params *params)
191 ret = snewn(5, config_item);
193 ret[0].name = "Width";
194 ret[0].type = C_STRING;
195 sprintf(buf, "%d", params->w);
196 ret[0].sval = dupstr(buf);
199 ret[1].name = "Height";
200 ret[1].type = C_STRING;
201 sprintf(buf, "%d", params->h);
202 ret[1].sval = dupstr(buf);
205 ret[2].name = "Mines";
206 ret[2].type = C_STRING;
207 sprintf(buf, "%d", params->n);
208 ret[2].sval = dupstr(buf);
211 ret[3].name = "Ensure solubility";
212 ret[3].type = C_BOOLEAN;
214 ret[3].ival = params->unique;
224 static game_params *custom_params(config_item *cfg)
226 game_params *ret = snew(game_params);
228 ret->w = atoi(cfg[0].sval);
229 ret->h = atoi(cfg[1].sval);
230 ret->n = atoi(cfg[2].sval);
231 if (strchr(cfg[2].sval, '%'))
232 ret->n = ret->n * (ret->w * ret->h) / 100;
233 ret->unique = cfg[3].ival;
238 static char *validate_params(game_params *params)
240 if (params->w <= 0 && params->h <= 0)
241 return "Width and height must both be greater than zero";
243 return "Width must be greater than zero";
245 return "Height must be greater than zero";
246 if (params->n > params->w * params->h - 9)
247 return "Too many mines for grid size";
250 * FIXME: Need more constraints here. Not sure what the
251 * sensible limits for Minesweeper actually are. The limits
252 * probably ought to change, however, depending on uniqueness.
258 /* ----------------------------------------------------------------------
259 * Minesweeper solver, used to ensure the generated grids are
260 * solvable without having to take risks.
264 * Count the bits in a word. Only needs to cope with 16 bits.
266 static int bitcount16(int word)
268 word = ((word & 0xAAAA) >> 1) + (word & 0x5555);
269 word = ((word & 0xCCCC) >> 2) + (word & 0x3333);
270 word = ((word & 0xF0F0) >> 4) + (word & 0x0F0F);
271 word = ((word & 0xFF00) >> 8) + (word & 0x00FF);
277 * We use a tree234 to store a large number of small localised
278 * sets, each with a mine count. We also keep some of those sets
279 * linked together into a to-do list.
282 short x, y, mask, mines;
284 struct set *prev, *next;
287 static int setcmp(void *av, void *bv)
289 struct set *a = (struct set *)av;
290 struct set *b = (struct set *)bv;
294 else if (a->y > b->y)
296 else if (a->x < b->x)
298 else if (a->x > b->x)
300 else if (a->mask < b->mask)
302 else if (a->mask > b->mask)
310 struct set *todo_head, *todo_tail;
313 static struct setstore *ss_new(void)
315 struct setstore *ss = snew(struct setstore);
316 ss->sets = newtree234(setcmp);
317 ss->todo_head = ss->todo_tail = NULL;
322 * Take two input sets, in the form (x,y,mask). Munge the first by
323 * taking either its intersection with the second or its difference
324 * with the second. Return the new mask part of the first set.
326 static int setmunge(int x1, int y1, int mask1, int x2, int y2, int mask2,
330 * Adjust the second set so that it has the same x,y
331 * coordinates as the first.
333 if (abs(x2-x1) >= 3 || abs(y2-y1) >= 3) {
337 mask2 &= ~(4|32|256);
347 mask2 &= ~(64|128|256);
359 * Invert the second set if `diff' is set (we're after A &~ B
360 * rather than A & B).
366 * Now all that's left is a logical AND.
368 return mask1 & mask2;
371 static void ss_add_todo(struct setstore *ss, struct set *s)
374 return; /* already on it */
376 #ifdef SOLVER_DIAGNOSTICS
377 printf("adding set on todo list: %d,%d %03x %d\n",
378 s->x, s->y, s->mask, s->mines);
381 s->prev = ss->todo_tail;
391 static void ss_add(struct setstore *ss, int x, int y, int mask, int mines)
398 * Normalise so that x and y are genuinely the bounding
401 while (!(mask & (1|8|64)))
403 while (!(mask & (1|2|4)))
407 * Create a set structure and add it to the tree.
409 s = snew(struct set);
415 if (add234(ss->sets, s) != s) {
417 * This set already existed! Free it and return.
424 * We've added a new set to the tree, so put it on the todo
430 static void ss_remove(struct setstore *ss, struct set *s)
432 struct set *next = s->next, *prev = s->prev;
434 #ifdef SOLVER_DIAGNOSTICS
435 printf("removing set %d,%d %03x\n", s->x, s->y, s->mask);
438 * Remove s from the todo list.
442 else if (s == ss->todo_head)
443 ss->todo_head = next;
447 else if (s == ss->todo_tail)
448 ss->todo_tail = prev;
453 * Remove s from the tree.
458 * Destroy the actual set structure.
464 * Return a dynamically allocated list of all the sets which
465 * overlap a provided input set.
467 static struct set **ss_overlap(struct setstore *ss, int x, int y, int mask)
469 struct set **ret = NULL;
470 int nret = 0, retsize = 0;
473 for (xx = x-3; xx < x+3; xx++)
474 for (yy = y-3; yy < y+3; yy++) {
479 * Find the first set with these top left coordinates.
485 if (findrelpos234(ss->sets, &stmp, NULL, REL234_GE, &pos)) {
486 while ((s = index234(ss->sets, pos)) != NULL &&
487 s->x == xx && s->y == yy) {
489 * This set potentially overlaps the input one.
490 * Compute the intersection to see if they
491 * really overlap, and add it to the list if
494 if (setmunge(x, y, mask, s->x, s->y, s->mask, FALSE)) {
496 * There's an overlap.
498 if (nret >= retsize) {
500 ret = sresize(ret, retsize, struct set *);
510 ret = sresize(ret, nret+1, struct set *);
517 * Get an element from the head of the set todo list.
519 static struct set *ss_todo(struct setstore *ss)
522 struct set *ret = ss->todo_head;
523 ss->todo_head = ret->next;
525 ss->todo_head->prev = NULL;
527 ss->todo_tail = NULL;
528 ret->next = ret->prev = NULL;
541 static void std_add(struct squaretodo *std, int i)
544 std->next[std->tail] = i;
551 static void known_squares(int w, int h, struct squaretodo *std,
553 int (*open)(void *ctx, int x, int y), void *openctx,
554 int x, int y, int mask, int mine)
560 for (yy = 0; yy < 3; yy++)
561 for (xx = 0; xx < 3; xx++) {
563 int i = (y + yy) * w + (x + xx);
566 * It's possible that this square is _already_
567 * known, in which case we don't try to add it to
573 grid[i] = -1; /* and don't open it! */
575 grid[i] = open(openctx, x + xx, y + yy);
576 assert(grid[i] != -1); /* *bang* */
587 * This is data returned from the `perturb' function. It details
588 * which squares have become mines and which have become clear. The
589 * solver is (of course) expected to honourably not use that
590 * knowledge directly, but to efficently adjust its internal data
591 * structures and proceed based on only the information it
594 struct perturbation {
596 int delta; /* +1 == become a mine; -1 == cleared */
598 struct perturbations {
600 struct perturbation *changes;
604 * Main solver entry point. You give it a grid of existing
605 * knowledge (-1 for a square known to be a mine, 0-8 for empty
606 * squares with a given number of neighbours, -2 for completely
607 * unknown), plus a function which you can call to open new squares
608 * once you're confident of them. It fills in as much more of the
613 * - -1 means deduction stalled and nothing could be done
614 * - 0 means deduction succeeded fully
615 * - >0 means deduction succeeded but some number of perturbation
616 * steps were required; the exact return value is the number of
619 static int minesolve(int w, int h, int n, signed char *grid,
620 int (*open)(void *ctx, int x, int y),
621 struct perturbations *(*perturb)(void *ctx,
623 int x, int y, int mask),
624 void *ctx, random_state *rs)
626 struct setstore *ss = ss_new();
628 struct squaretodo astd, *std = &astd;
633 * Set up a linked list of squares with known contents, so that
634 * we can process them one by one.
636 std->next = snewn(w*h, int);
637 std->head = std->tail = -1;
640 * Initialise that list with all known squares in the input
643 for (y = 0; y < h; y++) {
644 for (x = 0; x < w; x++) {
652 * Main deductive loop.
655 int done_something = FALSE;
659 * If there are any known squares on the todo list, process
660 * them and construct a set for each.
662 while (std->head != -1) {
664 #ifdef SOLVER_DIAGNOSTICS
665 printf("known square at %d,%d [%d]\n", i%w, i/w, grid[i]);
667 std->head = std->next[i];
675 int dx, dy, mines, bit, val;
676 #ifdef SOLVER_DIAGNOSTICS
677 printf("creating set around this square\n");
680 * Empty square. Construct the set of non-known squares
681 * around this one, and determine its mine count.
686 for (dy = -1; dy <= +1; dy++) {
687 for (dx = -1; dx <= +1; dx++) {
688 #ifdef SOLVER_DIAGNOSTICS
689 printf("grid %d,%d = %d\n", x+dx, y+dy, grid[i+dy*w+dx]);
691 if (x+dx < 0 || x+dx >= w || y+dy < 0 || y+dy >= h)
692 /* ignore this one */;
693 else if (grid[i+dy*w+dx] == -1)
695 else if (grid[i+dy*w+dx] == -2)
701 ss_add(ss, x-1, y-1, val, mines);
705 * Now, whether the square is empty or full, we must
706 * find any set which contains it and replace it with
707 * one which does not.
710 #ifdef SOLVER_DIAGNOSTICS
711 printf("finding sets containing known square %d,%d\n", x, y);
713 list = ss_overlap(ss, x, y, 1);
715 for (j = 0; list[j]; j++) {
716 int newmask, newmines;
721 * Compute the mask for this set minus the
722 * newly known square.
724 newmask = setmunge(s->x, s->y, s->mask, x, y, 1, TRUE);
727 * Compute the new mine count.
729 newmines = s->mines - (grid[i] == -1);
732 * Insert the new set into the collection,
733 * unless it's been whittled right down to
737 ss_add(ss, s->x, s->y, newmask, newmines);
740 * Destroy the old one; it is actually obsolete.
749 * Marking a fresh square as known certainly counts as
752 done_something = TRUE;
756 * Now pick a set off the to-do list and attempt deductions
759 if ((s = ss_todo(ss)) != NULL) {
761 #ifdef SOLVER_DIAGNOSTICS
762 printf("set to do: %d,%d %03x %d\n", s->x, s->y, s->mask, s->mines);
765 * Firstly, see if this set has a mine count of zero or
766 * of its own cardinality.
768 if (s->mines == 0 || s->mines == bitcount16(s->mask)) {
770 * If so, we can immediately mark all the squares
771 * in the set as known.
773 #ifdef SOLVER_DIAGNOSTICS
776 known_squares(w, h, std, grid, open, ctx,
777 s->x, s->y, s->mask, (s->mines != 0));
780 * Having done that, we need do nothing further
781 * with this set; marking all the squares in it as
782 * known will eventually eliminate it, and will
783 * also permit further deductions about anything
790 * Failing that, we now search through all the sets
791 * which overlap this one.
793 list = ss_overlap(ss, s->x, s->y, s->mask);
795 for (j = 0; list[j]; j++) {
796 struct set *s2 = list[j];
797 int swing, s2wing, swc, s2wc;
800 * Find the non-overlapping parts s2-s and s-s2,
801 * and their cardinalities.
803 * I'm going to refer to these parts as `wings'
804 * surrounding the central part common to both
805 * sets. The `s wing' is s-s2; the `s2 wing' is
808 swing = setmunge(s->x, s->y, s->mask, s2->x, s2->y, s2->mask,
810 s2wing = setmunge(s2->x, s2->y, s2->mask, s->x, s->y, s->mask,
812 swc = bitcount16(swing);
813 s2wc = bitcount16(s2wing);
816 * If one set has more mines than the other, and
817 * the number of extra mines is equal to the
818 * cardinality of that set's wing, then we can mark
819 * every square in the wing as a known mine, and
820 * every square in the other wing as known clear.
822 if (swc == s->mines - s2->mines ||
823 s2wc == s2->mines - s->mines) {
824 known_squares(w, h, std, grid, open, ctx,
826 (swc == s->mines - s2->mines));
827 known_squares(w, h, std, grid, open, ctx,
828 s2->x, s2->y, s2wing,
829 (s2wc == s2->mines - s->mines));
834 * Failing that, see if one set is a subset of the
835 * other. If so, we can divide up the mine count of
836 * the larger set between the smaller set and its
837 * complement, even if neither smaller set ends up
838 * being immediately clearable.
840 if (swc == 0 && s2wc != 0) {
841 /* s is a subset of s2. */
842 assert(s2->mines > s->mines);
843 ss_add(ss, s2->x, s2->y, s2wing, s2->mines - s->mines);
844 } else if (s2wc == 0 && swc != 0) {
845 /* s2 is a subset of s. */
846 assert(s->mines > s2->mines);
847 ss_add(ss, s->x, s->y, swing, s->mines - s2->mines);
854 * In this situation we have definitely done
855 * _something_, even if it's only reducing the size of
858 done_something = TRUE;
861 * We have nothing left on our todo list, which means
862 * all localised deductions have failed. Our next step
863 * is to resort to global deduction based on the total
864 * mine count. This is computationally expensive
865 * compared to any of the above deductions, which is
866 * why we only ever do it when all else fails, so that
867 * hopefully it won't have to happen too often.
869 * If you pass n<0 into this solver, that informs it
870 * that you do not know the total mine count, so it
871 * won't even attempt these deductions.
874 int minesleft, squaresleft;
875 int nsets, setused[10], cursor;
878 * Start by scanning the current grid state to work out
879 * how many unknown squares we still have, and how many
880 * mines are to be placed in them.
884 for (i = 0; i < w*h; i++) {
887 else if (grid[i] == -2)
891 #ifdef SOLVER_DIAGNOSTICS
892 printf("global deduction time: squaresleft=%d minesleft=%d\n",
893 squaresleft, minesleft);
894 for (y = 0; y < h; y++) {
895 for (x = 0; x < w; x++) {
911 * If there _are_ no unknown squares, we have actually
914 if (squaresleft == 0) {
915 assert(minesleft == 0);
920 * First really simple case: if there are no more mines
921 * left, or if there are exactly as many mines left as
922 * squares to play them in, then it's all easy.
924 if (minesleft == 0 || minesleft == squaresleft) {
925 for (i = 0; i < w*h; i++)
927 known_squares(w, h, std, grid, open, ctx,
928 i % w, i / w, 1, minesleft != 0);
929 continue; /* now go back to main deductive loop */
933 * Failing that, we have to do some _real_ work.
934 * Ideally what we do here is to try every single
935 * combination of the currently available sets, in an
936 * attempt to find a disjoint union (i.e. a set of
937 * squares with a known mine count between them) such
938 * that the remaining unknown squares _not_ contained
939 * in that union either contain no mines or are all
942 * Actually enumerating all 2^n possibilities will get
943 * a bit slow for large n, so I artificially cap this
944 * recursion at n=10 to avoid too much pain.
946 nsets = count234(ss->sets);
947 if (nsets <= lenof(setused)) {
949 * Doing this with actual recursive function calls
950 * would get fiddly because a load of local
951 * variables from this function would have to be
952 * passed down through the recursion. So instead
953 * I'm going to use a virtual recursion within this
954 * function. The way this works is:
956 * - we have an array `setused', such that
957 * setused[n] is 0 or 1 depending on whether set
958 * n is currently in the union we are
961 * - we have a value `cursor' which indicates how
962 * much of `setused' we have so far filled in.
963 * It's conceptually the recursion depth.
965 * We begin by setting `cursor' to zero. Then:
967 * - if cursor can advance, we advance it by one.
968 * We set the value in `setused' that it went
969 * past to 1 if that set is disjoint from
970 * anything else currently in `setused', or to 0
973 * - If cursor cannot advance because it has
974 * reached the end of the setused list, then we
975 * have a maximal disjoint union. Check to see
976 * whether its mine count has any useful
977 * properties. If so, mark all the squares not
978 * in the union as known and terminate.
980 * - If cursor has reached the end of setused and
981 * the algorithm _hasn't_ terminated, back
982 * cursor up to the nearest 1, turn it into a 0
983 * and advance cursor just past it.
985 * - If we attempt to back up to the nearest 1 and
986 * there isn't one at all, then we have gone
987 * through all disjoint unions of sets in the
988 * list and none of them has been helpful, so we
991 struct set *sets[lenof(setused)];
992 for (i = 0; i < nsets; i++)
993 sets[i] = index234(ss->sets, i);
998 if (cursor < nsets) {
1001 /* See if any existing set overlaps this one. */
1002 for (i = 0; i < cursor; i++)
1004 setmunge(sets[cursor]->x,
1007 sets[i]->x, sets[i]->y, sets[i]->mask,
1015 * We're adding this set to our union,
1016 * so adjust minesleft and squaresleft
1019 minesleft -= sets[cursor]->mines;
1020 squaresleft -= bitcount16(sets[cursor]->mask);
1023 setused[cursor++] = ok;
1025 #ifdef SOLVER_DIAGNOSTICS
1026 printf("trying a set combination with %d %d\n",
1027 squaresleft, minesleft);
1028 #endif /* SOLVER_DIAGNOSTICS */
1031 * We've reached the end. See if we've got
1032 * anything interesting.
1034 if (squaresleft > 0 &&
1035 (minesleft == 0 || minesleft == squaresleft)) {
1037 * We have! There is at least one
1038 * square not contained within the set
1039 * union we've just found, and we can
1040 * deduce that either all such squares
1041 * are mines or all are not (depending
1042 * on whether minesleft==0). So now all
1043 * we have to do is actually go through
1044 * the grid, find those squares, and
1047 for (i = 0; i < w*h; i++)
1048 if (grid[i] == -2) {
1052 for (j = 0; j < nsets; j++)
1054 setmunge(sets[j]->x, sets[j]->y,
1055 sets[j]->mask, x, y, 1,
1061 known_squares(w, h, std, grid,
1063 x, y, 1, minesleft != 0);
1066 done_something = TRUE;
1067 break; /* return to main deductive loop */
1071 * If we reach here, then this union hasn't
1072 * done us any good, so move on to the
1073 * next. Backtrack cursor to the nearest 1,
1074 * change it to a 0 and continue.
1076 while (--cursor >= 0 && !setused[cursor]);
1078 assert(setused[cursor]);
1081 * We're removing this set from our
1082 * union, so re-increment minesleft and
1085 minesleft += sets[cursor]->mines;
1086 squaresleft += bitcount16(sets[cursor]->mask);
1088 setused[cursor++] = 0;
1091 * We've backtracked all the way to the
1092 * start without finding a single 1,
1093 * which means that our virtual
1094 * recursion is complete and nothing
1109 #ifdef SOLVER_DIAGNOSTICS
1111 * Dump the current known state of the grid.
1113 printf("solver ran out of steam, ret=%d, grid:\n", nperturbs);
1114 for (y = 0; y < h; y++) {
1115 for (x = 0; x < w; x++) {
1116 int v = grid[y*w+x];
1132 for (i = 0; (s = index234(ss->sets, i)) != NULL; i++)
1133 printf("remaining set: %d,%d %03x %d\n", s->x, s->y, s->mask, s->mines);
1138 * Now we really are at our wits' end as far as solving
1139 * this grid goes. Our only remaining option is to call
1140 * a perturb function and ask it to modify the grid to
1144 struct perturbations *ret;
1150 * Choose a set at random from the current selection,
1151 * and ask the perturb function to either fill or empty
1154 * If we have no sets at all, we must give up.
1156 if (count234(ss->sets) == 0) {
1157 #ifdef SOLVER_DIAGNOSTICS
1158 printf("perturbing on entire unknown set\n");
1160 ret = perturb(ctx, grid, 0, 0, 0);
1162 s = index234(ss->sets, random_upto(rs, count234(ss->sets)));
1163 #ifdef SOLVER_DIAGNOSTICS
1164 printf("perturbing on set %d,%d %03x\n", s->x, s->y, s->mask);
1166 ret = perturb(ctx, grid, s->x, s->y, s->mask);
1170 assert(ret->n > 0); /* otherwise should have been NULL */
1173 * A number of squares have been fiddled with, and
1174 * the returned structure tells us which. Adjust
1175 * the mine count in any set which overlaps one of
1176 * those squares, and put them back on the to-do
1177 * list. Also, if the square itself is marked as a
1178 * known non-mine, put it back on the squares-to-do
1181 for (i = 0; i < ret->n; i++) {
1182 #ifdef SOLVER_DIAGNOSTICS
1183 printf("perturbation %s mine at %d,%d\n",
1184 ret->changes[i].delta > 0 ? "added" : "removed",
1185 ret->changes[i].x, ret->changes[i].y);
1188 if (ret->changes[i].delta < 0 &&
1189 grid[ret->changes[i].y*w+ret->changes[i].x] != -2) {
1190 std_add(std, ret->changes[i].y*w+ret->changes[i].x);
1193 list = ss_overlap(ss,
1194 ret->changes[i].x, ret->changes[i].y, 1);
1196 for (j = 0; list[j]; j++) {
1197 list[j]->mines += ret->changes[i].delta;
1198 ss_add_todo(ss, list[j]);
1205 * Now free the returned data.
1207 sfree(ret->changes);
1210 #ifdef SOLVER_DIAGNOSTICS
1212 * Dump the current known state of the grid.
1214 printf("state after perturbation:\n");
1215 for (y = 0; y < h; y++) {
1216 for (x = 0; x < w; x++) {
1217 int v = grid[y*w+x];
1233 for (i = 0; (s = index234(ss->sets, i)) != NULL; i++)
1234 printf("remaining set: %d,%d %03x %d\n", s->x, s->y, s->mask, s->mines);
1239 * And now we can go back round the deductive loop.
1246 * If we get here, even that didn't work (either we didn't
1247 * have a perturb function or it returned failure), so we
1254 * See if we've got any unknown squares left.
1256 for (y = 0; y < h; y++)
1257 for (x = 0; x < w; x++)
1258 if (grid[y*w+x] == -2) {
1259 nperturbs = -1; /* failed to complete */
1264 * Free the set list and square-todo list.
1268 while ((s = delpos234(ss->sets, 0)) != NULL)
1270 freetree234(ss->sets);
1278 /* ----------------------------------------------------------------------
1279 * Grid generator which uses the above solver.
1286 int allow_big_perturbs;
1290 static int mineopen(void *vctx, int x, int y)
1292 struct minectx *ctx = (struct minectx *)vctx;
1295 assert(x >= 0 && x < ctx->w && y >= 0 && y < ctx->h);
1296 if (ctx->grid[y * ctx->w + x])
1297 return -1; /* *bang* */
1300 for (i = -1; i <= +1; i++) {
1301 if (x + i < 0 || x + i >= ctx->w)
1303 for (j = -1; j <= +1; j++) {
1304 if (y + j < 0 || y + j >= ctx->h)
1306 if (i == 0 && j == 0)
1308 if (ctx->grid[(y+j) * ctx->w + (x+i)])
1316 /* Structure used internally to mineperturb(). */
1318 int x, y, type, random;
1320 static int squarecmp(const void *av, const void *bv)
1322 const struct square *a = (const struct square *)av;
1323 const struct square *b = (const struct square *)bv;
1324 if (a->type < b->type)
1326 else if (a->type > b->type)
1328 else if (a->random < b->random)
1330 else if (a->random > b->random)
1332 else if (a->y < b->y)
1334 else if (a->y > b->y)
1336 else if (a->x < b->x)
1338 else if (a->x > b->x)
1344 * Normally this function is passed an (x,y,mask) set description.
1345 * On occasions, though, there is no _localised_ set being used,
1346 * and the set being perturbed is supposed to be the entirety of
1347 * the unreachable area. This is signified by the special case
1348 * mask==0: in this case, anything labelled -2 in the grid is part
1351 * Allowing perturbation in this special case appears to make it
1352 * guaranteeably possible to generate a workable grid for any mine
1353 * density, but they tend to be a bit boring, with mines packed
1354 * densely into far corners of the grid and the remainder being
1355 * less dense than one might like. Therefore, to improve overall
1356 * grid quality I disable this feature for the first few attempts,
1357 * and fall back to it after no useful grid has been generated.
1359 static struct perturbations *mineperturb(void *vctx, signed char *grid,
1360 int setx, int sety, int mask)
1362 struct minectx *ctx = (struct minectx *)vctx;
1363 struct square *sqlist;
1364 int x, y, dx, dy, i, n, nfull, nempty;
1365 struct square **tofill, **toempty, **todo;
1366 int ntofill, ntoempty, ntodo, dtodo, dset;
1367 struct perturbations *ret;
1370 if (!mask && !ctx->allow_big_perturbs)
1374 * Make a list of all the squares in the grid which we can
1375 * possibly use. This list should be in preference order, which
1378 * - first, unknown squares on the boundary of known space
1379 * - next, unknown squares beyond that boundary
1380 * - as a very last resort, known squares, but not within one
1381 * square of the starting position.
1383 * Each of these sections needs to be shuffled independently.
1384 * We do this by preparing list of all squares and then sorting
1385 * it with a random secondary key.
1387 sqlist = snewn(ctx->w * ctx->h, struct square);
1389 for (y = 0; y < ctx->h; y++)
1390 for (x = 0; x < ctx->w; x++) {
1392 * If this square is too near the starting position,
1393 * don't put it on the list at all.
1395 if (abs(y - ctx->sy) <= 1 && abs(x - ctx->sx) <= 1)
1399 * If this square is in the input set, also don't put
1402 if ((mask == 0 && grid[y*ctx->w+x] == -2) ||
1403 (x >= setx && x < setx + 3 &&
1404 y >= sety && y < sety + 3 &&
1405 mask & (1 << ((y-sety)*3+(x-setx)))))
1411 if (grid[y*ctx->w+x] != -2) {
1412 sqlist[n].type = 3; /* known square */
1415 * Unknown square. Examine everything around it and
1416 * see if it borders on any known squares. If it
1417 * does, it's class 1, otherwise it's 2.
1422 for (dy = -1; dy <= +1; dy++)
1423 for (dx = -1; dx <= +1; dx++)
1424 if (x+dx >= 0 && x+dx < ctx->w &&
1425 y+dy >= 0 && y+dy < ctx->h &&
1426 grid[(y+dy)*ctx->w+(x+dx)] != -2) {
1433 * Finally, a random number to cause qsort to
1434 * shuffle within each group.
1436 sqlist[n].random = random_bits(ctx->rs, 31);
1441 qsort(sqlist, n, sizeof(struct square), squarecmp);
1444 * Now count up the number of full and empty squares in the set
1445 * we've been provided.
1449 for (dy = 0; dy < 3; dy++)
1450 for (dx = 0; dx < 3; dx++)
1451 if (mask & (1 << (dy*3+dx))) {
1452 assert(setx+dx <= ctx->w);
1453 assert(sety+dy <= ctx->h);
1454 if (ctx->grid[(sety+dy)*ctx->w+(setx+dx)])
1460 for (y = 0; y < ctx->h; y++)
1461 for (x = 0; x < ctx->w; x++)
1462 if (grid[y*ctx->w+x] == -2) {
1463 if (ctx->grid[y*ctx->w+x])
1471 * Now go through our sorted list until we find either `nfull'
1472 * empty squares, or `nempty' full squares; these will be
1473 * swapped with the appropriate squares in the set to either
1474 * fill or empty the set while keeping the same number of mines
1477 ntofill = ntoempty = 0;
1479 tofill = snewn(9, struct square *);
1480 toempty = snewn(9, struct square *);
1482 tofill = snewn(ctx->w * ctx->h, struct square *);
1483 toempty = snewn(ctx->w * ctx->h, struct square *);
1485 for (i = 0; i < n; i++) {
1486 struct square *sq = &sqlist[i];
1487 if (ctx->grid[sq->y * ctx->w + sq->x])
1488 toempty[ntoempty++] = sq;
1490 tofill[ntofill++] = sq;
1491 if (ntofill == nfull || ntoempty == nempty)
1496 * If we haven't found enough empty squares outside the set to
1497 * empty it into _or_ enough full squares outside it to fill it
1498 * up with, we'll have to settle for doing only a partial job.
1499 * In this case we choose to always _fill_ the set (because
1500 * this case will tend to crop up when we're working with very
1501 * high mine densities and the only way to get a solvable grid
1502 * is going to be to pack most of the mines solidly around the
1503 * edges). So now our job is to make a list of the empty
1504 * squares in the set, and shuffle that list so that we fill a
1505 * random selection of them.
1507 if (ntofill != nfull && ntoempty != nempty) {
1510 assert(ntoempty != 0);
1512 setlist = snewn(ctx->w * ctx->h, int);
1515 for (dy = 0; dy < 3; dy++)
1516 for (dx = 0; dx < 3; dx++)
1517 if (mask & (1 << (dy*3+dx))) {
1518 assert(setx+dx <= ctx->w);
1519 assert(sety+dy <= ctx->h);
1520 if (!ctx->grid[(sety+dy)*ctx->w+(setx+dx)])
1521 setlist[i++] = (sety+dy)*ctx->w+(setx+dx);
1524 for (y = 0; y < ctx->h; y++)
1525 for (x = 0; x < ctx->w; x++)
1526 if (grid[y*ctx->w+x] == -2) {
1527 if (!ctx->grid[y*ctx->w+x])
1528 setlist[i++] = y*ctx->w+x;
1531 assert(i > ntoempty);
1533 * Now pick `ntoempty' items at random from the list.
1535 for (k = 0; k < ntoempty; k++) {
1536 int index = k + random_upto(ctx->rs, i - k);
1540 setlist[k] = setlist[index];
1541 setlist[index] = tmp;
1547 * Now we're pretty much there. We need to either
1548 * (a) put a mine in each of the empty squares in the set, and
1549 * take one out of each square in `toempty'
1550 * (b) take a mine out of each of the full squares in the set,
1551 * and put one in each square in `tofill'
1552 * depending on which one we've found enough squares to do.
1554 * So we start by constructing our list of changes to return to
1555 * the solver, so that it can update its data structures
1556 * efficiently rather than having to rescan the whole grid.
1558 ret = snew(struct perturbations);
1559 if (ntofill == nfull) {
1567 * (We also fall into this case if we've constructed a
1577 ret->changes = snewn(ret->n, struct perturbation);
1578 for (i = 0; i < ntodo; i++) {
1579 ret->changes[i].x = todo[i]->x;
1580 ret->changes[i].y = todo[i]->y;
1581 ret->changes[i].delta = dtodo;
1583 /* now i == ntodo */
1586 assert(todo == toempty);
1587 for (j = 0; j < ntoempty; j++) {
1588 ret->changes[i].x = setlist[j] % ctx->w;
1589 ret->changes[i].y = setlist[j] / ctx->w;
1590 ret->changes[i].delta = dset;
1595 for (dy = 0; dy < 3; dy++)
1596 for (dx = 0; dx < 3; dx++)
1597 if (mask & (1 << (dy*3+dx))) {
1598 int currval = (ctx->grid[(sety+dy)*ctx->w+(setx+dx)] ? +1 : -1);
1599 if (dset == -currval) {
1600 ret->changes[i].x = setx + dx;
1601 ret->changes[i].y = sety + dy;
1602 ret->changes[i].delta = dset;
1607 for (y = 0; y < ctx->h; y++)
1608 for (x = 0; x < ctx->w; x++)
1609 if (grid[y*ctx->w+x] == -2) {
1610 int currval = (ctx->grid[y*ctx->w+x] ? +1 : -1);
1611 if (dset == -currval) {
1612 ret->changes[i].x = x;
1613 ret->changes[i].y = y;
1614 ret->changes[i].delta = dset;
1619 assert(i == ret->n);
1625 * Having set up the precise list of changes we're going to
1626 * make, we now simply make them and return.
1628 for (i = 0; i < ret->n; i++) {
1631 x = ret->changes[i].x;
1632 y = ret->changes[i].y;
1633 delta = ret->changes[i].delta;
1636 * Check we're not trying to add an existing mine or remove
1639 assert((delta < 0) ^ (ctx->grid[y*ctx->w+x] == 0));
1642 * Actually make the change.
1644 ctx->grid[y*ctx->w+x] = (delta > 0);
1647 * Update any numbers already present in the grid.
1649 for (dy = -1; dy <= +1; dy++)
1650 for (dx = -1; dx <= +1; dx++)
1651 if (x+dx >= 0 && x+dx < ctx->w &&
1652 y+dy >= 0 && y+dy < ctx->h &&
1653 grid[(y+dy)*ctx->w+(x+dx)] != -2) {
1654 if (dx == 0 && dy == 0) {
1656 * The square itself is marked as known in
1657 * the grid. Mark it as a mine if it's a
1658 * mine, or else work out its number.
1661 grid[y*ctx->w+x] = -1;
1663 int dx2, dy2, minecount = 0;
1664 for (dy2 = -1; dy2 <= +1; dy2++)
1665 for (dx2 = -1; dx2 <= +1; dx2++)
1666 if (x+dx2 >= 0 && x+dx2 < ctx->w &&
1667 y+dy2 >= 0 && y+dy2 < ctx->h &&
1668 ctx->grid[(y+dy2)*ctx->w+(x+dx2)])
1670 grid[y*ctx->w+x] = minecount;
1673 if (grid[(y+dy)*ctx->w+(x+dx)] >= 0)
1674 grid[(y+dy)*ctx->w+(x+dx)] += delta;
1679 #ifdef GENERATION_DIAGNOSTICS
1682 printf("grid after perturbing:\n");
1683 for (yy = 0; yy < ctx->h; yy++) {
1684 for (xx = 0; xx < ctx->w; xx++) {
1685 int v = ctx->grid[yy*ctx->w+xx];
1686 if (yy == ctx->sy && xx == ctx->sx) {
1704 static char *minegen(int w, int h, int n, int x, int y, int unique,
1707 char *ret = snewn(w*h, char);
1715 memset(ret, 0, w*h);
1718 * Start by placing n mines, none of which is at x,y or within
1722 int *tmp = snewn(w*h, int);
1726 * Write down the list of possible mine locations.
1729 for (i = 0; i < h; i++)
1730 for (j = 0; j < w; j++)
1731 if (abs(i - y) > 1 || abs(j - x) > 1)
1735 * Now pick n off the list at random.
1739 i = random_upto(rs, k);
1747 #ifdef GENERATION_DIAGNOSTICS
1750 printf("grid after initial generation:\n");
1751 for (yy = 0; yy < h; yy++) {
1752 for (xx = 0; xx < w; xx++) {
1753 int v = ret[yy*w+xx];
1754 if (yy == y && xx == x) {
1770 * Now set up a results grid to run the solver in, and a
1771 * context for the solver to open squares. Then run the solver
1772 * repeatedly; if the number of perturb steps ever goes up or
1773 * it ever returns -1, give up completely.
1775 * We bypass this bit if we're not after a unique grid.
1778 signed char *solvegrid = snewn(w*h, char);
1779 struct minectx actx, *ctx = &actx;
1780 int solveret, prevret = -2;
1788 ctx->allow_big_perturbs = (ntries > 100);
1791 memset(solvegrid, -2, w*h);
1792 solvegrid[y*w+x] = mineopen(ctx, x, y);
1793 assert(solvegrid[y*w+x] == 0); /* by deliberate arrangement */
1796 minesolve(w, h, n, solvegrid, mineopen, mineperturb, ctx, rs);
1797 if (solveret < 0 || (prevret >= 0 && solveret >= prevret)) {
1800 } else if (solveret == 0) {
1817 * The Mines game descriptions contain the location of every mine,
1818 * and can therefore be used to cheat.
1820 * It would be pointless to attempt to _prevent_ this form of
1821 * cheating by encrypting the description, since Mines is
1822 * open-source so anyone can find out the encryption key. However,
1823 * I think it is worth doing a bit of gentle obfuscation to prevent
1824 * _accidental_ spoilers: if you happened to note that the game ID
1825 * starts with an F, for example, you might be unable to put the
1826 * knowledge of those mines out of your mind while playing. So,
1827 * just as discussions of film endings are rot13ed to avoid
1828 * spoiling it for people who don't want to be told, we apply a
1829 * keyless, reversible, but visually completely obfuscatory masking
1830 * function to the mine bitmap.
1832 static void obfuscate_bitmap(unsigned char *bmp, int bits, int decode)
1834 int bytes, firsthalf, secondhalf;
1836 unsigned char *seedstart;
1838 unsigned char *targetstart;
1844 * My obfuscation algorithm is similar in concept to the OAEP
1845 * encoding used in some forms of RSA. Here's a specification
1848 * + We have a `masking function' which constructs a stream of
1849 * pseudorandom bytes from a seed of some number of input
1852 * + We pad out our input bit stream to a whole number of
1853 * bytes by adding up to 7 zero bits on the end. (In fact
1854 * the bitmap passed as input to this function will already
1855 * have had this done in practice.)
1857 * + We divide the _byte_ stream exactly in half, rounding the
1858 * half-way position _down_. So an 81-bit input string, for
1859 * example, rounds up to 88 bits or 11 bytes, and then
1860 * dividing by two gives 5 bytes in the first half and 6 in
1863 * + We generate a mask from the second half of the bytes, and
1864 * XOR it over the first half.
1866 * + We generate a mask from the (encoded) first half of the
1867 * bytes, and XOR it over the second half. Any null bits at
1868 * the end which were added as padding are cleared back to
1869 * zero even if this operation would have made them nonzero.
1871 * To de-obfuscate, the steps are precisely the same except
1872 * that the final two are reversed.
1874 * Finally, our masking function. Given an input seed string of
1875 * bytes, the output mask consists of concatenating the SHA-1
1876 * hashes of the seed string and successive decimal integers,
1880 bytes = (bits + 7) / 8;
1881 firsthalf = bytes / 2;
1882 secondhalf = bytes - firsthalf;
1884 steps[decode ? 1 : 0].seedstart = bmp + firsthalf;
1885 steps[decode ? 1 : 0].seedlen = secondhalf;
1886 steps[decode ? 1 : 0].targetstart = bmp;
1887 steps[decode ? 1 : 0].targetlen = firsthalf;
1889 steps[decode ? 0 : 1].seedstart = bmp;
1890 steps[decode ? 0 : 1].seedlen = firsthalf;
1891 steps[decode ? 0 : 1].targetstart = bmp + firsthalf;
1892 steps[decode ? 0 : 1].targetlen = secondhalf;
1894 for (i = 0; i < 2; i++) {
1895 SHA_State base, final;
1896 unsigned char digest[20];
1898 int digestpos = 20, counter = 0;
1901 SHA_Bytes(&base, steps[i].seedstart, steps[i].seedlen);
1903 for (j = 0; j < steps[i].targetlen; j++) {
1904 if (digestpos >= 20) {
1905 sprintf(numberbuf, "%d", counter++);
1907 SHA_Bytes(&final, numberbuf, strlen(numberbuf));
1908 SHA_Final(&final, digest);
1911 steps[i].targetstart[j] ^= digest[digestpos++];
1915 * Mask off the pad bits in the final byte after both steps.
1918 bmp[bits / 8] &= 0xFF & (0xFF00 >> (bits % 8));
1922 static char *new_mine_layout(int w, int h, int n, int x, int y, int unique,
1923 random_state *rs, char **game_desc)
1925 signed char *grid, *ret, *p;
1929 #ifdef TEST_OBFUSCATION
1930 static int tested_obfuscation = FALSE;
1931 if (!tested_obfuscation) {
1933 * A few simple test vectors for the obfuscator.
1935 * First test: the 28-bit stream 1234567. This divides up
1936 * into 1234 and 567[0]. The SHA of 56 70 30 (appending
1937 * "0") is 15ce8ab946640340bbb99f3f48fd2c45d1a31d30. Thus,
1938 * we XOR the 16-bit string 15CE into the input 1234 to get
1939 * 07FA. Next, we SHA that with "0": the SHA of 07 FA 30 is
1940 * 3370135c5e3da4fed937adc004a79533962b6391. So we XOR the
1941 * 12-bit string 337 into the input 567 to get 650. Thus
1942 * our output is 07FA650.
1945 unsigned char bmp1[] = "\x12\x34\x56\x70";
1946 obfuscate_bitmap(bmp1, 28, FALSE);
1947 printf("test 1 encode: %s\n",
1948 memcmp(bmp1, "\x07\xfa\x65\x00", 4) ? "failed" : "passed");
1949 obfuscate_bitmap(bmp1, 28, TRUE);
1950 printf("test 1 decode: %s\n",
1951 memcmp(bmp1, "\x12\x34\x56\x70", 4) ? "failed" : "passed");
1954 * Second test: a long string to make sure we switch from
1955 * one SHA to the next correctly. My input string this time
1956 * is simply fifty bytes of zeroes.
1959 unsigned char bmp2[50];
1960 unsigned char bmp2a[50];
1961 memset(bmp2, 0, 50);
1962 memset(bmp2a, 0, 50);
1963 obfuscate_bitmap(bmp2, 50 * 8, FALSE);
1965 * SHA of twenty-five zero bytes plus "0" is
1966 * b202c07b990c01f6ff2d544707f60e506019b671. SHA of
1967 * twenty-five zero bytes plus "1" is
1968 * fcb1d8b5a2f6b592fe6780b36aa9d65dd7aa6db9. Thus our
1969 * first half becomes
1970 * b202c07b990c01f6ff2d544707f60e506019b671fcb1d8b5a2.
1972 * SHA of that lot plus "0" is
1973 * 10b0af913db85d37ca27f52a9f78bba3a80030db. SHA of the
1974 * same string plus "1" is
1975 * 3d01d8df78e76d382b8106f480135a1bc751d725. So the
1976 * second half becomes
1977 * 10b0af913db85d37ca27f52a9f78bba3a80030db3d01d8df78.
1979 printf("test 2 encode: %s\n",
1980 memcmp(bmp2, "\xb2\x02\xc0\x7b\x99\x0c\x01\xf6\xff\x2d\x54"
1981 "\x47\x07\xf6\x0e\x50\x60\x19\xb6\x71\xfc\xb1\xd8"
1982 "\xb5\xa2\x10\xb0\xaf\x91\x3d\xb8\x5d\x37\xca\x27"
1983 "\xf5\x2a\x9f\x78\xbb\xa3\xa8\x00\x30\xdb\x3d\x01"
1984 "\xd8\xdf\x78", 50) ? "failed" : "passed");
1985 obfuscate_bitmap(bmp2, 50 * 8, TRUE);
1986 printf("test 2 decode: %s\n",
1987 memcmp(bmp2, bmp2a, 50) ? "failed" : "passed");
1992 grid = minegen(w, h, n, x, y, unique, rs);
1996 * Set up the mine bitmap and obfuscate it.
1999 bmp = snewn((area + 7) / 8, unsigned char);
2000 memset(bmp, 0, (area + 7) / 8);
2001 for (i = 0; i < area; i++) {
2003 bmp[i / 8] |= 0x80 >> (i % 8);
2005 obfuscate_bitmap(bmp, area, FALSE);
2008 * Now encode the resulting bitmap in hex. We can work to
2009 * nibble rather than byte granularity, since the obfuscation
2010 * function guarantees to return a bit string of the same
2011 * length as its input.
2013 ret = snewn((area+3)/4 + 100, char);
2014 p = ret + sprintf(ret, "%d,%d,m", x, y); /* 'm' == masked */
2015 for (i = 0; i < (area+3)/4; i++) {
2019 *p++ = "0123456789abcdef"[v & 0xF];
2031 static char *new_game_desc(game_params *params, random_state *rs,
2032 game_aux_info **aux, int interactive)
2036 * For batch-generated grids, pre-open one square.
2038 int x = random_upto(rs, params->w);
2039 int y = random_upto(rs, params->h);
2043 grid = new_mine_layout(params->w, params->h, params->n,
2044 x, y, params->unique, rs, &desc);
2048 char *rsdesc, *desc;
2050 rsdesc = random_state_encode(rs);
2051 desc = snewn(strlen(rsdesc) + 100, char);
2052 sprintf(desc, "r%d,%c,%s", params->n, params->unique ? 'u' : 'a', rsdesc);
2058 static void game_free_aux_info(game_aux_info *aux)
2060 assert(!"Shouldn't happen");
2063 static char *validate_desc(game_params *params, char *desc)
2065 int wh = params->w * params->h;
2069 if (!*desc || !isdigit((unsigned char)*desc))
2070 return "No initial mine count in game description";
2071 while (*desc && isdigit((unsigned char)*desc))
2072 desc++; /* skip over mine count */
2074 return "No ',' after initial x-coordinate in game description";
2076 if (*desc != 'u' && *desc != 'a')
2077 return "No uniqueness specifier in game description";
2080 return "No ',' after uniqueness specifier in game description";
2081 /* now ignore the rest */
2083 if (!*desc || !isdigit((unsigned char)*desc))
2084 return "No initial x-coordinate in game description";
2086 if (x < 0 || x >= params->w)
2087 return "Initial x-coordinate was out of range";
2088 while (*desc && isdigit((unsigned char)*desc))
2089 desc++; /* skip over x coordinate */
2091 return "No ',' after initial x-coordinate in game description";
2092 desc++; /* eat comma */
2093 if (!*desc || !isdigit((unsigned char)*desc))
2094 return "No initial y-coordinate in game description";
2096 if (y < 0 || y >= params->h)
2097 return "Initial y-coordinate was out of range";
2098 while (*desc && isdigit((unsigned char)*desc))
2099 desc++; /* skip over y coordinate */
2101 return "No ',' after initial y-coordinate in game description";
2102 desc++; /* eat comma */
2103 /* eat `m', meaning `masked', if present */
2106 /* now just check length of remainder */
2107 if (strlen(desc) != (wh+3)/4)
2108 return "Game description is wrong length";
2114 static int open_square(game_state *state, int x, int y)
2116 int w = state->w, h = state->h;
2117 int xx, yy, nmines, ncovered;
2119 if (!state->layout->mines) {
2121 * We have a preliminary game in which the mine layout
2122 * hasn't been generated yet. Generate it based on the
2123 * initial click location.
2126 state->layout->mines = new_mine_layout(w, h, state->layout->n,
2127 x, y, state->layout->unique,
2130 midend_supersede_game_desc(state->layout->me, desc);
2132 random_free(state->layout->rs);
2133 state->layout->rs = NULL;
2136 if (state->layout->mines[y*w+x]) {
2138 * The player has landed on a mine. Bad luck. Expose the
2139 * mine that killed them, but not the rest (in case they
2140 * want to Undo and carry on playing).
2143 state->grid[y*w+x] = 65;
2148 * Otherwise, the player has opened a safe square. Mark it to-do.
2150 state->grid[y*w+x] = -10; /* `todo' value internal to this func */
2153 * Now go through the grid finding all `todo' values and
2154 * opening them. Every time one of them turns out to have no
2155 * neighbouring mines, we add all its unopened neighbours to
2158 * FIXME: We really ought to be able to do this better than
2159 * using repeated N^2 scans of the grid.
2162 int done_something = FALSE;
2164 for (yy = 0; yy < h; yy++)
2165 for (xx = 0; xx < w; xx++)
2166 if (state->grid[yy*w+xx] == -10) {
2169 assert(!state->layout->mines[yy*w+xx]);
2173 for (dx = -1; dx <= +1; dx++)
2174 for (dy = -1; dy <= +1; dy++)
2175 if (xx+dx >= 0 && xx+dx < state->w &&
2176 yy+dy >= 0 && yy+dy < state->h &&
2177 state->layout->mines[(yy+dy)*w+(xx+dx)])
2180 state->grid[yy*w+xx] = v;
2183 for (dx = -1; dx <= +1; dx++)
2184 for (dy = -1; dy <= +1; dy++)
2185 if (xx+dx >= 0 && xx+dx < state->w &&
2186 yy+dy >= 0 && yy+dy < state->h &&
2187 state->grid[(yy+dy)*w+(xx+dx)] == -2)
2188 state->grid[(yy+dy)*w+(xx+dx)] = -10;
2191 done_something = TRUE;
2194 if (!done_something)
2199 * Finally, scan the grid and see if exactly as many squares
2200 * are still covered as there are mines. If so, set the `won'
2201 * flag and fill in mine markers on all covered squares.
2203 nmines = ncovered = 0;
2204 for (yy = 0; yy < h; yy++)
2205 for (xx = 0; xx < w; xx++) {
2206 if (state->grid[yy*w+xx] < 0)
2208 if (state->layout->mines[yy*w+xx])
2211 assert(ncovered >= nmines);
2212 if (ncovered == nmines) {
2213 for (yy = 0; yy < h; yy++)
2214 for (xx = 0; xx < w; xx++) {
2215 if (state->grid[yy*w+xx] < 0)
2216 state->grid[yy*w+xx] = -1;
2224 static game_state *new_game(midend_data *me, game_params *params, char *desc)
2226 game_state *state = snew(game_state);
2227 int i, wh, x, y, ret, masked;
2230 state->w = params->w;
2231 state->h = params->h;
2232 state->n = params->n;
2233 state->dead = state->won = FALSE;
2234 state->used_solve = state->just_used_solve = FALSE;
2236 wh = state->w * state->h;
2238 state->layout = snew(struct mine_layout);
2239 state->layout->refcount = 1;
2241 state->grid = snewn(wh, char);
2242 memset(state->grid, -2, wh);
2246 state->layout->n = atoi(desc);
2247 while (*desc && isdigit((unsigned char)*desc))
2248 desc++; /* skip over mine count */
2249 if (*desc) desc++; /* eat comma */
2251 state->layout->unique = FALSE;
2253 state->layout->unique = TRUE;
2255 if (*desc) desc++; /* eat comma */
2257 state->layout->mines = NULL;
2258 state->layout->rs = random_state_decode(desc);
2259 state->layout->me = me;
2262 state->layout->rs = NULL;
2263 state->layout->me = NULL;
2265 state->layout->mines = snewn(wh, char);
2267 while (*desc && isdigit((unsigned char)*desc))
2268 desc++; /* skip over x coordinate */
2269 if (*desc) desc++; /* eat comma */
2271 while (*desc && isdigit((unsigned char)*desc))
2272 desc++; /* skip over y coordinate */
2273 if (*desc) desc++; /* eat comma */
2280 * We permit game IDs to be entered by hand without the
2281 * masking transformation.
2286 bmp = snewn((wh + 7) / 8, unsigned char);
2287 memset(bmp, 0, (wh + 7) / 8);
2288 for (i = 0; i < (wh+3)/4; i++) {
2292 assert(c != 0); /* validate_desc should have caught */
2293 if (c >= '0' && c <= '9')
2295 else if (c >= 'a' && c <= 'f')
2297 else if (c >= 'A' && c <= 'F')
2302 bmp[i / 2] |= v << (4 * (1 - (i % 2)));
2306 obfuscate_bitmap(bmp, wh, TRUE);
2308 memset(state->layout->mines, 0, wh);
2309 for (i = 0; i < wh; i++) {
2310 if (bmp[i / 8] & (0x80 >> (i % 8)))
2311 state->layout->mines[i] = 1;
2314 ret = open_square(state, x, y);
2320 static game_state *dup_game(game_state *state)
2322 game_state *ret = snew(game_state);
2327 ret->dead = state->dead;
2328 ret->won = state->won;
2329 ret->used_solve = state->used_solve;
2330 ret->just_used_solve = state->just_used_solve;
2331 ret->layout = state->layout;
2332 ret->layout->refcount++;
2333 ret->grid = snewn(ret->w * ret->h, char);
2334 memcpy(ret->grid, state->grid, ret->w * ret->h);
2339 static void free_game(game_state *state)
2341 if (--state->layout->refcount <= 0) {
2342 sfree(state->layout->mines);
2343 if (state->layout->rs)
2344 random_free(state->layout->rs);
2345 sfree(state->layout);
2351 static game_state *solve_game(game_state *state, game_aux_info *aux,
2355 * Simply expose the entire grid as if it were a completed
2361 if (!state->layout->mines) {
2362 *error = "Game has not been started yet";
2366 ret = dup_game(state);
2367 for (yy = 0; yy < ret->h; yy++)
2368 for (xx = 0; xx < ret->w; xx++) {
2370 if (ret->layout->mines[yy*ret->w+xx]) {
2371 ret->grid[yy*ret->w+xx] = -1;
2377 for (dx = -1; dx <= +1; dx++)
2378 for (dy = -1; dy <= +1; dy++)
2379 if (xx+dx >= 0 && xx+dx < ret->w &&
2380 yy+dy >= 0 && yy+dy < ret->h &&
2381 ret->layout->mines[(yy+dy)*ret->w+(xx+dx)])
2384 ret->grid[yy*ret->w+xx] = v;
2387 ret->used_solve = ret->just_used_solve = TRUE;
2393 static char *game_text_format(game_state *state)
2398 ret = snewn((state->w + 1) * state->h + 1, char);
2399 for (y = 0; y < state->h; y++) {
2400 for (x = 0; x < state->w; x++) {
2401 int v = state->grid[y*state->w+x];
2404 else if (v >= 1 && v <= 8)
2408 else if (v == -2 || v == -3)
2412 ret[y * (state->w+1) + x] = v;
2414 ret[y * (state->w+1) + state->w] = '\n';
2416 ret[(state->w + 1) * state->h] = '\0';
2422 int hx, hy, hradius; /* for mouse-down highlights */
2427 static game_ui *new_ui(game_state *state)
2429 game_ui *ui = snew(game_ui);
2430 ui->hx = ui->hy = -1;
2433 ui->flash_is_death = FALSE; /* *shrug* */
2437 static void free_ui(game_ui *ui)
2442 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
2443 int x, int y, int button)
2448 if (from->dead || from->won)
2449 return NULL; /* no further moves permitted */
2451 if (!IS_MOUSE_DOWN(button) && !IS_MOUSE_DRAG(button) &&
2452 !IS_MOUSE_RELEASE(button))
2457 if (cx < 0 || cx >= from->w || cy < 0 || cy >= from->h)
2460 if (button == LEFT_BUTTON || button == LEFT_DRAG ||
2461 button == MIDDLE_BUTTON || button == MIDDLE_DRAG) {
2463 * Mouse-downs and mouse-drags just cause highlighting
2468 ui->hradius = (from->grid[cy*from->w+cx] >= 0 ? 1 : 0);
2472 if (button == RIGHT_BUTTON) {
2474 * Right-clicking only works on a covered square, and it
2475 * toggles between -1 (marked as mine) and -2 (not marked
2478 * FIXME: question marks.
2480 if (from->grid[cy * from->w + cx] != -2 &&
2481 from->grid[cy * from->w + cx] != -1)
2484 ret = dup_game(from);
2485 ret->just_used_solve = FALSE;
2486 ret->grid[cy * from->w + cx] ^= (-2 ^ -1);
2491 if (button == LEFT_RELEASE || button == MIDDLE_RELEASE) {
2492 ui->hx = ui->hy = -1;
2496 * At this stage we must never return NULL: we have adjusted
2497 * the ui, so at worst we return `from'.
2501 * Left-clicking on a covered square opens a tile. Not
2502 * permitted if the tile is marked as a mine, for safety.
2503 * (Unmark it and _then_ open it.)
2505 if (button == LEFT_RELEASE &&
2506 (from->grid[cy * from->w + cx] == -2 ||
2507 from->grid[cy * from->w + cx] == -3)) {
2508 ret = dup_game(from);
2509 ret->just_used_solve = FALSE;
2510 open_square(ret, cx, cy);
2517 * Left-clicking or middle-clicking on an uncovered tile:
2518 * first we check to see if the number of mine markers
2519 * surrounding the tile is equal to its mine count, and if
2520 * so then we open all other surrounding squares.
2522 if (from->grid[cy * from->w + cx] > 0) {
2525 /* Count mine markers. */
2527 for (dy = -1; dy <= +1; dy++)
2528 for (dx = -1; dx <= +1; dx++)
2529 if (cx+dx >= 0 && cx+dx < from->w &&
2530 cy+dy >= 0 && cy+dy < from->h) {
2531 if (from->grid[(cy+dy)*from->w+(cx+dx)] == -1)
2535 if (n == from->grid[cy * from->w + cx]) {
2536 ret = dup_game(from);
2537 ret->just_used_solve = FALSE;
2538 for (dy = -1; dy <= +1; dy++)
2539 for (dx = -1; dx <= +1; dx++)
2540 if (cx+dx >= 0 && cx+dx < ret->w &&
2541 cy+dy >= 0 && cy+dy < ret->h &&
2542 (ret->grid[(cy+dy)*ret->w+(cx+dx)] == -2 ||
2543 ret->grid[(cy+dy)*ret->w+(cx+dx)] == -3))
2544 open_square(ret, cx+dx, cy+dy);
2557 /* ----------------------------------------------------------------------
2561 struct game_drawstate {
2565 * Items in this `grid' array have all the same values as in
2566 * the game_state grid, and in addition:
2568 * - -10 means the tile was drawn `specially' as a result of a
2569 * flash, so it will always need redrawing.
2571 * - -22 and -23 mean the tile is highlighted for a possible
2576 static void game_size(game_params *params, int *x, int *y)
2578 *x = BORDER * 2 + TILE_SIZE * params->w;
2579 *y = BORDER * 2 + TILE_SIZE * params->h;
2582 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
2584 float *ret = snewn(3 * NCOLOURS, float);
2586 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2588 ret[COL_BACKGROUND2 * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 19.0 / 20.0;
2589 ret[COL_BACKGROUND2 * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 19.0 / 20.0;
2590 ret[COL_BACKGROUND2 * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 19.0 / 20.0;
2592 ret[COL_1 * 3 + 0] = 0.0F;
2593 ret[COL_1 * 3 + 1] = 0.0F;
2594 ret[COL_1 * 3 + 2] = 1.0F;
2596 ret[COL_2 * 3 + 0] = 0.0F;
2597 ret[COL_2 * 3 + 1] = 0.5F;
2598 ret[COL_2 * 3 + 2] = 0.0F;
2600 ret[COL_3 * 3 + 0] = 1.0F;
2601 ret[COL_3 * 3 + 1] = 0.0F;
2602 ret[COL_3 * 3 + 2] = 0.0F;
2604 ret[COL_4 * 3 + 0] = 0.0F;
2605 ret[COL_4 * 3 + 1] = 0.0F;
2606 ret[COL_4 * 3 + 2] = 0.5F;
2608 ret[COL_5 * 3 + 0] = 0.5F;
2609 ret[COL_5 * 3 + 1] = 0.0F;
2610 ret[COL_5 * 3 + 2] = 0.0F;
2612 ret[COL_6 * 3 + 0] = 0.0F;
2613 ret[COL_6 * 3 + 1] = 0.5F;
2614 ret[COL_6 * 3 + 2] = 0.5F;
2616 ret[COL_7 * 3 + 0] = 0.0F;
2617 ret[COL_7 * 3 + 1] = 0.0F;
2618 ret[COL_7 * 3 + 2] = 0.0F;
2620 ret[COL_8 * 3 + 0] = 0.5F;
2621 ret[COL_8 * 3 + 1] = 0.5F;
2622 ret[COL_8 * 3 + 2] = 0.5F;
2624 ret[COL_MINE * 3 + 0] = 0.0F;
2625 ret[COL_MINE * 3 + 1] = 0.0F;
2626 ret[COL_MINE * 3 + 2] = 0.0F;
2628 ret[COL_BANG * 3 + 0] = 1.0F;
2629 ret[COL_BANG * 3 + 1] = 0.0F;
2630 ret[COL_BANG * 3 + 2] = 0.0F;
2632 ret[COL_CROSS * 3 + 0] = 1.0F;
2633 ret[COL_CROSS * 3 + 1] = 0.0F;
2634 ret[COL_CROSS * 3 + 2] = 0.0F;
2636 ret[COL_FLAG * 3 + 0] = 1.0F;
2637 ret[COL_FLAG * 3 + 1] = 0.0F;
2638 ret[COL_FLAG * 3 + 2] = 0.0F;
2640 ret[COL_FLAGBASE * 3 + 0] = 0.0F;
2641 ret[COL_FLAGBASE * 3 + 1] = 0.0F;
2642 ret[COL_FLAGBASE * 3 + 2] = 0.0F;
2644 ret[COL_QUERY * 3 + 0] = 0.0F;
2645 ret[COL_QUERY * 3 + 1] = 0.0F;
2646 ret[COL_QUERY * 3 + 2] = 0.0F;
2648 ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
2649 ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
2650 ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
2652 ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
2653 ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
2654 ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
2656 *ncolours = NCOLOURS;
2660 static game_drawstate *game_new_drawstate(game_state *state)
2662 struct game_drawstate *ds = snew(struct game_drawstate);
2666 ds->started = FALSE;
2667 ds->grid = snewn(ds->w * ds->h, char);
2669 memset(ds->grid, -99, ds->w * ds->h);
2674 static void game_free_drawstate(game_drawstate *ds)
2680 static void draw_tile(frontend *fe, int x, int y, int v, int bg)
2686 if (v == -22 || v == -23) {
2690 * Omit the highlights in this case.
2692 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
2693 bg == COL_BACKGROUND ? COL_BACKGROUND2 : bg);
2694 draw_line(fe, x, y, x + TILE_SIZE - 1, y, COL_LOWLIGHT);
2695 draw_line(fe, x, y, x, y + TILE_SIZE - 1, COL_LOWLIGHT);
2698 * Draw highlights to indicate the square is covered.
2700 coords[0] = x + TILE_SIZE - 1;
2701 coords[1] = y + TILE_SIZE - 1;
2702 coords[2] = x + TILE_SIZE - 1;
2705 coords[5] = y + TILE_SIZE - 1;
2706 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT ^ hl);
2707 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT ^ hl);
2711 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT ^ hl);
2712 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT ^ hl);
2714 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
2715 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
2723 #define SETCOORD(n, dx, dy) do { \
2724 coords[(n)*2+0] = x + TILE_SIZE * (dx); \
2725 coords[(n)*2+1] = y + TILE_SIZE * (dy); \
2727 SETCOORD(0, 0.6, 0.35);
2728 SETCOORD(1, 0.6, 0.7);
2729 SETCOORD(2, 0.8, 0.8);
2730 SETCOORD(3, 0.25, 0.8);
2731 SETCOORD(4, 0.55, 0.7);
2732 SETCOORD(5, 0.55, 0.35);
2733 draw_polygon(fe, coords, 6, TRUE, COL_FLAGBASE);
2734 draw_polygon(fe, coords, 6, FALSE, COL_FLAGBASE);
2736 SETCOORD(0, 0.6, 0.2);
2737 SETCOORD(1, 0.6, 0.5);
2738 SETCOORD(2, 0.2, 0.35);
2739 draw_polygon(fe, coords, 3, TRUE, COL_FLAG);
2740 draw_polygon(fe, coords, 3, FALSE, COL_FLAG);
2743 } else if (v == -3) {
2745 * Draw a question mark.
2747 draw_text(fe, x + TILE_SIZE / 2, y + TILE_SIZE / 2,
2748 FONT_VARIABLE, TILE_SIZE * 6 / 8,
2749 ALIGN_VCENTRE | ALIGN_HCENTRE,
2754 * Clear the square to the background colour, and draw thin
2755 * grid lines along the top and left.
2757 * Exception is that for value 65 (mine we've just trodden
2758 * on), we clear the square to COL_BANG.
2760 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
2761 (v == 65 ? COL_BANG :
2762 bg == COL_BACKGROUND ? COL_BACKGROUND2 : bg));
2763 draw_line(fe, x, y, x + TILE_SIZE - 1, y, COL_LOWLIGHT);
2764 draw_line(fe, x, y, x, y + TILE_SIZE - 1, COL_LOWLIGHT);
2766 if (v > 0 && v <= 8) {
2773 draw_text(fe, x + TILE_SIZE / 2, y + TILE_SIZE / 2,
2774 FONT_VARIABLE, TILE_SIZE * 7 / 8,
2775 ALIGN_VCENTRE | ALIGN_HCENTRE,
2776 (COL_1 - 1) + v, str);
2778 } else if (v >= 64) {
2782 * FIXME: this could be done better!
2785 draw_text(fe, x + TILE_SIZE / 2, y + TILE_SIZE / 2,
2786 FONT_VARIABLE, TILE_SIZE * 7 / 8,
2787 ALIGN_VCENTRE | ALIGN_HCENTRE,
2791 int cx = x + TILE_SIZE / 2;
2792 int cy = y + TILE_SIZE / 2;
2793 int r = TILE_SIZE / 2 - 3;
2795 int xdx = 1, xdy = 0, ydx = 0, ydy = 1;
2798 for (i = 0; i < 4*5*2; i += 5*2) {
2799 coords[i+2*0+0] = cx - r/6*xdx + r*4/5*ydx;
2800 coords[i+2*0+1] = cy - r/6*xdy + r*4/5*ydy;
2801 coords[i+2*1+0] = cx - r/6*xdx + r*ydx;
2802 coords[i+2*1+1] = cy - r/6*xdy + r*ydy;
2803 coords[i+2*2+0] = cx + r/6*xdx + r*ydx;
2804 coords[i+2*2+1] = cy + r/6*xdy + r*ydy;
2805 coords[i+2*3+0] = cx + r/6*xdx + r*4/5*ydx;
2806 coords[i+2*3+1] = cy + r/6*xdy + r*4/5*ydy;
2807 coords[i+2*4+0] = cx + r*3/5*xdx + r*3/5*ydx;
2808 coords[i+2*4+1] = cy + r*3/5*xdy + r*3/5*ydy;
2818 draw_polygon(fe, coords, 5*4, TRUE, COL_MINE);
2819 draw_polygon(fe, coords, 5*4, FALSE, COL_MINE);
2821 draw_rect(fe, cx-r/3, cy-r/3, r/3, r/4, COL_HIGHLIGHT);
2827 * Cross through the mine.
2830 for (dx = -1; dx <= +1; dx++) {
2831 draw_line(fe, x + 3 + dx, y + 2,
2832 x + TILE_SIZE - 3 + dx,
2833 y + TILE_SIZE - 2, COL_CROSS);
2834 draw_line(fe, x + TILE_SIZE - 3 + dx, y + 2,
2835 x + 3 + dx, y + TILE_SIZE - 2,
2842 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
2845 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
2846 game_state *state, int dir, game_ui *ui,
2847 float animtime, float flashtime)
2850 int mines, markers, bg;
2853 int frame = (flashtime / FLASH_FRAME);
2855 bg = (ui->flash_is_death ? COL_BACKGROUND : COL_LOWLIGHT);
2857 bg = (ui->flash_is_death ? COL_BANG : COL_HIGHLIGHT);
2859 bg = COL_BACKGROUND;
2865 TILE_SIZE * state->w + 2 * BORDER,
2866 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
2867 draw_update(fe, 0, 0,
2868 TILE_SIZE * state->w + 2 * BORDER,
2869 TILE_SIZE * state->h + 2 * BORDER);
2872 * Recessed area containing the whole puzzle.
2874 coords[0] = COORD(state->w) + OUTER_HIGHLIGHT_WIDTH - 1;
2875 coords[1] = COORD(state->h) + OUTER_HIGHLIGHT_WIDTH - 1;
2876 coords[2] = COORD(state->w) + OUTER_HIGHLIGHT_WIDTH - 1;
2877 coords[3] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2878 coords[4] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2879 coords[5] = COORD(state->h) + OUTER_HIGHLIGHT_WIDTH - 1;
2880 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
2881 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
2883 coords[1] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2884 coords[0] = COORD(0) - OUTER_HIGHLIGHT_WIDTH;
2885 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
2886 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
2892 * Now draw the tiles. Also in this loop, count up the number
2893 * of mines and mine markers.
2895 mines = markers = 0;
2896 for (y = 0; y < ds->h; y++)
2897 for (x = 0; x < ds->w; x++) {
2898 int v = state->grid[y*ds->w+x];
2902 if (state->layout->mines && state->layout->mines[y*ds->w+x])
2905 if ((v == -2 || v == -3) &&
2906 (abs(x-ui->hx) <= ui->hradius && abs(y-ui->hy) <= ui->hradius))
2909 if (ds->grid[y*ds->w+x] != v || bg != COL_BACKGROUND) {
2910 draw_tile(fe, COORD(x), COORD(y), v, bg);
2911 ds->grid[y*ds->w+x] = (bg == COL_BACKGROUND ? v : -10);
2915 if (!state->layout->mines)
2916 mines = state->layout->n;
2919 * Update the status bar.
2922 char statusbar[512];
2924 sprintf(statusbar, "DEAD!");
2925 } else if (state->won) {
2926 if (state->used_solve)
2927 sprintf(statusbar, "Auto-solved.");
2929 sprintf(statusbar, "COMPLETED!");
2931 sprintf(statusbar, "Marked: %d / %d", markers, mines);
2934 sprintf(statusbar + strlen(statusbar),
2935 " Deaths: %d", ui->deaths);
2936 status_bar(fe, statusbar);
2940 static float game_anim_length(game_state *oldstate, game_state *newstate,
2941 int dir, game_ui *ui)
2946 static float game_flash_length(game_state *oldstate, game_state *newstate,
2947 int dir, game_ui *ui)
2949 if (oldstate->used_solve || newstate->used_solve)
2952 if (dir > 0 && !oldstate->dead && !oldstate->won) {
2953 if (newstate->dead) {
2954 ui->flash_is_death = TRUE;
2955 return 3 * FLASH_FRAME;
2957 if (newstate->won) {
2958 ui->flash_is_death = FALSE;
2959 return 2 * FLASH_FRAME;
2965 static int game_wants_statusbar(void)
2970 static int game_timing_state(game_state *state)
2972 if (state->dead || state->won || !state->layout->mines)
2978 #define thegame mines
2981 const struct game thegame = {
2982 "Mines", "games.mines",
2989 TRUE, game_configure, custom_params,
2998 TRUE, game_text_format,
3005 game_free_drawstate,
3009 game_wants_statusbar,
3010 TRUE, game_timing_state,
3011 BUTTON_BEATS(LEFT_BUTTON, RIGHT_BUTTON),