X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/f17c2cda3b8ae5c8889529d53f95f14521c7f102..61072f7be9d1ab83c459ec1eabc0f9503b57fac6:/mines.c diff --git a/mines.c b/mines.c index df8ecfa..6895b2e 100644 --- a/mines.c +++ b/mines.c @@ -25,10 +25,11 @@ enum { NCOLOURS }; -#define TILE_SIZE 20 +#define PREFERRED_TILE_SIZE 20 +#define TILE_SIZE (ds->tilesize) #define BORDER (TILE_SIZE * 3 / 2) -#define HIGHLIGHT_WIDTH 2 -#define OUTER_HIGHLIGHT_WIDTH 3 +#define HIGHLIGHT_WIDTH (TILE_SIZE / 10) +#define OUTER_HIGHLIGHT_WIDTH (BORDER / 10) #define COORD(x) ( (x) * TILE_SIZE + BORDER ) #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 ) @@ -97,8 +98,11 @@ static game_params *default_params(void) static const struct game_params mines_presets[] = { {9, 9, 10, TRUE}, + {9, 9, 35, TRUE}, {16, 16, 40, TRUE}, + {16, 16, 99, TRUE}, {30, 16, 99, TRUE}, + {30, 16, 170, TRUE}, }; static int game_fetch_preset(int i, char **name, game_params **params) @@ -1824,112 +1828,6 @@ static char *minegen(int w, int h, int n, int x, int y, int unique, return ret; } -/* - * The Mines game descriptions contain the location of every mine, - * and can therefore be used to cheat. - * - * It would be pointless to attempt to _prevent_ this form of - * cheating by encrypting the description, since Mines is - * open-source so anyone can find out the encryption key. However, - * I think it is worth doing a bit of gentle obfuscation to prevent - * _accidental_ spoilers: if you happened to note that the game ID - * starts with an F, for example, you might be unable to put the - * knowledge of those mines out of your mind while playing. So, - * just as discussions of film endings are rot13ed to avoid - * spoiling it for people who don't want to be told, we apply a - * keyless, reversible, but visually completely obfuscatory masking - * function to the mine bitmap. - */ -static void obfuscate_bitmap(unsigned char *bmp, int bits, int decode) -{ - int bytes, firsthalf, secondhalf; - struct step { - unsigned char *seedstart; - int seedlen; - unsigned char *targetstart; - int targetlen; - } steps[2]; - int i, j; - - /* - * My obfuscation algorithm is similar in concept to the OAEP - * encoding used in some forms of RSA. Here's a specification - * of it: - * - * + We have a `masking function' which constructs a stream of - * pseudorandom bytes from a seed of some number of input - * bytes. - * - * + We pad out our input bit stream to a whole number of - * bytes by adding up to 7 zero bits on the end. (In fact - * the bitmap passed as input to this function will already - * have had this done in practice.) - * - * + We divide the _byte_ stream exactly in half, rounding the - * half-way position _down_. So an 81-bit input string, for - * example, rounds up to 88 bits or 11 bytes, and then - * dividing by two gives 5 bytes in the first half and 6 in - * the second half. - * - * + We generate a mask from the second half of the bytes, and - * XOR it over the first half. - * - * + We generate a mask from the (encoded) first half of the - * bytes, and XOR it over the second half. Any null bits at - * the end which were added as padding are cleared back to - * zero even if this operation would have made them nonzero. - * - * To de-obfuscate, the steps are precisely the same except - * that the final two are reversed. - * - * Finally, our masking function. Given an input seed string of - * bytes, the output mask consists of concatenating the SHA-1 - * hashes of the seed string and successive decimal integers, - * starting from 0. - */ - - bytes = (bits + 7) / 8; - firsthalf = bytes / 2; - secondhalf = bytes - firsthalf; - - steps[decode ? 1 : 0].seedstart = bmp + firsthalf; - steps[decode ? 1 : 0].seedlen = secondhalf; - steps[decode ? 1 : 0].targetstart = bmp; - steps[decode ? 1 : 0].targetlen = firsthalf; - - steps[decode ? 0 : 1].seedstart = bmp; - steps[decode ? 0 : 1].seedlen = firsthalf; - steps[decode ? 0 : 1].targetstart = bmp + firsthalf; - steps[decode ? 0 : 1].targetlen = secondhalf; - - for (i = 0; i < 2; i++) { - SHA_State base, final; - unsigned char digest[20]; - char numberbuf[80]; - int digestpos = 20, counter = 0; - - SHA_Init(&base); - SHA_Bytes(&base, steps[i].seedstart, steps[i].seedlen); - - for (j = 0; j < steps[i].targetlen; j++) { - if (digestpos >= 20) { - sprintf(numberbuf, "%d", counter++); - final = base; - SHA_Bytes(&final, numberbuf, strlen(numberbuf)); - SHA_Final(&final, digest); - digestpos = 0; - } - steps[i].targetstart[j] ^= digest[digestpos++]; - } - - /* - * Mask off the pad bits in the final byte after both steps. - */ - if (bits % 8) - bmp[bits / 8] &= 0xFF & (0xFF00 >> (bits % 8)); - } -} - static char *describe_layout(char *grid, int area, int x, int y, int obfuscate) { @@ -2381,8 +2279,8 @@ static void free_game(game_state *state) sfree(state); } -static game_state *solve_game(game_state *state, game_aux_info *aux, - char **error) +static game_state *solve_game(game_state *state, game_state *currstate, + game_aux_info *aux, char **error) { /* * Simply expose the entire grid as if it were a completed @@ -2472,6 +2370,26 @@ static void free_ui(game_ui *ui) sfree(ui); } +static void game_changed_state(game_ui *ui, game_state *oldstate, + game_state *newstate) +{ +} + +struct game_drawstate { + int w, h, started, tilesize; + signed char *grid; + /* + * Items in this `grid' array have all the same values as in + * the game_state grid, and in addition: + * + * - -10 means the tile was drawn `specially' as a result of a + * flash, so it will always need redrawing. + * + * - -22 and -23 mean the tile is highlighted for a possible + * click. + */ +}; + static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, int x, int y, int button) { @@ -2487,11 +2405,12 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, cx = FROMCOORD(x); cy = FROMCOORD(y); - if (cx < 0 || cx >= from->w || cy < 0 || cy >= from->h) - return NULL; if (button == LEFT_BUTTON || button == LEFT_DRAG || button == MIDDLE_BUTTON || button == MIDDLE_DRAG) { + if (cx < 0 || cx >= from->w || cy < 0 || cy >= from->h) + return NULL; + /* * Mouse-downs and mouse-drags just cause highlighting * updates. @@ -2503,6 +2422,9 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, } if (button == RIGHT_BUTTON) { + if (cx < 0 || cx >= from->w || cy < 0 || cy >= from->h) + return NULL; + /* * Right-clicking only works on a covered square, and it * toggles between -1 (marked as mine) and -2 (not marked @@ -2529,6 +2451,8 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, * At this stage we must never return NULL: we have adjusted * the ui, so at worst we return `from'. */ + if (cx < 0 || cx >= from->w || cy < 0 || cy >= from->h) + return from; /* * Left-clicking on a covered square opens a tile. Not @@ -2591,23 +2515,23 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, * Drawing routines. */ -struct game_drawstate { - int w, h, started; - signed char *grid; +static void game_size(game_params *params, game_drawstate *ds, + int *x, int *y, int expand) +{ + int tsx, tsy, ts; /* - * Items in this `grid' array have all the same values as in - * the game_state grid, and in addition: - * - * - -10 means the tile was drawn `specially' as a result of a - * flash, so it will always need redrawing. - * - * - -22 and -23 mean the tile is highlighted for a possible - * click. + * Each window dimension equals the tile size times 3 more than + * the grid dimension (the border is 3/2 the width of the + * tiles). */ -}; + tsx = *x / (params->w + 3); + tsy = *y / (params->h + 3); + ts = min(tsx, tsy); + if (expand) + ds->tilesize = ts; + else + ds->tilesize = min(ts, PREFERRED_TILE_SIZE); -static void game_size(game_params *params, int *x, int *y) -{ *x = BORDER * 2 + TILE_SIZE * params->w; *y = BORDER * 2 + TILE_SIZE * params->h; } @@ -2697,6 +2621,7 @@ static game_drawstate *game_new_drawstate(game_state *state) ds->w = state->w; ds->h = state->h; ds->started = FALSE; + ds->tilesize = 0; /* not decided yet */ ds->grid = snewn(ds->w * ds->h, signed char); memset(ds->grid, -99, ds->w * ds->h); @@ -2710,7 +2635,8 @@ static void game_free_drawstate(game_drawstate *ds) sfree(ds); } -static void draw_tile(frontend *fe, int x, int y, int v, int bg) +static void draw_tile(frontend *fe, game_drawstate *ds, + int x, int y, int v, int bg) { if (v < 0) { int coords[12]; @@ -2944,7 +2870,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, v -= 20; if (ds->grid[y*ds->w+x] != v || bg != COL_BACKGROUND) { - draw_tile(fe, COORD(x), COORD(y), v, bg); + draw_tile(fe, ds, COORD(x), COORD(y), v, bg); ds->grid[y*ds->w+x] = (bg == COL_BACKGROUND ? v : -10); } } @@ -3035,6 +2961,7 @@ const struct game thegame = { TRUE, game_text_format, new_ui, free_ui, + game_changed_state, make_move, game_size, game_colours,