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 * - think about configurably supporting question marks. Once,
14 * that is, we've thought about configurability in general!
28 COL_BACKGROUND
, COL_BACKGROUND2
,
29 COL_1
, COL_2
, COL_3
, COL_4
, COL_5
, COL_6
, COL_7
, COL_8
,
30 COL_MINE
, COL_BANG
, COL_CROSS
, COL_FLAG
, COL_FLAGBASE
, COL_QUERY
,
31 COL_HIGHLIGHT
, COL_LOWLIGHT
,
36 #define BORDER (TILE_SIZE * 3 / 2)
37 #define HIGHLIGHT_WIDTH 2
38 #define OUTER_HIGHLIGHT_WIDTH 3
39 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
40 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
42 #define FLASH_FRAME 0.13F
51 * This structure is shared between all the game_states for a
52 * given instance of the puzzle, so we reference-count it.
57 * If we haven't yet actually generated the mine layout, here's
58 * all the data we will need to do so.
62 midend_data
*me
; /* to give back the new game desc */
66 int w
, h
, n
, dead
, won
;
67 int used_solve
, just_used_solve
;
68 struct mine_layout
*layout
; /* real mine positions */
69 signed char *grid
; /* player knowledge */
71 * Each item in the `grid' array is one of the following values:
73 * - 0 to 8 mean the square is open and has a surrounding mine
76 * - -1 means the square is marked as a mine.
78 * - -2 means the square is unknown.
80 * - -3 means the square is marked with a question mark
81 * (FIXME: do we even want to bother with this?).
83 * - 64 means the square has had a mine revealed when the game
86 * - 65 means the square had a mine revealed and this was the
87 * one the player hits.
89 * - 66 means the square has a crossed-out mine because the
90 * player had incorrectly marked it.
94 static game_params
*default_params(void)
96 game_params
*ret
= snew(game_params
);
105 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
109 static const struct { int w
, h
, n
; } values
[] = {
115 if (i
< 0 || i
>= lenof(values
))
118 ret
= snew(game_params
);
119 ret
->w
= values
[i
].w
;
120 ret
->h
= values
[i
].h
;
121 ret
->n
= values
[i
].n
;
124 sprintf(str
, "%dx%d, %d mines", ret
->w
, ret
->h
, ret
->n
);
131 static void free_params(game_params
*params
)
136 static game_params
*dup_params(game_params
*params
)
138 game_params
*ret
= snew(game_params
);
139 *ret
= *params
; /* structure copy */
143 static void decode_params(game_params
*params
, char const *string
)
145 char const *p
= string
;
148 while (*p
&& isdigit((unsigned char)*p
)) p
++;
152 while (*p
&& isdigit((unsigned char)*p
)) p
++;
154 params
->h
= params
->w
;
159 while (*p
&& (*p
== '.' || isdigit((unsigned char)*p
))) p
++;
161 params
->n
= params
->w
* params
->h
/ 10;
167 params
->unique
= FALSE
;
169 p
++; /* skip any other gunk */
173 static char *encode_params(game_params
*params
, int full
)
178 len
= sprintf(ret
, "%dx%d", params
->w
, params
->h
);
180 * Mine count is a generation-time parameter, since it can be
181 * deduced from the mine bitmap!
184 len
+= sprintf(ret
+len
, "n%d", params
->n
);
185 if (full
&& !params
->unique
)
187 assert(len
< lenof(ret
));
193 static config_item
*game_configure(game_params
*params
)
198 ret
= snewn(5, config_item
);
200 ret
[0].name
= "Width";
201 ret
[0].type
= C_STRING
;
202 sprintf(buf
, "%d", params
->w
);
203 ret
[0].sval
= dupstr(buf
);
206 ret
[1].name
= "Height";
207 ret
[1].type
= C_STRING
;
208 sprintf(buf
, "%d", params
->h
);
209 ret
[1].sval
= dupstr(buf
);
212 ret
[2].name
= "Mines";
213 ret
[2].type
= C_STRING
;
214 sprintf(buf
, "%d", params
->n
);
215 ret
[2].sval
= dupstr(buf
);
218 ret
[3].name
= "Ensure solubility";
219 ret
[3].type
= C_BOOLEAN
;
221 ret
[3].ival
= params
->unique
;
231 static game_params
*custom_params(config_item
*cfg
)
233 game_params
*ret
= snew(game_params
);
235 ret
->w
= atoi(cfg
[0].sval
);
236 ret
->h
= atoi(cfg
[1].sval
);
237 ret
->n
= atoi(cfg
[2].sval
);
238 if (strchr(cfg
[2].sval
, '%'))
239 ret
->n
= ret
->n
* (ret
->w
* ret
->h
) / 100;
240 ret
->unique
= cfg
[3].ival
;
245 static char *validate_params(game_params
*params
)
247 if (params
->w
<= 0 && params
->h
<= 0)
248 return "Width and height must both be greater than zero";
250 return "Width must be greater than zero";
252 return "Height must be greater than zero";
253 if (params
->n
> params
->w
* params
->h
- 9)
254 return "Too many mines for grid size";
257 * FIXME: Need more constraints here. Not sure what the
258 * sensible limits for Minesweeper actually are. The limits
259 * probably ought to change, however, depending on uniqueness.
265 /* ----------------------------------------------------------------------
266 * Minesweeper solver, used to ensure the generated grids are
267 * solvable without having to take risks.
271 * Count the bits in a word. Only needs to cope with 16 bits.
273 static int bitcount16(int word
)
275 word
= ((word
& 0xAAAA) >> 1) + (word
& 0x5555);
276 word
= ((word
& 0xCCCC) >> 2) + (word
& 0x3333);
277 word
= ((word
& 0xF0F0) >> 4) + (word
& 0x0F0F);
278 word
= ((word
& 0xFF00) >> 8) + (word
& 0x00FF);
284 * We use a tree234 to store a large number of small localised
285 * sets, each with a mine count. We also keep some of those sets
286 * linked together into a to-do list.
289 short x
, y
, mask
, mines
;
291 struct set
*prev
, *next
;
294 static int setcmp(void *av
, void *bv
)
296 struct set
*a
= (struct set
*)av
;
297 struct set
*b
= (struct set
*)bv
;
301 else if (a
->y
> b
->y
)
303 else if (a
->x
< b
->x
)
305 else if (a
->x
> b
->x
)
307 else if (a
->mask
< b
->mask
)
309 else if (a
->mask
> b
->mask
)
317 struct set
*todo_head
, *todo_tail
;
320 static struct setstore
*ss_new(void)
322 struct setstore
*ss
= snew(struct setstore
);
323 ss
->sets
= newtree234(setcmp
);
324 ss
->todo_head
= ss
->todo_tail
= NULL
;
329 * Take two input sets, in the form (x,y,mask). Munge the first by
330 * taking either its intersection with the second or its difference
331 * with the second. Return the new mask part of the first set.
333 static int setmunge(int x1
, int y1
, int mask1
, int x2
, int y2
, int mask2
,
337 * Adjust the second set so that it has the same x,y
338 * coordinates as the first.
340 if (abs(x2
-x1
) >= 3 || abs(y2
-y1
) >= 3) {
344 mask2
&= ~(4|32|256);
354 mask2
&= ~(64|128|256);
366 * Invert the second set if `diff' is set (we're after A &~ B
367 * rather than A & B).
373 * Now all that's left is a logical AND.
375 return mask1
& mask2
;
378 static void ss_add_todo(struct setstore
*ss
, struct set
*s
)
381 return; /* already on it */
383 #ifdef SOLVER_DIAGNOSTICS
384 printf("adding set on todo list: %d,%d %03x %d\n",
385 s
->x
, s
->y
, s
->mask
, s
->mines
);
388 s
->prev
= ss
->todo_tail
;
398 static void ss_add(struct setstore
*ss
, int x
, int y
, int mask
, int mines
)
405 * Normalise so that x and y are genuinely the bounding
408 while (!(mask
& (1|8|64)))
410 while (!(mask
& (1|2|4)))
414 * Create a set structure and add it to the tree.
416 s
= snew(struct set
);
422 if (add234(ss
->sets
, s
) != s
) {
424 * This set already existed! Free it and return.
431 * We've added a new set to the tree, so put it on the todo
437 static void ss_remove(struct setstore
*ss
, struct set
*s
)
439 struct set
*next
= s
->next
, *prev
= s
->prev
;
441 #ifdef SOLVER_DIAGNOSTICS
442 printf("removing set %d,%d %03x\n", s
->x
, s
->y
, s
->mask
);
445 * Remove s from the todo list.
449 else if (s
== ss
->todo_head
)
450 ss
->todo_head
= next
;
454 else if (s
== ss
->todo_tail
)
455 ss
->todo_tail
= prev
;
460 * Remove s from the tree.
465 * Destroy the actual set structure.
471 * Return a dynamically allocated list of all the sets which
472 * overlap a provided input set.
474 static struct set
**ss_overlap(struct setstore
*ss
, int x
, int y
, int mask
)
476 struct set
**ret
= NULL
;
477 int nret
= 0, retsize
= 0;
480 for (xx
= x
-3; xx
< x
+3; xx
++)
481 for (yy
= y
-3; yy
< y
+3; yy
++) {
486 * Find the first set with these top left coordinates.
492 if (findrelpos234(ss
->sets
, &stmp
, NULL
, REL234_GE
, &pos
)) {
493 while ((s
= index234(ss
->sets
, pos
)) != NULL
&&
494 s
->x
== xx
&& s
->y
== yy
) {
496 * This set potentially overlaps the input one.
497 * Compute the intersection to see if they
498 * really overlap, and add it to the list if
501 if (setmunge(x
, y
, mask
, s
->x
, s
->y
, s
->mask
, FALSE
)) {
503 * There's an overlap.
505 if (nret
>= retsize
) {
507 ret
= sresize(ret
, retsize
, struct set
*);
517 ret
= sresize(ret
, nret
+1, struct set
*);
524 * Get an element from the head of the set todo list.
526 static struct set
*ss_todo(struct setstore
*ss
)
529 struct set
*ret
= ss
->todo_head
;
530 ss
->todo_head
= ret
->next
;
532 ss
->todo_head
->prev
= NULL
;
534 ss
->todo_tail
= NULL
;
535 ret
->next
= ret
->prev
= NULL
;
548 static void std_add(struct squaretodo
*std
, int i
)
551 std
->next
[std
->tail
] = i
;
558 static void known_squares(int w
, int h
, struct squaretodo
*std
,
560 int (*open
)(void *ctx
, int x
, int y
), void *openctx
,
561 int x
, int y
, int mask
, int mine
)
567 for (yy
= 0; yy
< 3; yy
++)
568 for (xx
= 0; xx
< 3; xx
++) {
570 int i
= (y
+ yy
) * w
+ (x
+ xx
);
573 * It's possible that this square is _already_
574 * known, in which case we don't try to add it to
580 grid
[i
] = -1; /* and don't open it! */
582 grid
[i
] = open(openctx
, x
+ xx
, y
+ yy
);
583 assert(grid
[i
] != -1); /* *bang* */
594 * This is data returned from the `perturb' function. It details
595 * which squares have become mines and which have become clear. The
596 * solver is (of course) expected to honourably not use that
597 * knowledge directly, but to efficently adjust its internal data
598 * structures and proceed based on only the information it
601 struct perturbation
{
603 int delta
; /* +1 == become a mine; -1 == cleared */
605 struct perturbations
{
607 struct perturbation
*changes
;
611 * Main solver entry point. You give it a grid of existing
612 * knowledge (-1 for a square known to be a mine, 0-8 for empty
613 * squares with a given number of neighbours, -2 for completely
614 * unknown), plus a function which you can call to open new squares
615 * once you're confident of them. It fills in as much more of the
620 * - -1 means deduction stalled and nothing could be done
621 * - 0 means deduction succeeded fully
622 * - >0 means deduction succeeded but some number of perturbation
623 * steps were required; the exact return value is the number of
626 static int minesolve(int w
, int h
, int n
, signed char *grid
,
627 int (*open
)(void *ctx
, int x
, int y
),
628 struct perturbations
*(*perturb
)(void *ctx
,
630 int x
, int y
, int mask
),
631 void *ctx
, random_state
*rs
)
633 struct setstore
*ss
= ss_new();
635 struct squaretodo astd
, *std
= &astd
;
640 * Set up a linked list of squares with known contents, so that
641 * we can process them one by one.
643 std
->next
= snewn(w
*h
, int);
644 std
->head
= std
->tail
= -1;
647 * Initialise that list with all known squares in the input
650 for (y
= 0; y
< h
; y
++) {
651 for (x
= 0; x
< w
; x
++) {
659 * Main deductive loop.
662 int done_something
= FALSE
;
666 * If there are any known squares on the todo list, process
667 * them and construct a set for each.
669 while (std
->head
!= -1) {
671 #ifdef SOLVER_DIAGNOSTICS
672 printf("known square at %d,%d [%d]\n", i
%w
, i
/w
, grid
[i
]);
674 std
->head
= std
->next
[i
];
682 int dx
, dy
, mines
, bit
, val
;
683 #ifdef SOLVER_DIAGNOSTICS
684 printf("creating set around this square\n");
687 * Empty square. Construct the set of non-known squares
688 * around this one, and determine its mine count.
693 for (dy
= -1; dy
<= +1; dy
++) {
694 for (dx
= -1; dx
<= +1; dx
++) {
695 #ifdef SOLVER_DIAGNOSTICS
696 printf("grid %d,%d = %d\n", x
+dx
, y
+dy
, grid
[i
+dy
*w
+dx
]);
698 if (x
+dx
< 0 || x
+dx
>= w
|| y
+dy
< 0 || y
+dy
>= h
)
699 /* ignore this one */;
700 else if (grid
[i
+dy
*w
+dx
] == -1)
702 else if (grid
[i
+dy
*w
+dx
] == -2)
708 ss_add(ss
, x
-1, y
-1, val
, mines
);
712 * Now, whether the square is empty or full, we must
713 * find any set which contains it and replace it with
714 * one which does not.
717 #ifdef SOLVER_DIAGNOSTICS
718 printf("finding sets containing known square %d,%d\n", x
, y
);
720 list
= ss_overlap(ss
, x
, y
, 1);
722 for (j
= 0; list
[j
]; j
++) {
723 int newmask
, newmines
;
728 * Compute the mask for this set minus the
729 * newly known square.
731 newmask
= setmunge(s
->x
, s
->y
, s
->mask
, x
, y
, 1, TRUE
);
734 * Compute the new mine count.
736 newmines
= s
->mines
- (grid
[i
] == -1);
739 * Insert the new set into the collection,
740 * unless it's been whittled right down to
744 ss_add(ss
, s
->x
, s
->y
, newmask
, newmines
);
747 * Destroy the old one; it is actually obsolete.
756 * Marking a fresh square as known certainly counts as
759 done_something
= TRUE
;
763 * Now pick a set off the to-do list and attempt deductions
766 if ((s
= ss_todo(ss
)) != NULL
) {
768 #ifdef SOLVER_DIAGNOSTICS
769 printf("set to do: %d,%d %03x %d\n", s
->x
, s
->y
, s
->mask
, s
->mines
);
772 * Firstly, see if this set has a mine count of zero or
773 * of its own cardinality.
775 if (s
->mines
== 0 || s
->mines
== bitcount16(s
->mask
)) {
777 * If so, we can immediately mark all the squares
778 * in the set as known.
780 #ifdef SOLVER_DIAGNOSTICS
783 known_squares(w
, h
, std
, grid
, open
, ctx
,
784 s
->x
, s
->y
, s
->mask
, (s
->mines
!= 0));
787 * Having done that, we need do nothing further
788 * with this set; marking all the squares in it as
789 * known will eventually eliminate it, and will
790 * also permit further deductions about anything
797 * Failing that, we now search through all the sets
798 * which overlap this one.
800 list
= ss_overlap(ss
, s
->x
, s
->y
, s
->mask
);
802 for (j
= 0; list
[j
]; j
++) {
803 struct set
*s2
= list
[j
];
804 int swing
, s2wing
, swc
, s2wc
;
807 * Find the non-overlapping parts s2-s and s-s2,
808 * and their cardinalities.
810 * I'm going to refer to these parts as `wings'
811 * surrounding the central part common to both
812 * sets. The `s wing' is s-s2; the `s2 wing' is
815 swing
= setmunge(s
->x
, s
->y
, s
->mask
, s2
->x
, s2
->y
, s2
->mask
,
817 s2wing
= setmunge(s2
->x
, s2
->y
, s2
->mask
, s
->x
, s
->y
, s
->mask
,
819 swc
= bitcount16(swing
);
820 s2wc
= bitcount16(s2wing
);
823 * If one set has more mines than the other, and
824 * the number of extra mines is equal to the
825 * cardinality of that set's wing, then we can mark
826 * every square in the wing as a known mine, and
827 * every square in the other wing as known clear.
829 if (swc
== s
->mines
- s2
->mines
||
830 s2wc
== s2
->mines
- s
->mines
) {
831 known_squares(w
, h
, std
, grid
, open
, ctx
,
833 (swc
== s
->mines
- s2
->mines
));
834 known_squares(w
, h
, std
, grid
, open
, ctx
,
835 s2
->x
, s2
->y
, s2wing
,
836 (s2wc
== s2
->mines
- s
->mines
));
841 * Failing that, see if one set is a subset of the
842 * other. If so, we can divide up the mine count of
843 * the larger set between the smaller set and its
844 * complement, even if neither smaller set ends up
845 * being immediately clearable.
847 if (swc
== 0 && s2wc
!= 0) {
848 /* s is a subset of s2. */
849 assert(s2
->mines
> s
->mines
);
850 ss_add(ss
, s2
->x
, s2
->y
, s2wing
, s2
->mines
- s
->mines
);
851 } else if (s2wc
== 0 && swc
!= 0) {
852 /* s2 is a subset of s. */
853 assert(s
->mines
> s2
->mines
);
854 ss_add(ss
, s
->x
, s
->y
, swing
, s
->mines
- s2
->mines
);
861 * In this situation we have definitely done
862 * _something_, even if it's only reducing the size of
865 done_something
= TRUE
;
868 * We have nothing left on our todo list, which means
869 * all localised deductions have failed. Our next step
870 * is to resort to global deduction based on the total
871 * mine count. This is computationally expensive
872 * compared to any of the above deductions, which is
873 * why we only ever do it when all else fails, so that
874 * hopefully it won't have to happen too often.
876 * If you pass n<0 into this solver, that informs it
877 * that you do not know the total mine count, so it
878 * won't even attempt these deductions.
881 int minesleft
, squaresleft
;
882 int nsets
, setused
[10], cursor
;
885 * Start by scanning the current grid state to work out
886 * how many unknown squares we still have, and how many
887 * mines are to be placed in them.
891 for (i
= 0; i
< w
*h
; i
++) {
894 else if (grid
[i
] == -2)
898 #ifdef SOLVER_DIAGNOSTICS
899 printf("global deduction time: squaresleft=%d minesleft=%d\n",
900 squaresleft
, minesleft
);
901 for (y
= 0; y
< h
; y
++) {
902 for (x
= 0; x
< w
; x
++) {
918 * If there _are_ no unknown squares, we have actually
921 if (squaresleft
== 0) {
922 assert(minesleft
== 0);
927 * First really simple case: if there are no more mines
928 * left, or if there are exactly as many mines left as
929 * squares to play them in, then it's all easy.
931 if (minesleft
== 0 || minesleft
== squaresleft
) {
932 for (i
= 0; i
< w
*h
; i
++)
934 known_squares(w
, h
, std
, grid
, open
, ctx
,
935 i
% w
, i
/ w
, 1, minesleft
!= 0);
936 continue; /* now go back to main deductive loop */
940 * Failing that, we have to do some _real_ work.
941 * Ideally what we do here is to try every single
942 * combination of the currently available sets, in an
943 * attempt to find a disjoint union (i.e. a set of
944 * squares with a known mine count between them) such
945 * that the remaining unknown squares _not_ contained
946 * in that union either contain no mines or are all
949 * Actually enumerating all 2^n possibilities will get
950 * a bit slow for large n, so I artificially cap this
951 * recursion at n=10 to avoid too much pain.
953 nsets
= count234(ss
->sets
);
954 if (nsets
<= lenof(setused
)) {
956 * Doing this with actual recursive function calls
957 * would get fiddly because a load of local
958 * variables from this function would have to be
959 * passed down through the recursion. So instead
960 * I'm going to use a virtual recursion within this
961 * function. The way this works is:
963 * - we have an array `setused', such that
964 * setused[n] is 0 or 1 depending on whether set
965 * n is currently in the union we are
968 * - we have a value `cursor' which indicates how
969 * much of `setused' we have so far filled in.
970 * It's conceptually the recursion depth.
972 * We begin by setting `cursor' to zero. Then:
974 * - if cursor can advance, we advance it by one.
975 * We set the value in `setused' that it went
976 * past to 1 if that set is disjoint from
977 * anything else currently in `setused', or to 0
980 * - If cursor cannot advance because it has
981 * reached the end of the setused list, then we
982 * have a maximal disjoint union. Check to see
983 * whether its mine count has any useful
984 * properties. If so, mark all the squares not
985 * in the union as known and terminate.
987 * - If cursor has reached the end of setused and
988 * the algorithm _hasn't_ terminated, back
989 * cursor up to the nearest 1, turn it into a 0
990 * and advance cursor just past it.
992 * - If we attempt to back up to the nearest 1 and
993 * there isn't one at all, then we have gone
994 * through all disjoint unions of sets in the
995 * list and none of them has been helpful, so we
998 struct set
*sets
[lenof(setused
)];
999 for (i
= 0; i
< nsets
; i
++)
1000 sets
[i
] = index234(ss
->sets
, i
);
1005 if (cursor
< nsets
) {
1008 /* See if any existing set overlaps this one. */
1009 for (i
= 0; i
< cursor
; i
++)
1011 setmunge(sets
[cursor
]->x
,
1014 sets
[i
]->x
, sets
[i
]->y
, sets
[i
]->mask
,
1022 * We're adding this set to our union,
1023 * so adjust minesleft and squaresleft
1026 minesleft
-= sets
[cursor
]->mines
;
1027 squaresleft
-= bitcount16(sets
[cursor
]->mask
);
1030 setused
[cursor
++] = ok
;
1032 #ifdef SOLVER_DIAGNOSTICS
1033 printf("trying a set combination with %d %d\n",
1034 squaresleft
, minesleft
);
1035 #endif /* SOLVER_DIAGNOSTICS */
1038 * We've reached the end. See if we've got
1039 * anything interesting.
1041 if (squaresleft
> 0 &&
1042 (minesleft
== 0 || minesleft
== squaresleft
)) {
1044 * We have! There is at least one
1045 * square not contained within the set
1046 * union we've just found, and we can
1047 * deduce that either all such squares
1048 * are mines or all are not (depending
1049 * on whether minesleft==0). So now all
1050 * we have to do is actually go through
1051 * the grid, find those squares, and
1054 for (i
= 0; i
< w
*h
; i
++)
1055 if (grid
[i
] == -2) {
1059 for (j
= 0; j
< nsets
; j
++)
1061 setmunge(sets
[j
]->x
, sets
[j
]->y
,
1062 sets
[j
]->mask
, x
, y
, 1,
1068 known_squares(w
, h
, std
, grid
,
1070 x
, y
, 1, minesleft
!= 0);
1073 done_something
= TRUE
;
1074 break; /* return to main deductive loop */
1078 * If we reach here, then this union hasn't
1079 * done us any good, so move on to the
1080 * next. Backtrack cursor to the nearest 1,
1081 * change it to a 0 and continue.
1083 while (--cursor
>= 0 && !setused
[cursor
]);
1085 assert(setused
[cursor
]);
1088 * We're removing this set from our
1089 * union, so re-increment minesleft and
1092 minesleft
+= sets
[cursor
]->mines
;
1093 squaresleft
+= bitcount16(sets
[cursor
]->mask
);
1095 setused
[cursor
++] = 0;
1098 * We've backtracked all the way to the
1099 * start without finding a single 1,
1100 * which means that our virtual
1101 * recursion is complete and nothing
1116 #ifdef SOLVER_DIAGNOSTICS
1118 * Dump the current known state of the grid.
1120 printf("solver ran out of steam, ret=%d, grid:\n", nperturbs
);
1121 for (y
= 0; y
< h
; y
++) {
1122 for (x
= 0; x
< w
; x
++) {
1123 int v
= grid
[y
*w
+x
];
1139 for (i
= 0; (s
= index234(ss
->sets
, i
)) != NULL
; i
++)
1140 printf("remaining set: %d,%d %03x %d\n", s
->x
, s
->y
, s
->mask
, s
->mines
);
1145 * Now we really are at our wits' end as far as solving
1146 * this grid goes. Our only remaining option is to call
1147 * a perturb function and ask it to modify the grid to
1151 struct perturbations
*ret
;
1157 * Choose a set at random from the current selection,
1158 * and ask the perturb function to either fill or empty
1161 * If we have no sets at all, we must give up.
1163 if (count234(ss
->sets
) == 0) {
1164 #ifdef SOLVER_DIAGNOSTICS
1165 printf("perturbing on entire unknown set\n");
1167 ret
= perturb(ctx
, grid
, 0, 0, 0);
1169 s
= index234(ss
->sets
, random_upto(rs
, count234(ss
->sets
)));
1170 #ifdef SOLVER_DIAGNOSTICS
1171 printf("perturbing on set %d,%d %03x\n", s
->x
, s
->y
, s
->mask
);
1173 ret
= perturb(ctx
, grid
, s
->x
, s
->y
, s
->mask
);
1177 assert(ret
->n
> 0); /* otherwise should have been NULL */
1180 * A number of squares have been fiddled with, and
1181 * the returned structure tells us which. Adjust
1182 * the mine count in any set which overlaps one of
1183 * those squares, and put them back on the to-do
1184 * list. Also, if the square itself is marked as a
1185 * known non-mine, put it back on the squares-to-do
1188 for (i
= 0; i
< ret
->n
; i
++) {
1189 #ifdef SOLVER_DIAGNOSTICS
1190 printf("perturbation %s mine at %d,%d\n",
1191 ret
->changes
[i
].delta
> 0 ?
"added" : "removed",
1192 ret
->changes
[i
].x
, ret
->changes
[i
].y
);
1195 if (ret
->changes
[i
].delta
< 0 &&
1196 grid
[ret
->changes
[i
].y
*w
+ret
->changes
[i
].x
] != -2) {
1197 std_add(std
, ret
->changes
[i
].y
*w
+ret
->changes
[i
].x
);
1200 list
= ss_overlap(ss
,
1201 ret
->changes
[i
].x
, ret
->changes
[i
].y
, 1);
1203 for (j
= 0; list
[j
]; j
++) {
1204 list
[j
]->mines
+= ret
->changes
[i
].delta
;
1205 ss_add_todo(ss
, list
[j
]);
1212 * Now free the returned data.
1214 sfree(ret
->changes
);
1217 #ifdef SOLVER_DIAGNOSTICS
1219 * Dump the current known state of the grid.
1221 printf("state after perturbation:\n");
1222 for (y
= 0; y
< h
; y
++) {
1223 for (x
= 0; x
< w
; x
++) {
1224 int v
= grid
[y
*w
+x
];
1240 for (i
= 0; (s
= index234(ss
->sets
, i
)) != NULL
; i
++)
1241 printf("remaining set: %d,%d %03x %d\n", s
->x
, s
->y
, s
->mask
, s
->mines
);
1246 * And now we can go back round the deductive loop.
1253 * If we get here, even that didn't work (either we didn't
1254 * have a perturb function or it returned failure), so we
1261 * See if we've got any unknown squares left.
1263 for (y
= 0; y
< h
; y
++)
1264 for (x
= 0; x
< w
; x
++)
1265 if (grid
[y
*w
+x
] == -2) {
1266 nperturbs
= -1; /* failed to complete */
1271 * Free the set list and square-todo list.
1275 while ((s
= delpos234(ss
->sets
, 0)) != NULL
)
1277 freetree234(ss
->sets
);
1285 /* ----------------------------------------------------------------------
1286 * Grid generator which uses the above solver.
1293 int allow_big_perturbs
;
1297 static int mineopen(void *vctx
, int x
, int y
)
1299 struct minectx
*ctx
= (struct minectx
*)vctx
;
1302 assert(x
>= 0 && x
< ctx
->w
&& y
>= 0 && y
< ctx
->h
);
1303 if (ctx
->grid
[y
* ctx
->w
+ x
])
1304 return -1; /* *bang* */
1307 for (i
= -1; i
<= +1; i
++) {
1308 if (x
+ i
< 0 || x
+ i
>= ctx
->w
)
1310 for (j
= -1; j
<= +1; j
++) {
1311 if (y
+ j
< 0 || y
+ j
>= ctx
->h
)
1313 if (i
== 0 && j
== 0)
1315 if (ctx
->grid
[(y
+j
) * ctx
->w
+ (x
+i
)])
1323 /* Structure used internally to mineperturb(). */
1325 int x
, y
, type
, random
;
1327 static int squarecmp(const void *av
, const void *bv
)
1329 const struct square
*a
= (const struct square
*)av
;
1330 const struct square
*b
= (const struct square
*)bv
;
1331 if (a
->type
< b
->type
)
1333 else if (a
->type
> b
->type
)
1335 else if (a
->random
< b
->random
)
1337 else if (a
->random
> b
->random
)
1339 else if (a
->y
< b
->y
)
1341 else if (a
->y
> b
->y
)
1343 else if (a
->x
< b
->x
)
1345 else if (a
->x
> b
->x
)
1351 * Normally this function is passed an (x,y,mask) set description.
1352 * On occasions, though, there is no _localised_ set being used,
1353 * and the set being perturbed is supposed to be the entirety of
1354 * the unreachable area. This is signified by the special case
1355 * mask==0: in this case, anything labelled -2 in the grid is part
1358 * Allowing perturbation in this special case appears to make it
1359 * guaranteeably possible to generate a workable grid for any mine
1360 * density, but they tend to be a bit boring, with mines packed
1361 * densely into far corners of the grid and the remainder being
1362 * less dense than one might like. Therefore, to improve overall
1363 * grid quality I disable this feature for the first few attempts,
1364 * and fall back to it after no useful grid has been generated.
1366 static struct perturbations
*mineperturb(void *vctx
, signed char *grid
,
1367 int setx
, int sety
, int mask
)
1369 struct minectx
*ctx
= (struct minectx
*)vctx
;
1370 struct square
*sqlist
;
1371 int x
, y
, dx
, dy
, i
, n
, nfull
, nempty
;
1372 struct square
**tofill
, **toempty
, **todo
;
1373 int ntofill
, ntoempty
, ntodo
, dtodo
, dset
;
1374 struct perturbations
*ret
;
1377 if (!mask
&& !ctx
->allow_big_perturbs
)
1381 * Make a list of all the squares in the grid which we can
1382 * possibly use. This list should be in preference order, which
1385 * - first, unknown squares on the boundary of known space
1386 * - next, unknown squares beyond that boundary
1387 * - as a very last resort, known squares, but not within one
1388 * square of the starting position.
1390 * Each of these sections needs to be shuffled independently.
1391 * We do this by preparing list of all squares and then sorting
1392 * it with a random secondary key.
1394 sqlist
= snewn(ctx
->w
* ctx
->h
, struct square
);
1396 for (y
= 0; y
< ctx
->h
; y
++)
1397 for (x
= 0; x
< ctx
->w
; x
++) {
1399 * If this square is too near the starting position,
1400 * don't put it on the list at all.
1402 if (abs(y
- ctx
->sy
) <= 1 && abs(x
- ctx
->sx
) <= 1)
1406 * If this square is in the input set, also don't put
1409 if ((mask
== 0 && grid
[y
*ctx
->w
+x
] == -2) ||
1410 (x
>= setx
&& x
< setx
+ 3 &&
1411 y
>= sety
&& y
< sety
+ 3 &&
1412 mask
& (1 << ((y
-sety
)*3+(x
-setx
)))))
1418 if (grid
[y
*ctx
->w
+x
] != -2) {
1419 sqlist
[n
].type
= 3; /* known square */
1422 * Unknown square. Examine everything around it and
1423 * see if it borders on any known squares. If it
1424 * does, it's class 1, otherwise it's 2.
1429 for (dy
= -1; dy
<= +1; dy
++)
1430 for (dx
= -1; dx
<= +1; dx
++)
1431 if (x
+dx
>= 0 && x
+dx
< ctx
->w
&&
1432 y
+dy
>= 0 && y
+dy
< ctx
->h
&&
1433 grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] != -2) {
1440 * Finally, a random number to cause qsort to
1441 * shuffle within each group.
1443 sqlist
[n
].random
= random_bits(ctx
->rs
, 31);
1448 qsort(sqlist
, n
, sizeof(struct square
), squarecmp
);
1451 * Now count up the number of full and empty squares in the set
1452 * we've been provided.
1456 for (dy
= 0; dy
< 3; dy
++)
1457 for (dx
= 0; dx
< 3; dx
++)
1458 if (mask
& (1 << (dy
*3+dx
))) {
1459 assert(setx
+dx
<= ctx
->w
);
1460 assert(sety
+dy
<= ctx
->h
);
1461 if (ctx
->grid
[(sety
+dy
)*ctx
->w
+(setx
+dx
)])
1467 for (y
= 0; y
< ctx
->h
; y
++)
1468 for (x
= 0; x
< ctx
->w
; x
++)
1469 if (grid
[y
*ctx
->w
+x
] == -2) {
1470 if (ctx
->grid
[y
*ctx
->w
+x
])
1478 * Now go through our sorted list until we find either `nfull'
1479 * empty squares, or `nempty' full squares; these will be
1480 * swapped with the appropriate squares in the set to either
1481 * fill or empty the set while keeping the same number of mines
1484 ntofill
= ntoempty
= 0;
1486 tofill
= snewn(9, struct square
*);
1487 toempty
= snewn(9, struct square
*);
1489 tofill
= snewn(ctx
->w
* ctx
->h
, struct square
*);
1490 toempty
= snewn(ctx
->w
* ctx
->h
, struct square
*);
1492 for (i
= 0; i
< n
; i
++) {
1493 struct square
*sq
= &sqlist
[i
];
1494 if (ctx
->grid
[sq
->y
* ctx
->w
+ sq
->x
])
1495 toempty
[ntoempty
++] = sq
;
1497 tofill
[ntofill
++] = sq
;
1498 if (ntofill
== nfull
|| ntoempty
== nempty
)
1503 * If we haven't found enough empty squares outside the set to
1504 * empty it into _or_ enough full squares outside it to fill it
1505 * up with, we'll have to settle for doing only a partial job.
1506 * In this case we choose to always _fill_ the set (because
1507 * this case will tend to crop up when we're working with very
1508 * high mine densities and the only way to get a solvable grid
1509 * is going to be to pack most of the mines solidly around the
1510 * edges). So now our job is to make a list of the empty
1511 * squares in the set, and shuffle that list so that we fill a
1512 * random selection of them.
1514 if (ntofill
!= nfull
&& ntoempty
!= nempty
) {
1517 assert(ntoempty
!= 0);
1519 setlist
= snewn(ctx
->w
* ctx
->h
, int);
1522 for (dy
= 0; dy
< 3; dy
++)
1523 for (dx
= 0; dx
< 3; dx
++)
1524 if (mask
& (1 << (dy
*3+dx
))) {
1525 assert(setx
+dx
<= ctx
->w
);
1526 assert(sety
+dy
<= ctx
->h
);
1527 if (!ctx
->grid
[(sety
+dy
)*ctx
->w
+(setx
+dx
)])
1528 setlist
[i
++] = (sety
+dy
)*ctx
->w
+(setx
+dx
);
1531 for (y
= 0; y
< ctx
->h
; y
++)
1532 for (x
= 0; x
< ctx
->w
; x
++)
1533 if (grid
[y
*ctx
->w
+x
] == -2) {
1534 if (!ctx
->grid
[y
*ctx
->w
+x
])
1535 setlist
[i
++] = y
*ctx
->w
+x
;
1538 assert(i
> ntoempty
);
1540 * Now pick `ntoempty' items at random from the list.
1542 for (k
= 0; k
< ntoempty
; k
++) {
1543 int index
= k
+ random_upto(ctx
->rs
, i
- k
);
1547 setlist
[k
] = setlist
[index
];
1548 setlist
[index
] = tmp
;
1554 * Now we're pretty much there. We need to either
1555 * (a) put a mine in each of the empty squares in the set, and
1556 * take one out of each square in `toempty'
1557 * (b) take a mine out of each of the full squares in the set,
1558 * and put one in each square in `tofill'
1559 * depending on which one we've found enough squares to do.
1561 * So we start by constructing our list of changes to return to
1562 * the solver, so that it can update its data structures
1563 * efficiently rather than having to rescan the whole grid.
1565 ret
= snew(struct perturbations
);
1566 if (ntofill
== nfull
) {
1574 * (We also fall into this case if we've constructed a
1584 ret
->changes
= snewn(ret
->n
, struct perturbation
);
1585 for (i
= 0; i
< ntodo
; i
++) {
1586 ret
->changes
[i
].x
= todo
[i
]->x
;
1587 ret
->changes
[i
].y
= todo
[i
]->y
;
1588 ret
->changes
[i
].delta
= dtodo
;
1590 /* now i == ntodo */
1593 assert(todo
== toempty
);
1594 for (j
= 0; j
< ntoempty
; j
++) {
1595 ret
->changes
[i
].x
= setlist
[j
] % ctx
->w
;
1596 ret
->changes
[i
].y
= setlist
[j
] / ctx
->w
;
1597 ret
->changes
[i
].delta
= dset
;
1602 for (dy
= 0; dy
< 3; dy
++)
1603 for (dx
= 0; dx
< 3; dx
++)
1604 if (mask
& (1 << (dy
*3+dx
))) {
1605 int currval
= (ctx
->grid
[(sety
+dy
)*ctx
->w
+(setx
+dx
)] ?
+1 : -1);
1606 if (dset
== -currval
) {
1607 ret
->changes
[i
].x
= setx
+ dx
;
1608 ret
->changes
[i
].y
= sety
+ dy
;
1609 ret
->changes
[i
].delta
= dset
;
1614 for (y
= 0; y
< ctx
->h
; y
++)
1615 for (x
= 0; x
< ctx
->w
; x
++)
1616 if (grid
[y
*ctx
->w
+x
] == -2) {
1617 int currval
= (ctx
->grid
[y
*ctx
->w
+x
] ?
+1 : -1);
1618 if (dset
== -currval
) {
1619 ret
->changes
[i
].x
= x
;
1620 ret
->changes
[i
].y
= y
;
1621 ret
->changes
[i
].delta
= dset
;
1626 assert(i
== ret
->n
);
1632 * Having set up the precise list of changes we're going to
1633 * make, we now simply make them and return.
1635 for (i
= 0; i
< ret
->n
; i
++) {
1638 x
= ret
->changes
[i
].x
;
1639 y
= ret
->changes
[i
].y
;
1640 delta
= ret
->changes
[i
].delta
;
1643 * Check we're not trying to add an existing mine or remove
1646 assert((delta
< 0) ^ (ctx
->grid
[y
*ctx
->w
+x
] == 0));
1649 * Actually make the change.
1651 ctx
->grid
[y
*ctx
->w
+x
] = (delta
> 0);
1654 * Update any numbers already present in the grid.
1656 for (dy
= -1; dy
<= +1; dy
++)
1657 for (dx
= -1; dx
<= +1; dx
++)
1658 if (x
+dx
>= 0 && x
+dx
< ctx
->w
&&
1659 y
+dy
>= 0 && y
+dy
< ctx
->h
&&
1660 grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] != -2) {
1661 if (dx
== 0 && dy
== 0) {
1663 * The square itself is marked as known in
1664 * the grid. Mark it as a mine if it's a
1665 * mine, or else work out its number.
1668 grid
[y
*ctx
->w
+x
] = -1;
1670 int dx2
, dy2
, minecount
= 0;
1671 for (dy2
= -1; dy2
<= +1; dy2
++)
1672 for (dx2
= -1; dx2
<= +1; dx2
++)
1673 if (x
+dx2
>= 0 && x
+dx2
< ctx
->w
&&
1674 y
+dy2
>= 0 && y
+dy2
< ctx
->h
&&
1675 ctx
->grid
[(y
+dy2
)*ctx
->w
+(x
+dx2
)])
1677 grid
[y
*ctx
->w
+x
] = minecount
;
1680 if (grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] >= 0)
1681 grid
[(y
+dy
)*ctx
->w
+(x
+dx
)] += delta
;
1686 #ifdef GENERATION_DIAGNOSTICS
1689 printf("grid after perturbing:\n");
1690 for (yy
= 0; yy
< ctx
->h
; yy
++) {
1691 for (xx
= 0; xx
< ctx
->w
; xx
++) {
1692 int v
= ctx
->grid
[yy
*ctx
->w
+xx
];
1693 if (yy
== ctx
->sy
&& xx
== ctx
->sx
) {
1711 static char *minegen(int w
, int h
, int n
, int x
, int y
, int unique
,
1714 char *ret
= snewn(w
*h
, char);
1722 memset(ret
, 0, w
*h
);
1725 * Start by placing n mines, none of which is at x,y or within
1729 int *tmp
= snewn(w
*h
, int);
1733 * Write down the list of possible mine locations.
1736 for (i
= 0; i
< h
; i
++)
1737 for (j
= 0; j
< w
; j
++)
1738 if (abs(i
- y
) > 1 || abs(j
- x
) > 1)
1742 * Now pick n off the list at random.
1746 i
= random_upto(rs
, k
);
1754 #ifdef GENERATION_DIAGNOSTICS
1757 printf("grid after initial generation:\n");
1758 for (yy
= 0; yy
< h
; yy
++) {
1759 for (xx
= 0; xx
< w
; xx
++) {
1760 int v
= ret
[yy
*w
+xx
];
1761 if (yy
== y
&& xx
== x
) {
1777 * Now set up a results grid to run the solver in, and a
1778 * context for the solver to open squares. Then run the solver
1779 * repeatedly; if the number of perturb steps ever goes up or
1780 * it ever returns -1, give up completely.
1782 * We bypass this bit if we're not after a unique grid.
1785 signed char *solvegrid
= snewn(w
*h
, char);
1786 struct minectx actx
, *ctx
= &actx
;
1787 int solveret
, prevret
= -2;
1795 ctx
->allow_big_perturbs
= (ntries
> 100);
1798 memset(solvegrid
, -2, w
*h
);
1799 solvegrid
[y
*w
+x
] = mineopen(ctx
, x
, y
);
1800 assert(solvegrid
[y
*w
+x
] == 0); /* by deliberate arrangement */
1803 minesolve(w
, h
, n
, solvegrid
, mineopen
, mineperturb
, ctx
, rs
);
1804 if (solveret
< 0 || (prevret
>= 0 && solveret
>= prevret
)) {
1807 } else if (solveret
== 0) {
1824 * The Mines game descriptions contain the location of every mine,
1825 * and can therefore be used to cheat.
1827 * It would be pointless to attempt to _prevent_ this form of
1828 * cheating by encrypting the description, since Mines is
1829 * open-source so anyone can find out the encryption key. However,
1830 * I think it is worth doing a bit of gentle obfuscation to prevent
1831 * _accidental_ spoilers: if you happened to note that the game ID
1832 * starts with an F, for example, you might be unable to put the
1833 * knowledge of those mines out of your mind while playing. So,
1834 * just as discussions of film endings are rot13ed to avoid
1835 * spoiling it for people who don't want to be told, we apply a
1836 * keyless, reversible, but visually completely obfuscatory masking
1837 * function to the mine bitmap.
1839 static void obfuscate_bitmap(unsigned char *bmp
, int bits
, int decode
)
1841 int bytes
, firsthalf
, secondhalf
;
1843 unsigned char *seedstart
;
1845 unsigned char *targetstart
;
1851 * My obfuscation algorithm is similar in concept to the OAEP
1852 * encoding used in some forms of RSA. Here's a specification
1855 * + We have a `masking function' which constructs a stream of
1856 * pseudorandom bytes from a seed of some number of input
1859 * + We pad out our input bit stream to a whole number of
1860 * bytes by adding up to 7 zero bits on the end. (In fact
1861 * the bitmap passed as input to this function will already
1862 * have had this done in practice.)
1864 * + We divide the _byte_ stream exactly in half, rounding the
1865 * half-way position _down_. So an 81-bit input string, for
1866 * example, rounds up to 88 bits or 11 bytes, and then
1867 * dividing by two gives 5 bytes in the first half and 6 in
1870 * + We generate a mask from the second half of the bytes, and
1871 * XOR it over the first half.
1873 * + We generate a mask from the (encoded) first half of the
1874 * bytes, and XOR it over the second half. Any null bits at
1875 * the end which were added as padding are cleared back to
1876 * zero even if this operation would have made them nonzero.
1878 * To de-obfuscate, the steps are precisely the same except
1879 * that the final two are reversed.
1881 * Finally, our masking function. Given an input seed string of
1882 * bytes, the output mask consists of concatenating the SHA-1
1883 * hashes of the seed string and successive decimal integers,
1887 bytes
= (bits
+ 7) / 8;
1888 firsthalf
= bytes
/ 2;
1889 secondhalf
= bytes
- firsthalf
;
1891 steps
[decode ?
1 : 0].seedstart
= bmp
+ firsthalf
;
1892 steps
[decode ?
1 : 0].seedlen
= secondhalf
;
1893 steps
[decode ?
1 : 0].targetstart
= bmp
;
1894 steps
[decode ?
1 : 0].targetlen
= firsthalf
;
1896 steps
[decode ?
0 : 1].seedstart
= bmp
;
1897 steps
[decode ?
0 : 1].seedlen
= firsthalf
;
1898 steps
[decode ?
0 : 1].targetstart
= bmp
+ firsthalf
;
1899 steps
[decode ?
0 : 1].targetlen
= secondhalf
;
1901 for (i
= 0; i
< 2; i
++) {
1902 SHA_State base
, final
;
1903 unsigned char digest
[20];
1905 int digestpos
= 20, counter
= 0;
1908 SHA_Bytes(&base
, steps
[i
].seedstart
, steps
[i
].seedlen
);
1910 for (j
= 0; j
< steps
[i
].targetlen
; j
++) {
1911 if (digestpos
>= 20) {
1912 sprintf(numberbuf
, "%d", counter
++);
1914 SHA_Bytes(&final
, numberbuf
, strlen(numberbuf
));
1915 SHA_Final(&final
, digest
);
1918 steps
[i
].targetstart
[j
] ^= digest
[digestpos
++];
1922 * Mask off the pad bits in the final byte after both steps.
1925 bmp
[bits
/ 8] &= 0xFF & (0xFF00 >> (bits
% 8));
1929 static char *new_mine_layout(int w
, int h
, int n
, int x
, int y
, int unique
,
1930 random_state
*rs
, char **game_desc
)
1932 signed char *grid
, *ret
, *p
;
1936 #ifdef TEST_OBFUSCATION
1937 static int tested_obfuscation
= FALSE
;
1938 if (!tested_obfuscation
) {
1940 * A few simple test vectors for the obfuscator.
1942 * First test: the 28-bit stream 1234567. This divides up
1943 * into 1234 and 567[0]. The SHA of 56 70 30 (appending
1944 * "0") is 15ce8ab946640340bbb99f3f48fd2c45d1a31d30. Thus,
1945 * we XOR the 16-bit string 15CE into the input 1234 to get
1946 * 07FA. Next, we SHA that with "0": the SHA of 07 FA 30 is
1947 * 3370135c5e3da4fed937adc004a79533962b6391. So we XOR the
1948 * 12-bit string 337 into the input 567 to get 650. Thus
1949 * our output is 07FA650.
1952 unsigned char bmp1
[] = "\x12\x34\x56\x70";
1953 obfuscate_bitmap(bmp1
, 28, FALSE
);
1954 printf("test 1 encode: %s\n",
1955 memcmp(bmp1
, "\x07\xfa\x65\x00", 4) ?
"failed" : "passed");
1956 obfuscate_bitmap(bmp1
, 28, TRUE
);
1957 printf("test 1 decode: %s\n",
1958 memcmp(bmp1
, "\x12\x34\x56\x70", 4) ?
"failed" : "passed");
1961 * Second test: a long string to make sure we switch from
1962 * one SHA to the next correctly. My input string this time
1963 * is simply fifty bytes of zeroes.
1966 unsigned char bmp2
[50];
1967 unsigned char bmp2a
[50];
1968 memset(bmp2
, 0, 50);
1969 memset(bmp2a
, 0, 50);
1970 obfuscate_bitmap(bmp2
, 50 * 8, FALSE
);
1972 * SHA of twenty-five zero bytes plus "0" is
1973 * b202c07b990c01f6ff2d544707f60e506019b671. SHA of
1974 * twenty-five zero bytes plus "1" is
1975 * fcb1d8b5a2f6b592fe6780b36aa9d65dd7aa6db9. Thus our
1976 * first half becomes
1977 * b202c07b990c01f6ff2d544707f60e506019b671fcb1d8b5a2.
1979 * SHA of that lot plus "0" is
1980 * 10b0af913db85d37ca27f52a9f78bba3a80030db. SHA of the
1981 * same string plus "1" is
1982 * 3d01d8df78e76d382b8106f480135a1bc751d725. So the
1983 * second half becomes
1984 * 10b0af913db85d37ca27f52a9f78bba3a80030db3d01d8df78.
1986 printf("test 2 encode: %s\n",
1987 memcmp(bmp2
, "\xb2\x02\xc0\x7b\x99\x0c\x01\xf6\xff\x2d\x54"
1988 "\x47\x07\xf6\x0e\x50\x60\x19\xb6\x71\xfc\xb1\xd8"
1989 "\xb5\xa2\x10\xb0\xaf\x91\x3d\xb8\x5d\x37\xca\x27"
1990 "\xf5\x2a\x9f\x78\xbb\xa3\xa8\x00\x30\xdb\x3d\x01"
1991 "\xd8\xdf\x78", 50) ?
"failed" : "passed");
1992 obfuscate_bitmap(bmp2
, 50 * 8, TRUE
);
1993 printf("test 2 decode: %s\n",
1994 memcmp(bmp2
, bmp2a
, 50) ?
"failed" : "passed");
1999 grid
= minegen(w
, h
, n
, x
, y
, unique
, rs
);
2003 * Set up the mine bitmap and obfuscate it.
2006 bmp
= snewn((area
+ 7) / 8, unsigned char);
2007 memset(bmp
, 0, (area
+ 7) / 8);
2008 for (i
= 0; i
< area
; i
++) {
2010 bmp
[i
/ 8] |= 0x80 >> (i
% 8);
2012 obfuscate_bitmap(bmp
, area
, FALSE
);
2015 * Now encode the resulting bitmap in hex. We can work to
2016 * nibble rather than byte granularity, since the obfuscation
2017 * function guarantees to return a bit string of the same
2018 * length as its input.
2020 ret
= snewn((area
+3)/4 + 100, char);
2021 p
= ret
+ sprintf(ret
, "%d,%d,m", x
, y
); /* 'm' == masked */
2022 for (i
= 0; i
< (area
+3)/4; i
++) {
2026 *p
++ = "0123456789abcdef"[v
& 0xF];
2038 static char *new_game_desc(game_params
*params
, random_state
*rs
,
2039 game_aux_info
**aux
, int interactive
)
2043 * For batch-generated grids, pre-open one square.
2045 int x
= random_upto(rs
, params
->w
);
2046 int y
= random_upto(rs
, params
->h
);
2050 grid
= new_mine_layout(params
->w
, params
->h
, params
->n
,
2051 x
, y
, params
->unique
, rs
, &desc
);
2055 char *rsdesc
, *desc
;
2057 rsdesc
= random_state_encode(rs
);
2058 desc
= snewn(strlen(rsdesc
) + 100, char);
2059 sprintf(desc
, "r%d,%c,%s", params
->n
, params
->unique ?
'u' : 'a', rsdesc
);
2065 static void game_free_aux_info(game_aux_info
*aux
)
2067 assert(!"Shouldn't happen");
2070 static char *validate_desc(game_params
*params
, char *desc
)
2072 int wh
= params
->w
* params
->h
;
2076 if (!*desc
|| !isdigit((unsigned char)*desc
))
2077 return "No initial mine count in game description";
2078 while (*desc
&& isdigit((unsigned char)*desc
))
2079 desc
++; /* skip over mine count */
2081 return "No ',' after initial x-coordinate in game description";
2083 if (*desc
!= 'u' && *desc
!= 'a')
2084 return "No uniqueness specifier in game description";
2087 return "No ',' after uniqueness specifier in game description";
2088 /* now ignore the rest */
2090 if (!*desc
|| !isdigit((unsigned char)*desc
))
2091 return "No initial x-coordinate in game description";
2093 if (x
< 0 || x
>= params
->w
)
2094 return "Initial x-coordinate was out of range";
2095 while (*desc
&& isdigit((unsigned char)*desc
))
2096 desc
++; /* skip over x coordinate */
2098 return "No ',' after initial x-coordinate in game description";
2099 desc
++; /* eat comma */
2100 if (!*desc
|| !isdigit((unsigned char)*desc
))
2101 return "No initial y-coordinate in game description";
2103 if (y
< 0 || y
>= params
->h
)
2104 return "Initial y-coordinate was out of range";
2105 while (*desc
&& isdigit((unsigned char)*desc
))
2106 desc
++; /* skip over y coordinate */
2108 return "No ',' after initial y-coordinate in game description";
2109 desc
++; /* eat comma */
2110 /* eat `m', meaning `masked', if present */
2113 /* now just check length of remainder */
2114 if (strlen(desc
) != (wh
+3)/4)
2115 return "Game description is wrong length";
2121 static int open_square(game_state
*state
, int x
, int y
)
2123 int w
= state
->w
, h
= state
->h
;
2124 int xx
, yy
, nmines
, ncovered
;
2126 if (!state
->layout
->mines
) {
2128 * We have a preliminary game in which the mine layout
2129 * hasn't been generated yet. Generate it based on the
2130 * initial click location.
2133 state
->layout
->mines
= new_mine_layout(w
, h
, state
->layout
->n
,
2134 x
, y
, state
->layout
->unique
,
2137 midend_supersede_game_desc(state
->layout
->me
, desc
);
2139 random_free(state
->layout
->rs
);
2140 state
->layout
->rs
= NULL
;
2143 if (state
->layout
->mines
[y
*w
+x
]) {
2145 * The player has landed on a mine. Bad luck. Expose all
2149 for (yy
= 0; yy
< h
; yy
++)
2150 for (xx
= 0; xx
< w
; xx
++) {
2151 if (state
->layout
->mines
[yy
*w
+xx
] &&
2152 (state
->grid
[yy
*w
+xx
] == -2 ||
2153 state
->grid
[yy
*w
+xx
] == -3)) {
2154 state
->grid
[yy
*w
+xx
] = 64;
2156 if (!state
->layout
->mines
[yy
*w
+xx
] &&
2157 state
->grid
[yy
*w
+xx
] == -1) {
2158 state
->grid
[yy
*w
+xx
] = 66;
2161 state
->grid
[y
*w
+x
] = 65;
2166 * Otherwise, the player has opened a safe square. Mark it to-do.
2168 state
->grid
[y
*w
+x
] = -10; /* `todo' value internal to this func */
2171 * Now go through the grid finding all `todo' values and
2172 * opening them. Every time one of them turns out to have no
2173 * neighbouring mines, we add all its unopened neighbours to
2176 * FIXME: We really ought to be able to do this better than
2177 * using repeated N^2 scans of the grid.
2180 int done_something
= FALSE
;
2182 for (yy
= 0; yy
< h
; yy
++)
2183 for (xx
= 0; xx
< w
; xx
++)
2184 if (state
->grid
[yy
*w
+xx
] == -10) {
2187 assert(!state
->layout
->mines
[yy
*w
+xx
]);
2191 for (dx
= -1; dx
<= +1; dx
++)
2192 for (dy
= -1; dy
<= +1; dy
++)
2193 if (xx
+dx
>= 0 && xx
+dx
< state
->w
&&
2194 yy
+dy
>= 0 && yy
+dy
< state
->h
&&
2195 state
->layout
->mines
[(yy
+dy
)*w
+(xx
+dx
)])
2198 state
->grid
[yy
*w
+xx
] = v
;
2201 for (dx
= -1; dx
<= +1; dx
++)
2202 for (dy
= -1; dy
<= +1; dy
++)
2203 if (xx
+dx
>= 0 && xx
+dx
< state
->w
&&
2204 yy
+dy
>= 0 && yy
+dy
< state
->h
&&
2205 state
->grid
[(yy
+dy
)*w
+(xx
+dx
)] == -2)
2206 state
->grid
[(yy
+dy
)*w
+(xx
+dx
)] = -10;
2209 done_something
= TRUE
;
2212 if (!done_something
)
2217 * Finally, scan the grid and see if exactly as many squares
2218 * are still covered as there are mines. If so, set the `won'
2219 * flag and fill in mine markers on all covered squares.
2221 nmines
= ncovered
= 0;
2222 for (yy
= 0; yy
< h
; yy
++)
2223 for (xx
= 0; xx
< w
; xx
++) {
2224 if (state
->grid
[yy
*w
+xx
] < 0)
2226 if (state
->layout
->mines
[yy
*w
+xx
])
2229 assert(ncovered
>= nmines
);
2230 if (ncovered
== nmines
) {
2231 for (yy
= 0; yy
< h
; yy
++)
2232 for (xx
= 0; xx
< w
; xx
++) {
2233 if (state
->grid
[yy
*w
+xx
] < 0)
2234 state
->grid
[yy
*w
+xx
] = -1;
2242 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
2244 game_state
*state
= snew(game_state
);
2245 int i
, wh
, x
, y
, ret
, masked
;
2248 state
->w
= params
->w
;
2249 state
->h
= params
->h
;
2250 state
->n
= params
->n
;
2251 state
->dead
= state
->won
= FALSE
;
2252 state
->used_solve
= state
->just_used_solve
= FALSE
;
2254 wh
= state
->w
* state
->h
;
2256 state
->layout
= snew(struct mine_layout
);
2257 state
->layout
->refcount
= 1;
2259 state
->grid
= snewn(wh
, char);
2260 memset(state
->grid
, -2, wh
);
2264 state
->layout
->n
= atoi(desc
);
2265 while (*desc
&& isdigit((unsigned char)*desc
))
2266 desc
++; /* skip over mine count */
2267 if (*desc
) desc
++; /* eat comma */
2269 state
->layout
->unique
= FALSE
;
2271 state
->layout
->unique
= TRUE
;
2273 if (*desc
) desc
++; /* eat comma */
2275 state
->layout
->mines
= NULL
;
2276 state
->layout
->rs
= random_state_decode(desc
);
2277 state
->layout
->me
= me
;
2280 state
->layout
->rs
= NULL
;
2281 state
->layout
->me
= NULL
;
2283 state
->layout
->mines
= snewn(wh
, char);
2285 while (*desc
&& isdigit((unsigned char)*desc
))
2286 desc
++; /* skip over x coordinate */
2287 if (*desc
) desc
++; /* eat comma */
2289 while (*desc
&& isdigit((unsigned char)*desc
))
2290 desc
++; /* skip over y coordinate */
2291 if (*desc
) desc
++; /* eat comma */
2298 * We permit game IDs to be entered by hand without the
2299 * masking transformation.
2304 bmp
= snewn((wh
+ 7) / 8, unsigned char);
2305 memset(bmp
, 0, (wh
+ 7) / 8);
2306 for (i
= 0; i
< (wh
+3)/4; i
++) {
2310 assert(c
!= 0); /* validate_desc should have caught */
2311 if (c
>= '0' && c
<= '9')
2313 else if (c
>= 'a' && c
<= 'f')
2315 else if (c
>= 'A' && c
<= 'F')
2320 bmp
[i
/ 2] |= v
<< (4 * (1 - (i
% 2)));
2324 obfuscate_bitmap(bmp
, wh
, TRUE
);
2326 memset(state
->layout
->mines
, 0, wh
);
2327 for (i
= 0; i
< wh
; i
++) {
2328 if (bmp
[i
/ 8] & (0x80 >> (i
% 8)))
2329 state
->layout
->mines
[i
] = 1;
2332 ret
= open_square(state
, x
, y
);
2338 static game_state
*dup_game(game_state
*state
)
2340 game_state
*ret
= snew(game_state
);
2345 ret
->dead
= state
->dead
;
2346 ret
->won
= state
->won
;
2347 ret
->used_solve
= state
->used_solve
;
2348 ret
->just_used_solve
= state
->just_used_solve
;
2349 ret
->layout
= state
->layout
;
2350 ret
->layout
->refcount
++;
2351 ret
->grid
= snewn(ret
->w
* ret
->h
, char);
2352 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
);
2357 static void free_game(game_state
*state
)
2359 if (--state
->layout
->refcount
<= 0) {
2360 sfree(state
->layout
->mines
);
2361 if (state
->layout
->rs
)
2362 random_free(state
->layout
->rs
);
2363 sfree(state
->layout
);
2369 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
2373 * Simply expose the entire grid as if it were a completed
2379 if (!state
->layout
->mines
) {
2380 *error
= "Game has not been started yet";
2384 ret
= dup_game(state
);
2385 for (yy
= 0; yy
< ret
->h
; yy
++)
2386 for (xx
= 0; xx
< ret
->w
; xx
++) {
2388 if (ret
->layout
->mines
[yy
*ret
->w
+xx
]) {
2389 ret
->grid
[yy
*ret
->w
+xx
] = -1;
2395 for (dx
= -1; dx
<= +1; dx
++)
2396 for (dy
= -1; dy
<= +1; dy
++)
2397 if (xx
+dx
>= 0 && xx
+dx
< ret
->w
&&
2398 yy
+dy
>= 0 && yy
+dy
< ret
->h
&&
2399 ret
->layout
->mines
[(yy
+dy
)*ret
->w
+(xx
+dx
)])
2402 ret
->grid
[yy
*ret
->w
+xx
] = v
;
2405 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
2411 static char *game_text_format(game_state
*state
)
2416 ret
= snewn((state
->w
+ 1) * state
->h
+ 1, char);
2417 for (y
= 0; y
< state
->h
; y
++) {
2418 for (x
= 0; x
< state
->w
; x
++) {
2419 int v
= state
->grid
[y
*state
->w
+x
];
2422 else if (v
>= 1 && v
<= 8)
2426 else if (v
== -2 || v
== -3)
2430 ret
[y
* (state
->w
+1) + x
] = v
;
2432 ret
[y
* (state
->w
+1) + state
->w
] = '\n';
2434 ret
[(state
->w
+ 1) * state
->h
] = '\0';
2440 int hx
, hy
, hradius
; /* for mouse-down highlights */
2444 static game_ui
*new_ui(game_state
*state
)
2446 game_ui
*ui
= snew(game_ui
);
2447 ui
->hx
= ui
->hy
= -1;
2449 ui
->flash_is_death
= FALSE
; /* *shrug* */
2453 static void free_ui(game_ui
*ui
)
2458 static game_state
*make_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
2459 int x
, int y
, int button
)
2464 if (from
->dead
|| from
->won
)
2465 return NULL
; /* no further moves permitted */
2467 if (!IS_MOUSE_DOWN(button
) && !IS_MOUSE_DRAG(button
) &&
2468 !IS_MOUSE_RELEASE(button
))
2473 if (cx
< 0 || cx
>= from
->w
|| cy
< 0 || cy
> from
->h
)
2476 if (button
== LEFT_BUTTON
|| button
== LEFT_DRAG
) {
2478 * Mouse-downs and mouse-drags just cause highlighting
2483 ui
->hradius
= (from
->grid
[cy
*from
->w
+cx
] >= 0 ?
1 : 0);
2487 if (button
== RIGHT_BUTTON
) {
2489 * Right-clicking only works on a covered square, and it
2490 * toggles between -1 (marked as mine) and -2 (not marked
2493 * FIXME: question marks.
2495 if (from
->grid
[cy
* from
->w
+ cx
] != -2 &&
2496 from
->grid
[cy
* from
->w
+ cx
] != -1)
2499 ret
= dup_game(from
);
2500 ret
->just_used_solve
= FALSE
;
2501 ret
->grid
[cy
* from
->w
+ cx
] ^= (-2 ^ -1);
2506 if (button
== LEFT_RELEASE
) {
2507 ui
->hx
= ui
->hy
= -1;
2511 * At this stage we must never return NULL: we have adjusted
2512 * the ui, so at worst we return `from'.
2516 * Left-clicking on a covered square opens a tile. Not
2517 * permitted if the tile is marked as a mine, for safety.
2518 * (Unmark it and _then_ open it.)
2520 if (from
->grid
[cy
* from
->w
+ cx
] == -2 ||
2521 from
->grid
[cy
* from
->w
+ cx
] == -3) {
2522 ret
= dup_game(from
);
2523 ret
->just_used_solve
= FALSE
;
2524 open_square(ret
, cx
, cy
);
2529 * Left-clicking on an uncovered tile: first we check to see if
2530 * the number of mine markers surrounding the tile is equal to
2531 * its mine count, and if so then we open all other surrounding
2534 if (from
->grid
[cy
* from
->w
+ cx
] > 0) {
2537 /* Count mine markers. */
2539 for (dy
= -1; dy
<= +1; dy
++)
2540 for (dx
= -1; dx
<= +1; dx
++)
2541 if (cx
+dx
>= 0 && cx
+dx
< from
->w
&&
2542 cy
+dy
>= 0 && cy
+dy
< from
->h
) {
2543 if (from
->grid
[(cy
+dy
)*from
->w
+(cx
+dx
)] == -1)
2547 if (n
== from
->grid
[cy
* from
->w
+ cx
]) {
2548 ret
= dup_game(from
);
2549 ret
->just_used_solve
= FALSE
;
2550 for (dy
= -1; dy
<= +1; dy
++)
2551 for (dx
= -1; dx
<= +1; dx
++)
2552 if (cx
+dx
>= 0 && cx
+dx
< ret
->w
&&
2553 cy
+dy
>= 0 && cy
+dy
< ret
->h
&&
2554 (ret
->grid
[(cy
+dy
)*ret
->w
+(cx
+dx
)] == -2 ||
2555 ret
->grid
[(cy
+dy
)*ret
->w
+(cx
+dx
)] == -3))
2556 open_square(ret
, cx
+dx
, cy
+dy
);
2567 /* ----------------------------------------------------------------------
2571 struct game_drawstate
{
2575 * Items in this `grid' array have all the same values as in
2576 * the game_state grid, and in addition:
2578 * - -10 means the tile was drawn `specially' as a result of a
2579 * flash, so it will always need redrawing.
2581 * - -22 and -23 mean the tile is highlighted for a possible
2586 static void game_size(game_params
*params
, int *x
, int *y
)
2588 *x
= BORDER
* 2 + TILE_SIZE
* params
->w
;
2589 *y
= BORDER
* 2 + TILE_SIZE
* params
->h
;
2592 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2594 float *ret
= snewn(3 * NCOLOURS
, float);
2596 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2598 ret
[COL_BACKGROUND2
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 19.0 / 20.0;
2599 ret
[COL_BACKGROUND2
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 19.0 / 20.0;
2600 ret
[COL_BACKGROUND2
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 19.0 / 20.0;
2602 ret
[COL_1
* 3 + 0] = 0.0F
;
2603 ret
[COL_1
* 3 + 1] = 0.0F
;
2604 ret
[COL_1
* 3 + 2] = 1.0F
;
2606 ret
[COL_2
* 3 + 0] = 0.0F
;
2607 ret
[COL_2
* 3 + 1] = 0.5F
;
2608 ret
[COL_2
* 3 + 2] = 0.0F
;
2610 ret
[COL_3
* 3 + 0] = 1.0F
;
2611 ret
[COL_3
* 3 + 1] = 0.0F
;
2612 ret
[COL_3
* 3 + 2] = 0.0F
;
2614 ret
[COL_4
* 3 + 0] = 0.0F
;
2615 ret
[COL_4
* 3 + 1] = 0.0F
;
2616 ret
[COL_4
* 3 + 2] = 0.5F
;
2618 ret
[COL_5
* 3 + 0] = 0.5F
;
2619 ret
[COL_5
* 3 + 1] = 0.0F
;
2620 ret
[COL_5
* 3 + 2] = 0.0F
;
2622 ret
[COL_6
* 3 + 0] = 0.0F
;
2623 ret
[COL_6
* 3 + 1] = 0.5F
;
2624 ret
[COL_6
* 3 + 2] = 0.5F
;
2626 ret
[COL_7
* 3 + 0] = 0.0F
;
2627 ret
[COL_7
* 3 + 1] = 0.0F
;
2628 ret
[COL_7
* 3 + 2] = 0.0F
;
2630 ret
[COL_8
* 3 + 0] = 0.5F
;
2631 ret
[COL_8
* 3 + 1] = 0.5F
;
2632 ret
[COL_8
* 3 + 2] = 0.5F
;
2634 ret
[COL_MINE
* 3 + 0] = 0.0F
;
2635 ret
[COL_MINE
* 3 + 1] = 0.0F
;
2636 ret
[COL_MINE
* 3 + 2] = 0.0F
;
2638 ret
[COL_BANG
* 3 + 0] = 1.0F
;
2639 ret
[COL_BANG
* 3 + 1] = 0.0F
;
2640 ret
[COL_BANG
* 3 + 2] = 0.0F
;
2642 ret
[COL_CROSS
* 3 + 0] = 1.0F
;
2643 ret
[COL_CROSS
* 3 + 1] = 0.0F
;
2644 ret
[COL_CROSS
* 3 + 2] = 0.0F
;
2646 ret
[COL_FLAG
* 3 + 0] = 1.0F
;
2647 ret
[COL_FLAG
* 3 + 1] = 0.0F
;
2648 ret
[COL_FLAG
* 3 + 2] = 0.0F
;
2650 ret
[COL_FLAGBASE
* 3 + 0] = 0.0F
;
2651 ret
[COL_FLAGBASE
* 3 + 1] = 0.0F
;
2652 ret
[COL_FLAGBASE
* 3 + 2] = 0.0F
;
2654 ret
[COL_QUERY
* 3 + 0] = 0.0F
;
2655 ret
[COL_QUERY
* 3 + 1] = 0.0F
;
2656 ret
[COL_QUERY
* 3 + 2] = 0.0F
;
2658 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
2659 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
2660 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
2662 ret
[COL_LOWLIGHT
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 2.0 / 3.0;
2663 ret
[COL_LOWLIGHT
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 2.0 / 3.0;
2664 ret
[COL_LOWLIGHT
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 2.0 / 3.0;
2666 *ncolours
= NCOLOURS
;
2670 static game_drawstate
*game_new_drawstate(game_state
*state
)
2672 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2676 ds
->started
= FALSE
;
2677 ds
->grid
= snewn(ds
->w
* ds
->h
, char);
2679 memset(ds
->grid
, -99, ds
->w
* ds
->h
);
2684 static void game_free_drawstate(game_drawstate
*ds
)
2690 static void draw_tile(frontend
*fe
, int x
, int y
, int v
, int bg
)
2696 if (v
== -22 || v
== -23) {
2700 * Omit the highlights in this case.
2702 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
2703 bg
== COL_BACKGROUND ? COL_BACKGROUND2
: bg
);
2704 draw_line(fe
, x
, y
, x
+ TILE_SIZE
- 1, y
, COL_LOWLIGHT
);
2705 draw_line(fe
, x
, y
, x
, y
+ TILE_SIZE
- 1, COL_LOWLIGHT
);
2708 * Draw highlights to indicate the square is covered.
2710 coords
[0] = x
+ TILE_SIZE
- 1;
2711 coords
[1] = y
+ TILE_SIZE
- 1;
2712 coords
[2] = x
+ TILE_SIZE
- 1;
2715 coords
[5] = y
+ TILE_SIZE
- 1;
2716 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
^ hl
);
2717 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
^ hl
);
2721 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
^ hl
);
2722 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
^ hl
);
2724 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
2725 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
2733 #define SETCOORD(n, dx, dy) do { \
2734 coords[(n)*2+0] = x + TILE_SIZE * (dx); \
2735 coords[(n)*2+1] = y + TILE_SIZE * (dy); \
2737 SETCOORD(0, 0.6, 0.35);
2738 SETCOORD(1, 0.6, 0.7);
2739 SETCOORD(2, 0.8, 0.8);
2740 SETCOORD(3, 0.25, 0.8);
2741 SETCOORD(4, 0.55, 0.7);
2742 SETCOORD(5, 0.55, 0.35);
2743 draw_polygon(fe
, coords
, 6, TRUE
, COL_FLAGBASE
);
2744 draw_polygon(fe
, coords
, 6, FALSE
, COL_FLAGBASE
);
2746 SETCOORD(0, 0.6, 0.2);
2747 SETCOORD(1, 0.6, 0.5);
2748 SETCOORD(2, 0.2, 0.35);
2749 draw_polygon(fe
, coords
, 3, TRUE
, COL_FLAG
);
2750 draw_polygon(fe
, coords
, 3, FALSE
, COL_FLAG
);
2753 } else if (v
== -3) {
2755 * Draw a question mark.
2757 draw_text(fe
, x
+ TILE_SIZE
/ 2, y
+ TILE_SIZE
/ 2,
2758 FONT_VARIABLE
, TILE_SIZE
* 6 / 8,
2759 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2764 * Clear the square to the background colour, and draw thin
2765 * grid lines along the top and left.
2767 * Exception is that for value 65 (mine we've just trodden
2768 * on), we clear the square to COL_BANG.
2770 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
2771 (v
== 65 ? COL_BANG
:
2772 bg
== COL_BACKGROUND ? COL_BACKGROUND2
: bg
));
2773 draw_line(fe
, x
, y
, x
+ TILE_SIZE
- 1, y
, COL_LOWLIGHT
);
2774 draw_line(fe
, x
, y
, x
, y
+ TILE_SIZE
- 1, COL_LOWLIGHT
);
2776 if (v
> 0 && v
<= 8) {
2783 draw_text(fe
, x
+ TILE_SIZE
/ 2, y
+ TILE_SIZE
/ 2,
2784 FONT_VARIABLE
, TILE_SIZE
* 7 / 8,
2785 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2786 (COL_1
- 1) + v
, str
);
2788 } else if (v
>= 64) {
2792 * FIXME: this could be done better!
2795 draw_text(fe
, x
+ TILE_SIZE
/ 2, y
+ TILE_SIZE
/ 2,
2796 FONT_VARIABLE
, TILE_SIZE
* 7 / 8,
2797 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2801 int cx
= x
+ TILE_SIZE
/ 2;
2802 int cy
= y
+ TILE_SIZE
/ 2;
2803 int r
= TILE_SIZE
/ 2 - 3;
2805 int xdx
= 1, xdy
= 0, ydx
= 0, ydy
= 1;
2808 for (i
= 0; i
< 4*5*2; i
+= 5*2) {
2809 coords
[i
+2*0+0] = cx
- r
/6*xdx
+ r
*4/5*ydx
;
2810 coords
[i
+2*0+1] = cy
- r
/6*xdy
+ r
*4/5*ydy
;
2811 coords
[i
+2*1+0] = cx
- r
/6*xdx
+ r
*ydx
;
2812 coords
[i
+2*1+1] = cy
- r
/6*xdy
+ r
*ydy
;
2813 coords
[i
+2*2+0] = cx
+ r
/6*xdx
+ r
*ydx
;
2814 coords
[i
+2*2+1] = cy
+ r
/6*xdy
+ r
*ydy
;
2815 coords
[i
+2*3+0] = cx
+ r
/6*xdx
+ r
*4/5*ydx
;
2816 coords
[i
+2*3+1] = cy
+ r
/6*xdy
+ r
*4/5*ydy
;
2817 coords
[i
+2*4+0] = cx
+ r
*3/5*xdx
+ r
*3/5*ydx
;
2818 coords
[i
+2*4+1] = cy
+ r
*3/5*xdy
+ r
*3/5*ydy
;
2828 draw_polygon(fe
, coords
, 5*4, TRUE
, COL_MINE
);
2829 draw_polygon(fe
, coords
, 5*4, FALSE
, COL_MINE
);
2831 draw_rect(fe
, cx
-r
/3, cy
-r
/3, r
/3, r
/4, COL_HIGHLIGHT
);
2837 * Cross through the mine.
2840 for (dx
= -1; dx
<= +1; dx
++) {
2841 draw_line(fe
, x
+ 3 + dx
, y
+ 2,
2842 x
+ TILE_SIZE
- 3 + dx
,
2843 y
+ TILE_SIZE
- 2, COL_CROSS
);
2844 draw_line(fe
, x
+ TILE_SIZE
- 3 + dx
, y
+ 2,
2845 x
+ 3 + dx
, y
+ TILE_SIZE
- 2,
2852 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
2855 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
2856 game_state
*state
, int dir
, game_ui
*ui
,
2857 float animtime
, float flashtime
)
2860 int mines
, markers
, bg
;
2863 int frame
= (flashtime
/ FLASH_FRAME
);
2865 bg
= (ui
->flash_is_death ? COL_BACKGROUND
: COL_LOWLIGHT
);
2867 bg
= (ui
->flash_is_death ? COL_BANG
: COL_HIGHLIGHT
);
2869 bg
= COL_BACKGROUND
;
2875 TILE_SIZE
* state
->w
+ 2 * BORDER
,
2876 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
2877 draw_update(fe
, 0, 0,
2878 TILE_SIZE
* state
->w
+ 2 * BORDER
,
2879 TILE_SIZE
* state
->h
+ 2 * BORDER
);
2882 * Recessed area containing the whole puzzle.
2884 coords
[0] = COORD(state
->w
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2885 coords
[1] = COORD(state
->h
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2886 coords
[2] = COORD(state
->w
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2887 coords
[3] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2888 coords
[4] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2889 coords
[5] = COORD(state
->h
) + OUTER_HIGHLIGHT_WIDTH
- 1;
2890 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
2891 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
2893 coords
[1] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2894 coords
[0] = COORD(0) - OUTER_HIGHLIGHT_WIDTH
;
2895 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
2896 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
2902 * Now draw the tiles. Also in this loop, count up the number
2903 * of mines and mine markers.
2905 mines
= markers
= 0;
2906 for (y
= 0; y
< ds
->h
; y
++)
2907 for (x
= 0; x
< ds
->w
; x
++) {
2908 int v
= state
->grid
[y
*ds
->w
+x
];
2912 if (state
->layout
->mines
&& state
->layout
->mines
[y
*ds
->w
+x
])
2915 if ((v
== -2 || v
== -3) &&
2916 (abs(x
-ui
->hx
) <= ui
->hradius
&& abs(y
-ui
->hy
) <= ui
->hradius
))
2919 if (ds
->grid
[y
*ds
->w
+x
] != v
|| bg
!= COL_BACKGROUND
) {
2920 draw_tile(fe
, COORD(x
), COORD(y
), v
, bg
);
2921 ds
->grid
[y
*ds
->w
+x
] = (bg
== COL_BACKGROUND ? v
: -10);
2925 if (!state
->layout
->mines
)
2926 mines
= state
->layout
->n
;
2929 * Update the status bar.
2932 char statusbar
[512];
2934 sprintf(statusbar
, "GAME OVER!");
2935 } else if (state
->won
) {
2936 if (state
->used_solve
)
2937 sprintf(statusbar
, "Auto-solved.");
2939 sprintf(statusbar
, "COMPLETED!");
2941 sprintf(statusbar
, "Mines marked: %d / %d", markers
, mines
);
2943 status_bar(fe
, statusbar
);
2947 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2948 int dir
, game_ui
*ui
)
2953 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2954 int dir
, game_ui
*ui
)
2956 if (oldstate
->used_solve
|| newstate
->used_solve
)
2959 if (dir
> 0 && !oldstate
->dead
&& !oldstate
->won
) {
2960 if (newstate
->dead
) {
2961 ui
->flash_is_death
= TRUE
;
2962 return 3 * FLASH_FRAME
;
2964 if (newstate
->won
) {
2965 ui
->flash_is_death
= FALSE
;
2966 return 2 * FLASH_FRAME
;
2972 static int game_wants_statusbar(void)
2977 static int game_timing_state(game_state
*state
)
2979 if (state
->dead
|| state
->won
|| !state
->layout
->mines
)
2985 #define thegame mines
2988 const struct game thegame
= {
2989 "Mines", "games.mines",
2996 TRUE
, game_configure
, custom_params
,
3005 TRUE
, game_text_format
,
3012 game_free_drawstate
,
3016 game_wants_statusbar
,
3017 TRUE
, game_timing_state
,