X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/6c42c563460ffaa9fd04b7e3f336646065f33992..bf58c93a413e6924dc8d8664363084fbdfaf8b56:/loopy.c diff --git a/loopy.c b/loopy.c index 71aaa31..ce49c73 100644 --- a/loopy.c +++ b/loopy.c @@ -66,7 +66,7 @@ #define PREFERRED_TILE_SIZE 32 #define TILE_SIZE (ds->tilesize) -#define LINEWIDTH TILE_SIZE / 16 +#define LINEWIDTH (ds->linewidth) #define BORDER (TILE_SIZE / 2) #define FLASH_TIME 0.5F @@ -148,7 +148,7 @@ enum { #define ENCODE(upper,title,lower) #lower #define CONFIG(upper,title,lower) ":" #title enum { DIFFLIST(ENUM) DIFFCOUNT }; -static char const *const loopy_diffnames[] = { DIFFLIST(TITLE) }; +/* static char const *const loopy_diffnames[] = { DIFFLIST(TITLE) }; */ static char const loopy_diffchars[] = DIFFLIST(ENCODE); #define DIFFCONFIG DIFFLIST(CONFIG) @@ -683,13 +683,12 @@ static void print_board(const game_params *params, const char *board) #endif } -static char *new_fullyclued_board(game_params *params, random_state *rs) +static void add_full_clues(game_state *state, game_params *params, + random_state *rs) { char *clues; char *board; int i, j, a, b, c; - game_state s; - game_state *state = &s; int board_area = SQUARE_COUNT(params); int t; @@ -763,11 +762,7 @@ static char *new_fullyclued_board(game_params *params, random_state *rs) #define SCORE_DISTANCE 1 board = snewn(board_area, char); - clues = snewn(board_area, char); - - state->h = params->h; - state->w = params->w; - state->clues = clues; + clues = state->clues; /* Make a board */ memset(board, SQUARE_UNLIT, board_area); @@ -932,7 +927,6 @@ static char *new_fullyclued_board(game_params *params, random_state *rs) } sfree(board); - return clues; } static solver_state *solve_game_rec(const solver_state *sstate, int diff); @@ -1004,6 +998,7 @@ static char *new_game_desc(game_params *params, random_state *rs, state->h = params->h; state->w = params->w; + state->clues = snewn(SQUARE_COUNT(params), char); state->hl = snewn(HL_COUNT(params), char); state->vl = snewn(VL_COUNT(params), char); @@ -1019,7 +1014,7 @@ newboard_please: * preventing games smaller than 4x4 seems to stop this happening */ do { - state->clues = new_fullyclued_board(params, rs); + add_full_clues(state, params, rs); } while (!game_has_unique_soln(state, params->diff)); state_new = remove_clues(state, rs, params->diff); @@ -1210,8 +1205,14 @@ static char *encode_solve_move(const game_state *state) } } - /* No point in doing sums like that if they're going to be wrong */ - assert(strlen(ret) == (size_t)len); + /* + * Ensure we haven't overrun the buffer we allocated (which we + * really shouldn't have, since we computed its maximum size). + * Note that this assert is <= rather than ==, because the + * solver is permitted to produce an incomplete solution in + * which case the buffer will be only partially used. + */ + assert(strlen(ret) <= (size_t)len); return ret; } @@ -1282,7 +1283,7 @@ static int dot_setall_dlines(solver_state *sstate, enum dline dl, int i, int j, break; case DLINE_DL: case DLINE_DR: - if (j <= (state)->h && BELOW_DOT(state, i, j) == line_old) { + if (j < (state)->h && BELOW_DOT(state, i, j) == line_old) { LV_BELOW_DOT(state, i, j) = line_new; retval = TRUE; } @@ -1307,13 +1308,13 @@ static int dot_setall_dlines(solver_state *sstate, enum dline dl, int i, int j, case DLINE_UR: case DLINE_DR: case DLINE_HORIZ: - if (i <= (state)->w && RIGHTOF_DOT(state, i, j) == line_old) { + if (i < (state)->w && RIGHTOF_DOT(state, i, j) == line_old) { LV_RIGHTOF_DOT(state, i, j) = line_new; retval = TRUE; } break; case DLINE_VERT: - if (j <= (state)->h && BELOW_DOT(state, i, j) == line_old) { + if (j < (state)->h && BELOW_DOT(state, i, j) == line_old) { LV_BELOW_DOT(state, i, j) = line_new; retval = TRUE; } @@ -2078,7 +2079,7 @@ static void game_changed_state(game_ui *ui, game_state *oldstate, struct game_drawstate { int started; - int tilesize; + int tilesize, linewidth; int flashing; char *hl, *vl; char *clue_error; @@ -2360,9 +2361,10 @@ static void game_set_size(drawing *dr, game_drawstate *ds, game_params *params, int tilesize) { ds->tilesize = tilesize; + ds->linewidth = max(1,tilesize/16); } -static float *game_colours(frontend *fe, game_state *state, int *ncolours) +static float *game_colours(frontend *fe, int *ncolours) { float *ret = snewn(4 * NCOLOURS, float); @@ -2388,7 +2390,7 @@ static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) { struct game_drawstate *ds = snew(struct game_drawstate); - ds->tilesize = 0; + ds->tilesize = ds->linewidth = 0; ds->started = 0; ds->hl = snewn(HL_COUNT(state), char); ds->vl = snewn(VL_COUNT(state), char); @@ -2639,11 +2641,6 @@ static float game_flash_length(game_state *oldstate, game_state *newstate, return 0.0F; } -static int game_wants_statusbar(void) -{ - return FALSE; -} - static int game_timing_state(game_state *state, game_ui *ui) { return TRUE; @@ -2667,7 +2664,8 @@ static void game_print(drawing *dr, game_state *state, int tilesize) int ink = print_mono_colour(dr, 0); int x, y; game_drawstate ads, *ds = &ads; - ds->tilesize = tilesize; + + game_set_size(dr, ds, NULL, tilesize); /* * Dots. I'll deliberately make the dots a bit wider than the @@ -2749,7 +2747,7 @@ const struct game thegame = { game_anim_length, game_flash_length, TRUE, FALSE, game_print_size, game_print, - game_wants_statusbar, + FALSE, /* wants_statusbar */ FALSE, game_timing_state, - 0, /* mouse_priorities */ + 0, /* flags */ };