Added an automatic `Solve' feature to most games. This is useful for

various things:
 - if you haven't fully understood what a game is about, it gives
   you an immediate example of a puzzle plus its solution so you can
   understand it
 - in some games it's useful to compare your solution with the real
   one and see where you made a mistake
 - in the rearrangement games (Fifteen, Sixteen, Twiddle) it's handy
   to be able to get your hands on a pristine grid quickly so you
   can practise or experiment with manoeuvres on it
 - it provides a good way of debugging the games if you think you've
   encountered an unsolvable grid!

[originally from svn r5731]
This commit is contained in:
Simon Tatham
2005-05-02 13:17:10 +00:00
parent aea7b61815
commit 4f7b65de2e
16 changed files with 604 additions and 51 deletions

View File

@ -42,6 +42,8 @@ struct game_state {
int w, h, n;
int *tiles;
int completed;
int just_used_solve; /* used to suppress undo animation */
int used_solve; /* used to suppress completion flash */
int movecount;
int last_movement_sense;
};
@ -279,7 +281,7 @@ 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");
}
@ -359,6 +361,7 @@ static game_state *new_game(game_params *params, char *seed)
assert(!*p);
state->completed = state->movecount = 0;
state->used_solve = state->just_used_solve = FALSE;
state->last_movement_sense = 0;
return state;
@ -375,6 +378,8 @@ static game_state *dup_game(game_state *state)
memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
ret->completed = state->completed;
ret->movecount = state->movecount;
ret->used_solve = state->used_solve;
ret->just_used_solve = state->just_used_solve;
ret->last_movement_sense = state->last_movement_sense;
return ret;
@ -385,6 +390,27 @@ 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->used_solve = ret->just_used_solve = TRUE;
ret->completed = ret->movecount;
return ret;
}
static char *game_text_format(game_state *state)
{
char *ret, *p, buf[80];
@ -464,6 +490,7 @@ static game_state *make_move(game_state *from, game_ui *ui,
}
ret = dup_game(from);
ret->just_used_solve = FALSE; /* zero this in a hurry */
do {
cx += dx;
@ -786,9 +813,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);
}
@ -797,13 +828,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;
@ -834,6 +870,7 @@ const struct game thegame = {
new_game,
dup_game,
free_game,
TRUE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,