X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/5d3f9ea6b3599c21b3e1fbf819dd22dccbacc2d3..dfc39b12684cfb7afc705684f73a296a29ed5208:/mines.c diff --git a/mines.c b/mines.c index 73d4c09..725fdcf 100644 --- a/mines.c +++ b/mines.c @@ -31,7 +31,7 @@ #include "puzzles.h" enum { - COL_BACKGROUND, + COL_BACKGROUND, COL_BACKGROUND2, COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_MINE, COL_BANG, COL_CROSS, COL_FLAG, COL_FLAGBASE, COL_QUERY, COL_HIGHLIGHT, COL_LOWLIGHT, @@ -70,8 +70,9 @@ struct mine_layout { struct game_state { int w, h, n, dead, won; + int used_solve, just_used_solve; struct mine_layout *layout; /* real mine positions */ - char *grid; /* player knowledge */ + signed char *grid; /* player knowledge */ /* * Each item in the `grid' array is one of the following values: * @@ -560,7 +561,8 @@ static void std_add(struct squaretodo *std, int i) std->next[i] = -1; } -static void known_squares(int w, int h, struct squaretodo *std, char *grid, +static void known_squares(int w, int h, struct squaretodo *std, + signed char *grid, int (*open)(void *ctx, int x, int y), void *openctx, int x, int y, int mask, int mine) { @@ -627,9 +629,10 @@ struct perturbations { * steps were required; the exact return value is the number of * perturb calls. */ -static int minesolve(int w, int h, int n, char *grid, +static int minesolve(int w, int h, int n, signed char *grid, int (*open)(void *ctx, int x, int y), - struct perturbations *(*perturb)(void *ctx, char *grid, + struct perturbations *(*perturb)(void *ctx, + signed char *grid, int x, int y, int mask), void *ctx, random_state *rs) { @@ -1278,7 +1281,7 @@ static int minesolve(int w, int h, int n, char *grid, */ struct minectx { - char *grid; + signed char *grid; int w, h; int sx, sy; random_state *rs; @@ -1337,7 +1340,7 @@ static int squarecmp(const void *av, const void *bv) return 0; } -static struct perturbations *mineperturb(void *vctx, char *grid, +static struct perturbations *mineperturb(void *vctx, signed char *grid, int setx, int sety, int mask) { struct minectx *ctx = (struct minectx *)vctx; @@ -1657,7 +1660,7 @@ static char *minegen(int w, int h, int n, int x, int y, int unique, * We bypass this bit if we're not after a unique grid. */ if (unique) { - char *solvegrid = snewn(w*h, char); + signed char *solvegrid = snewn(w*h, char); struct minectx actx, *ctx = &actx; int solveret, prevret = -2; @@ -1803,7 +1806,7 @@ static void obfuscate_bitmap(unsigned char *bmp, int bits, int decode) static char *new_mine_layout(int w, int h, int n, int x, int y, int unique, random_state *rs, char **game_desc) { - char *grid, *ret, *p; + signed char *grid, *ret, *p; unsigned char *bmp; int i, area; @@ -1847,24 +1850,30 @@ static char *new_mine_layout(int w, int h, int n, int x, int y, int unique, } static char *new_game_desc(game_params *params, random_state *rs, - game_aux_info **aux) + game_aux_info **aux, int interactive) { -#ifdef PREOPENED - int x = random_upto(rs, params->w); - int y = random_upto(rs, params->h); - char *grid, *desc; + if (!interactive) { + /* + * For batch-generated grids, pre-open one square. + */ + int x = random_upto(rs, params->w); + int y = random_upto(rs, params->h); + signed char *grid; + char *desc; - grid = new_mine_layout(params->w, params->h, params->n, - x, y, params->unique, rs); -#else - char *rsdesc, *desc; + grid = new_mine_layout(params->w, params->h, params->n, + x, y, params->unique, rs, &desc); + sfree(grid); + return desc; + } else { + char *rsdesc, *desc; - rsdesc = random_state_encode(rs); - desc = snewn(strlen(rsdesc) + 100, char); - sprintf(desc, "r%d,%c,%s", params->n, params->unique ? 'u' : 'a', rsdesc); - sfree(rsdesc); - return desc; -#endif + rsdesc = random_state_encode(rs); + desc = snewn(strlen(rsdesc) + 100, char); + sprintf(desc, "r%d,%c,%s", params->n, params->unique ? 'u' : 'a', rsdesc); + sfree(rsdesc); + return desc; + } } static void game_free_aux_info(game_aux_info *aux) @@ -2054,6 +2063,7 @@ static game_state *new_game(midend_data *me, game_params *params, char *desc) state->h = params->h; state->n = params->n; state->dead = state->won = FALSE; + state->used_solve = state->just_used_solve = FALSE; wh = state->w * state->h; @@ -2081,6 +2091,8 @@ static game_state *new_game(midend_data *me, game_params *params, char *desc) state->layout->me = me; } else { + state->layout->rs = NULL; + state->layout->me = NULL; state->layout->mines = snewn(wh, char); x = atoi(desc); @@ -2146,6 +2158,8 @@ static game_state *dup_game(game_state *state) ret->n = state->n; ret->dead = state->dead; ret->won = state->won; + ret->used_solve = state->used_solve; + ret->just_used_solve = state->just_used_solve; ret->layout = state->layout; ret->layout->refcount++; ret->grid = snewn(ret->w * ret->h, char); @@ -2169,12 +2183,71 @@ static void free_game(game_state *state) static game_state *solve_game(game_state *state, game_aux_info *aux, char **error) { - return NULL; + /* + * Simply expose the entire grid as if it were a completed + * solution. + */ + game_state *ret; + int yy, xx; + + if (!state->layout->mines) { + *error = "Game has not been started yet"; + return NULL; + } + + ret = dup_game(state); + for (yy = 0; yy < ret->h; yy++) + for (xx = 0; xx < ret->w; xx++) { + + if (ret->layout->mines[yy*ret->w+xx]) { + ret->grid[yy*ret->w+xx] = -1; + } else { + int dx, dy, v; + + v = 0; + + for (dx = -1; dx <= +1; dx++) + for (dy = -1; dy <= +1; dy++) + if (xx+dx >= 0 && xx+dx < ret->w && + yy+dy >= 0 && yy+dy < ret->h && + ret->layout->mines[(yy+dy)*ret->w+(xx+dx)]) + v++; + + ret->grid[yy*ret->w+xx] = v; + } + } + ret->used_solve = ret->just_used_solve = TRUE; + ret->won = TRUE; + + return ret; } static char *game_text_format(game_state *state) { - return NULL; + char *ret; + int x, y; + + ret = snewn((state->w + 1) * state->h + 1, char); + for (y = 0; y < state->h; y++) { + for (x = 0; x < state->w; x++) { + int v = state->grid[y*state->w+x]; + if (v == 0) + v = '-'; + else if (v >= 1 && v <= 8) + v = '0' + v; + else if (v == -1) + v = '*'; + else if (v == -2 || v == -3) + v = '?'; + else if (v >= 64) + v = '!'; + ret[y * (state->w+1) + x] = v; + } + ret[y * (state->w+1) + state->w] = '\n'; + } + ret[(state->w + 1) * state->h] = '\0'; + + return ret; } struct game_ui { @@ -2196,8 +2269,8 @@ static void free_ui(game_ui *ui) sfree(ui); } -static game_state *make_move(game_state *from, game_ui *ui, int x, int y, - int button) +static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, + int x, int y, int button) { game_state *ret; int cx, cy; @@ -2238,6 +2311,7 @@ static game_state *make_move(game_state *from, game_ui *ui, int x, int y, return NULL; ret = dup_game(from); + ret->just_used_solve = FALSE; ret->grid[cy * from->w + cx] ^= (-2 ^ -1); return ret; @@ -2260,6 +2334,7 @@ static game_state *make_move(game_state *from, game_ui *ui, int x, int y, if (from->grid[cy * from->w + cx] == -2 || from->grid[cy * from->w + cx] == -3) { ret = dup_game(from); + ret->just_used_solve = FALSE; open_square(ret, cx, cy); return ret; } @@ -2285,6 +2360,7 @@ static game_state *make_move(game_state *from, game_ui *ui, int x, int y, if (n == from->grid[cy * from->w + cx]) { ret = dup_game(from); + ret->just_used_solve = FALSE; for (dy = -1; dy <= +1; dy++) for (dx = -1; dx <= +1; dx++) if (cx+dx >= 0 && cx+dx < ret->w && @@ -2308,7 +2384,7 @@ static game_state *make_move(game_state *from, game_ui *ui, int x, int y, struct game_drawstate { int w, h, started; - char *grid; + signed char *grid; /* * Items in this `grid' array have all the same values as in * the game_state grid, and in addition: @@ -2333,6 +2409,10 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours) frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); + ret[COL_BACKGROUND2 * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 19.0 / 20.0; + ret[COL_BACKGROUND2 * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 19.0 / 20.0; + ret[COL_BACKGROUND2 * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 19.0 / 20.0; + ret[COL_1 * 3 + 0] = 0.0F; ret[COL_1 * 3 + 1] = 0.0F; ret[COL_1 * 3 + 2] = 1.0F; @@ -2433,7 +2513,8 @@ static void draw_tile(frontend *fe, int x, int y, int v, int bg) /* * Omit the highlights in this case. */ - draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, bg); + draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, + bg == COL_BACKGROUND ? COL_BACKGROUND2 : bg); draw_line(fe, x, y, x + TILE_SIZE - 1, y, COL_LOWLIGHT); draw_line(fe, x, y, x, y + TILE_SIZE - 1, COL_LOWLIGHT); } else { @@ -2501,7 +2582,8 @@ static void draw_tile(frontend *fe, int x, int y, int v, int bg) * on), we clear the square to COL_BANG. */ draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, - (v == 65 ? COL_BANG : bg)); + (v == 65 ? COL_BANG : + bg == COL_BACKGROUND ? COL_BACKGROUND2 : bg)); draw_line(fe, x, y, x + TILE_SIZE - 1, y, COL_LOWLIGHT); draw_line(fe, x, y, x, y + TILE_SIZE - 1, COL_LOWLIGHT); @@ -2665,7 +2747,10 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, if (state->dead) { sprintf(statusbar, "GAME OVER!"); } else if (state->won) { - sprintf(statusbar, "COMPLETED!"); + if (state->used_solve) + sprintf(statusbar, "Auto-solved."); + else + sprintf(statusbar, "COMPLETED!"); } else { sprintf(statusbar, "Mines marked: %d / %d", markers, mines); } @@ -2682,6 +2767,9 @@ static float game_anim_length(game_state *oldstate, game_state *newstate, static float game_flash_length(game_state *oldstate, game_state *newstate, int dir, game_ui *ui) { + if (oldstate->used_solve || newstate->used_solve) + return 0.0F; + if (dir > 0 && !oldstate->dead && !oldstate->won) { if (newstate->dead) { ui->flash_is_death = TRUE; @@ -2727,8 +2815,8 @@ const struct game thegame = { new_game, dup_game, free_game, - FALSE, solve_game, - FALSE, game_text_format, + TRUE, solve_game, + TRUE, game_text_format, new_ui, free_ui, make_move,