chiark / gitweb /
Added an `interactive' flag to new_game_desc(), which toggles Mines
[sgt-puzzles.git] / fifteen.c
index 2263bc1a1998b744d9283a66a12ba8df93c61156..e7a41ef32fcdbdd21749c3bf1965e6579043bce3 100644 (file)
--- 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,8 @@ 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 interactive)
 {
     int gap, n, i, x;
     int x1, x2, p1, p2, parity;
@@ -244,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;
@@ -267,14 +266,19 @@ static char *new_game_seed(game_params *params, random_state *rs)
     return ret;
 }
 
-static char *validate_seed(game_params *params, char *seed)
+static void game_free_aux_info(game_aux_info *aux)
+{
+    assert(!"Shouldn't happen");
+}
+
+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);
@@ -318,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(midend_data *me, game_params *params, char *desc)
 {
     game_state *state = snew(game_state);
     int i;
@@ -330,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);
@@ -345,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;
 }
@@ -361,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;
 }
@@ -370,6 +377,70 @@ 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];
+    int x, y, col, maxlen;
+
+    /*
+     * First work out how many characters we need to display each
+     * number.
+     */
+    col = sprintf(buf, "%d", state->n-1);
+
+    /*
+     * Now we know the exact total size of the grid we're going to
+     * produce: it's got h rows, each containing w lots of col, w-1
+     * spaces and a trailing newline.
+     */
+    maxlen = state->h * state->w * (col+1);
+
+    ret = snewn(maxlen+1, char);
+    p = ret;
+
+    for (y = 0; y < state->h; y++) {
+       for (x = 0; x < state->w; x++) {
+           int v = state->tiles[state->w*y+x];
+           if (v == 0)
+               sprintf(buf, "%*s", col, "");
+           else
+               sprintf(buf, "%*d", col, v);
+           memcpy(p, buf, col);
+           p += col;
+           if (x+1 == state->w)
+               *p++ = '\n';
+           else
+               *p++ = ' ';
+       }
+    }
+
+    assert(p - ret == maxlen);
+    *p = '\0';
+    return ret;
+}
+
 static game_ui *new_ui(game_state *state)
 {
     return NULL;
@@ -385,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);
 
@@ -419,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);
@@ -691,24 +765,33 @@ 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);
     }
 }
 
 static float game_anim_length(game_state *oldstate,
-                             game_state *newstate, int dir)
+                             game_state *newstate, int dir, game_ui *ui)
 {
-    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)
+                              game_state *newstate, int dir, game_ui *ui)
 {
-    if (!oldstate->completed && newstate->completed)
+    if (!oldstate->completed && newstate->completed &&
+       !oldstate->used_solve && !newstate->used_solve)
         return 2 * FLASH_FRAME;
     else
         return 0.0F;
@@ -719,26 +802,33 @@ static int game_wants_statusbar(void)
     return TRUE;
 }
 
+static int game_timing_state(game_state *state)
+{
+    return TRUE;
+}
+
 #ifdef COMBINED
 #define thegame fifteen
 #endif
 
 const struct game thegame = {
-    "Fifteen", "games.fifteen", TRUE,
+    "Fifteen", "games.fifteen",
     default_params,
     game_fetch_preset,
     decode_params,
     encode_params,
     free_params,
     dup_params,
-    game_configure,
-    custom_params,
+    TRUE, game_configure, custom_params,
     validate_params,
-    new_game_seed,
-    validate_seed,
+    new_game_desc,
+    game_free_aux_info,
+    validate_desc,
     new_game,
     dup_game,
     free_game,
+    TRUE, solve_game,
+    TRUE, game_text_format,
     new_ui,
     free_ui,
     make_move,
@@ -750,4 +840,5 @@ const struct game thegame = {
     game_anim_length,
     game_flash_length,
     game_wants_statusbar,
+    FALSE, game_timing_state,
 };