X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/6f2d8d7c70f6bbf8bce982ced1fa879e967afbbf..aa27d4932844e6aa590afba411b1503217f1602d:/fifteen.c diff --git a/fifteen.c b/fifteen.c index e31843d..700c26c 100644 --- a/fifteen.c +++ b/fifteen.c @@ -41,6 +41,8 @@ struct game_state { int *tiles; int gap_pos; int completed; + int just_used_solve; /* used to suppress undo animation */ + int used_solve; /* used to suppress completion flash */ int movecount; }; @@ -70,21 +72,17 @@ static game_params *dup_params(game_params *params) return ret; } -static game_params *decode_params(char const *string) +static void decode_params(game_params *ret, char const *string) { - game_params *ret = default_params(); - ret->w = ret->h = atoi(string); while (*string && isdigit(*string)) string++; if (*string == 'x') { string++; ret->h = atoi(string); } - - return ret; } -static char *encode_params(game_params *params) +static char *encode_params(game_params *params, int full) { char data[256]; @@ -152,7 +150,7 @@ static int perm_parity(int *perm, int n) return ret; } -static char *new_game_seed(game_params *params, random_state *rs, +static char *new_game_desc(game_params *params, random_state *rs, game_aux_info **aux) { int gap, n, i, x; @@ -245,8 +243,8 @@ static char *new_game_seed(game_params *params, random_state *rs, } /* - * Now construct the game seed, by describing the tile array as - * a simple sequence of comma-separated integers. + * Now construct the game description, by describing the tile + * array as a simple sequence of comma-separated integers. */ ret = NULL; retlen = 0; @@ -268,19 +266,19 @@ static char *new_game_seed(game_params *params, random_state *rs, return ret; } -void game_free_aux_info(game_aux_info *aux) +static void game_free_aux_info(game_aux_info *aux) { assert(!"Shouldn't happen"); } -static char *validate_seed(game_params *params, char *seed) +static char *validate_desc(game_params *params, char *desc) { char *p, *err; int i, area; int *used; area = params->w * params->h; - p = seed; + p = desc; err = NULL; used = snewn(area, int); @@ -324,7 +322,7 @@ static char *validate_seed(game_params *params, char *seed) return err; } -static game_state *new_game(game_params *params, char *seed) +static game_state *new_game(game_params *params, char *desc) { game_state *state = snew(game_state); int i; @@ -336,7 +334,7 @@ static game_state *new_game(game_params *params, char *seed) state->tiles = snewn(state->n, int); state->gap_pos = 0; - p = seed; + p = desc; i = 0; for (i = 0; i < state->n; i++) { assert(*p); @@ -351,6 +349,7 @@ static game_state *new_game(game_params *params, char *seed) assert(state->tiles[state->gap_pos] == 0); state->completed = state->movecount = 0; + state->used_solve = state->just_used_solve = FALSE; return state; } @@ -367,6 +366,8 @@ static game_state *dup_game(game_state *state) ret->gap_pos = state->gap_pos; ret->completed = state->completed; ret->movecount = state->movecount; + ret->used_solve = state->used_solve; + ret->just_used_solve = state->just_used_solve; return ret; } @@ -376,6 +377,28 @@ static void free_game(game_state *state) sfree(state); } +static game_state *solve_game(game_state *state, game_aux_info *aux, + char **error) +{ + game_state *ret = dup_game(state); + int i; + + /* + * Simply replace the grid with a solved one. For this game, + * this isn't a useful operation for actually telling the user + * what they should have done, but it is useful for + * conveniently being able to get hold of a clean state from + * which to practise manoeuvres. + */ + for (i = 0; i < ret->n; i++) + ret->tiles[i] = (i+1) % ret->n; + ret->gap_pos = ret->n-1; + ret->used_solve = ret->just_used_solve = TRUE; + ret->completed = ret->movecount = 1; + + return ret; +} + static char *game_text_format(game_state *state) { char *ret, *p, buf[80]; @@ -394,7 +417,7 @@ static char *game_text_format(game_state *state) */ maxlen = state->h * state->w * (col+1); - ret = snewn(maxlen, char); + ret = snewn(maxlen+1, char); p = ret; for (y = 0; y < state->h; y++) { @@ -433,6 +456,8 @@ static game_state *make_move(game_state *from, game_ui *ui, int gx, gy, dx, dy, ux, uy, up, p; game_state *ret; + button &= ~MOD_MASK; + gx = X(from, from->gap_pos); gy = Y(from, from->gap_pos); @@ -467,6 +492,7 @@ static game_state *make_move(game_state *from, game_ui *ui, up = C(from, ux, uy); ret = dup_game(from); + ret->just_used_solve = FALSE; /* zero this in a hurry */ ret->gap_pos = C(from, dx, dy); assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n); @@ -739,9 +765,13 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, if (oldstate) state = oldstate; - sprintf(statusbuf, "%sMoves: %d", - (state->completed ? "COMPLETED! " : ""), - (state->completed ? state->completed : state->movecount)); + if (state->used_solve) + sprintf(statusbuf, "Moves since auto-solve: %d", + state->movecount - state->completed); + else + sprintf(statusbuf, "%sMoves: %d", + (state->completed ? "COMPLETED! " : ""), + (state->completed ? state->completed : state->movecount)); status_bar(fe, statusbuf); } @@ -750,13 +780,18 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, static float game_anim_length(game_state *oldstate, game_state *newstate, int dir) { - return ANIM_TIME; + if ((dir > 0 && newstate->just_used_solve) || + (dir < 0 && oldstate->just_used_solve)) + return 0.0F; + else + return ANIM_TIME; } static float game_flash_length(game_state *oldstate, game_state *newstate, int dir) { - if (!oldstate->completed && newstate->completed) + if (!oldstate->completed && newstate->completed && + !oldstate->used_solve && !newstate->used_solve) return 2 * FLASH_FRAME; else return 0.0F; @@ -781,12 +816,13 @@ const struct game thegame = { dup_params, TRUE, game_configure, custom_params, validate_params, - new_game_seed, + new_game_desc, game_free_aux_info, - validate_seed, + validate_desc, new_game, dup_game, free_game, + TRUE, solve_game, TRUE, game_text_format, new_ui, free_ui,