2 * mines.c: Minesweeper clone with sophisticated grid generation.
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.
13 * - question marks (arrgh, preferences?)
15 * - sensible parameter constraints
16 * + 30x16: 191 mines just about works if rather slowly, 192 is
17 * just about doom (the latter corresponding to a density of
19 * + 9x9: 45 mines works - over 1 in 2! 50 seems a bit slow.
20 * + it might not be feasible to work out the exact limit
34 COL_BACKGROUND
, COL_BACKGROUND2
,
35 COL_1
, COL_2
, COL_3
, COL_4
, COL_5
, COL_6
, COL_7
, COL_8
,
36 COL_MINE
, COL_BANG
, COL_CROSS
, COL_FLAG
, COL_FLAGBASE
, COL_QUERY
,
37 COL_HIGHLIGHT
, COL_LOWLIGHT
,
42 #define BORDER (TILE_SIZE * 3 / 2)
43 #define HIGHLIGHT_WIDTH 2
44 #define OUTER_HIGHLIGHT_WIDTH 3
45 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
46 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
48 #define FLASH_FRAME 0.13F
57 * This structure is shared between all the game_states for a
58 * given instance of the puzzle, so we reference-count it.
63 * If we haven't yet actually generated the mine layout, here's
64 * all the data we will need to do so.
68 midend_data
*me
; /* to give back the new game desc */
72 int w
, h
, n
, dead
, won
;
73 int used_solve
, just_used_solve
;
74 struct mine_layout
*layout
; /* real mine positions */
75 signed char *grid
; /* player knowledge */
77 * Each item in the `grid' array is one of the following values:
79 * - 0 to 8 mean the square is open and has a surrounding mine
82 * - -1 means the square is marked as a mine.
84 * - -2 means the square is unknown.
86 * - -3 means the square is marked with a question mark
87 * (FIXME: do we even want to bother with this?).
89 * - 64 means the square has had a mine revealed when the game
92 * - 65 means the square had a mine revealed and this was the
93 * one the player hits.
95 * - 66 means the square has a crossed-out mine because the
96 * player had incorrectly marked it.
100 static game_params
*default_params(void)
102 game_params
*ret
= snew(game_params
);
111 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
115 static const struct { int w
, h
, n
; } values
[] = {
121 if (i
< 0 || i
>= lenof(values
))
124 ret
= snew(game_params
);
125 ret
->w
= values
[i
].w
;
126 ret
->h
= values
[i
].h
;
127 ret
->n
= values
[i
].n
;
130 sprintf(str
, "%dx%d, %d mines", ret
->w
, ret
->h
, ret
->n
);
137 static void free_params(game_params
*params
)
142 static game_params
*dup_params(game_params
*params
)
144 game_params
*ret
= snew(game_params
);
145 *ret
= *params
; /* structure copy */
149 static void decode_params(game_params
*params
, char const *string
)
151 char const *p
= string
;
154 while (*p
&& isdigit((unsigned char)*p
)) p
++;
158 while (*p
&& isdigit((unsigned char)*p
)) p
++;
160 params
->h
= params
->w
;
165 while (*p
&& (*p
== '.' || isdigit((unsigned char)*p
))) p
++;
167 params
->n
= params
->w
* params
->h
/ 10;
173 params
->unique
= FALSE
;
175 p
++; /* skip any other gunk */
179 static char *encode_params(game_params
*params
, int full
)
184 len
= sprintf(ret
, "%dx%d", params
->w
, params
->h
);
186 * Mine count is a generation-time parameter, since it can be
187 * deduced from the mine bitmap!
190 len
+= sprintf(ret
+len
, "n%d", params
->n
);
191 if (full
&& !params
->unique
)
193 assert(len
< lenof(ret
));
199 static config_item
*game_configure(game_params
*params
)
204 ret
= snewn(5, config_item
);
206 ret
[0].name
= "Width";
207 ret
[0].type
= C_STRING
;
208 sprintf(buf
, "%d", params
->w
);
209 ret
[0].sval
= dupstr(buf
);
212 ret
[1].name
= "Height";
213 ret
[1].type
= C_STRING
;
214 sprintf(buf
, "%d", params
->h
);
215 ret
[1].sval
= dupstr(buf
);
218 ret
[2].name
= "Mines";
219 ret
[2].type
= C_STRING
;
220 sprintf(buf
, "%d", params
->n
);
221 ret
[2].sval
= dupstr(buf
);
224 ret
[3].name
= "Ensure solubility";
225 ret
[3].type
= C_BOOLEAN
;
227 ret
[3].ival
= params
->unique
;
237 static game_params
*custom_params(config_item
*cfg
)
239 game_params
*ret
= snew(game_params
);
241 ret
->w
= atoi(cfg
[0].sval
);
242 ret
->h
= atoi(cfg
[1].sval
);
243 ret
->n
= atoi(cfg
[2].sval
);
244 if (strchr(cfg
[2].sval
, '%'))
245 ret
->n
= ret
->n
* (ret
->w
* ret
->h
) / 100;
246 ret
->unique
= cfg
[3].ival
;
251 static char *validate_params(game_params
*params
)
253 if (params
->w
<= 0 && params
->h
<= 0)
254 return "Width and height must both be greater than zero";
256 return "Width must be greater than zero";
258 return "Height must be greater than zero";
259 if (params
->n
> params
->w
* params
->h
- 9)
260 return "Too many mines for grid size";
263 * FIXME: Need more constraints here. Not sure what the
264 * sensible limits for Minesweeper actually are. The limits
265 * probably ought to change, however, depending on uniqueness.
271 /* ----------------------------------------------------------------------
272 * Minesweeper solver, used to ensure the generated grids are
273 * solvable without having to take risks.
277 * Count the bits in a word. Only needs to cope with 16 bits.
279 static int bitcount16(int word
)
281 word
= ((word
& 0xAAAA) >> 1) + (word
& 0x5555);
282 word
= ((word
& 0xCCCC) >> 2) + (word
& 0x3333);
283 word
= ((word
& 0xF0F0) >> 4) + (word
& 0x0F0F);
284 word
= ((word
& 0xFF00) >> 8) + (word
& 0x00FF);
290 * We use a tree234 to store a large number of small localised
291 * sets, each with a mine count. We also keep some of those sets
292 * linked together into a to-do list.
295 short x
, y
, mask
, mines
;
297 struct set
*prev
, *next
;
300 static int setcmp(void *av
, void *bv
)
302 struct set
*a
= (struct set
*)av
;
303 struct set
*b
= (struct set
*)bv
;
307 else if (a
->y
> b
->y
)
309 else if (a
->x
< b
->x
)
311 else if (a
->x
> b
->x
)
313 else if (a
->mask
< b
->mask
)
315 else if (a
->mask
> b
->mask
)
323 struct set
*todo_head
, *todo_tail
;
326 static struct setstore
*ss_new(void)
328 struct setstore
*ss
= snew(struct setstore
);
329 ss
->sets
= newtree234(setcmp
);
330 ss
->todo_head
= ss
->todo_tail
= NULL
;
335 * Take two input sets, in the form (x,y,mask). Munge the first by
336 * taking either its intersection with the second or its difference
337 * with the second. Return the new mask part of the first set.
339 static int setmunge(int x1
, int y1
, int mask1
, int x2
, int y2
, int mask2
,
343 * Adjust the second set so that it has the same x,y
344 * coordinates as the first.
346 if (abs(x2
-x1
) >= 3 || abs(y2
-y1
) >= 3) {
350 mask2
&= ~(4|32|256);
360 mask2
&= ~(64|128|256);
372 * Invert the second set if `diff' is set (we're after A &~ B
373 * rather than A & B).
379 * Now all that's left is a logical AND.
381 return mask1
& mask2
;
384 static void ss_add_todo(struct setstore
*ss
, struct set
*s
)
387 return; /* already on it */
389 #ifdef SOLVER_DIAGNOSTICS
390 printf("adding set on todo list: %d,%d %03x %d\n",
391 s
->x
, s
->y
, s
->mask
, s
->mines
);
394 s
->prev
= ss
->todo_tail
;
404 static void ss_add(struct setstore
*ss
, int x
, int y
, int mask
, int mines
)
411 * Normalise so that x and y are genuinely the bounding
414 while (!(mask
& (1|8|64)))
416 while (!(mask
& (1|2|4)))
420 * Create a set structure and add it to the tree.
422 s
= snew(struct set
);
428 if (add234(ss
->sets
, s
) != s
) {
430 * This set already existed! Free it and return.
437 * We've added a new set to the tree, so put it on the todo
443 static void ss_remove(struct setstore
*ss
, struct set
*s
)
445 struct set
*next
= s
->next
, *prev
= s
->prev
;
447 #ifdef SOLVER_DIAGNOSTICS
448 printf("removing set %d,%d %03x\n", s
->x
, s
->y
, s
->mask
);
451 * Remove s from the todo list.
455 else if (s
== ss
->todo_head
)
456 ss
->todo_head
= next
;
460 else if (s
== ss
->todo_tail
)
461 ss
->todo_tail
= prev
;
466 * Remove s from the tree.
471 * Destroy the actual set structure.
477 * Return a dynamically allocated list of all the sets which
478 * overlap a provided input set.
480 static struct set
**ss_overlap(struct setstore
*ss
, int x
, int y
, int mask
)
482 struct set
**ret
= NULL
;
483 int nret
= 0, retsize
= 0;
486 for (xx
= x
-3; xx
< x
+3; xx
++)
487 for (yy
= y
-3; yy
< y
+3; yy
++) {
492 * Find the first set with these top left coordinates.
498 if (findrelpos234(ss
->sets
, &stmp
, NULL
, REL234_GE
, &pos
)) {
499 while ((s
= index234(ss
->sets
, pos
)) != NULL
&&
500 s
->x
== xx
&& s
->y
== yy
) {
502 * This set potentially overlaps the input one.
503 * Compute the intersection to see if they
504 * really overlap, and add it to the list if
507 if (setmunge(x
, y
, mask
, s
->x
, s
->y
, s
->mask
, FALSE
)) {
509 * There's an overlap.
511 if (nret
>= retsize
) {
513 ret
= sresize(ret
, retsize
, struct set
*);
523 ret
= sresize(ret
, nret
+1, struct set
*);
530 * Get an element from the head of the set todo list.
532 static struct set
*ss_todo(struct setstore
*ss
)
535 struct set
*ret
= ss
->todo_head
;
536 ss
->todo_head
= ret
->next
;
538 ss
->todo_head
->prev
= NULL
;
540 ss
->todo_tail
= NULL
;
541 ret
->next
= ret
->prev
= NULL
;
554 static void std_add(struct squaretodo
*std
, int i
)
557 std
->next
[std
->tail
] = i
;
564 static void known_squares(int w
, int h
, struct squaretodo
*std
,
566 int (*open
)(void *ctx
, int x
, int y
), void *openctx
,
567 int x
, int y
, int mask
, int mine
)
573 for (yy
= 0; yy
< 3; yy
++)
574 for (xx
= 0; xx
< 3; xx
++) {
576 int i
= (y
+ yy
) * w
+ (x
+ xx
);
579 * It's possible that this square is _already_
580 * known, in which case we don't try to add it to
586 grid
[i
] = -1; /* and don't open it! */
588 grid
[i
] = open(openctx
, x
+ xx
, y
+ yy
);
589 assert(grid
[i
] != -1); /* *bang* */
600 * This is data returned from the `perturb' function. It details
601 * which squares have become mines and which have become clear. The
602 * solver is (of course) expected to honourably not use that
603 * knowledge directly, but to efficently adjust its internal data
604 * structures and proceed based on only the information it
607 struct perturbation
{
609 int delta
; /* +1 == become a mine; -1 == cleared */
611 struct perturbations
{
613 struct perturbation
*changes
;
617 * Main solver entry point. You give it a grid of existing
618 * knowledge (-1 for a square known to be a mine, 0-8 for empty
619 * squares with a given number of neighbours, -2 for completely
620 * unknown), plus a function which you can call to open new squares
621 * once you're confident of them. It fills in as much more of the
626 * - -1 means deduction stalled and nothing could be done
627 * - 0 means deduction succeeded fully
628 * - >0 means deduction succeeded but some number of perturbation
629 * steps were required; the exact return value is the number of
632 static int minesolve(int w
, int h
, int n
, signed char *grid
,
633 int (*open
)(void *ctx
, int x
, int y
),
634 struct perturbations
*(*perturb
)(void *ctx
,
636 int x
, int y
, int mask
),
637 void *ctx
, random_state
*rs
)
639 struct setstore
*ss
= ss_new();
641 struct squaretodo astd
, *std
= &astd
;
646 * Set up a linked list of squares with known contents, so that
647 * we can process them one by one.
649 std
->next
= snewn(w
*h
, int);
650 std
->head
= std
->tail
= -1;
653 * Initialise that list with all known squares in the input
656 for (y
= 0; y
< h
; y
++) {
657 for (x
= 0; x
< w
; x
++) {
665 * Main deductive loop.
668 int done_something
= FALSE
;
672 * If there are any known squares on the todo list, process
673 * them and construct a set for each.
675 while (std
->head
!= -1) {
677 #ifdef SOLVER_DIAGNOSTICS
678 printf("known square at %d,%d [%d]\n", i
%w
, i
/w
, grid
[i
]);
680 std
->head
= std
->next
[i
];
688 int dx
, dy
, mines
, bit
, val
;
689 #ifdef SOLVER_DIAGNOSTICS
690 printf("creating set around this square\n");
693 * Empty square. Construct the set of non-known squares
694 * around this one, and determine its mine count.
699 for (dy
= -1; dy
<= +1; dy
++) {
700 for (dx
= -1; dx
<= +1; dx
++) {
701 #ifdef SOLVER_DIAGNOSTICS
702 printf("grid %d,%d = %d\n", x
+dx
, y
+dy
, grid
[i
+dy
*w
+dx
]);
704 if (x
+dx
< 0 || x
+dx
>= w
|| y
+dy
< 0 || y
+dy
>= h
)
705 /* ignore this one */;
706 else if (grid
[i
+dy
*w
+dx
] == -1)
708 else if (grid
[i
+dy
*w
+dx
] == -2)
714 ss_add(ss
, x
-1, y
-1, val
, mines
);
718 * Now, whether the square is empty or full, we must
719 * find any set which contains it and replace it with
720 * one which does not.
723 #ifdef SOLVER_DIAGNOSTICS
724 printf("finding sets containing known square %d,%d\n", x
, y
);
726 list
= ss_overlap(ss
, x
, y
, 1);
728 for (j
= 0; list
[j
]; j
++) {
729 int newmask
, newmines
;
734 * Compute the mask for this set minus the
735 * newly known square.
737 newmask
= setmunge(s
->x
, s
->y
, s
->mask
, x
, y
, 1, TRUE
);
740 * Compute the new mine count.
742 newmines
= s
->mines
- (grid
[i
] == -1);
745 * Insert the new set into the collection,
746 * unless it's been whittled right down to
750 ss_add(ss
, s
->x
, s
->y
, newmask
, newmines
);
753 * Destroy the old one; it is actually obsolete.
762 * Marking a fresh square as known certainly counts as
765 done_something
= TRUE
;
769 * Now pick a set off the to-do list and attempt deductions
772 if ((s
= ss_todo(ss
)) != NULL
) {
774 #ifdef SOLVER_DIAGNOSTICS
775 printf("set to do: %d,%d %03x %d\n", s
->x
, s
->y
, s
->mask
, s
->mines
);
778 * Firstly, see if this set has a mine count of zero or
779 * of its own cardinality.
781 if (s
->mines
== 0 || s
->mines
== bitcount16(s
->mask
)) {
783 * If so, we can immediately mark all the squares
784 * in the set as known.
786 #ifdef SOLVER_DIAGNOSTICS
789 known_squares(w
, h
, std
, grid
, open
, ctx
,
790 s
->x
, s
->y
, s
->mask
, (s
->mines
!= 0));
793 * Having done that, we need do nothing further
794 * with this set; marking all the squares in it as
795 * known will eventually eliminate it, and will
796 * also permit further deductions about anything
803 * Failing that, we now search through all the sets
804 * which overlap this one.
806 list
= ss_overlap(ss
, s
->x
, s
->y
, s
->mask
);
808 for (j
= 0; list
[j
]; j
++) {
809 struct set
*s2
= list
[j
];
810 int swing
, s2wing
, swc
, s2wc
;
813 * Find the non-overlapping parts s2-s and s-s2,
814 * and their cardinalities.
816 * I'm going to refer to these parts as `wings'
817 * surrounding the central part common to both
818 * sets. The `s wing' is s-s2; the `s2 wing' is
821 swing
= setmunge(s
->x
, s
->y
, s
->mask
, s2
->x
, s2
->y
, s2
->mask
,
823 s2wing
= setmunge(s2
->x
, s2
->y
, s2
->mask
, s
->x
, s
->y
, s
->mask
,
825 swc
= bitcount16(swing
);
826 s2wc
= bitcount16(s2wing
);
829 * If one set has more mines than the other, and
830 * the number of extra mines is equal to the
831 * cardinality of that set's wing, then we can mark
832 * every square in the wing as a known mine, and
833 * every square in the other wing as known clear.
835 if (swc
== s
->mines
- s2
->mines
||
836 s2wc
== s2
->mines
- s
->mines
) {
837 known_squares(w
, h
, std
, grid
, open
, ctx
,
839 (swc
== s
->mines
- s2
->mines
));
840 known_squares(w
, h
, std
, grid
, open
, ctx
,
841 s2
->x
, s2
->y
, s2wing
,
842 (s2wc
== s2
->mines
- s
->mines
));
847 * Failing that, see if one set is a subset of the
848 * other. If so, we can divide up the mine count of
849 * the larger set between the smaller set and its
850 * complement, even if neither smaller set ends up
851 * being immediately clearable.
853 if (swc
== 0 && s2wc
!= 0) {
854 /* s is a subset of s2. */
855 assert(s2
->mines
> s
->mines
);
856 ss_add(ss
, s2
->x
, s2
->y
, s2wing
, s2
->mines
- s
->mines
);
857 } else if (s2wc
== 0 && swc
!= 0) {
858 /* s2 is a subset of s. */
859 assert(s
->mines
> s2
->mines
);
860 ss_add(ss
, s
->x
, s
->y
, swing
, s
->mines
- s2
->mines
);
867 * In this situation we have definitely done
868 * _something_, even if it's only reducing the size of
871 done_something
= TRUE
;
874 * We have nothing left on our todo list, which means
875 * all localised deductions have failed. Our next step
876 * is to resort to global deduction based on the total
877 * mine count. This is computationally expensive
878 * compared to any of the above deductions, which is
879 * why we only ever do it when all else fails, so that
880 * hopefully it won't have to happen too often.
882 * If you pass n<0 into this solver, that informs it
883 * that you do not know the total mine count, so it
884 * won't even attempt these deductions.
887 int minesleft
, squaresleft
;
888 int nsets
, setused
[10], cursor
;
891 * Start by scanning the current grid state to work out
892 * how many unknown squares we still have, and how many
893 * mines are to be placed in them.
897 for (i
= 0; i
< w
*h
; i
++) {
900 else if (grid
[i
] == -2)
904 #ifdef SOLVER_DIAGNOSTICS
905 printf("global deduction time: squaresleft=%d minesleft=%d\n",
906 squaresleft
, minesleft
);
907 for (y
= 0; y
< h
; y
++) {
908 for (x
= 0; x
< w
; x
++) {
924 * If there _are_ no unknown squares, we have actually
927 if (squaresleft
== 0) {
928 assert(minesleft
== 0);
933 * First really simple case: if there are no more mines
934 * left, or if there are exactly as many mines left as
935 * squares to play them in, then it's all easy.
937 if (minesleft
== 0 || minesleft
== squaresleft
) {
938 for (i
= 0; i
< w
*h
; i
++)
940 known_squares(w
, h
, std
, grid
, open
, ctx
,
941 i
% w
, i
/ w
, 1, minesleft
!= 0);
942 continue; /* now go back to main deductive loop */
946 * Failing that, we have to do some _real_ work.
947 * Ideally what we do here is to try every single
948 * combination of the currently available sets, in an
949 * attempt to find a disjoint union (i.e. a set of
950 * squares with a known mine count between them) such
951 * that the remaining unknown squares _not_ contained
952 * in that union either contain no mines or are all
955 * Actually enumerating all 2^n possibilities will get
956 * a bit slow for large n, so I artificially cap this
957 * recursion at n=10 to avoid too much pain.
959 nsets
= count234(ss
->sets
);
960 if (nsets
<= lenof(setused
)) {
962 * Doing this with actual recursive function calls
963 * would get fiddly because a load of local
964 * variables from this function would have to be
965 * passed down through the recursion. So instead
966 * I'm going to use a virtual recursion within this
967 * function. The way this works is:
969 * - we have an array `setused', such that
970 * setused[n] is 0 or 1 depending on whether set
971 * n is currently in the union we are
974 * - we have a value `cursor' which indicates how
975 * much of `setused' we have so far filled in.
976 * It's conceptually the recursion depth.
978 * We begin by setting `cursor' to zero. Then:
980 * - if cursor can advance, we advance it by one.
981 * We set the value in `setused' that it went
982 * past to 1 if that set is disjoint from
983 * anything else currently in `setused', or to 0
986 * - If cursor cannot advance because it has
987 * reached the end of the setused list, then we
988 * have a maximal disjoint union. Check to see
989 * whether its mine count has any useful
990 * properties. If so, mark all the squares not
991 * in the union as known and terminate.
993 * - If cursor has reached the end of setused and
994 * the algorithm _hasn't_ terminated, back
995 * cursor up to the nearest 1, turn it into a 0
996 * and advance cursor just past it.
998 * - If we attempt to back up to the nearest 1 and
999 * there isn't one at all, then we have gone
1000 * through all disjoint unions of sets in the
1001 * list and none of them has been helpful, so we
1004 struct set
*sets
[lenof(setused
)];
1005 for (i
= 0; i
< nsets
; i
++)
1006 sets
[i
] = index234(ss
->sets
, i
);
1011 if (cursor
< nsets
) {
1014 /* See if any existing set overlaps this one. */
1015 for (i
= 0; i
< cursor
; i
++)
1017 setmunge(sets
[cursor
]->x
,
1020 sets
[i
]->x
, sets
[i
]->y
, sets
[i
]->mask
,
1028 * We're adding this set to our union,
1029 * so adjust minesleft and squaresleft
1032 minesleft
-= sets
[cursor
]->mines
;
1033 squaresleft
-= bitcount16(sets
[cursor
]->mask
);
1036 setused
[cursor
++] = ok
;
1038 #ifdef SOLVER_DIAGNOSTICS
1039 printf("trying a set combination with %d %d\n",
1040 squaresleft
, minesleft
);
1041 #endif /* SOLVER_DIAGNOSTICS */
1044 * We've reached the end. See if we've got
1045 * anything interesting.
1047 if (squaresleft
> 0 &&
1048 (minesleft
== 0 || minesleft
== squaresleft
)) {
1050 * We have! There is at least one
1051 * square not contained within the set
1052 * union we've just found, and we can
1053 * deduce that either all such squares
1054 * are mines or all are not (depending
1055 * on whether minesleft==0). So now all
1056 * we have to do is actually go through
1057 * the grid, find those squares, and
1060 for (i
= 0; i
< w
*h
; i
++)
1061 if (grid
[i
] == -2) {
1065 for (j
= 0; j
< nsets
; j
++)
1067 setmunge(sets
[j
]->x
, sets
[j
]->y
,
1068 sets
[j
]->mask
, x
, y
, 1,
1074 known_squares(w
, h
, std
, grid
,
1076 x
, y
, 1, minesleft
!= 0);
1079 done_something
= TRUE
;
1080 break; /* return to main deductive loop */
1084 * If we reach here, then this union hasn't
1085 * done us any good, so move on to the
1086 * next. Backtrack cursor to the nearest 1,
1087 * change it to a 0 and continue.
1089 while (cursor
-- >= 0 && !setused
[cursor
]);
1091 assert(setused
[cursor
]);
1094 * We're removing this set from our
1095 * union, so re-increment minesleft and
1098 minesleft
+= sets
[cursor
]->mines
;
1099 squaresleft
+= bitcount16(sets
[cursor
]->mask
);
1101 setused
[cursor
++] = 0;
1104 * We've backtracked all the way to the
1105 * start without finding a single 1,
1106 * which means that our virtual
1107 * recursion is complete and nothing
1122 #ifdef SOLVER_DIAGNOSTICS
1124 * Dump the current known state of the grid.
1126 printf("solver ran out of steam, ret=%d, grid:\n", nperturbs
);
1127 for (y
= 0; y
< h
; y
++) {
1128 for (x
= 0; x
< w
; x
++) {
1129 int v
= grid
[y
*w
+x
];
1145 for (i
= 0; (s
= index234(ss
->sets
, i
)) != NULL
; i
++)
1146 printf("remaining set: %d,%d %03x %d\n", s
->x
, s
->y
, s
->mask
, s
->mines
);
1151 * Now we really are at our wits' end as far as solving
1152 * this grid goes. Our only remaining option is to call
1153 * a perturb function and ask it to modify the grid to
1157 struct perturbations
*ret
;
1163 * Choose a set at random from the current selection,
1164 * and ask the perturb function to either fill or empty
1167 * If we have no sets at all, we must give up.
1169 if (count234(ss
->sets
) == 0)
1171 s
= index234(ss
->sets
, random_upto(rs
, count234(ss
->sets
)));
1172 #ifdef SOLVER_DIAGNOSTICS
1173 printf("perturbing on set %d,%d %03x\n", s
->x
, s
->y
, s
->mask
);
1175 ret
= perturb(ctx
, grid
, s
->x
, s
->y
, s
->mask
);
1178 assert(ret
->n
> 0); /* otherwise should have been NULL */
1181 * A number of squares have been fiddled with, and
1182 * the returned structure tells us which. Adjust
1183 * the mine count in any set which overlaps one of
1184 * those squares, and put them back on the to-do
1187 for (i
= 0; i
< ret
->n
; i
++) {
1188 #ifdef SOLVER_DIAGNOSTICS
1189 printf("perturbation %s mine at %d,%d\n",
1190 ret
->changes
[i
].delta
> 0 ?
"added" : "removed",
1191 ret
->changes
[i
].x
, ret
->changes
[i
].y
);
1194 list
= ss_overlap(ss
,
1195 ret
->changes
[i
].x
, ret
->changes
[i
].y
, 1);
1197 for (j
= 0; list
[j
]; j
++) {
1198 list
[j
]->mines
+= ret
->changes
[i
].delta
;
1199 ss_add_todo(ss
, list
[j
]);
1206 * Now free the returned data.
1208 sfree(ret
->changes
);
1211 #ifdef SOLVER_DIAGNOSTICS
1213 * Dump the current known state of the grid.
1215 printf("state after perturbation:\n", nperturbs
);
1216 for (y
= 0; y
< h
; y
++) {
1217 for (x
= 0; x
< w
; x
++) {
1218 int v
= grid
[y
*w
+x
];
1234 for (i
= 0; (s
= index234(ss
->sets
, i
)) != NULL
; i
++)
1235 printf("remaining set: %d,%d %03x %d\n", s
->x
, s
->y
, s
->mask
, s
->mines
);
1240 * And now we can go back round the deductive loop.
1247 * If we get here, even that didn't work (either we didn't
1248 * have a perturb function or it returned failure), so we
1255 * See if we've got any unknown squares left.
1257 for (y
= 0; y
< h
; y
++)
1258 for (x
= 0; x
< w
; x
++)
1259 if (grid
[y
*w
+x
] == -2) {
1260 nperturbs
= -1; /* failed to complete */
1265 * Free the set list and square-todo list.
1269 while ((s
= delpos234(ss
->sets
, 0)) != NULL
)
1271 freetree234(ss
->sets
);
1279 /* ----------------------------------------------------------------------
1280 * Grid generator which uses the above solver.
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
)
1343 static struct perturbations
*mineperturb(void *vctx
, signed char *grid
,
1344 int setx
, int sety
, int mask
)
1346 struct minectx
*ctx
= (struct minectx
*)vctx
;
1347 struct square
*sqlist
;
1348 int x
, y
, dx
, dy
, i
, n
, nfull
, nempty
;
1349 struct square
*tofill
[9], *toempty
[9], **todo
;
1350 int ntofill
, ntoempty
, ntodo
, dtodo
, dset
;
1351 struct perturbations
*ret
;
1354 * Make a list of all the squares in the grid which we can
1355 * possibly use. This list should be in preference order, which
1358 * - first, unknown squares on the boundary of known space
1359 * - next, unknown squares beyond that boundary
1360 * - as a very last resort, known squares, but not within one
1361 * square of the starting position.
1363 * Each of these sections needs to be shuffled independently.
1364 * We do this by preparing list of all squares and then sorting
1365 * it with a random secondary key.
1367 sqlist
= snewn(ctx
->w
* ctx
->h
, struct square
);
1369 for (y
= 0; y
< ctx
->h
; y
++)
1370 for (x
= 0; x
< ctx
->w
; x
++) {
1372 * If this square is too near the starting position,
1373 * don't put it on the list at all.
1375 if (abs(y
- ctx
->sy
) <= 1 && abs(x
- ctx
->sx
) <= 1)
1379 * If this square is in the input set, also don't put
1382 if (x
>= setx
&& x
< setx
+ 3 &&
1383 y
>= sety
&& y
< sety
+ 3 &&
1384 mask
& (1 << ((y
-sety
)*3+(x
-setx
))))
1390 if (grid
[y
*ctx
->w
+x
] != -2) {
1391 sqlist
[n
].type
= 3; /* known square */
1394 * Unknown square. Examine everything around it and
1395 * see if it borders on any known squares. If it
1396 * does, it's class 1, otherwise it's 2.
1401 for (dy
= -1; dy
<= +1; dy
++)
1402 for (dx
= -1; dx
<= +1; dx
++)
1403 if (x
+dx
>= 0 && x
+dx
< ctx
->w
&&
1404 y
+dy
>= 0 && y
+dy
< ctx
->h
&&
1405 grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] != -2) {
1412 * Finally, a random number to cause qsort to
1413 * shuffle within each group.
1415 sqlist
[n
].random
= random_bits(ctx
->rs
, 31);
1420 qsort(sqlist
, n
, sizeof(struct square
), squarecmp
);
1423 * Now count up the number of full and empty squares in the set
1424 * we've been provided.
1427 for (dy
= 0; dy
< 3; dy
++)
1428 for (dx
= 0; dx
< 3; dx
++)
1429 if (mask
& (1 << (dy
*3+dx
))) {
1430 assert(setx
+dx
<= ctx
->w
);
1431 assert(sety
+dy
<= ctx
->h
);
1432 if (ctx
->grid
[(sety
+dy
)*ctx
->w
+(setx
+dx
)])
1439 * Now go through our sorted list until we find either `nfull'
1440 * empty squares, or `nempty' full squares; these will be
1441 * swapped with the appropriate squares in the set to either
1442 * fill or empty the set while keeping the same number of mines
1445 ntofill
= ntoempty
= 0;
1446 for (i
= 0; i
< n
; i
++) {
1447 struct square
*sq
= &sqlist
[i
];
1448 if (ctx
->grid
[sq
->y
* ctx
->w
+ sq
->x
])
1449 toempty
[ntoempty
++] = sq
;
1451 tofill
[ntofill
++] = sq
;
1452 if (ntofill
== nfull
|| ntoempty
== nempty
)
1457 * If this didn't work at all, I think we just give up.
1459 if (ntofill
!= nfull
&& ntoempty
!= nempty
) {
1465 * Now we're pretty much there. We need to either
1466 * (a) put a mine in each of the empty squares in the set, and
1467 * take one out of each square in `toempty'
1468 * (b) take a mine out of each of the full squares in the set,
1469 * and put one in each square in `tofill'
1470 * depending on which one we've found enough squares to do.
1472 * So we start by constructing our list of changes to return to
1473 * the solver, so that it can update its data structures
1474 * efficiently rather than having to rescan the whole grid.
1476 ret
= snew(struct perturbations
);
1477 if (ntofill
== nfull
) {
1489 ret
->changes
= snewn(ret
->n
, struct perturbation
);
1490 for (i
= 0; i
< ntodo
; i
++) {
1491 ret
->changes
[i
].x
= todo
[i
]->x
;
1492 ret
->changes
[i
].y
= todo
[i
]->y
;
1493 ret
->changes
[i
].delta
= dtodo
;
1495 /* now i == ntodo */
1496 for (dy
= 0; dy
< 3; dy
++)
1497 for (dx
= 0; dx
< 3; dx
++)
1498 if (mask
& (1 << (dy
*3+dx
))) {
1499 int currval
= (ctx
->grid
[(sety
+dy
)*ctx
->w
+(setx
+dx
)] ?
+1 : -1);
1500 if (dset
== -currval
) {
1501 ret
->changes
[i
].x
= setx
+ dx
;
1502 ret
->changes
[i
].y
= sety
+ dy
;
1503 ret
->changes
[i
].delta
= dset
;
1507 assert(i
== ret
->n
);
1512 * Having set up the precise list of changes we're going to
1513 * make, we now simply make them and return.
1515 for (i
= 0; i
< ret
->n
; i
++) {
1518 x
= ret
->changes
[i
].x
;
1519 y
= ret
->changes
[i
].y
;
1520 delta
= ret
->changes
[i
].delta
;
1523 * Check we're not trying to add an existing mine or remove
1526 assert((delta
< 0) ^ (ctx
->grid
[y
*ctx
->w
+x
] == 0));
1529 * Actually make the change.
1531 ctx
->grid
[y
*ctx
->w
+x
] = (delta
> 0);
1534 * Update any numbers already present in the grid.
1536 for (dy
= -1; dy
<= +1; dy
++)
1537 for (dx
= -1; dx
<= +1; dx
++)
1538 if (x
+dx
>= 0 && x
+dx
< ctx
->w
&&
1539 y
+dy
>= 0 && y
+dy
< ctx
->h
&&
1540 grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] != -2) {
1541 if (dx
== 0 && dy
== 0) {
1543 * The square itself is marked as known in
1544 * the grid. Mark it as a mine if it's a
1545 * mine, or else work out its number.
1548 grid
[y
*ctx
->w
+x
] = -1;
1550 int dx2
, dy2
, minecount
= 0;
1551 for (dy2
= -1; dy2
<= +1; dy2
++)
1552 for (dx2
= -1; dx2
<= +1; dx2
++)
1553 if (x
+dx2
>= 0 && x
+dx2
< ctx
->w
&&
1554 y
+dy2
>= 0 && y
+dy2
< ctx
->h
&&
1555 ctx
->grid
[(y
+dy2
)*ctx
->w
+(x
+dx2
)])
1557 grid
[y
*ctx
->w
+x
] = minecount
;
1560 if (grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] >= 0)
1561 grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] += delta
;
1566 #ifdef GENERATION_DIAGNOSTICS
1569 printf("grid after perturbing:\n");
1570 for (yy
= 0; yy
< ctx
->h
; yy
++) {
1571 for (xx
= 0; xx
< ctx
->w
; xx
++) {
1572 int v
= ctx
->grid
[yy
*ctx
->w
+xx
];
1573 if (yy
== ctx
->sy
&& xx
== ctx
->sx
) {
1591 static char *minegen(int w
, int h
, int n
, int x
, int y
, int unique
,
1594 char *ret
= snewn(w
*h
, char);
1600 memset(ret
, 0, w
*h
);
1603 * Start by placing n mines, none of which is at x,y or within
1607 int *tmp
= snewn(w
*h
, int);
1611 * Write down the list of possible mine locations.
1614 for (i
= 0; i
< h
; i
++)
1615 for (j
= 0; j
< w
; j
++)
1616 if (abs(i
- y
) > 1 || abs(j
- x
) > 1)
1620 * Now pick n off the list at random.
1624 i
= random_upto(rs
, k
);
1632 #ifdef GENERATION_DIAGNOSTICS
1635 printf("grid after initial generation:\n");
1636 for (yy
= 0; yy
< h
; yy
++) {
1637 for (xx
= 0; xx
< w
; xx
++) {
1638 int v
= ret
[yy
*w
+xx
];
1639 if (yy
== y
&& xx
== x
) {
1655 * Now set up a results grid to run the solver in, and a
1656 * context for the solver to open squares. Then run the solver
1657 * repeatedly; if the number of perturb steps ever goes up or
1658 * it ever returns -1, give up completely.
1660 * We bypass this bit if we're not after a unique grid.
1663 signed char *solvegrid
= snewn(w
*h
, char);
1664 struct minectx actx
, *ctx
= &actx
;
1665 int solveret
, prevret
= -2;
1675 memset(solvegrid
, -2, w
*h
);
1676 solvegrid
[y
*w
+x
] = mineopen(ctx
, x
, y
);
1677 assert(solvegrid
[y
*w
+x
] == 0); /* by deliberate arrangement */
1680 minesolve(w
, h
, n
, solvegrid
, mineopen
, mineperturb
, ctx
, rs
);
1681 if (solveret
< 0 || (prevret
>= 0 && solveret
>= prevret
)) {
1684 } else if (solveret
== 0) {
1701 * The Mines game descriptions contain the location of every mine,
1702 * and can therefore be used to cheat.
1704 * It would be pointless to attempt to _prevent_ this form of
1705 * cheating by encrypting the description, since Mines is
1706 * open-source so anyone can find out the encryption key. However,
1707 * I think it is worth doing a bit of gentle obfuscation to prevent
1708 * _accidental_ spoilers: if you happened to note that the game ID
1709 * starts with an F, for example, you might be unable to put the
1710 * knowledge of those mines out of your mind while playing. So,
1711 * just as discussions of film endings are rot13ed to avoid
1712 * spoiling it for people who don't want to be told, we apply a
1713 * keyless, reversible, but visually completely obfuscatory masking
1714 * function to the mine bitmap.
1716 static void obfuscate_bitmap(unsigned char *bmp
, int bits
, int decode
)
1718 int bytes
, firsthalf
, secondhalf
;
1720 unsigned char *seedstart
;
1722 unsigned char *targetstart
;
1728 * My obfuscation algorithm is similar in concept to the OAEP
1729 * encoding used in some forms of RSA. Here's a specification
1732 * + We have a `masking function' which constructs a stream of
1733 * pseudorandom bytes from a seed of some number of input
1736 * + We pad out our input bit stream to a whole number of
1737 * bytes by adding up to 7 zero bits on the end. (In fact
1738 * the bitmap passed as input to this function will already
1739 * have had this done in practice.)
1741 * + We divide the _byte_ stream exactly in half, rounding the
1742 * half-way position _down_. So an 81-bit input string, for
1743 * example, rounds up to 88 bits or 11 bytes, and then
1744 * dividing by two gives 5 bytes in the first half and 6 in
1747 * + We generate a mask from the second half of the bytes, and
1748 * XOR it over the first half.
1750 * + We generate a mask from the (encoded) first half of the
1751 * bytes, and XOR it over the second half. Any null bits at
1752 * the end which were added as padding are cleared back to
1753 * zero even if this operation would have made them nonzero.
1755 * To de-obfuscate, the steps are precisely the same except
1756 * that the final two are reversed.
1758 * Finally, our masking function. Given an input seed string of
1759 * bytes, the output mask consists of concatenating the SHA-1
1760 * hashes of the seed string and successive decimal integers,
1764 bytes
= (bits
+ 7) / 8;
1765 firsthalf
= bytes
/ 2;
1766 secondhalf
= bytes
- firsthalf
;
1768 steps
[decode ?
1 : 0].seedstart
= bmp
+ firsthalf
;
1769 steps
[decode ?
1 : 0].seedlen
= secondhalf
;
1770 steps
[decode ?
1 : 0].targetstart
= bmp
;
1771 steps
[decode ?
1 : 0].targetlen
= firsthalf
;
1773 steps
[decode ?
0 : 1].seedstart
= bmp
;
1774 steps
[decode ?
0 : 1].seedlen
= firsthalf
;
1775 steps
[decode ?
0 : 1].targetstart
= bmp
+ firsthalf
;
1776 steps
[decode ?
0 : 1].targetlen
= secondhalf
;
1778 for (i
= 0; i
< 2; i
++) {
1779 SHA_State base
, final
;
1780 unsigned char digest
[20];
1782 int digestpos
= 20, counter
= 0;
1785 SHA_Bytes(&base
, steps
[i
].seedstart
, steps
[i
].seedlen
);
1787 for (j
= 0; j
< steps
[i
].targetlen
; j
++) {
1788 if (digestpos
>= 20) {
1789 sprintf(numberbuf
, "%d", counter
++);
1791 SHA_Bytes(&final
, numberbuf
, strlen(numberbuf
));
1792 SHA_Final(&final
, digest
);
1795 steps
[i
].targetstart
[j
] ^= digest
[digestpos
++];
1799 * Mask off the pad bits in the final byte after both steps.
1802 bmp
[bits
/ 8] &= 0xFF & (0xFF00 >> (bits
% 8));
1806 static char *new_mine_layout(int w
, int h
, int n
, int x
, int y
, int unique
,
1807 random_state
*rs
, char **game_desc
)
1809 signed char *grid
, *ret
, *p
;
1813 #ifdef TEST_OBFUSCATION
1814 static int tested_obfuscation
= FALSE
;
1815 if (!tested_obfuscation
) {
1817 * A few simple test vectors for the obfuscator.
1819 * First test: the 28-bit stream 1234567. This divides up
1820 * into 1234 and 567[0]. The SHA of 56 70 30 (appending
1821 * "0") is 15ce8ab946640340bbb99f3f48fd2c45d1a31d30. Thus,
1822 * we XOR the 16-bit string 15CE into the input 1234 to get
1823 * 07FA. Next, we SHA that with "0": the SHA of 07 FA 30 is
1824 * 3370135c5e3da4fed937adc004a79533962b6391. So we XOR the
1825 * 12-bit string 337 into the input 567 to get 650. Thus
1826 * our output is 07FA650.
1829 unsigned char bmp1
[] = "\x12\x34\x56\x70";
1830 obfuscate_bitmap(bmp1
, 28, FALSE
);
1831 printf("test 1 encode: %s\n",
1832 memcmp(bmp1
, "\x07\xfa\x65\x00", 4) ?
"failed" : "passed");
1833 obfuscate_bitmap(bmp1
, 28, TRUE
);
1834 printf("test 1 decode: %s\n",
1835 memcmp(bmp1
, "\x12\x34\x56\x70", 4) ?
"failed" : "passed");
1838 * Second test: a long string to make sure we switch from
1839 * one SHA to the next correctly. My input string this time
1840 * is simply fifty bytes of zeroes.
1843 unsigned char bmp2
[50];
1844 unsigned char bmp2a
[50];
1845 memset(bmp2
, 0, 50);
1846 memset(bmp2a
, 0, 50);
1847 obfuscate_bitmap(bmp2
, 50 * 8, FALSE
);
1849 * SHA of twenty-five zero bytes plus "0" is
1850 * b202c07b990c01f6ff2d544707f60e506019b671. SHA of
1851 * twenty-five zero bytes plus "1" is
1852 * fcb1d8b5a2f6b592fe6780b36aa9d65dd7aa6db9. Thus our
1853 * first half becomes
1854 * b202c07b990c01f6ff2d544707f60e506019b671fcb1d8b5a2.
1856 * SHA of that lot plus "0" is
1857 * 10b0af913db85d37ca27f52a9f78bba3a80030db. SHA of the
1858 * same string plus "1" is
1859 * 3d01d8df78e76d382b8106f480135a1bc751d725. So the
1860 * second half becomes
1861 * 10b0af913db85d37ca27f52a9f78bba3a80030db3d01d8df78.
1863 printf("test 2 encode: %s\n",
1864 memcmp(bmp2
, "\xb2\x02\xc0\x7b\x99\x0c\x01\xf6\xff\x2d\x54"
1865 "\x47\x07\xf6\x0e\x50\x60\x19\xb6\x71\xfc\xb1\xd8"
1866 "\xb5\xa2\x10\xb0\xaf\x91\x3d\xb8\x5d\x37\xca\x27"
1867 "\xf5\x2a\x9f\x78\xbb\xa3\xa8\x00\x30\xdb\x3d\x01"
1868 "\xd8\xdf\x78", 50) ?
"failed" : "passed");
1869 obfuscate_bitmap(bmp2
, 50 * 8, TRUE
);
1870 printf("test 2 decode: %s\n",
1871 memcmp(bmp2
, bmp2a
, 50) ?
"failed" : "passed");
1876 grid
= minegen(w
, h
, n
, x
, y
, unique
, rs
);
1880 * Set up the mine bitmap and obfuscate it.
1883 bmp
= snewn((area
+ 7) / 8, unsigned char);
1884 memset(bmp
, 0, (area
+ 7) / 8);
1885 for (i
= 0; i
< area
; i
++) {
1887 bmp
[i
/ 8] |= 0x80 >> (i
% 8);
1889 obfuscate_bitmap(bmp
, area
, FALSE
);
1892 * Now encode the resulting bitmap in hex. We can work to
1893 * nibble rather than byte granularity, since the obfuscation
1894 * function guarantees to return a bit string of the same
1895 * length as its input.
1897 ret
= snewn((area
+3)/4 + 100, char);
1898 p
= ret
+ sprintf(ret
, "%d,%d,m", x
, y
); /* 'm' == masked */
1899 for (i
= 0; i
< (area
+3)/4; i
++) {
1903 *p
++ = "0123456789abcdef"[v
& 0xF];
1915 static char *new_game_desc(game_params
*params
, random_state
*rs
,
1916 game_aux_info
**aux
, int interactive
)
1920 * For batch-generated grids, pre-open one square.
1922 int x
= random_upto(rs
, params
->w
);
1923 int y
= random_upto(rs
, params
->h
);
1927 grid
= new_mine_layout(params
->w
, params
->h
, params
->n
,
1928 x
, y
, params
->unique
, rs
, &desc
);
1932 char *rsdesc
, *desc
;
1934 rsdesc
= random_state_encode(rs
);
1935 desc
= snewn(strlen(rsdesc
) + 100, char);
1936 sprintf(desc
, "r%d,%c,%s", params
->n
, params
->unique ?
'u' : 'a', rsdesc
);
1942 static void game_free_aux_info(game_aux_info
*aux
)
1944 assert(!"Shouldn't happen");
1947 static char *validate_desc(game_params
*params
, char *desc
)
1949 int wh
= params
->w
* params
->h
;
1953 if (!*desc
|| !isdigit((unsigned char)*desc
))
1954 return "No initial mine count in game description";
1955 while (*desc
&& isdigit((unsigned char)*desc
))
1956 desc
++; /* skip over mine count */
1958 return "No ',' after initial x-coordinate in game description";
1960 if (*desc
!= 'u' && *desc
!= 'a')
1961 return "No uniqueness specifier in game description";
1964 return "No ',' after uniqueness specifier in game description";
1965 /* now ignore the rest */
1967 if (!*desc
|| !isdigit((unsigned char)*desc
))
1968 return "No initial x-coordinate in game description";
1970 if (x
< 0 || x
>= params
->w
)
1971 return "Initial x-coordinate was out of range";
1972 while (*desc
&& isdigit((unsigned char)*desc
))
1973 desc
++; /* skip over x coordinate */
1975 return "No ',' after initial x-coordinate in game description";
1976 desc
++; /* eat comma */
1977 if (!*desc
|| !isdigit((unsigned char)*desc
))
1978 return "No initial y-coordinate in game description";
1980 if (y
< 0 || y
>= params
->h
)
1981 return "Initial y-coordinate was out of range";
1982 while (*desc
&& isdigit((unsigned char)*desc
))
1983 desc
++; /* skip over y coordinate */
1985 return "No ',' after initial y-coordinate in game description";
1986 desc
++; /* eat comma */
1987 /* eat `m', meaning `masked', if present */
1990 /* now just check length of remainder */
1991 if (strlen(desc
) != (wh
+3)/4)
1992 return "Game description is wrong length";
1998 static int open_square(game_state
*state
, int x
, int y
)
2000 int w
= state
->w
, h
= state
->h
;
2001 int xx
, yy
, nmines
, ncovered
;
2003 if (!state
->layout
->mines
) {
2005 * We have a preliminary game in which the mine layout
2006 * hasn't been generated yet. Generate it based on the
2007 * initial click location.
2010 state
->layout
->mines
= new_mine_layout(w
, h
, state
->layout
->n
,
2011 x
, y
, state
->layout
->unique
,
2014 midend_supersede_game_desc(state
->layout
->me
, desc
);
2016 random_free(state
->layout
->rs
);
2017 state
->layout
->rs
= NULL
;
2020 if (state
->layout
->mines
[y
*w
+x
]) {
2022 * The player has landed on a mine. Bad luck. Expose all
2026 for (yy
= 0; yy
< h
; yy
++)
2027 for (xx
= 0; xx
< w
; xx
++) {
2028 if (state
->layout
->mines
[yy
*w
+xx
] &&
2029 (state
->grid
[yy
*w
+xx
] == -2 ||
2030 state
->grid
[yy
*w
+xx
] == -3)) {
2031 state
->grid
[yy
*w
+xx
] = 64;
2033 if (!state
->layout
->mines
[yy
*w
+xx
] &&
2034 state
->grid
[yy
*w
+xx
] == -1) {
2035 state
->grid
[yy
*w
+xx
] = 66;
2038 state
->grid
[y
*w
+x
] = 65;
2043 * Otherwise, the player has opened a safe square. Mark it to-do.
2045 state
->grid
[y
*w
+x
] = -10; /* `todo' value internal to this func */
2048 * Now go through the grid finding all `todo' values and
2049 * opening them. Every time one of them turns out to have no
2050 * neighbouring mines, we add all its unopened neighbours to
2053 * FIXME: We really ought to be able to do this better than
2054 * using repeated N^2 scans of the grid.
2057 int done_something
= FALSE
;
2059 for (yy
= 0; yy
< h
; yy
++)
2060 for (xx
= 0; xx
< w
; xx
++)
2061 if (state
->grid
[yy
*w
+xx
] == -10) {
2064 assert(!state
->layout
->mines
[yy
*w
+xx
]);
2068 for (dx
= -1; dx
<= +1; dx
++)
2069 for (dy
= -1; dy
<= +1; dy
++)
2070 if (xx
+dx
>= 0 && xx
+dx
< state
->w
&&
2071 yy
+dy
>= 0 && yy
+dy
< state
->h
&&
2072 state
->layout
->mines
[(yy
+dy
)*w
+(xx
+dx
)])
2075 state
->grid
[yy
*w
+xx
] = v
;
2078 for (dx
= -1; dx
<= +1; dx
++)
2079 for (dy
= -1; dy
<= +1; dy
++)
2080 if (xx
+dx
>= 0 && xx
+dx
< state
->w
&&
2081 yy
+dy
>= 0 && yy
+dy
< state
->h
&&
2082 state
->grid
[(yy
+dy
)*w
+(xx
+dx
)] == -2)
2083 state
->grid
[(yy
+dy
)*w
+(xx
+dx
)] = -10;
2086 done_something
= TRUE
;
2089 if (!done_something
)
2094 * Finally, scan the grid and see if exactly as many squares
2095 * are still covered as there are mines. If so, set the `won'
2096 * flag and fill in mine markers on all covered squares.
2098 nmines
= ncovered
= 0;
2099 for (yy
= 0; yy
< h
; yy
++)
2100 for (xx
= 0; xx
< w
; xx
++) {
2101 if (state
->grid
[yy
*w
+xx
] < 0)
2103 if (state
->layout
->mines
[yy
*w
+xx
])
2106 assert(ncovered
>= nmines
);
2107 if (ncovered
== nmines
) {
2108 for (yy
= 0; yy
< h
; yy
++)
2109 for (xx
= 0; xx
< w
; xx
++) {
2110 if (state
->grid
[yy
*w
+xx
] < 0)
2111 state
->grid
[yy
*w
+xx
] = -1;
2119 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
2121 game_state
*state
= snew(game_state
);
2122 int i
, wh
, x
, y
, ret
, masked
;
2125 state
->w
= params
->w
;
2126 state
->h
= params
->h
;
2127 state
->n
= params
->n
;
2128 state
->dead
= state
->won
= FALSE
;
2129 state
->used_solve
= state
->just_used_solve
= FALSE
;
2131 wh
= state
->w
* state
->h
;
2133 state
->layout
= snew(struct mine_layout
);
2134 state
->layout
->refcount
= 1;
2136 state
->grid
= snewn(wh
, char);
2137 memset(state
->grid
, -2, wh
);
2141 state
->layout
->n
= atoi(desc
);
2142 while (*desc
&& isdigit((unsigned char)*desc
))
2143 desc
++; /* skip over mine count */
2144 if (*desc
) desc
++; /* eat comma */
2146 state
->layout
->unique
= FALSE
;
2148 state
->layout
->unique
= TRUE
;
2150 if (*desc
) desc
++; /* eat comma */
2152 state
->layout
->mines
= NULL
;
2153 state
->layout
->rs
= random_state_decode(desc
);
2154 state
->layout
->me
= me
;
2157 state
->layout
->rs
= NULL
;
2158 state
->layout
->me
= NULL
;
2160 state
->layout
->mines
= snewn(wh
, char);
2162 while (*desc
&& isdigit((unsigned char)*desc
))
2163 desc
++; /* skip over x coordinate */
2164 if (*desc
) desc
++; /* eat comma */
2166 while (*desc
&& isdigit((unsigned char)*desc
))
2167 desc
++; /* skip over y coordinate */
2168 if (*desc
) desc
++; /* eat comma */
2175 * We permit game IDs to be entered by hand without the
2176 * masking transformation.
2181 bmp
= snewn((wh
+ 7) / 8, unsigned char);
2182 memset(bmp
, 0, (wh
+ 7) / 8);
2183 for (i
= 0; i
< (wh
+3)/4; i
++) {
2187 assert(c
!= 0); /* validate_desc should have caught */
2188 if (c
>= '0' && c
<= '9')
2190 else if (c
>= 'a' && c
<= 'f')
2192 else if (c
>= 'A' && c
<= 'F')
2197 bmp
[i
/ 2] |= v
<< (4 * (1 - (i
% 2)));
2201 obfuscate_bitmap(bmp
, wh
, TRUE
);
2203 memset(state
->layout
->mines
, 0, wh
);
2204 for (i
= 0; i
< wh
; i
++) {
2205 if (bmp
[i
/ 8] & (0x80 >> (i
% 8)))
2206 state
->layout
->mines
[i
] = 1;
2209 ret
= open_square(state
, x
, y
);
2215 static game_state
*dup_game(game_state
*state
)
2217 game_state
*ret
= snew(game_state
);
2222 ret
->dead
= state
->dead
;
2223 ret
->won
= state
->won
;
2224 ret
->used_solve
= state
->used_solve
;
2225 ret
->just_used_solve
= state
->just_used_solve
;
2226 ret
->layout
= state
->layout
;
2227 ret
->layout
->refcount
++;
2228 ret
->grid
= snewn(ret
->w
* ret
->h
, char);
2229 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
);
2234 static void free_game(game_state
*state
)
2236 if (--state
->layout
->refcount
<= 0) {
2237 sfree(state
->layout
->mines
);
2238 if (state
->layout
->rs
)
2239 random_free(state
->layout
->rs
);
2240 sfree(state
->layout
);
2246 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
2250 * Simply expose the entire grid as if it were a completed
2256 if (!state
->layout
->mines
) {
2257 *error
= "Game has not been started yet";
2261 ret
= dup_game(state
);
2262 for (yy
= 0; yy
< ret
->h
; yy
++)
2263 for (xx
= 0; xx
< ret
->w
; xx
++) {
2265 if (ret
->layout
->mines
[yy
*ret
->w
+xx
]) {
2266 ret
->grid
[yy
*ret
->w
+xx
] = -1;
2272 for (dx
= -1; dx
<= +1; dx
++)
2273 for (dy
= -1; dy
<= +1; dy
++)
2274 if (xx
+dx
>= 0 && xx
+dx
< ret
->w
&&
2275 yy
+dy
>= 0 && yy
+dy
< ret
->h
&&
2276 ret
->layout
->mines
[(yy
+dy
)*ret
->w
+(xx
+dx
)])
2279 ret
->grid
[yy
*ret
->w
+xx
] = v
;
2282 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
2288 static char *game_text_format(game_state
*state
)
2293 ret
= snewn((state
->w
+ 1) * state
->h
+ 1, char);
2294 for (y
= 0; y
< state
->h
; y
++) {
2295 for (x
= 0; x
< state
->w
; x
++) {
2296 int v
= state
->grid
[y
*state
->w
+x
];
2299 else if (v
>= 1 && v
<= 8)
2303 else if (v
== -2 || v
== -3)
2307 ret
[y
* (state
->w
+1) + x
] = v
;
2309 ret
[y
* (state
->w
+1) + state
->w
] = '\n';
2311 ret
[(state
->w
+ 1) * state
->h
] = '\0';
2317 int hx
, hy
, hradius
; /* for mouse-down highlights */
2321 static game_ui
*new_ui(game_state
*state
)
2323 game_ui
*ui
= snew(game_ui
);
2324 ui
->hx
= ui
->hy
= -1;
2326 ui
->flash_is_death
= FALSE
; /* *shrug* */
2330 static void free_ui(game_ui
*ui
)
2335 static game_state
*make_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
2336 int x
, int y
, int button
)
2341 if (from
->dead
|| from
->won
)
2342 return NULL
; /* no further moves permitted */
2344 if (!IS_MOUSE_DOWN(button
) && !IS_MOUSE_DRAG(button
) &&
2345 !IS_MOUSE_RELEASE(button
))
2350 if (cx
< 0 || cx
>= from
->w
|| cy
< 0 || cy
> from
->h
)
2353 if (button
== LEFT_BUTTON
|| button
== LEFT_DRAG
) {
2355 * Mouse-downs and mouse-drags just cause highlighting
2360 ui
->hradius
= (from
->grid
[cy
*from
->w
+cx
] >= 0 ?
1 : 0);
2364 if (button
== RIGHT_BUTTON
) {
2366 * Right-clicking only works on a covered square, and it
2367 * toggles between -1 (marked as mine) and -2 (not marked
2370 * FIXME: question marks.
2372 if (from
->grid
[cy
* from
->w
+ cx
] != -2 &&
2373 from
->grid
[cy
* from
->w
+ cx
] != -1)
2376 ret
= dup_game(from
);
2377 ret
->just_used_solve
= FALSE
;
2378 ret
->grid
[cy
* from
->w
+ cx
] ^= (-2 ^ -1);
2383 if (button
== LEFT_RELEASE
) {
2384 ui
->hx
= ui
->hy
= -1;
2388 * At this stage we must never return NULL: we have adjusted
2389 * the ui, so at worst we return `from'.
2393 * Left-clicking on a covered square opens a tile. Not
2394 * permitted if the tile is marked as a mine, for safety.
2395 * (Unmark it and _then_ open it.)
2397 if (from
->grid
[cy
* from
->w
+ cx
] == -2 ||
2398 from
->grid
[cy
* from
->w
+ cx
] == -3) {
2399 ret
= dup_game(from
);
2400 ret
->just_used_solve
= FALSE
;
2401 open_square(ret
, cx
, cy
);
2406 * Left-clicking on an uncovered tile: first we check to see if
2407 * the number of mine markers surrounding the tile is equal to
2408 * its mine count, and if so then we open all other surrounding
2411 if (from
->grid
[cy
* from
->w
+ cx
] > 0) {
2414 /* Count mine markers. */
2416 for (dy
= -1; dy
<= +1; dy
++)
2417 for (dx
= -1; dx
<= +1; dx
++)
2418 if (cx
+dx
>= 0 && cx
+dx
< from
->w
&&
2419 cy
+dy
>= 0 && cy
+dy
< from
->h
) {
2420 if (from
->grid
[(cy
+dy
)*from
->w
+(cx
+dx
)] == -1)
2424 if (n
== from
->grid
[cy
* from
->w
+ cx
]) {
2425 ret
= dup_game(from
);
2426 ret
->just_used_solve
= FALSE
;
2427 for (dy
= -1; dy
<= +1; dy
++)
2428 for (dx
= -1; dx
<= +1; dx
++)
2429 if (cx
+dx
>= 0 && cx
+dx
< ret
->w
&&
2430 cy
+dy
>= 0 && cy
+dy
< ret
->h
&&
2431 (ret
->grid
[(cy
+dy
)*ret
->w
+(cx
+dx
)] == -2 ||
2432 ret
->grid
[(cy
+dy
)*ret
->w
+(cx
+dx
)] == -3))
2433 open_square(ret
, cx
+dx
, cy
+dy
);
2444 /* ----------------------------------------------------------------------
2448 struct game_drawstate
{
2452 * Items in this `grid' array have all the same values as in
2453 * the game_state grid, and in addition:
2455 * - -10 means the tile was drawn `specially' as a result of a
2456 * flash, so it will always need redrawing.
2458 * - -22 and -23 mean the tile is highlighted for a possible
2463 static void game_size(game_params
*params
, int *x
, int *y
)
2465 *x
= BORDER
* 2 + TILE_SIZE
* params
->w
;
2466 *y
= BORDER
* 2 + TILE_SIZE
* params
->h
;
2469 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2471 float *ret
= snewn(3 * NCOLOURS
, float);
2473 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2475 ret
[COL_BACKGROUND2
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 19.0 / 20.0;
2476 ret
[COL_BACKGROUND2
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 19.0 / 20.0;
2477 ret
[COL_BACKGROUND2
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 19.0 / 20.0;
2479 ret
[COL_1
* 3 + 0] = 0.0F
;
2480 ret
[COL_1
* 3 + 1] = 0.0F
;
2481 ret
[COL_1
* 3 + 2] = 1.0F
;
2483 ret
[COL_2
* 3 + 0] = 0.0F
;
2484 ret
[COL_2
* 3 + 1] = 0.5F
;
2485 ret
[COL_2
* 3 + 2] = 0.0F
;
2487 ret
[COL_3
* 3 + 0] = 1.0F
;
2488 ret
[COL_3
* 3 + 1] = 0.0F
;
2489 ret
[COL_3
* 3 + 2] = 0.0F
;
2491 ret
[COL_4
* 3 + 0] = 0.0F
;
2492 ret
[COL_4
* 3 + 1] = 0.0F
;
2493 ret
[COL_4
* 3 + 2] = 0.5F
;
2495 ret
[COL_5
* 3 + 0] = 0.5F
;
2496 ret
[COL_5
* 3 + 1] = 0.0F
;
2497 ret
[COL_5
* 3 + 2] = 0.0F
;
2499 ret
[COL_6
* 3 + 0] = 0.0F
;
2500 ret
[COL_6
* 3 + 1] = 0.5F
;
2501 ret
[COL_6
* 3 + 2] = 0.5F
;
2503 ret
[COL_7
* 3 + 0] = 0.0F
;
2504 ret
[COL_7
* 3 + 1] = 0.0F
;
2505 ret
[COL_7
* 3 + 2] = 0.0F
;
2507 ret
[COL_8
* 3 + 0] = 0.5F
;
2508 ret
[COL_8
* 3 + 1] = 0.5F
;
2509 ret
[COL_8
* 3 + 2] = 0.5F
;
2511 ret
[COL_MINE
* 3 + 0] = 0.0F
;
2512 ret
[COL_MINE
* 3 + 1] = 0.0F
;
2513 ret
[COL_MINE
* 3 + 2] = 0.0F
;
2515 ret
[COL_BANG
* 3 + 0] = 1.0F
;
2516 ret
[COL_BANG
* 3 + 1] = 0.0F
;
2517 ret
[COL_BANG
* 3 + 2] = 0.0F
;
2519 ret
[COL_CROSS
* 3 + 0] = 1.0F
;
2520 ret
[COL_CROSS
* 3 + 1] = 0.0F
;
2521 ret
[COL_CROSS
* 3 + 2] = 0.0F
;
2523 ret
[COL_FLAG
* 3 + 0] = 1.0F
;
2524 ret
[COL_FLAG
* 3 + 1] = 0.0F
;
2525 ret
[COL_FLAG
* 3 + 2] = 0.0F
;
2527 ret
[COL_FLAGBASE
* 3 + 0] = 0.0F
;
2528 ret
[COL_FLAGBASE
* 3 + 1] = 0.0F
;
2529 ret
[COL_FLAGBASE
* 3 + 2] = 0.0F
;
2531 ret
[COL_QUERY
* 3 + 0] = 0.0F
;
2532 ret
[COL_QUERY
* 3 + 1] = 0.0F
;
2533 ret
[COL_QUERY
* 3 + 2] = 0.0F
;
2535 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
2536 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
2537 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
2539 ret
[COL_LOWLIGHT
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 2.0 / 3.0;
2540 ret
[COL_LOWLIGHT
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 2.0 / 3.0;
2541 ret
[COL_LOWLIGHT
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 2.0 / 3.0;
2543 *ncolours
= NCOLOURS
;
2547 static game_drawstate
*game_new_drawstate(game_state
*state
)
2549 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2553 ds
->started
= FALSE
;
2554 ds
->grid
= snewn(ds
->w
* ds
->h
, char);
2556 memset(ds
->grid
, -99, ds
->w
* ds
->h
);
2561 static void game_free_drawstate(game_drawstate
*ds
)
2567 static void draw_tile(frontend
*fe
, int x
, int y
, int v
, int bg
)
2573 if (v
== -22 || v
== -23) {
2577 * Omit the highlights in this case.
2579 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
2580 bg
== COL_BACKGROUND ? COL_BACKGROUND2
: bg
);
2581 draw_line(fe
, x
, y
, x
+ TILE_SIZE
- 1, y
, COL_LOWLIGHT
);
2582 draw_line(fe
, x
, y
, x
, y
+ TILE_SIZE
- 1, COL_LOWLIGHT
);
2585 * Draw highlights to indicate the square is covered.
2587 coords
[0] = x
+ TILE_SIZE
- 1;
2588 coords
[1] = y
+ TILE_SIZE
- 1;
2589 coords
[2] = x
+ TILE_SIZE
- 1;
2592 coords
[5] = y
+ TILE_SIZE
- 1;
2593 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
^ hl
);
2594 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
^ hl
);
2598 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
^ hl
);
2599 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
^ hl
);
2601 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
2602 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
2610 #define SETCOORD(n, dx, dy) do { \
2611 coords[(n)*2+0] = x + TILE_SIZE * (dx); \
2612 coords[(n)*2+1] = y + TILE_SIZE * (dy); \
2614 SETCOORD(0, 0.6, 0.35);
2615 SETCOORD(1, 0.6, 0.7);
2616 SETCOORD(2, 0.8, 0.8);
2617 SETCOORD(3, 0.25, 0.8);
2618 SETCOORD(4, 0.55, 0.7);
2619 SETCOORD(5, 0.55, 0.35);
2620 draw_polygon(fe
, coords
, 6, TRUE
, COL_FLAGBASE
);
2621 draw_polygon(fe
, coords
, 6, FALSE
, COL_FLAGBASE
);
2623 SETCOORD(0, 0.6, 0.2);
2624 SETCOORD(1, 0.6, 0.5);
2625 SETCOORD(2, 0.2, 0.35);
2626 draw_polygon(fe
, coords
, 3, TRUE
, COL_FLAG
);
2627 draw_polygon(fe
, coords
, 3, FALSE
, COL_FLAG
);
2630 } else if (v
== -3) {
2632 * Draw a question mark.
2634 draw_text(fe
, x
+ TILE_SIZE
/ 2, y
+ TILE_SIZE
/ 2,
2635 FONT_VARIABLE
, TILE_SIZE
* 6 / 8,
2636 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2641 * Clear the square to the background colour, and draw thin
2642 * grid lines along the top and left.
2644 * Exception is that for value 65 (mine we've just trodden
2645 * on), we clear the square to COL_BANG.
2647 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
2648 (v
== 65 ? COL_BANG
:
2649 bg
== COL_BACKGROUND ? COL_BACKGROUND2
: bg
));
2650 draw_line(fe
, x
, y
, x
+ TILE_SIZE
- 1, y
, COL_LOWLIGHT
);
2651 draw_line(fe
, x
, y
, x
, y
+ TILE_SIZE
- 1, COL_LOWLIGHT
);
2653 if (v
> 0 && v
<= 8) {
2660 draw_text(fe
, x
+ TILE_SIZE
/ 2, y
+ TILE_SIZE
/ 2,
2661 FONT_VARIABLE
, TILE_SIZE
* 7 / 8,
2662 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2663 (COL_1
- 1) + v
, str
);
2665 } else if (v
>= 64) {
2669 * FIXME: this could be done better!
2672 draw_text(fe
, x
+ TILE_SIZE
/ 2, y
+ TILE_SIZE
/ 2,
2673 FONT_VARIABLE
, TILE_SIZE
* 7 / 8,
2674 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2678 int cx
= x
+ TILE_SIZE
/ 2;
2679 int cy
= y
+ TILE_SIZE
/ 2;
2680 int r
= TILE_SIZE
/ 2 - 3;
2682 int xdx
= 1, xdy
= 0, ydx
= 0, ydy
= 1;
2685 for (i
= 0; i
< 4*5*2; i
+= 5*2) {
2686 coords
[i
+2*0+0] = cx
- r
/6*xdx
+ r
*4/5*ydx
;
2687 coords
[i
+2*0+1] = cy
- r
/6*xdy
+ r
*4/5*ydy
;
2688 coords
[i
+2*1+0] = cx
- r
/6*xdx
+ r
*ydx
;
2689 coords
[i
+2*1+1] = cy
- r
/6*xdy
+ r
*ydy
;
2690 coords
[i
+2*2+0] = cx
+ r
/6*xdx
+ r
*ydx
;
2691 coords
[i
+2*2+1] = cy
+ r
/6*xdy
+ r
*ydy
;
2692 coords
[i
+2*3+0] = cx
+ r
/6*xdx
+ r
*4/5*ydx
;
2693 coords
[i
+2*3+1] = cy
+ r
/6*xdy
+ r
*4/5*ydy
;
2694 coords
[i
+2*4+0] = cx
+ r
*3/5*xdx
+ r
*3/5*ydx
;
2695 coords
[i
+2*4+1] = cy
+ r
*3/5*xdy
+ r
*3/5*ydy
;
2705 draw_polygon(fe
, coords
, 5*4, TRUE
, COL_MINE
);
2706 draw_polygon(fe
, coords
, 5*4, FALSE
, COL_MINE
);
2708 draw_rect(fe
, cx
-r
/3, cy
-r
/3, r
/3, r
/4, COL_HIGHLIGHT
);
2714 * Cross through the mine.
2717 for (dx
= -1; dx
<= +1; dx
++) {
2718 draw_line(fe
, x
+ 3 + dx
, y
+ 2,
2719 x
+ TILE_SIZE
- 3 + dx
,
2720 y
+ TILE_SIZE
- 2, COL_CROSS
);
2721 draw_line(fe
, x
+ TILE_SIZE
- 3 + dx
, y
+ 2,
2722 x
+ 3 + dx
, y
+ TILE_SIZE
- 2,
2729 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
2732 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
2733 game_state
*state
, int dir
, game_ui
*ui
,
2734 float animtime
, float flashtime
)
2737 int mines
, markers
, bg
;
2740 int frame
= (flashtime
/ FLASH_FRAME
);
2742 bg
= (ui
->flash_is_death ? COL_BACKGROUND
: COL_LOWLIGHT
);
2744 bg
= (ui
->flash_is_death ? COL_BANG
: COL_HIGHLIGHT
);
2746 bg
= COL_BACKGROUND
;
2752 TILE_SIZE
* state
->w
+ 2 * BORDER
,
2753 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
2754 draw_update(fe
, 0, 0,
2755 TILE_SIZE
* state
->w
+ 2 * BORDER
,
2756 TILE_SIZE
* state
->h
+ 2 * BORDER
);
2759 * Recessed area containing the whole puzzle.
2761 coords
[0] = COORD(state
->w
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2762 coords
[1] = COORD(state
->h
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2763 coords
[2] = COORD(state
->w
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2764 coords
[3] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2765 coords
[4] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2766 coords
[5] = COORD(state
->h
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2767 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
2768 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
2770 coords
[1] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2771 coords
[0] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2772 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
2773 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
2779 * Now draw the tiles. Also in this loop, count up the number
2780 * of mines and mine markers.
2782 mines
= markers
= 0;
2783 for (y
= 0; y
< ds
->h
; y
++)
2784 for (x
= 0; x
< ds
->w
; x
++) {
2785 int v
= state
->grid
[y
*ds
->w
+x
];
2789 if (state
->layout
->mines
&& state
->layout
->mines
[y
*ds
->w
+x
])
2792 if ((v
== -2 || v
== -3) &&
2793 (abs(x
-ui
->hx
) <= ui
->hradius
&& abs(y
-ui
->hy
) <= ui
->hradius
))
2796 if (ds
->grid
[y
*ds
->w
+x
] != v
|| bg
!= COL_BACKGROUND
) {
2797 draw_tile(fe
, COORD(x
), COORD(y
), v
, bg
);
2798 ds
->grid
[y
*ds
->w
+x
] = (bg
== COL_BACKGROUND ? v
: -10);
2802 if (!state
->layout
->mines
)
2803 mines
= state
->layout
->n
;
2806 * Update the status bar.
2809 char statusbar
[512];
2811 sprintf(statusbar
, "GAME OVER!");
2812 } else if (state
->won
) {
2813 if (state
->used_solve
)
2814 sprintf(statusbar
, "Auto-solved.");
2816 sprintf(statusbar
, "COMPLETED!");
2818 sprintf(statusbar
, "Mines marked: %d / %d", markers
, mines
);
2820 status_bar(fe
, statusbar
);
2824 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2825 int dir
, game_ui
*ui
)
2830 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2831 int dir
, game_ui
*ui
)
2833 if (oldstate
->used_solve
|| newstate
->used_solve
)
2836 if (dir
> 0 && !oldstate
->dead
&& !oldstate
->won
) {
2837 if (newstate
->dead
) {
2838 ui
->flash_is_death
= TRUE
;
2839 return 3 * FLASH_FRAME
;
2841 if (newstate
->won
) {
2842 ui
->flash_is_death
= FALSE
;
2843 return 2 * FLASH_FRAME
;
2849 static int game_wants_statusbar(void)
2854 static int game_timing_state(game_state
*state
)
2856 if (state
->dead
|| state
->won
|| !state
->layout
->mines
)
2862 #define thegame mines
2865 const struct game thegame
= {
2866 "Mines", "games.mines",
2873 TRUE
, game_configure
, custom_params
,
2882 TRUE
, game_text_format
,
2889 game_free_drawstate
,
2893 game_wants_statusbar
,
2894 TRUE
, game_timing_state
,