X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/937a9efffe3be5ecb6c6b9fb6596644d05aed3d9..055b9cd27af52f30534c77af067a98e5ca9c4461:/pegs.c diff --git a/pegs.c b/pegs.c index 2a03902..d1addd1 100644 --- a/pegs.c +++ b/pegs.c @@ -530,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; @@ -540,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; + } + } } /* @@ -569,7 +668,7 @@ static char *validate_desc(game_params *params, char *desc) return NULL; } -static game_state *new_game(midend_data *me, game_params *params, char *desc) +static game_state *new_game(midend *me, game_params *params, char *desc) { int w = params->w, h = params->h; game_state *state = snew(game_state); @@ -770,7 +869,7 @@ static game_state *execute_move(game_state *state, char *move) int sx, sy, tx, ty; game_state *ret; - if (sscanf(move, "%d,%d-%d,%d", &sx, &sy, &tx, &ty)) { + if (sscanf(move, "%d,%d-%d,%d", &sx, &sy, &tx, &ty) == 4) { int mx, my, dx, dy; if (sx < 0 || sx >= w || sy < 0 || sy >= h) @@ -830,19 +929,18 @@ static void game_compute_size(game_params *params, int tilesize, *y = TILESIZE * params->h + 2 * BORDER; } -static void game_set_size(game_drawstate *ds, game_params *params, - int tilesize) +static void game_set_size(drawing *dr, game_drawstate *ds, + game_params *params, int tilesize) { ds->tilesize = tilesize; assert(TILESIZE > 0); - if (ds->drag_background) - blitter_free(ds->drag_background); - ds->drag_background = blitter_new(TILESIZE, TILESIZE); + assert(!ds->drag_background); /* set_size is never called twice */ + ds->drag_background = blitter_new(dr, TILESIZE, TILESIZE); } -static float *game_colours(frontend *fe, game_state *state, int *ncolours) +static float *game_colours(frontend *fe, int *ncolours) { float *ret = snewn(3 * NCOLOURS, float); @@ -856,7 +954,7 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours) return ret; } -static game_drawstate *game_new_drawstate(game_state *state) +static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) { int w = state->w, h = state->h; struct game_drawstate *ds = snew(struct game_drawstate); @@ -879,33 +977,33 @@ static game_drawstate *game_new_drawstate(game_state *state) return ds; } -static void game_free_drawstate(game_drawstate *ds) +static void game_free_drawstate(drawing *dr, game_drawstate *ds) { if (ds->drag_background) - blitter_free(ds->drag_background); + blitter_free(dr, ds->drag_background); sfree(ds->grid); sfree(ds); } -static void draw_tile(frontend *fe, game_drawstate *ds, +static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, int v, int bgcolour) { if (bgcolour >= 0) { - draw_rect(fe, x, y, TILESIZE, TILESIZE, bgcolour); + draw_rect(dr, x, y, TILESIZE, TILESIZE, bgcolour); } if (v == GRID_HOLE) { - draw_circle(fe, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4, + draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4, COL_LOWLIGHT, COL_LOWLIGHT); } else if (v == GRID_PEG) { - draw_circle(fe, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3, + draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3, COL_PEG, COL_PEG); } - draw_update(fe, x, y, TILESIZE, TILESIZE); + draw_update(dr, x, y, TILESIZE, TILESIZE); } -static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, +static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, game_state *state, int dir, game_ui *ui, float animtime, float flashtime) { @@ -924,13 +1022,13 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, */ if (ds->dragging) { assert(ds->drag_background); - blitter_load(fe, ds->drag_background, ds->dragx, ds->dragy); - draw_update(fe, ds->dragx, ds->dragy, TILESIZE, TILESIZE); + blitter_load(dr, ds->drag_background, ds->dragx, ds->dragy); + draw_update(dr, ds->dragx, ds->dragy, TILESIZE, TILESIZE); ds->dragging = FALSE; } if (!ds->started) { - draw_rect(fe, 0, 0, + draw_rect(dr, 0, 0, TILESIZE * state->w + 2 * BORDER, TILESIZE * state->h + 2 * BORDER, COL_BACKGROUND); @@ -951,10 +1049,10 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, coords[3] = COORD(y+1) + HIGHLIGHT_WIDTH - 1; coords[4] = COORD(x) - HIGHLIGHT_WIDTH; coords[5] = COORD(y) - HIGHLIGHT_WIDTH; - draw_polygon(fe, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); + draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); coords[4] = COORD(x+1) + HIGHLIGHT_WIDTH - 1; coords[5] = COORD(y+1) + HIGHLIGHT_WIDTH - 1; - draw_polygon(fe, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT); + draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT); } for (y = 0; y < h; y++) for (x = 0; x < w; x++) @@ -963,11 +1061,11 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, * Second pass: draw everything but the two * diagonal corners. */ - draw_rect(fe, COORD(x) - HIGHLIGHT_WIDTH, + draw_rect(dr, COORD(x) - HIGHLIGHT_WIDTH, COORD(y) - HIGHLIGHT_WIDTH, TILESIZE + HIGHLIGHT_WIDTH, TILESIZE + HIGHLIGHT_WIDTH, COL_HIGHLIGHT); - draw_rect(fe, COORD(x), + draw_rect(dr, COORD(x), COORD(y), TILESIZE + HIGHLIGHT_WIDTH, TILESIZE + HIGHLIGHT_WIDTH, COL_LOWLIGHT); @@ -995,7 +1093,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, coords[5] = coords[3] - HIGHLIGHT_WIDTH * (dx-sn*dy); coords[6] = coords[0] + HIGHLIGHT_WIDTH * (dy+sn*dx); coords[7] = coords[1] + HIGHLIGHT_WIDTH * (dx+sn*dy); - draw_polygon(fe, coords, 4, c, c); + draw_polygon(dr, coords, 4, c, c); } } } @@ -1006,7 +1104,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, * Second pass: draw everything but the two * diagonal corners. */ - draw_rect(fe, COORD(x), + draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE, COL_BACKGROUND); @@ -1014,7 +1112,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, ds->started = TRUE; - draw_update(fe, 0, 0, + draw_update(dr, 0, 0, TILESIZE * state->w + 2 * BORDER, TILESIZE * state->h + 2 * BORDER); } @@ -1037,7 +1135,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, 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); + draw_tile(dr, ds, COORD(x), COORD(y), v, bgcolour); } } @@ -1048,8 +1146,8 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, ds->dragging = TRUE; 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, -1); + blitter_save(dr, ds->drag_background, ds->dragx, ds->dragy); + draw_tile(dr, ds, ds->dragx, ds->dragy, GRID_PEG, -1); } ds->bgcolour = bgcolour; @@ -1070,14 +1168,17 @@ static float game_flash_length(game_state *oldstate, game_state *newstate, return 0.0F; } -static int game_wants_statusbar(void) +static int game_timing_state(game_state *state, game_ui *ui) { - return FALSE; + return TRUE; } -static int game_timing_state(game_state *state) +static void game_print_size(game_params *params, float *x, float *y) +{ +} + +static void game_print(drawing *dr, game_state *state, int tilesize) { - return TRUE; } #ifdef COMBINED @@ -1085,7 +1186,7 @@ static int game_timing_state(game_state *state) #endif const struct game thegame = { - "Pegs", "games.pegs", + "Pegs", "games.pegs", "pegs", default_params, game_fetch_preset, decode_params, @@ -1115,7 +1216,8 @@ const struct game thegame = { game_redraw, game_anim_length, game_flash_length, - game_wants_statusbar, + FALSE, FALSE, game_print_size, game_print, + FALSE, /* wants_statusbar */ FALSE, game_timing_state, - 0, /* mouse_priorities */ + 0, /* flags */ };