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