Update doc for recent changes in Restart behaviour.
[sgt/puzzles] / rect.c
diff --git a/rect.c b/rect.c
index e3914b0..78c9e08 100644 (file)
--- a/rect.c
+++ b/rect.c
@@ -82,7 +82,7 @@ struct game_state {
     int *grid;                        /* contains the numbers */
     unsigned char *vedge;             /* (w+1) x h */
     unsigned char *hedge;             /* w x (h+1) */
-    int completed;
+    int completed, cheated;
 };
 
 static game_params *default_params(void)
@@ -130,12 +130,9 @@ 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);
-    ret->expandfactor = 0.0F;
     while (*string && isdigit((unsigned char)*string)) string++;
     if (*string == 'x') {
         string++;
@@ -146,15 +143,15 @@ static game_params *decode_params(char const *string)
        string++;
        ret->expandfactor = atof(string);
     }
-
-    return ret;
 }
 
-static char *encode_params(game_params *params)
+static char *encode_params(game_params *params, int full)
 {
     char data[256];
 
     sprintf(data, "%dx%d", params->w, params->h);
+    if (full)
+        sprintf(data + strlen(data), "e%g", params->expandfactor);
 
     return dupstr(data);
 }
@@ -386,12 +383,19 @@ static void display_grid(game_params *params, int *grid, int *numbers, int all)
 }
 #endif
 
-static char *new_game_seed(game_params *params, random_state *rs)
+struct game_aux_info {
+    int w, h;
+    unsigned char *vedge;             /* (w+1) x h */
+    unsigned char *hedge;             /* w x (h+1) */
+};
+
+static char *new_game_desc(game_params *params, random_state *rs,
+                          game_aux_info **aux)
 {
     int *grid, *numbers;
     struct rectlist *list;
     int x, y, y2, y2last, yx, run, i;
-    char *seed, *p;
+    char *desc, *p;
     game_params params2real, *params2 = &params2real;
 
     /*
@@ -828,6 +832,31 @@ static char *new_game_seed(game_params *params, random_state *rs)
     }
 
     /*
+     * Store the rectangle data in the game_aux_info.
+     */
+    {
+       game_aux_info *ai = snew(game_aux_info);
+
+       ai->w = params->w;
+       ai->h = params->h;
+       ai->vedge = snewn(ai->w * ai->h, unsigned char);
+       ai->hedge = snewn(ai->w * ai->h, unsigned char);
+
+       for (y = 0; y < params->h; y++)
+           for (x = 1; x < params->w; x++) {
+               vedge(ai, x, y) =
+                   index(params, grid, x, y) != index(params, grid, x-1, y);
+           }
+       for (y = 1; y < params->h; y++)
+           for (x = 0; x < params->w; x++) {
+               hedge(ai, x, y) =
+                   index(params, grid, x, y) != index(params, grid, x, y-1);
+           }
+
+       *aux = ai;
+    }
+
+    /*
      * Place numbers.
      */
     numbers = snewn(params->w * params->h, int);
@@ -859,8 +888,8 @@ static char *new_game_seed(game_params *params, random_state *rs)
     display_grid(params, grid, numbers, FALSE);
 #endif
 
-    seed = snewn(11 * params->w * params->h, char);
-    p = seed;
+    desc = snewn(11 * params->w * params->h, char);
+    p = desc;
     run = 0;
     for (i = 0; i <= params->w * params->h; i++) {
         int n = (i < params->w * params->h ? numbers[i] : -1);
@@ -882,7 +911,7 @@ static char *new_game_seed(game_params *params, random_state *rs)
                  * bottom right, there's no point putting an
                  * unnecessary _ before or after it.
                  */
-                if (p > seed && n > 0)
+                if (p > desc && n > 0)
                     *p++ = '_';
             }
             if (n > 0)
@@ -895,26 +924,33 @@ static char *new_game_seed(game_params *params, random_state *rs)
     sfree(grid);
     sfree(numbers);
 
-    return seed;
+    return desc;
 }
 
-static char *validate_seed(game_params *params, char *seed)
+static void game_free_aux_info(game_aux_info *ai)
+{
+    sfree(ai->vedge);
+    sfree(ai->hedge);
+    sfree(ai);
+}
+
+static char *validate_desc(game_params *params, char *desc)
 {
     int area = params->w * params->h;
     int squares = 0;
 
-    while (*seed) {
-        int n = *seed++;
+    while (*desc) {
+        int n = *desc++;
         if (n >= 'a' && n <= 'z') {
             squares += n - 'a' + 1;
         } else if (n == '_') {
             /* do nothing */;
         } else if (n > '0' && n <= '9') {
             squares++;
-            while (*seed >= '0' && *seed <= '9')
-                seed++;
+            while (*desc >= '0' && *desc <= '9')
+                desc++;
         } else
-            return "Invalid character in game specification";
+            return "Invalid character in game description";
     }
 
     if (squares < area)
@@ -926,7 +962,7 @@ static char *validate_seed(game_params *params, char *seed)
     return NULL;
 }
 
-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 x, y, i, area;
@@ -939,11 +975,11 @@ static game_state *new_game(game_params *params, char *seed)
     state->grid = snewn(area, int);
     state->vedge = snewn(area, unsigned char);
     state->hedge = snewn(area, unsigned char);
-    state->completed = FALSE;
+    state->completed = state->cheated = FALSE;
 
     i = 0;
-    while (*seed) {
-        int n = *seed++;
+    while (*desc) {
+        int n = *desc++;
         if (n >= 'a' && n <= 'z') {
             int run = n - 'a' + 1;
             assert(i + run <= area);
@@ -953,9 +989,9 @@ static game_state *new_game(game_params *params, char *seed)
             /* do nothing */;
         } else if (n > '0' && n <= '9') {
             assert(i < area);
-            state->grid[i++] = atoi(seed-1);
-            while (*seed >= '0' && *seed <= '9')
-                seed++;
+            state->grid[i++] = atoi(desc-1);
+            while (*desc >= '0' && *desc <= '9')
+                desc++;
         } else {
             assert(!"We can't get here");
         }
@@ -981,6 +1017,7 @@ static game_state *dup_game(game_state *state)
     ret->grid = snewn(state->w * state->h, int);
 
     ret->completed = state->completed;
+    ret->cheated = state->cheated;
 
     memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int));
     memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char));
@@ -997,6 +1034,120 @@ static void free_game(game_state *state)
     sfree(state);
 }
 
+static game_state *solve_game(game_state *state, game_aux_info *ai,
+                             char **error)
+{
+    game_state *ret;
+
+    if (!ai) {
+       *error = "Solution not known for this puzzle";
+       return NULL;
+    }
+
+    assert(state->w == ai->w);
+    assert(state->h == ai->h);
+
+    ret = dup_game(state);
+    memcpy(ret->vedge, ai->vedge, ai->w * ai->h * sizeof(unsigned char));
+    memcpy(ret->hedge, ai->hedge, ai->w * ai->h * sizeof(unsigned char));
+    ret->cheated = TRUE;
+
+    return ret;
+}
+
+static char *game_text_format(game_state *state)
+{
+    char *ret, *p, buf[80];
+    int i, x, y, col, maxlen;
+
+    /*
+     * First determine the number of spaces required to display a
+     * number. We'll use at least two, because one looks a bit
+     * silly.
+     */
+    col = 2;
+    for (i = 0; i < state->w * state->h; i++) {
+       x = sprintf(buf, "%d", state->grid[i]);
+       if (col < x) col = x;
+    }
+
+    /*
+     * Now we know the exact total size of the grid we're going to
+     * produce: it's got 2*h+1 rows, each containing w lots of col,
+     * w+1 boundary characters and a trailing newline.
+     */
+    maxlen = (2*state->h+1) * (state->w * (col+1) + 2);
+
+    ret = snewn(maxlen+1, char);
+    p = ret;
+
+    for (y = 0; y <= 2*state->h; y++) {
+       for (x = 0; x <= 2*state->w; x++) {
+           if (x & y & 1) {
+               /*
+                * Display a number.
+                */
+               int v = grid(state, x/2, y/2);
+               if (v)
+                   sprintf(buf, "%*d", col, v);
+               else
+                   sprintf(buf, "%*s", col, "");
+               memcpy(p, buf, col);
+               p += col;
+           } else if (x & 1) {
+               /*
+                * Display a horizontal edge or nothing.
+                */
+               int h = (y==0 || y==2*state->h ? 1 :
+                        HRANGE(state, x/2, y/2) && hedge(state, x/2, y/2));
+               int i;
+               if (h)
+                   h = '-';
+               else
+                   h = ' ';
+               for (i = 0; i < col; i++)
+                   *p++ = h;
+           } else if (y & 1) {
+               /*
+                * Display a vertical edge or nothing.
+                */
+               int v = (x==0 || x==2*state->w ? 1 :
+                        VRANGE(state, x/2, y/2) && vedge(state, x/2, y/2));
+               if (v)
+                   *p++ = '|';
+               else
+                   *p++ = ' ';
+           } else {
+               /*
+                * Display a corner, or a vertical edge, or a
+                * horizontal edge, or nothing.
+                */
+               int hl = (y==0 || y==2*state->h ? 1 :
+                         HRANGE(state, (x-1)/2, y/2) && hedge(state, (x-1)/2, y/2));
+               int hr = (y==0 || y==2*state->h ? 1 :
+                         HRANGE(state, (x+1)/2, y/2) && hedge(state, (x+1)/2, y/2));
+               int vu = (x==0 || x==2*state->w ? 1 :
+                         VRANGE(state, x/2, (y-1)/2) && vedge(state, x/2, (y-1)/2));
+               int vd = (x==0 || x==2*state->w ? 1 :
+                         VRANGE(state, x/2, (y+1)/2) && vedge(state, x/2, (y+1)/2));
+               if (!hl && !hr && !vu && !vd)
+                   *p++ = ' ';
+               else if (hl && hr && !vu && !vd)
+                   *p++ = '-';
+               else if (!hl && !hr && vu && vd)
+                   *p++ = '|';
+               else
+                   *p++ = '+';
+           }
+       }
+       *p++ = '\n';
+    }
+
+    assert(p - ret == maxlen);
+    *p = '\0';
+    return ret;
+}
+
 static unsigned char *get_correct(game_state *state)
 {
     unsigned char *ret;
@@ -1585,7 +1736,8 @@ static float game_anim_length(game_state *oldstate,
 static float game_flash_length(game_state *oldstate,
                               game_state *newstate, int dir)
 {
-    if (!oldstate->completed && newstate->completed)
+    if (!oldstate->completed && newstate->completed &&
+       !oldstate->cheated && !newstate->cheated)
         return FLASH_TIME;
     return 0.0F;
 }
@@ -1600,21 +1752,23 @@ static int game_wants_statusbar(void)
 #endif
 
 const struct game thegame = {
-    "Rectangles", "games.rectangles", TRUE,
+    "Rectangles", "games.rectangles",
     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,