aboutsummaryrefslogtreecommitdiff
path: root/pattern.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2005-05-02 16:59:50 +0000
committerSimon Tatham <anakin@pobox.com>2005-05-02 16:59:50 +0000
commitb32b4f87632e97dbdaeb5813183879507e494626 (patch)
tree3063c3a522b5d21cc31832c86e57257bf63a8394 /pattern.c
parentcf7988afb39eacb608eeacc73eb08a42db91b3d4 (diff)
downloadpuzzles-b32b4f87632e97dbdaeb5813183879507e494626.zip
puzzles-b32b4f87632e97dbdaeb5813183879507e494626.tar.gz
puzzles-b32b4f87632e97dbdaeb5813183879507e494626.tar.bz2
puzzles-b32b4f87632e97dbdaeb5813183879507e494626.tar.xz
I've changed my mind. For the benefit of users with slower
computers, let's save the Solo and Pattern grids at generation time and regurgitate them when asked to solve, rather than doing all the work over again. [originally from svn r5737]
Diffstat (limited to 'pattern.c')
-rw-r--r--pattern.c40
1 files changed, 32 insertions, 8 deletions
diff --git a/pattern.c b/pattern.c
index 0f0a9c6..a728e15 100644
--- a/pattern.c
+++ b/pattern.c
@@ -472,6 +472,11 @@ static unsigned char *generate_soluble(random_state *rs, int w, int h)
return grid;
}
+struct game_aux_info {
+ int w, h;
+ unsigned char *grid;
+};
+
static char *new_game_seed(game_params *params, random_state *rs,
game_aux_info **aux)
{
@@ -485,6 +490,20 @@ static char *new_game_seed(game_params *params, random_state *rs,
rowdata = snewn(max, int);
/*
+ * Save the solved game in an aux_info.
+ */
+ {
+ game_aux_info *ai = snew(game_aux_info);
+
+ ai->w = params->w;
+ ai->h = params->h;
+ ai->grid = snewn(ai->w * ai->h, unsigned char);
+ memcpy(ai->grid, grid, ai->w * ai->h);
+
+ *aux = ai;
+ }
+
+ /*
* Seed is a slash-separated list of row contents; each row
* contents section is a dot-separated list of integers. Row
* contents are listed in the order (columns left to right,
@@ -539,7 +558,8 @@ static char *new_game_seed(game_params *params, random_state *rs,
static void game_free_aux_info(game_aux_info *aux)
{
- assert(!"Shouldn't happen");
+ sfree(aux->grid);
+ sfree(aux);
}
static char *validate_seed(game_params *params, char *seed)
@@ -651,21 +671,25 @@ static void free_game(game_state *state)
sfree(state);
}
-static game_state *solve_game(game_state *state, game_aux_info *aux,
+static game_state *solve_game(game_state *state, game_aux_info *ai,
char **error)
{
game_state *ret;
+ ret = dup_game(state);
+ ret->completed = ret->cheated = TRUE;
+
/*
- * I could have stored the grid I invented in the game_aux_info
- * and extracted it here where available, but it seems easier
- * just to run my internal solver in all cases.
+ * If we already have the solved state in an aux_info, copy it
+ * out.
*/
+ if (ai) {
- ret = dup_game(state);
- ret->completed = ret->cheated = TRUE;
+ assert(ret->w == ai->w);
+ assert(ret->h == ai->h);
+ memcpy(ret->grid, ai->grid, ai->w * ai->h);
- {
+ } else {
int w = state->w, h = state->h, i, j, done_any, max;
unsigned char *matrix, *workspace;
int *rowdata;