Black Box: fix "reveal" button location, explain what's meant by the
[sgt/puzzles] / pegs.c
diff --git a/pegs.c b/pegs.c
index 0cd740f..5bb5de2 100644 (file)
--- a/pegs.c
+++ b/pegs.c
@@ -42,6 +42,8 @@ static char const *const pegs_titletypes[] = { TYPELIST(TITLE) };
 static char const *const pegs_lowertypes[] = { TYPELIST(LOWER) };
 #define TYPECONFIG TYPELIST(CONFIG)
 
+#define FLASH_FRAME 0.13F
+
 struct game_params {
     int w, h;
     int type;
@@ -49,6 +51,7 @@ struct game_params {
 
 struct game_state {
     int w, h;
+    int completed;
     unsigned char *grid;
 };
 
@@ -175,9 +178,9 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
-    if (params->w <= 3 || params->h <= 3)
+    if (full && (params->w <= 3 || params->h <= 3))
        return "Width and height must both be greater than three";
 
     /*
@@ -186,7 +189,7 @@ static char *validate_params(game_params *params)
      * soluble. For the moment, therefore, I'm going to disallow
      * them at any size other than the standard one.
      */
-    if (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON) {
+    if (full && (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON)) {
        if (params->w != 7 || params->h != 7)
            return "This board type is only supported at 7x7";
     }
@@ -527,9 +530,7 @@ static char *new_game_desc(game_params *params, random_state *rs,
                  case TYPE_OCTAGON:
                    cx = abs(x - w/2);
                    cy = abs(y - h/2);
-                   if (cx == 0 && cy == 0)
-                       v = GRID_HOLE;
-                   else if (cx + cy > 1 + max(w,h)/2)
+                   if (cx + cy > 1 + max(w,h)/2)
                        v = GRID_OBST;
                    else
                        v = GRID_PEG;
@@ -537,6 +538,107 @@ static char *new_game_desc(game_params *params, random_state *rs,
                }
                grid[y*w+x] = v;
            }
+
+       if (params->type == TYPE_OCTAGON) {
+           /*
+            * The octagonal (European) solitaire layout is
+            * actually _insoluble_ with the starting hole at the
+            * centre. Here's a proof:
+            * 
+            * Colour the squares of the board diagonally in
+            * stripes of three different colours, which I'll call
+            * A, B and C. So the board looks like this:
+            * 
+            *     A B C
+            *   A B C A B
+            * A B C A B C A
+            * B C A B C A B
+            * C A B C A B C
+            *   B C A B C
+            *     A B C
+            * 
+            * Suppose we keep running track of the number of pegs
+            * occuping each colour of square. This colouring has
+            * the property that any valid move whatsoever changes
+            * all three of those counts by one (two of them go
+            * down and one goes up), which means that the _parity_
+            * of every count flips on every move.
+            * 
+            * If the centre square starts off unoccupied, then
+            * there are twelve pegs on each colour and all three
+            * counts start off even; therefore, after 35 moves all
+            * three counts would have to be odd, which isn't
+            * possible if there's only one peg left. []
+            * 
+            * This proof works just as well if the starting hole
+            * is _any_ of the thirteen positions labelled B. Also,
+            * we can stripe the board in the opposite direction
+            * and rule out any square labelled B in that colouring
+            * as well. This leaves:
+            * 
+            *     Y n Y
+            *   n n Y n n
+            * Y n n Y n n Y
+            * n Y Y n Y Y n
+            * Y n n Y n n Y
+            *   n n Y n n
+            *     Y n Y
+            * 
+            * where the ns are squares we've proved insoluble, and
+            * the Ys are the ones remaining.
+            * 
+            * That doesn't prove all those starting positions to
+            * be soluble, of course; they're merely the ones we
+            * _haven't_ proved to be impossible. Nevertheless, it
+            * turns out that they are all soluble, so when the
+            * user requests an Octagon board the simplest thing is
+            * to pick one of these at random.
+            * 
+            * Rather than picking equiprobably from those twelve
+            * positions, we'll pick equiprobably from the three
+            * equivalence classes
+            */
+           switch (random_upto(rs, 3)) {
+             case 0:
+               /* Remove a random corner piece. */
+               {
+                   int dx, dy;
+
+                   dx = random_upto(rs, 2) * 2 - 1;   /* +1 or -1 */
+                   dy = random_upto(rs, 2) * 2 - 1;   /* +1 or -1 */
+                   if (random_upto(rs, 2))
+                       dy *= 3;
+                   else
+                       dx *= 3;
+                   grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
+               }
+               break;
+             case 1:
+               /* Remove a random piece two from the centre. */
+               {
+                   int dx, dy;
+                   dx = 2 * (random_upto(rs, 2) * 2 - 1);
+                   if (random_upto(rs, 2))
+                       dy = 0;
+                   else
+                       dy = dx, dx = 0;
+                   grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
+               }
+               break;
+             default /* case 2 */:
+               /* Remove a random piece one from the centre. */
+               {
+                   int dx, dy;
+                   dx = random_upto(rs, 2) * 2 - 1;
+                   if (random_upto(rs, 2))
+                       dy = 0;
+                   else
+                       dy = dx, dx = 0;
+                   grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
+               }
+               break;
+           }
+       }
     }
 
     /*
@@ -574,6 +676,7 @@ static game_state *new_game(midend_data *me, game_params *params, char *desc)
 
     state->w = w;
     state->h = h;
+    state->completed = 0;
     state->grid = snewn(w*h, unsigned char);
     for (i = 0; i < w*h; i++)
        state->grid[i] = (desc[i] == 'P' ? GRID_PEG :
@@ -589,6 +692,7 @@ static game_state *dup_game(game_state *state)
 
     ret->w = state->w;
     ret->h = state->h;
+    ret->completed = state->completed;
     ret->grid = snewn(w*h, unsigned char);
     memcpy(ret->grid, state->grid, w*h);
 
@@ -682,6 +786,7 @@ struct game_drawstate {
     int w, h;
     unsigned char *grid;
     int started;
+    int bgcolour;
 };
 
 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
@@ -789,6 +894,21 @@ static game_state *execute_move(game_state *state, char *move)
        ret->grid[my*w+mx] = GRID_HOLE;
        ret->grid[ty*w+tx] = GRID_PEG;
 
+        /*
+         * Opinion varies on whether getting to a single peg counts as
+         * completing the game, or whether that peg has to be at a
+         * specific location (central in the classic cross game, for
+         * instance). For now we take the former, rather lax position.
+         */
+        if (!ret->completed) {
+            int count = 0, i;
+            for (i = 0; i < w*h; i++)
+                if (ret->grid[i] == GRID_PEG)
+                    count++;
+            if (count == 1)
+                ret->completed = 1;
+        }
+
        return ret;
     }
     return NULL;
@@ -824,28 +944,8 @@ static void game_set_size(game_drawstate *ds, game_params *params,
 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
-    int i;
-    float max;
-
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
-
-    /*
-     * Drop the background colour so that the highlight is
-     * noticeably brighter than it while still being under 1.
-     */
-    max = ret[COL_BACKGROUND*3];
-    for (i = 1; i < 3; i++)
-        if (ret[COL_BACKGROUND*3+i] > max)
-            max = ret[COL_BACKGROUND*3+i];
-    if (max * 1.2F > 1.0F) {
-        for (i = 0; i < 3; i++)
-            ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
-    }
 
-    for (i = 0; i < 3; i++) {
-        ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
-        ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
-    }
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
 
     ret[COL_PEG * 3 + 0] = 0.0F;
     ret[COL_PEG * 3 + 1] = 0.0F;
@@ -873,6 +973,7 @@ static game_drawstate *game_new_drawstate(game_state *state)
     memset(ds->grid, 255, w*h);
 
     ds->started = FALSE;
+    ds->bgcolour = -1;
 
     return ds;
 }
@@ -886,10 +987,10 @@ static void game_free_drawstate(game_drawstate *ds)
 }
 
 static void draw_tile(frontend *fe, game_drawstate *ds,
-                     int x, int y, int v, int erasebg)
+                     int x, int y, int v, int bgcolour)
 {
-    if (erasebg) {
-       draw_rect(fe, x, y, TILESIZE, TILESIZE, COL_BACKGROUND);
+    if (bgcolour >= 0) {
+       draw_rect(fe, x, y, TILESIZE, TILESIZE, bgcolour);
     }
 
     if (v == GRID_HOLE) {
@@ -909,6 +1010,13 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
 {
     int w = state->w, h = state->h;
     int x, y;
+    int bgcolour;
+
+    if (flashtime > 0) {
+        int frame = (int)(flashtime / FLASH_FRAME);
+        bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
+    } else
+        bgcolour = COL_BACKGROUND;
 
     /*
      * Erase the sprite currently being dragged, if any.
@@ -1025,8 +1133,10 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
             */
            if (ui->dragging && ui->sx == x && ui->sy == y && v == GRID_PEG)
                v = GRID_HOLE;
-           if (v != ds->grid[y*w+x] && v != GRID_OBST) {
-               draw_tile(fe, ds, COORD(x), COORD(y), v, TRUE);
+           if (v != GRID_OBST &&
+                (bgcolour != ds->bgcolour || /* always redraw when flashing */
+                 v != ds->grid[y*w+x])) {
+               draw_tile(fe, ds, COORD(x), COORD(y), v, bgcolour);
            }
        }
 
@@ -1038,8 +1148,10 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
        ds->dragx = ui->dx - TILESIZE/2;
        ds->dragy = ui->dy - TILESIZE/2;
        blitter_save(fe, ds->drag_background, ds->dragx, ds->dragy);
-       draw_tile(fe, ds, ds->dragx, ds->dragy, GRID_PEG, FALSE);
+       draw_tile(fe, ds, ds->dragx, ds->dragy, GRID_PEG, -1);
     }
+
+    ds->bgcolour = bgcolour;
 }
 
 static float game_anim_length(game_state *oldstate, game_state *newstate,
@@ -1051,7 +1163,10 @@ static float game_anim_length(game_state *oldstate, game_state *newstate,
 static float game_flash_length(game_state *oldstate, game_state *newstate,
                               int dir, game_ui *ui)
 {
-    return 0.0F;
+    if (!oldstate->completed && newstate->completed)
+        return 2 * FLASH_FRAME;
+    else
+        return 0.0F;
 }
 
 static int game_wants_statusbar(void)
@@ -1059,7 +1174,7 @@ static int game_wants_statusbar(void)
     return FALSE;
 }
 
-static int game_timing_state(game_state *state)
+static int game_timing_state(game_state *state, game_ui *ui)
 {
     return TRUE;
 }