X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/ac511ec9c1cd34babf973d3615abd30169c11050..HEAD:/unfinished/slide.c diff --git a/unfinished/slide.c b/unfinished/slide.c index c9afe41..38ef4d0 100644 --- a/unfinished/slide.c +++ b/unfinished/slide.c @@ -5,23 +5,26 @@ /* * TODO: * - * - Solve function: - * * try to generate a solution when Solve is pressed - * + from the start, or from here? From here, I fear. - * + hence, not much point saving the solution in an aux - * string - * * Inertia-like method for telling the user the solution - * * standalone solver which draws diagrams - * - * - The dragging semantics are still subtly wrong in complex - * cases. - * * - Improve the generator. + * * actually, we seem to be mostly sensible already now. I + * want more choice over the type of main block and location + * of the exit/target, and I think I probably ought to give + * up on compactness and just bite the bullet and have the + * target area right outside the main wall, but mostly I + * think it's OK. + * * the move limit tends to make the game _slower_ to + * generate, which is odd. Perhaps investigate why. * - * - All the colours are a bit wishy-washy. _Some_ dark colours - * would surely not be excessive? Probably darken the tiles, - * the walls and the main block, and leave the target marker - * pale. + * - Improve the graphics. + * * All the colours are a bit wishy-washy. _Some_ dark + * colours would surely not be excessive? Probably darken + * the tiles, the walls and the main block, and leave the + * target marker pale. + * * The cattle grid effect is still disgusting. Think of + * something completely different. + * * The highlight for next-piece-to-move in the solver is + * excessive, and the shadow blends in too well with the + * piece lowlights. Adjust both. */ #include @@ -120,6 +123,7 @@ enum { struct game_params { int w, h; + int maxmoves; }; struct game_immutable_state { @@ -127,6 +131,12 @@ struct game_immutable_state { unsigned char *forcefield; }; +struct game_solution { + int nmoves; + int *moves; /* just like from solve_board() */ + int refcount; +}; + struct game_state { int w, h; unsigned char *board; @@ -135,24 +145,27 @@ struct game_state { int lastmoved, lastmoved_pos; /* for move counting */ int movecount; int completed; + int cheated; struct game_immutable_state *imm; + struct game_solution *soln; + int soln_index; }; static game_params *default_params(void) { game_params *ret = snew(game_params); - ret->w = 8; + ret->w = 7; ret->h = 6; + ret->maxmoves = 40; return ret; } static const struct game_params slide_presets[] = { - {6, 5}, - {7, 5}, - {7, 6}, - {8, 6}, + {7, 6, 25}, + {7, 6, -1}, + {8, 6, -1}, }; static int game_fetch_preset(int i, char **name, game_params **params) @@ -167,6 +180,10 @@ static int game_fetch_preset(int i, char **name, game_params **params) *ret = slide_presets[i]; sprintf(str, "%dx%d", ret->w, ret->h); + if (ret->maxmoves >= 0) + sprintf(str + strlen(str), ", max %d moves", ret->maxmoves); + else + sprintf(str + strlen(str), ", no move limit"); *name = dupstr(str); *params = ret; @@ -192,6 +209,15 @@ static void decode_params(game_params *params, char const *string) if (*string == 'x') { string++; params->h = atoi(string); + while (*string && isdigit((unsigned char)*string)) string++; + } + if (*string == 'm') { + string++; + params->maxmoves = atoi(string); + while (*string && isdigit((unsigned char)*string)) string++; + } else if (*string == 'u') { + string++; + params->maxmoves = -1; } } @@ -200,6 +226,10 @@ static char *encode_params(game_params *params, int full) char data[256]; sprintf(data, "%dx%d", params->w, params->h); + if (params->maxmoves >= 0) + sprintf(data + strlen(data), "m%d", params->maxmoves); + else + sprintf(data + strlen(data), "u"); return dupstr(data); } @@ -209,7 +239,7 @@ static config_item *game_configure(game_params *params) config_item *ret; char buf[80]; - ret = snewn(3, config_item); + ret = snewn(4, config_item); ret[0].name = "Width"; ret[0].type = C_STRING; @@ -223,11 +253,17 @@ static config_item *game_configure(game_params *params) ret[1].sval = dupstr(buf); ret[1].ival = 0; - ret[2].name = NULL; - ret[2].type = C_END; - ret[2].sval = NULL; + ret[2].name = "Solution length limit"; + ret[2].type = C_STRING; + sprintf(buf, "%d", params->maxmoves); + ret[2].sval = dupstr(buf); ret[2].ival = 0; + ret[3].name = NULL; + ret[3].type = C_END; + ret[3].sval = NULL; + ret[3].ival = 0; + return ret; } @@ -237,6 +273,7 @@ static game_params *custom_params(config_item *cfg) ret->w = atoi(cfg[0].sval); ret->h = atoi(cfg[1].sval); + ret->maxmoves = atoi(cfg[2].sval); return ret; } @@ -364,12 +401,18 @@ static struct board *newboard(int w, int h, unsigned char *data) /* * The actual solver. Given a board, attempt to find the minimum * length of move sequence which moves MAINANCHOR to (tx,ty), or - * -1 if no solution exists. Returns that minimum length, and - * (FIXME) optionally also writes out the actual moves into an - * as-yet-unprovided parameter. + * -1 if no solution exists. Returns that minimum length. + * + * Also, if `moveout' is provided, writes out the moves in the + * form of a sequence of pairs of integers indicating the source + * and destination points of the anchor of the moved piece in each + * move. Exactly twice as many integers are written as the number + * returned from solve_board(), and `moveout' receives an int * + * which is a pointer to a dynamically allocated array. */ static int solve_board(int w, int h, unsigned char *board, - unsigned char *forcefield, int tx, int ty) + unsigned char *forcefield, int tx, int ty, + int movelimit, int **moveout) { int wh = w*h; struct board *b, *b2, *b3; @@ -423,6 +466,14 @@ static int solve_board(int w, int h, unsigned char *board, while ((b = delpos234(queue, 0)) != NULL) { qlen--; + if (movelimit >= 0 && b->dist >= movelimit) { + /* + * The problem is not soluble in under `movelimit' + * moves, so we can quit right now. + */ + b2 = NULL; + goto done; + } if (b->dist != lastdist) { #ifdef SOLVER_DIAGNOSTICS printf("dist %d (%d)\n", b->dist, count234(sorted)); @@ -526,10 +577,49 @@ static int solve_board(int w, int h, unsigned char *board, done: - if (b2) + if (b2) { ret = b2->dist; - else + if (moveout) { + /* + * Now b2 represents the solved position. Backtrack to + * output the solution. + */ + *moveout = snewn(ret * 2, int); + j = ret * 2; + + while (b2->prev) { + int from = -1, to = -1; + + b = b2->prev; + + /* + * Scan b and b2 to find out which piece has + * moved. + */ + for (i = 0; i < wh; i++) { + if (ISANCHOR(b->data[i]) && !ISANCHOR(b2->data[i])) { + assert(from == -1); + from = i; + } else if (!ISANCHOR(b->data[i]) && ISANCHOR(b2->data[i])){ + assert(to == -1); + to = i; + } + } + + assert(from >= 0 && to >= 0); + assert(j >= 2); + (*moveout)[--j] = to; + (*moveout)[--j] = from; + + b2 = b; + } + assert(j == 0); + } + } else { ret = -1; /* no solution */ + if (moveout) + *moveout = NULL; + } freetree234(queue); @@ -552,14 +642,16 @@ static int solve_board(int w, int h, unsigned char *board, static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves, random_state *rs, unsigned char **rboard, - unsigned char **rforcefield) + unsigned char **rforcefield, int movelimit) { int wh = w*h; unsigned char *board, *board2, *forcefield; + unsigned char *tried_merge; + int *dsf; int *list, nlist, pos; int tx, ty; int i, j; - int moves; + int moves = 0; /* placate optimiser */ /* * Set up a board and fill it with singletons, except for a @@ -575,6 +667,10 @@ static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves, for (i = 0; i < h; i++) board[i*w] = board[i*w+(w-1)] = WALL; + tried_merge = snewn(wh * wh, unsigned char); + memset(tried_merge, 0, wh*wh); + dsf = snew_dsf(wh); + /* * Invent a main piece at one extreme. (FIXME: vary the * extreme, and the piece.) @@ -602,7 +698,7 @@ static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves, * See if the board is already soluble. */ if ((moves = solve_board(w, h, board, forcefield, - tx, ty)) >= 0) + tx, ty, movelimit, NULL)) >= 0) goto soluble; /* @@ -628,19 +724,11 @@ static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves, /* * Now go through that list in random order, trying to merge * the blocks on each side of each edge. - * - * FIXME: this seems to produce unpleasantly unbalanced - * results. Perhaps we'd do better if we always tried to - * combine the _smallest_ block with something? - * - * FIXME: also one reason it's slow might be because we aren't - * tracking which blocks we've already tried to merge, when - * another edge ends up linking the same ones. */ shuffle(list, nlist, sizeof(*list), rs); while (nlist > 0) { - int x1, y1, p1; - int x2, y2, p2; + int x1, y1, p1, c1; + int x2, y2, p2, c2; pos = list[--nlist]; y1 = y2 = pos / (w*2); @@ -653,6 +741,16 @@ static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves, p2 = y2*w+x2; /* + * Immediately abandon the attempt if we've already tried + * to merge the same pair of blocks along a different + * edge. + */ + c1 = dsf_canonify(dsf, p1); + c2 = dsf_canonify(dsf, p2); + if (tried_merge[c1 * wh + c2]) + continue; + + /* * In order to be mergeable, these two squares must each * either be, or belong to, a non-main anchor, and their * anchors must also be distinct. @@ -700,17 +798,32 @@ static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves, } while (p2 < wh && board[p2] != DIST(p2-i)); } } - j = solve_board(w, h, board, forcefield, tx, ty); + j = solve_board(w, h, board, forcefield, tx, ty, movelimit, NULL); if (j < 0) { /* * Didn't work. Revert the merge. */ memcpy(board, board2, wh); + tried_merge[c1 * wh + c2] = tried_merge[c2 * wh + c1] = TRUE; } else { + int c; + moves = j; + + dsf_merge(dsf, c1, c2); + c = dsf_canonify(dsf, c1); + for (i = 0; i < wh; i++) + tried_merge[c*wh+i] = (tried_merge[c1*wh+i] | + tried_merge[c2*wh+i]); + for (i = 0; i < wh; i++) + tried_merge[i*wh+c] = (tried_merge[i*wh+c1] | + tried_merge[i*wh+c2]); } } + sfree(dsf); + sfree(list); + sfree(tried_merge); sfree(board2); *rtx = tx; @@ -734,7 +847,7 @@ static char *new_game_desc(game_params *params, random_state *rs, int i; generate_board(params->w, params->h, &tx, &ty, &minmoves, rs, - &board, &forcefield); + &board, &forcefield, params->maxmoves); #ifdef GENERATOR_DIAGNOSTICS { char *t = board_text_format(params->w, params->h, board); @@ -772,10 +885,6 @@ static char *new_game_desc(game_params *params, random_state *rs, p += sprintf(p, ",%d,%d,%d", tx, ty, minmoves); ret = sresize(ret, p+1 - ret, char); - /* - * FIXME: generate an aux string - */ - sfree(board); sfree(forcefield); @@ -786,8 +895,8 @@ static char *validate_desc(game_params *params, char *desc) { int w = params->w, h = params->h, wh = w*h; int *active, *link; - int mains = 0, mpos = -1; - int i, j, tx, ty, minmoves; + int mains = 0; + int i, tx, ty, minmoves; char *ret; active = snewn(wh, int); @@ -832,13 +941,6 @@ static char *validate_desc(game_params *params, char *desc) } link[i] = i - dist; - for (j = i; j > 0; j = link[j]) - if (j == i-1 || j == i-w) - break; - if (j < 0) { - ret = "Disconnected piece in game description"; - goto done; - } active[i] = TRUE; active[link[i]] = FALSE; @@ -864,7 +966,6 @@ static char *validate_desc(game_params *params, char *desc) link[i] = -1; if (strchr("mM", c) != NULL) { mains++; - mpos = i; } i++; } @@ -975,6 +1076,10 @@ static game_state *new_game(midend *me, game_params *params, char *desc) else state->completed = -1; + state->cheated = FALSE; + state->soln = NULL; + state->soln_index = -1; + return state; } @@ -994,8 +1099,13 @@ static game_state *dup_game(game_state *state) ret->lastmoved_pos = state->lastmoved_pos; ret->movecount = state->movecount; ret->completed = state->completed; + ret->cheated = state->cheated; ret->imm = state->imm; ret->imm->refcount++; + ret->soln = state->soln; + ret->soln_index = state->soln_index; + if (ret->soln) + ret->soln->refcount++; return ret; } @@ -1006,6 +1116,10 @@ static void free_game(game_state *state) sfree(state->imm->forcefield); sfree(state->imm); } + if (state->soln && --state->soln->refcount <= 0) { + sfree(state->soln->moves); + sfree(state->soln); + } sfree(state->board); sfree(state); } @@ -1013,12 +1127,50 @@ static void free_game(game_state *state) static char *solve_game(game_state *state, game_state *currstate, char *aux, char **error) { + int *moves; + int nmoves; + int i; + char *ret, *p, sep; + /* - * FIXME: we have a solver, so use it - * - * FIXME: we should have generated an aux string, so use that + * Run the solver and attempt to find the shortest solution + * from the current position. */ - return NULL; + nmoves = solve_board(state->w, state->h, state->board, + state->imm->forcefield, state->tx, state->ty, + -1, &moves); + + if (nmoves < 0) { + *error = "Unable to find a solution to this puzzle"; + return NULL; + } + if (nmoves == 0) { + *error = "Puzzle is already solved"; + return NULL; + } + + /* + * Encode the resulting solution as a move string. + */ + ret = snewn(nmoves * 40, char); + p = ret; + sep = 'S'; + + for (i = 0; i < nmoves; i++) { + p += sprintf(p, "%c%d-%d", sep, moves[i*2], moves[i*2+1]); + sep = ','; + } + + sfree(moves); + assert(p - ret < nmoves * 40); + ret = sresize(ret, p+1 - ret, char); + + return ret; +} + +static int game_can_format_as_text_now(game_params *params) +{ + return TRUE; } static char *game_text_format(game_state *state) @@ -1090,8 +1242,8 @@ struct game_drawstate { int started; }; -static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, - int x, int y, int button) +static char *interpret_move(game_state *state, game_ui *ui, + const game_drawstate *ds, int x, int y, int button) { int w = state->w, h = state->h, wh = w*h; int tx, ty, i, j; @@ -1197,17 +1349,36 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, */ return ""; } else if (button == LEFT_DRAG && ui->dragging) { + int dist, distlimit, dx, dy, s, px, py; + tx = FROMCOORD(x); ty = FROMCOORD(y); tx -= ui->drag_offset_x; ty -= ui->drag_offset_y; - if (tx < 0 || tx >= w || ty < 0 || ty >= h || - !ui->reachable[ty*w+tx]) - return NULL; /* this drag has no effect */ - ui->drag_currpos = ty*w+tx; - return ""; + /* + * Now search outwards from (tx,ty), in order of Manhattan + * distance, until we find a reachable square. + */ + distlimit = w+tx; + distlimit = max(distlimit, h+ty); + distlimit = max(distlimit, tx); + distlimit = max(distlimit, ty); + for (dist = 0; dist <= distlimit; dist++) { + for (dx = -dist; dx <= dist; dx++) + for (s = -1; s <= +1; s += 2) { + dy = s * (dist - abs(dx)); + px = tx + dx; + py = ty + dy; + if (px >= 0 && px < w && py >= 0 && py < h && + ui->reachable[py*w+px]) { + ui->drag_currpos = py*w+px; + return ""; + } + } + } + return NULL; /* give up - this drag has no effect */ } else if (button == LEFT_RELEASE && ui->dragging) { char data[256], *str; @@ -1228,6 +1399,20 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, memset(ui->reachable, 0, wh); return str; + } else if (button == ' ' && state->soln) { + /* + * Make the next move in the stored solution. + */ + char data[256]; + int a1, a2; + + a1 = state->soln->moves[state->soln_index*2]; + a2 = state->soln->moves[state->soln_index*2+1]; + if (a1 == state->lastmoved_pos) + a1 = state->lastmoved; + + sprintf(data, "M%d-%d", a1, a2); + return dupstr(data); } return NULL; @@ -1277,12 +1462,59 @@ static game_state *execute_move(game_state *state, char *move) { int w = state->w, h = state->h /* , wh = w*h */; char c; - int a1, a2, n; + int a1, a2, n, movesize; game_state *ret = dup_game(state); while (*move) { c = *move; - if (c == 'M') { + if (c == 'S') { + /* + * This is a solve move, so we just set up a stored + * solution path. + */ + if (ret->soln && --ret->soln->refcount <= 0) { + sfree(ret->soln->moves); + sfree(ret->soln); + } + ret->soln = snew(struct game_solution); + ret->soln->nmoves = 0; + ret->soln->moves = NULL; + ret->soln->refcount = 1; + ret->soln_index = 0; + ret->cheated = TRUE; + + movesize = 0; + move++; + while (1) { + if (sscanf(move, "%d-%d%n", &a1, &a2, &n) != 2) { + free_game(ret); + return NULL; + } + + /* + * Special case: if the first move in the solution + * involves the piece for which we already have a + * partial stored move, adjust the source point to + * the original starting point of that piece. + */ + if (ret->soln->nmoves == 0 && a1 == ret->lastmoved) + a1 = ret->lastmoved_pos; + + if (ret->soln->nmoves >= movesize) { + movesize = (ret->soln->nmoves + 48) * 4 / 3; + ret->soln->moves = sresize(ret->soln->moves, + 2*movesize, int); + } + + ret->soln->moves[2*ret->soln->nmoves] = a1; + ret->soln->moves[2*ret->soln->nmoves+1] = a2; + ret->soln->nmoves++; + move += n; + if (*move != ',') + break; + move++; /* eat comma */ + } + } else if (c == 'M') { move++; if (sscanf(move, "%d-%d%n", &a1, &a2, &n) != 2 || !move_piece(w, h, state->board, ret->board, @@ -1310,6 +1542,36 @@ static game_state *execute_move(game_state *state, char *move) ret->lastmoved_pos = a1; ret->movecount++; } + + /* + * If we have a stored solution path, see if we've + * strayed from it or successfully made the next move + * along it. + */ + if (ret->soln && ret->lastmoved_pos >= 0) { + if (ret->lastmoved_pos != + ret->soln->moves[ret->soln_index*2]) { + /* strayed from the path */ + ret->soln->refcount--; + assert(ret->soln->refcount > 0); + /* `state' at least still exists */ + ret->soln = NULL; + ret->soln_index = -1; + } else if (ret->lastmoved == + ret->soln->moves[ret->soln_index*2+1]) { + /* advanced along the path */ + ret->soln_index++; + if (ret->soln_index >= ret->soln->nmoves) { + /* finished the path! */ + ret->soln->refcount--; + assert(ret->soln->refcount > 0); + /* `state' at least still exists */ + ret->soln = NULL; + ret->soln_index = -1; + } + } + } + if (ret->board[a2] == MAINANCHOR && a2 == ret->ty * w + ret->tx && ret->completed < 0) ret->completed = ret->movecount; @@ -1337,7 +1599,8 @@ static void game_compute_size(game_params *params, int tilesize, int *x, int *y) { /* fool the macros */ - struct dummy { int tilesize; } dummy = { tilesize }, *ds = &dummy; + struct dummy { int tilesize; } dummy, *ds = &dummy; + dummy.tilesize = tilesize; *x = params->w * TILESIZE + 2*BORDER; *y = params->h * TILESIZE + 2*BORDER; @@ -1436,14 +1699,20 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds) #define FG_MAIN 0x00000040UL #define FG_NORMAL 0x00000080UL #define FG_DRAGGING 0x00000100UL -#define FG_LBORDER 0x00000200UL -#define FG_TBORDER 0x00000400UL -#define FG_RBORDER 0x00000800UL -#define FG_BBORDER 0x00001000UL -#define FG_TLCORNER 0x00002000UL -#define FG_TRCORNER 0x00004000UL -#define FG_BLCORNER 0x00008000UL -#define FG_BRCORNER 0x00010000UL +#define FG_SHADOW 0x00000200UL +#define FG_SOLVEPIECE 0x00000400UL +#define FG_MAINPIECESH 11 +#define FG_SHADOWSH 19 + +#define PIECE_LBORDER 0x00000001UL +#define PIECE_TBORDER 0x00000002UL +#define PIECE_RBORDER 0x00000004UL +#define PIECE_BBORDER 0x00000008UL +#define PIECE_TLCORNER 0x00000010UL +#define PIECE_TRCORNER 0x00000020UL +#define PIECE_BLCORNER 0x00000040UL +#define PIECE_BRCORNER 0x00000080UL +#define PIECE_MASK 0x000000FFUL /* * Utility function. @@ -1455,7 +1724,8 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds) #define TYPE_TRCIRC 0x5000 #define TYPE_BLCIRC 0x6000 #define TYPE_BRCIRC 0x7000 -static void maybe_rect(drawing *dr, int x, int y, int w, int h, int coltype) +static void maybe_rect(drawing *dr, int x, int y, int w, int h, + int coltype, int col2) { int colour = coltype & COL_MASK, type = coltype & TYPE_MASK; @@ -1470,18 +1740,273 @@ static void maybe_rect(drawing *dr, int x, int y, int w, int h, int coltype) cx = x; cy = y; - assert(w == h); r = w-1; if (type & 0x1000) cx += r; if (type & 0x2000) cy += r; - draw_circle(dr, cx, cy, r, colour, colour); + + if (col2 == -1 || col2 == coltype) { + assert(w == h); + draw_circle(dr, cx, cy, r, colour, colour); + } else { + /* + * We aim to draw a quadrant of a circle in two + * different colours. We do this using Bresenham's + * algorithm directly, because the Puzzles drawing API + * doesn't have a draw-sector primitive. + */ + int bx, by, bd, bd2; + int xm = (type & 0x1000 ? -1 : +1); + int ym = (type & 0x2000 ? -1 : +1); + + by = r; + bx = 0; + bd = 0; + while (by >= bx) { + /* + * Plot the point. + */ + { + int x1 = cx+xm*bx, y1 = cy+ym*bx; + int x2, y2; + + x2 = cx+xm*by; y2 = y1; + draw_rect(dr, min(x1,x2), min(y1,y2), + abs(x1-x2)+1, abs(y1-y2)+1, colour); + x2 = x1; y2 = cy+ym*by; + draw_rect(dr, min(x1,x2), min(y1,y2), + abs(x1-x2)+1, abs(y1-y2)+1, col2); + } + + bd += 2*bx + 1; + bd2 = bd - (2*by - 1); + if (abs(bd2) < abs(bd)) { + bd = bd2; + by--; + } + bx++; + } + } unclip(dr); } } +static void draw_wallpart(drawing *dr, game_drawstate *ds, + int tx, int ty, unsigned long val, + int cl, int cc, int ch) +{ + int coords[6]; + + draw_rect(dr, tx, ty, TILESIZE, TILESIZE, cc); + if (val & PIECE_LBORDER) + draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, TILESIZE, + ch); + if (val & PIECE_RBORDER) + draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, + HIGHLIGHT_WIDTH, TILESIZE, cl); + if (val & PIECE_TBORDER) + draw_rect(dr, tx, ty, TILESIZE, HIGHLIGHT_WIDTH, ch); + if (val & PIECE_BBORDER) + draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, + TILESIZE, HIGHLIGHT_WIDTH, cl); + if (!((PIECE_BBORDER | PIECE_LBORDER) &~ val)) { + draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl); + clip(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH); + coords[0] = tx - 1; + coords[1] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[2] = tx + HIGHLIGHT_WIDTH; + coords[3] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[4] = tx - 1; + coords[5] = ty + TILESIZE; + draw_polygon(dr, coords, 3, ch, ch); + unclip(dr); + } else if (val & PIECE_BLCORNER) { + draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch); + clip(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH); + coords[0] = tx - 1; + coords[1] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[2] = tx + HIGHLIGHT_WIDTH; + coords[3] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[4] = tx - 1; + coords[5] = ty + TILESIZE; + draw_polygon(dr, coords, 3, cl, cl); + unclip(dr); + } + if (!((PIECE_TBORDER | PIECE_RBORDER) &~ val)) { + draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl); + clip(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH); + coords[0] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[1] = ty - 1; + coords[2] = tx + TILESIZE; + coords[3] = ty - 1; + coords[4] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[5] = ty + HIGHLIGHT_WIDTH; + draw_polygon(dr, coords, 3, ch, ch); + unclip(dr); + } else if (val & PIECE_TRCORNER) { + draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch); + clip(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH); + coords[0] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[1] = ty - 1; + coords[2] = tx + TILESIZE; + coords[3] = ty - 1; + coords[4] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1; + coords[5] = ty + HIGHLIGHT_WIDTH; + draw_polygon(dr, coords, 3, cl, cl); + unclip(dr); + } + if (val & PIECE_TLCORNER) + draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch); + if (val & PIECE_BRCORNER) + draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, + ty+TILESIZE-HIGHLIGHT_WIDTH, + HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl); +} + +static void draw_piecepart(drawing *dr, game_drawstate *ds, + int tx, int ty, unsigned long val, + int cl, int cc, int ch) +{ + int x[6], y[6]; + + /* + * Drawing the blocks is hellishly fiddly. The blocks don't + * stretch to the full size of the tile; there's a border + * around them of size BORDER_WIDTH. Then they have bevelled + * borders of size HIGHLIGHT_WIDTH, and also rounded corners. + * + * I tried for some time to find a clean and clever way to + * figure out what needed drawing from the corner and border + * flags, but in the end the cleanest way I could find was the + * following. We divide the grid square into 25 parts by + * ruling four horizontal and four vertical lines across it; + * those lines are at BORDER_WIDTH and BORDER_WIDTH + + * HIGHLIGHT_WIDTH from the top, from the bottom, from the + * left and from the right. Then we carefully consider each of + * the resulting 25 sections of square, and decide separately + * what needs to go in it based on the flags. In complicated + * cases there can be up to five possibilities affecting any + * given section (no corner or border flags, just the corner + * flag, one border flag, the other border flag, both border + * flags). So there's a lot of very fiddly logic here and all + * I could really think to do was give it my best shot and + * then test it and correct all the typos. Not fun to write, + * and I'm sure it isn't fun to read either, but it seems to + * work. + */ + + x[0] = tx; + x[1] = x[0] + BORDER_WIDTH; + x[2] = x[1] + HIGHLIGHT_WIDTH; + x[5] = tx + TILESIZE; + x[4] = x[5] - BORDER_WIDTH; + x[3] = x[4] - HIGHLIGHT_WIDTH; + + y[0] = ty; + y[1] = y[0] + BORDER_WIDTH; + y[2] = y[1] + HIGHLIGHT_WIDTH; + y[5] = ty + TILESIZE; + y[4] = y[5] - BORDER_WIDTH; + y[3] = y[4] - HIGHLIGHT_WIDTH; + +#define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q] + + maybe_rect(dr, RECT(0,0), + (val & (PIECE_TLCORNER | PIECE_TBORDER | + PIECE_LBORDER)) ? -1 : cc, -1); + maybe_rect(dr, RECT(1,0), + (val & PIECE_TLCORNER) ? ch : (val & PIECE_TBORDER) ? -1 : + (val & PIECE_LBORDER) ? ch : cc, -1); + maybe_rect(dr, RECT(2,0), + (val & PIECE_TBORDER) ? -1 : cc, -1); + maybe_rect(dr, RECT(3,0), + (val & PIECE_TRCORNER) ? cl : (val & PIECE_TBORDER) ? -1 : + (val & PIECE_RBORDER) ? cl : cc, -1); + maybe_rect(dr, RECT(4,0), + (val & (PIECE_TRCORNER | PIECE_TBORDER | + PIECE_RBORDER)) ? -1 : cc, -1); + maybe_rect(dr, RECT(0,1), + (val & PIECE_TLCORNER) ? ch : (val & PIECE_LBORDER) ? -1 : + (val & PIECE_TBORDER) ? ch : cc, -1); + maybe_rect(dr, RECT(1,1), + (val & PIECE_TLCORNER) ? cc : -1, -1); + maybe_rect(dr, RECT(1,1), + (val & PIECE_TLCORNER) ? ch | TYPE_TLCIRC : + !((PIECE_TBORDER | PIECE_LBORDER) &~ val) ? ch | TYPE_BRCIRC : + (val & (PIECE_TBORDER | PIECE_LBORDER)) ? ch : cc, -1); + maybe_rect(dr, RECT(2,1), + (val & PIECE_TBORDER) ? ch : cc, -1); + maybe_rect(dr, RECT(3,1), + (val & PIECE_TRCORNER) ? cc : -1, -1); + maybe_rect(dr, RECT(3,1), + (val & (PIECE_TBORDER | PIECE_RBORDER)) == PIECE_TBORDER ? ch : + (val & (PIECE_TBORDER | PIECE_RBORDER)) == PIECE_RBORDER ? cl : + !((PIECE_TBORDER|PIECE_RBORDER) &~ val) ? cl | TYPE_BLCIRC : + (val & PIECE_TRCORNER) ? cl | TYPE_TRCIRC : + cc, ch); + maybe_rect(dr, RECT(4,1), + (val & PIECE_TRCORNER) ? ch : (val & PIECE_RBORDER) ? -1 : + (val & PIECE_TBORDER) ? ch : cc, -1); + maybe_rect(dr, RECT(0,2), + (val & PIECE_LBORDER) ? -1 : cc, -1); + maybe_rect(dr, RECT(1,2), + (val & PIECE_LBORDER) ? ch : cc, -1); + maybe_rect(dr, RECT(2,2), + cc, -1); + maybe_rect(dr, RECT(3,2), + (val & PIECE_RBORDER) ? cl : cc, -1); + maybe_rect(dr, RECT(4,2), + (val & PIECE_RBORDER) ? -1 : cc, -1); + maybe_rect(dr, RECT(0,3), + (val & PIECE_BLCORNER) ? cl : (val & PIECE_LBORDER) ? -1 : + (val & PIECE_BBORDER) ? cl : cc, -1); + maybe_rect(dr, RECT(1,3), + (val & PIECE_BLCORNER) ? cc : -1, -1); + maybe_rect(dr, RECT(1,3), + (val & (PIECE_BBORDER | PIECE_LBORDER)) == PIECE_BBORDER ? cl : + (val & (PIECE_BBORDER | PIECE_LBORDER)) == PIECE_LBORDER ? ch : + !((PIECE_BBORDER|PIECE_LBORDER) &~ val) ? ch | TYPE_TRCIRC : + (val & PIECE_BLCORNER) ? ch | TYPE_BLCIRC : + cc, cl); + maybe_rect(dr, RECT(2,3), + (val & PIECE_BBORDER) ? cl : cc, -1); + maybe_rect(dr, RECT(3,3), + (val & PIECE_BRCORNER) ? cc : -1, -1); + maybe_rect(dr, RECT(3,3), + (val & PIECE_BRCORNER) ? cl | TYPE_BRCIRC : + !((PIECE_BBORDER | PIECE_RBORDER) &~ val) ? cl | TYPE_TLCIRC : + (val & (PIECE_BBORDER | PIECE_RBORDER)) ? cl : cc, -1); + maybe_rect(dr, RECT(4,3), + (val & PIECE_BRCORNER) ? cl : (val & PIECE_RBORDER) ? -1 : + (val & PIECE_BBORDER) ? cl : cc, -1); + maybe_rect(dr, RECT(0,4), + (val & (PIECE_BLCORNER | PIECE_BBORDER | + PIECE_LBORDER)) ? -1 : cc, -1); + maybe_rect(dr, RECT(1,4), + (val & PIECE_BLCORNER) ? ch : (val & PIECE_BBORDER) ? -1 : + (val & PIECE_LBORDER) ? ch : cc, -1); + maybe_rect(dr, RECT(2,4), + (val & PIECE_BBORDER) ? -1 : cc, -1); + maybe_rect(dr, RECT(3,4), + (val & PIECE_BRCORNER) ? cl : (val & PIECE_BBORDER) ? -1 : + (val & PIECE_RBORDER) ? cl : cc, -1); + maybe_rect(dr, RECT(4,4), + (val & (PIECE_BRCORNER | PIECE_BBORDER | + PIECE_RBORDER)) ? -1 : cc, -1); + +#undef RECT +} + static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, unsigned long val) { @@ -1518,6 +2043,15 @@ static void draw_tile(drawing *dr, game_drawstate *ds, } /* + * Draw the tile midground: a shadow of a block, for + * displaying partial solutions. + */ + if (val & FG_SHADOW) { + draw_piecepart(dr, ds, tx, ty, (val >> FG_SHADOWSH) & PIECE_MASK, + cl, cl, cl); + } + + /* * Draw the tile foreground, i.e. some section of a block or * wall. */ @@ -1530,33 +2064,9 @@ static void draw_tile(drawing *dr, game_drawstate *ds, else if (val & FLASH_HIGH) cc = ch; - draw_rect(dr, tx, ty, TILESIZE, TILESIZE, cc); - if (val & FG_LBORDER) - draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, TILESIZE, - ch); - if (val & FG_RBORDER) - draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, - HIGHLIGHT_WIDTH, TILESIZE, cl); - if (val & FG_TBORDER) - draw_rect(dr, tx, ty, TILESIZE, HIGHLIGHT_WIDTH, ch); - if (val & FG_BBORDER) - draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, - TILESIZE, HIGHLIGHT_WIDTH, cl); - if (!((FG_BBORDER | FG_LBORDER) &~ val)) - draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH, - HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cc); - if (!((FG_TBORDER | FG_RBORDER) &~ val)) - draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty, - HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cc); - if (val & FG_TLCORNER) - draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch); - if (val & FG_BRCORNER) - draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, - ty+TILESIZE-HIGHLIGHT_WIDTH, - HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl); + draw_wallpart(dr, ds, tx, ty, (val >> FG_MAINPIECESH) & PIECE_MASK, + cl, cc, ch); } else if (val & (FG_MAIN | FG_NORMAL)) { - int x[6], y[6]; - if (val & FG_DRAGGING) cc = (val & FG_MAIN ? COL_MAIN_DRAGGING : COL_DRAGGING); else @@ -1566,131 +2076,45 @@ static void draw_tile(drawing *dr, game_drawstate *ds, if (val & FLASH_LOW) cc = cl; - else if (val & FLASH_HIGH) + else if (val & (FLASH_HIGH | FG_SOLVEPIECE)) cc = ch; - /* - * Drawing the blocks is hellishly fiddly. The blocks - * don't stretch to the full size of the tile; there's a - * border around them of size BORDER_WIDTH. Then they have - * bevelled borders of size HIGHLIGHT_WIDTH, and also - * rounded corners. - * - * I tried for some time to find a clean and clever way to - * figure out what needed drawing from the corner and - * border flags, but in the end the cleanest way I could - * find was the following. We divide the grid square into - * 25 parts by ruling four horizontal and four vertical - * lines across it; those lines are at BORDER_WIDTH and - * BORDER_WIDTH+HIGHLIGHT_WIDTH from the top, from the - * bottom, from the left and from the right. Then we - * carefully consider each of the resulting 25 sections of - * square, and decide separately what needs to go in it - * based on the flags. In complicated cases there can be - * up to five possibilities affecting any given section - * (no corner or border flags, just the corner flag, one - * border flag, the other border flag, both border flags). - * So there's a lot of very fiddly logic here and all I - * could really think to do was give it my best shot and - * then test it and correct all the typos. Not fun to - * write, and I'm sure it isn't fun to read either, but it - * seems to work. - */ - - x[0] = tx; - x[1] = x[0] + BORDER_WIDTH; - x[2] = x[1] + HIGHLIGHT_WIDTH; - x[5] = tx + TILESIZE; - x[4] = x[5] - BORDER_WIDTH; - x[3] = x[4] - HIGHLIGHT_WIDTH; - - y[0] = ty; - y[1] = y[0] + BORDER_WIDTH; - y[2] = y[1] + HIGHLIGHT_WIDTH; - y[5] = ty + TILESIZE; - y[4] = y[5] - BORDER_WIDTH; - y[3] = y[4] - HIGHLIGHT_WIDTH; - -#define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q] - - maybe_rect(dr, RECT(0,0), - (val & (FG_TLCORNER | FG_TBORDER | FG_LBORDER)) ? -1 : cc); - maybe_rect(dr, RECT(1,0), - (val & FG_TLCORNER) ? ch : (val & FG_TBORDER) ? -1 : - (val & FG_LBORDER) ? ch : cc); - maybe_rect(dr, RECT(2,0), - (val & FG_TBORDER) ? -1 : cc); - maybe_rect(dr, RECT(3,0), - (val & FG_TRCORNER) ? cl : (val & FG_TBORDER) ? -1 : - (val & FG_RBORDER) ? cl : cc); - maybe_rect(dr, RECT(4,0), - (val & (FG_TRCORNER | FG_TBORDER | FG_RBORDER)) ? -1 : cc); - maybe_rect(dr, RECT(0,1), - (val & FG_TLCORNER) ? ch : (val & FG_LBORDER) ? -1 : - (val & FG_TBORDER) ? ch : cc); - maybe_rect(dr, RECT(1,1), - (val & FG_TLCORNER) ? cc : -1); - maybe_rect(dr, RECT(1,1), - (val & FG_TLCORNER) ? ch | TYPE_TLCIRC : - !((FG_TBORDER | FG_LBORDER) &~ val) ? ch | TYPE_BRCIRC : - (val & (FG_TBORDER | FG_LBORDER)) ? ch : cc); - maybe_rect(dr, RECT(2,1), - (val & FG_TBORDER) ? ch : cc); - maybe_rect(dr, RECT(3,1), - (val & (FG_TBORDER | FG_RBORDER)) == FG_TBORDER ? ch : - (val & (FG_TBORDER | FG_RBORDER)) == FG_RBORDER ? cl : - !((FG_TBORDER|FG_RBORDER) &~ val) ? cc | TYPE_BLCIRC : cc); - maybe_rect(dr, RECT(4,1), - (val & FG_TRCORNER) ? ch : (val & FG_RBORDER) ? -1 : - (val & FG_TBORDER) ? ch : cc); - maybe_rect(dr, RECT(0,2), - (val & FG_LBORDER) ? -1 : cc); - maybe_rect(dr, RECT(1,2), - (val & FG_LBORDER) ? ch : cc); - maybe_rect(dr, RECT(2,2), - cc); - maybe_rect(dr, RECT(3,2), - (val & FG_RBORDER) ? cl : cc); - maybe_rect(dr, RECT(4,2), - (val & FG_RBORDER) ? -1 : cc); - maybe_rect(dr, RECT(0,3), - (val & FG_BLCORNER) ? cl : (val & FG_LBORDER) ? -1 : - (val & FG_BBORDER) ? cl : cc); - maybe_rect(dr, RECT(1,3), - (val & (FG_BBORDER | FG_LBORDER)) == FG_BBORDER ? cl : - (val & (FG_BBORDER | FG_LBORDER)) == FG_LBORDER ? ch : - !((FG_BBORDER|FG_LBORDER) &~ val) ? cc | TYPE_TRCIRC : cc); - maybe_rect(dr, RECT(2,3), - (val & FG_BBORDER) ? cl : cc); - maybe_rect(dr, RECT(3,3), - (val & FG_BRCORNER) ? cc : -1); - maybe_rect(dr, RECT(3,3), - (val & FG_BRCORNER) ? cl | TYPE_BRCIRC : - !((FG_BBORDER | FG_RBORDER) &~ val) ? cl | TYPE_TLCIRC : - (val & (FG_BBORDER | FG_RBORDER)) ? cl : cc); - maybe_rect(dr, RECT(4,3), - (val & FG_BRCORNER) ? cl : (val & FG_RBORDER) ? -1 : - (val & FG_BBORDER) ? cl : cc); - maybe_rect(dr, RECT(0,4), - (val & (FG_BLCORNER | FG_BBORDER | FG_LBORDER)) ? -1 : cc); - maybe_rect(dr, RECT(1,4), - (val & FG_BLCORNER) ? ch : (val & FG_BBORDER) ? -1 : - (val & FG_LBORDER) ? ch : cc); - maybe_rect(dr, RECT(2,4), - (val & FG_BBORDER) ? -1 : cc); - maybe_rect(dr, RECT(3,4), - (val & FG_BRCORNER) ? cl : (val & FG_BBORDER) ? -1 : - (val & FG_RBORDER) ? cl : cc); - maybe_rect(dr, RECT(4,4), - (val & (FG_BRCORNER | FG_BBORDER | FG_RBORDER)) ? -1 : cc); - -#undef RECT - + draw_piecepart(dr, ds, tx, ty, (val >> FG_MAINPIECESH) & PIECE_MASK, + cl, cc, ch); } draw_update(dr, tx, ty, TILESIZE, TILESIZE); } +static unsigned long find_piecepart(int w, int h, int *dsf, int x, int y) +{ + int i = y*w+x; + int canon = dsf_canonify(dsf, i); + unsigned long val = 0; + + if (x == 0 || canon != dsf_canonify(dsf, i-1)) + val |= PIECE_LBORDER; + if (y== 0 || canon != dsf_canonify(dsf, i-w)) + val |= PIECE_TBORDER; + if (x == w-1 || canon != dsf_canonify(dsf, i+1)) + val |= PIECE_RBORDER; + if (y == h-1 || canon != dsf_canonify(dsf, i+w)) + val |= PIECE_BBORDER; + if (!(val & (PIECE_TBORDER | PIECE_LBORDER)) && + canon != dsf_canonify(dsf, i-1-w)) + val |= PIECE_TLCORNER; + if (!(val & (PIECE_TBORDER | PIECE_RBORDER)) && + canon != dsf_canonify(dsf, i+1-w)) + val |= PIECE_TRCORNER; + if (!(val & (PIECE_BBORDER | PIECE_LBORDER)) && + canon != dsf_canonify(dsf, i-1+w)) + val |= PIECE_BLCORNER; + if (!(val & (PIECE_BBORDER | PIECE_RBORDER)) && + canon != dsf_canonify(dsf, i+1+w)) + val |= PIECE_BRCORNER; + return val; +} + static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, game_state *state, int dir, game_ui *ui, float animtime, float flashtime) @@ -1698,7 +2122,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, int w = state->w, h = state->h, wh = w*h; unsigned char *board; int *dsf; - int x, y, mainanchor, mainpos, dragpos; + int x, y, mainanchor, mainpos, dragpos, solvepos, solvesrc, solvedst; if (!ds->started) { /* @@ -1725,6 +2149,16 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, assert(mpret); } + if (state->soln) { + solvesrc = state->soln->moves[state->soln_index*2]; + solvedst = state->soln->moves[state->soln_index*2+1]; + if (solvesrc == state->lastmoved_pos) + solvesrc = state->lastmoved; + if (solvesrc == ui->drag_anchor) + solvesrc = ui->drag_currpos; + } else + solvesrc = solvedst = -1; + /* * Build a dsf out of that board, so we can conveniently tell * which edges are connected and which aren't. @@ -1749,6 +2183,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, assert(mainanchor >= 0); mainpos = dsf_canonify(dsf, mainanchor); dragpos = ui->drag_currpos > 0 ? dsf_canonify(dsf, ui->drag_currpos) : -1; + solvepos = solvesrc >= 0 ? dsf_canonify(dsf, solvesrc) : -1; /* * Now we can construct the data about what we want to draw. @@ -1790,31 +2225,28 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, val |= FG_NORMAL; if (canon == dragpos) val |= FG_DRAGGING; + if (canon == solvepos) + val |= FG_SOLVEPIECE; /* * Now look around to see if other squares * belonging to the same block are adjacent to us. */ - if (x == 0 || canon != dsf_canonify(dsf, i-1)) - val |= FG_LBORDER; - if (y== 0 || canon != dsf_canonify(dsf, i-w)) - val |= FG_TBORDER; - if (x == w-1 || canon != dsf_canonify(dsf, i+1)) - val |= FG_RBORDER; - if (y == h-1 || canon != dsf_canonify(dsf, i+w)) - val |= FG_BBORDER; - if (!(val & (FG_TBORDER | FG_LBORDER)) && - canon != dsf_canonify(dsf, i-1-w)) - val |= FG_TLCORNER; - if (!(val & (FG_TBORDER | FG_RBORDER)) && - canon != dsf_canonify(dsf, i+1-w)) - val |= FG_TRCORNER; - if (!(val & (FG_BBORDER | FG_LBORDER)) && - canon != dsf_canonify(dsf, i-1+w)) - val |= FG_BLCORNER; - if (!(val & (FG_BBORDER | FG_RBORDER)) && - canon != dsf_canonify(dsf, i+1+w)) - val |= FG_BRCORNER; + val |= find_piecepart(w, h, dsf, x, y) << FG_MAINPIECESH; + } + + /* + * If we're in the middle of showing a solution, + * display a shadow piece for the target of the + * current move. + */ + if (solvepos >= 0) { + int si = i - solvedst + solvesrc; + if (si >= 0 && si < wh && dsf_canonify(dsf, si) == solvepos) { + val |= find_piecepart(w, h, dsf, + si % w, si / w) << FG_SHADOWSH; + val |= FG_SHADOW; + } } if (val != ds->grid[i]) { @@ -1829,13 +2261,12 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, { char statusbuf[256]; - /* - * FIXME: do something about auto-solve? - */ sprintf(statusbuf, "%sMoves: %d", - (state->completed >= 0 ? "COMPLETED! " : ""), + (state->completed >= 0 ? + (state->cheated ? "Auto-solved. " : "COMPLETED! ") : + (state->cheated ? "Auto-solver used. " : "")), (state->completed >= 0 ? state->completed : state->movecount)); - if (state->minmoves) + if (state->minmoves >= 0) sprintf(statusbuf+strlen(statusbuf), " (min %d)", state->minmoves); @@ -1861,6 +2292,11 @@ static float game_flash_length(game_state *oldstate, game_state *newstate, return 0.0F; } +static int game_status(game_state *state) +{ + return state->completed ? +1 : 0; +} + static int game_timing_state(game_state *state, game_ui *ui) { return TRUE; @@ -1875,7 +2311,7 @@ static void game_print(drawing *dr, game_state *state, int tilesize) } #ifdef COMBINED -#define thegame nullgame +#define thegame slide #endif const struct game thegame = { @@ -1893,8 +2329,8 @@ const struct game thegame = { new_game, dup_game, free_game, - FALSE, solve_game, /* FIXME */ - TRUE, game_text_format, + TRUE, solve_game, + TRUE, game_can_format_as_text_now, game_text_format, new_ui, free_ui, encode_ui, @@ -1909,8 +2345,98 @@ const struct game thegame = { game_redraw, game_anim_length, game_flash_length, + game_status, FALSE, FALSE, game_print_size, game_print, TRUE, /* wants_statusbar */ FALSE, game_timing_state, 0, /* flags */ }; + +#ifdef STANDALONE_SOLVER + +#include + +int main(int argc, char **argv) +{ + game_params *p; + game_state *s; + char *id = NULL, *desc, *err; + int count = FALSE; + int ret; + int *moves; + + while (--argc > 0) { + char *p = *++argv; + /* + if (!strcmp(p, "-v")) { + verbose = TRUE; + } else + */ + if (!strcmp(p, "-c")) { + count = TRUE; + } else if (*p == '-') { + fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); + return 1; + } else { + id = p; + } + } + + if (!id) { + fprintf(stderr, "usage: %s [-c | -v] \n", argv[0]); + return 1; + } + + desc = strchr(id, ':'); + if (!desc) { + fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); + return 1; + } + *desc++ = '\0'; + + p = default_params(); + decode_params(p, id); + err = validate_desc(p, desc); + if (err) { + fprintf(stderr, "%s: %s\n", argv[0], err); + return 1; + } + s = new_game(NULL, p, desc); + + ret = solve_board(s->w, s->h, s->board, s->imm->forcefield, + s->tx, s->ty, -1, &moves); + if (ret < 0) { + printf("No solution found\n"); + } else { + int index = 0; + if (count) { + printf("%d moves required\n", ret); + return 0; + } + while (1) { + int moveret; + char *text = board_text_format(s->w, s->h, s->board, + s->imm->forcefield); + game_state *s2; + + printf("position %d:\n%s", index, text); + + if (index >= ret) + break; + + s2 = dup_game(s); + moveret = move_piece(s->w, s->h, s->board, + s2->board, s->imm->forcefield, + moves[index*2], moves[index*2+1]); + assert(moveret); + + free_game(s); + s = s2; + index++; + } + } + + return 0; +} + +#endif