Re-architecting of the game backend interface. make_move() has been

split into two functions. The first, interpret_move(), takes all the
arguments that make_move() used to get and may have the usual side
effects of modifying the game_ui, but instead of returning a
modified game_state it instead returns a string description of the
move to be made. This string description is then passed to a second
function, execute_move(), together with an input game_state, which
is responsible for actually producing the new state. (solve_game()
also returns a string to be passed to execute_move().)

The point of this is to work towards being able to serialise the
whole of a game midend into a byte stream such as a disk file, which
will eventually support save and load functions in the desktop
puzzles, as well as restoring half-finished games after a quit and
restart in James Harvey's Palm port. Making each game supply a
convert-to-string function for its game_state format would have been
an unreliable way to do this, since those functions would not have
been used in normal play, so they'd only have been tested when you
actually tried to save and load - a recipe for latent bugs if ever I
heard one. This way, you won't even be able to _make_ a move if
execute_move() doesn't work properly, which means that if you can
play a game at all I can have pretty high confidence that
serialising it will work first time.

This is only the groundwork; there will be more checkins to come on
this theme. But the major upheaval should now be done, and as far as
I can tell everything's still working normally.

[originally from svn r6024]
This commit is contained in:
Simon Tatham
2005-06-27 19:34:54 +00:00
parent 7cb29412c1
commit 76d50e6905
16 changed files with 1377 additions and 639 deletions

View File

@ -546,26 +546,10 @@ static int compare_int(const void *av, const void *bv)
return 0;
}
static game_state *solve_game(game_state *state, game_state *currstate,
game_aux_info *aux, char **error)
static char *solve_game(game_state *state, game_state *currstate,
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.
*/
qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
for (i = 0; i < ret->w*ret->h; i++)
ret->grid[i] &= ~3;
ret->used_solve = ret->just_used_solve = TRUE;
ret->completed = ret->movecount = 1;
return ret;
return dupstr("S");
}
static char *game_text_format(game_state *state)
@ -636,11 +620,11 @@ struct game_drawstate {
int tilesize;
};
static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
int x, int y, int button)
static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
int x, int y, int button)
{
int w = from->w, h = from->h, n = from->n, wh = w*h;
game_state *ret;
int w = state->w, h = state->h, n = state->n /* , wh = w*h */;
char buf[80];
int dir;
button = button & (~MOD_MASK | MOD_NUM_KEYPAD);
@ -698,8 +682,43 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
}
/*
* This is a valid move. Make it.
* If we reach here, we have a valid move.
*/
sprintf(buf, "M%d,%d,%d", x, y, dir);
return dupstr(buf);
}
static game_state *execute_move(game_state *from, char *move)
{
game_state *ret;
int w = from->w, h = from->h, n = from->n, wh = w*h;
int x, y, dir;
if (!strcmp(move, "S")) {
int i;
ret = dup_game(from);
/*
* 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.
*/
qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
for (i = 0; i < ret->w*ret->h; i++)
ret->grid[i] &= ~3;
ret->used_solve = ret->just_used_solve = TRUE;
ret->completed = ret->movecount = 1;
return ret;
}
if (move[0] != 'M' ||
sscanf(move+1, "%d,%d,%d", &x, &y, &dir) != 3 ||
x < 0 || y < 0 || x > from->w - n || y > from->h - n)
return NULL; /* can't parse this move string */
ret = dup_game(from);
ret->just_used_solve = FALSE; /* zero this in a hurry */
ret->movecount++;
@ -1206,7 +1225,8 @@ const struct game thegame = {
new_ui,
free_ui,
game_changed_state,
make_move,
interpret_move,
execute_move,
game_size,
game_colours,
game_new_drawstate,