2 * fifteen.c: standard 15-puzzle.
14 #define PREFERRED_TILE_SIZE 48
15 #define TILE_SIZE (ds->tilesize)
16 #define BORDER (TILE_SIZE / 2)
17 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
18 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
19 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
21 #define ANIM_TIME 0.13F
22 #define FLASH_FRAME 0.13F
24 #define X(state, i) ( (i) % (state)->w )
25 #define Y(state, i) ( (i) / (state)->w )
26 #define C(state, x, y) ( (y) * (state)->w + (x) )
28 #define PARITY_P(params, gap) (((X((params), (gap)) - ((params)->w - 1)) ^ \
29 (Y((params), (gap)) - ((params)->h - 1)) ^ \
30 (((params)->w * (params)->h) + 1)) & 1)
31 #define PARITY_S(state) PARITY_P((state), ((state)->gap_pos))
50 int used_solve; /* used to suppress completion flash */
54 static game_params *default_params(void)
56 game_params *ret = snew(game_params);
63 static int game_fetch_preset(int i, char **name, game_params **params)
66 *params = default_params();
67 *name = dupstr("4x4");
73 static void free_params(game_params *params)
78 static game_params *dup_params(const game_params *params)
80 game_params *ret = snew(game_params);
81 *ret = *params; /* structure copy */
85 static void decode_params(game_params *ret, char const *string)
87 ret->w = ret->h = atoi(string);
88 while (*string && isdigit((unsigned char)*string)) string++;
91 ret->h = atoi(string);
95 static char *encode_params(const game_params *params, int full)
99 sprintf(data, "%dx%d", params->w, params->h);
104 static config_item *game_configure(const game_params *params)
109 ret = snewn(3, config_item);
111 ret[0].name = "Width";
112 ret[0].type = C_STRING;
113 sprintf(buf, "%d", params->w);
114 ret[0].u.string.sval = dupstr(buf);
116 ret[1].name = "Height";
117 ret[1].type = C_STRING;
118 sprintf(buf, "%d", params->h);
119 ret[1].u.string.sval = dupstr(buf);
127 static game_params *custom_params(const config_item *cfg)
129 game_params *ret = snew(game_params);
131 ret->w = atoi(cfg[0].u.string.sval);
132 ret->h = atoi(cfg[1].u.string.sval);
137 static const char *validate_params(const game_params *params, int full)
139 if (params->w < 2 || params->h < 2)
140 return "Width and height must both be at least two";
145 static int perm_parity(int *perm, int n)
151 for (i = 0; i < n-1; i++)
152 for (j = i+1; j < n; j++)
153 if (perm[i] > perm[j])
159 static char *new_game_desc(const game_params *params, random_state *rs,
160 char **aux, int interactive)
163 int x1, x2, p1, p2, parity;
168 n = params->w * params->h;
170 tiles = snewn(n, int);
171 used = snewn(n, int);
173 for (i = 0; i < n; i++) {
178 gap = random_upto(rs, n);
183 * Place everything else except the last two tiles.
185 for (x = 0, i = n-1; i > 2; i--) {
186 int k = random_upto(rs, i);
189 for (j = 0; j < n; j++)
190 if (!used[j] && (k-- == 0))
193 assert(j < n && !used[j]);
196 while (tiles[x] >= 0)
203 * Find the last two locations, and the last two pieces.
205 while (tiles[x] >= 0)
210 while (tiles[x] >= 0)
215 for (i = 0; i < n; i++)
219 for (i = p1+1; i < n; i++)
225 * Determine the required parity of the overall permutation.
226 * This is the XOR of:
228 * - The chessboard parity ((x^y)&1) of the gap square. The
229 * bottom right counts as even.
231 * - The parity of n. (The target permutation is 1,...,n-1,0
232 * rather than 0,...,n-1; this is a cyclic permutation of
233 * the starting point and hence is odd iff n is even.)
235 parity = PARITY_P(params, gap);
238 * Try the last two tiles one way round. If that fails, swap
243 if (perm_parity(tiles, n) != parity) {
246 assert(perm_parity(tiles, n) == parity);
250 * Now construct the game description, by describing the tile
251 * array as a simple sequence of comma-separated integers.
255 for (i = 0; i < n; i++) {
259 k = sprintf(buf, "%d,", tiles[i]);
261 ret = sresize(ret, retlen + k + 1, char);
262 strcpy(ret + retlen, buf);
265 ret[retlen-1] = '\0'; /* delete last comma */
273 static const char *validate_desc(const game_params *params, const char *desc)
280 area = params->w * params->h;
284 used = snewn(area, int);
285 for (i = 0; i < area; i++)
288 for (i = 0; i < area; i++) {
292 if (*p < '0' || *p > '9') {
293 err = "Not enough numbers in string";
296 while (*p >= '0' && *p <= '9')
298 if (i < area-1 && *p != ',') {
299 err = "Expected comma after number";
302 else if (i == area-1 && *p) {
303 err = "Excess junk at end of string";
307 if (n < 0 || n >= area) {
308 err = "Number out of range";
312 err = "Number used twice";
317 if (*p) p++; /* eat comma */
325 static game_state *new_game(midend *me, const game_params *params,
328 game_state *state = snew(game_state);
332 state->w = params->w;
333 state->h = params->h;
334 state->n = params->w * params->h;
335 state->tiles = snewn(state->n, int);
340 for (i = 0; i < state->n; i++) {
342 state->tiles[i] = atoi(p);
343 if (state->tiles[i] == 0)
345 while (*p && *p != ',')
347 if (*p) p++; /* eat comma */
350 assert(state->tiles[state->gap_pos] == 0);
352 state->completed = state->movecount = 0;
353 state->used_solve = FALSE;
358 static game_state *dup_game(const game_state *state)
360 game_state *ret = snew(game_state);
365 ret->tiles = snewn(state->w * state->h, int);
366 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
367 ret->gap_pos = state->gap_pos;
368 ret->completed = state->completed;
369 ret->movecount = state->movecount;
370 ret->used_solve = state->used_solve;
375 static void free_game(game_state *state)
381 static char *solve_game(const game_state *state, const game_state *currstate,
382 const char *aux, const char **error)
387 static int game_can_format_as_text_now(const game_params *params)
392 static char *game_text_format(const game_state *state)
394 char *ret, *p, buf[80];
395 int x, y, col, maxlen;
398 * First work out how many characters we need to display each
401 col = sprintf(buf, "%d", state->n-1);
404 * Now we know the exact total size of the grid we're going to
405 * produce: it's got h rows, each containing w lots of col, w-1
406 * spaces and a trailing newline.
408 maxlen = state->h * state->w * (col+1);
410 ret = snewn(maxlen+1, char);
413 for (y = 0; y < state->h; y++) {
414 for (x = 0; x < state->w; x++) {
415 int v = state->tiles[state->w*y+x];
417 sprintf(buf, "%*s", col, "");
419 sprintf(buf, "%*d", col, v);
429 assert(p - ret == maxlen);
434 static game_ui *new_ui(const game_state *state)
439 static void free_ui(game_ui *ui)
443 static char *encode_ui(const game_ui *ui)
448 static void decode_ui(game_ui *ui, const char *encoding)
452 static void game_changed_state(game_ui *ui, const game_state *oldstate,
453 const game_state *newstate)
457 struct game_drawstate {
464 static int flip_cursor(int button)
467 case CURSOR_UP: return CURSOR_DOWN;
468 case CURSOR_DOWN: return CURSOR_UP;
469 case CURSOR_LEFT: return CURSOR_RIGHT;
470 case CURSOR_RIGHT: return CURSOR_LEFT;
475 static void next_move_3x2(int ax, int ay, int bx, int by,
476 int gx, int gy, int *dx, int *dy)
478 /* When w = 3 and h = 2 and the tile going in the top left corner
479 * is at (ax, ay) and the tile going in the bottom left corner is
480 * at (bx, by) and the blank tile is at (gx, gy), how do you move? */
482 /* Hard-coded shortest solutions. Sorry. */
483 static const unsigned char move[120] = {
508 static const struct { int dx, dy; } d[4] = {{+1,0},{-1,0},{0,+1},{0,-1}};
510 int ea = 3*ay + ax, eb = 3*by + bx, eg = 3*gy + gx, v;
514 v = move[ea + eb*6 + eg*5*6];
519 static void next_move(int nx, int ny, int ox, int oy, int gx, int gy,
520 int tx, int ty, int w, int *dx, int *dy)
522 const int to_tile_x = (gx < nx ? +1 : -1);
523 const int to_goal_x = (gx < tx ? +1 : -1);
524 const int gap_x_on_goal_side = ((nx-tx) * (nx-gx) > 0);
526 assert (nx != tx || ny != ty); /* not already in place */
527 assert (nx != gx || ny != gy); /* not placing the gap */
528 assert (ty <= ny); /* because we're greedy (and flipping) */
529 assert (ty <= gy); /* because we're greedy (and flipping) */
531 /* TODO: define a termination function. Idea: 0 if solved, or
532 * the number of moves to solve the next piece plus the number of
533 * further unsolved pieces times an upper bound on the number of
534 * moves required to solve any piece. If such a function can be
535 * found, we have (termination && (termination => correctness)).
536 * The catch is our temporary disturbance of 2x3 corners. */
538 /* handles end-of-row, when 3 and 4 are in the top right 2x3 box */
540 ny <= ty + 2 && (nx == tx || nx == tx + 1) &&
541 oy <= ty + 2 && (ox == tx || ox == tx + 1) &&
542 gy <= ty + 2 && (gx == tx || gx == tx + 1))
544 next_move_3x2(oy - ty, tx + 1 - ox,
545 ny - ty, tx + 1 - nx,
546 gy - ty, tx + 1 - gx, dy, dx);
552 if (ny <= ty + 2 && (nx == tx || nx == tx - 1) &&
553 gy <= ty + 2 && (gx == tx || gx == tx - 1)) {
554 next_move_3x2(ny - ty, tx - nx, 0, 1, gy - ty, tx - gx, dy, dx);
558 else if (nx != tx || ny != ty + 1) {
559 next_move((w - 1) - nx, ny, -1, -1, (w - 1) - gx, gy,
560 0, ty + 1, -1, dx, dy);
569 /* note that *dy = -1 is unsafe when gy = ty + 1 and gx < tx */
571 if (nx == gx || (gy == ty && gx == tx))
573 else if (!gap_x_on_goal_side)
575 else if (ny - ty > abs(nx - tx))
580 if (nx == tx) /* then we know ny > ty */
581 if (gx > nx || ny > ty + 1)
582 *dy = -1; /* ... so this is safe */
585 else if (gap_x_on_goal_side)
587 else if (gy == ty || (gy == ty + 1 && gx < tx))
592 else if (nx == tx) /* gy > ny */
599 else if (gap_x_on_goal_side)
600 if (gy == ty + 1 && gx < tx)
605 else if (ny - ty > abs(nx - tx))
611 static int compute_hint(const game_state *state, int *out_x, int *out_y)
613 /* The overall solving process is this:
614 * 1. Find the next piece to be put in its place
615 * 2. Move it diagonally towards its place
616 * 3. Move it horizontally or vertically towards its place
617 * (Modulo the last two tiles at the end of each row/column)
620 int gx = X(state, state->gap_pos);
621 int gy = Y(state, state->gap_pos);
623 int tx, ty, nx, ny, ox, oy, /* {target,next,next2}_{x,y} */ i;
626 /* 1. Find the next piece
627 * if (there are no more unfinished columns than rows) {
628 * fill the top-most row, left to right
629 * } else { fill the left-most column, top to bottom }
631 const int w = state->w, h = state->h, n = w*h;
632 int next_piece = 0, next_piece_2 = 0, solr = 0, solc = 0;
633 int unsolved_rows = h, unsolved_cols = w;
638 while (solr < h && solc < w) {
639 int start, step, stop;
640 if (unsolved_cols <= unsolved_rows)
641 start = solr*w + solc, step = 1, stop = unsolved_cols;
643 start = solr*w + solc, step = w, stop = unsolved_rows;
644 for (i = 0; i < stop; ++i) {
645 const int j = start + i*step;
646 if (state->tiles[j] != j + 1) {
648 next_piece_2 = next_piece + step;
654 (unsolved_cols <= unsolved_rows)
655 ? (++solr, --unsolved_rows)
656 : (++solc, --unsolved_cols);
662 /* 2, 3. Move the next piece towards its place */
664 /* gx, gy already set */
665 tx = X(state, next_piece - 1); /* where we're going */
666 ty = Y(state, next_piece - 1);
667 for (i = 0; i < n && state->tiles[i] != next_piece; ++i);
668 nx = X(state, i); /* where we're at */
670 for (i = 0; i < n && state->tiles[i] != next_piece_2; ++i);
674 if (unsolved_cols <= unsolved_rows)
675 next_move(nx, ny, ox, oy, gx, gy, tx, ty, w, &dx, &dy);
677 next_move(ny, nx, oy, ox, gy, gx, ty, tx, h, &dy, &dx);
686 static char *interpret_move(const game_state *state, game_ui *ui,
687 const game_drawstate *ds,
688 int x, int y, int button)
690 int cx = X(state, state->gap_pos), nx = cx;
691 int cy = Y(state, state->gap_pos), ny = cy;
696 if (button == LEFT_BUTTON) {
699 if (nx < 0 || nx >= state->w || ny < 0 || ny >= state->h)
700 return NULL; /* out of bounds */
701 } else if (IS_CURSOR_MOVE(button)) {
702 static int invert_cursor = -1;
703 if (invert_cursor == -1) {
704 char *env = getenv("FIFTEEN_INVERT_CURSOR");
705 invert_cursor = (env && (env[0] == 'y' || env[0] == 'Y'));
707 button = flip_cursor(button); /* the default */
709 button = flip_cursor(button); /* undoes the first flip */
710 move_cursor(button, &nx, &ny, state->w, state->h, FALSE);
711 } else if ((button == 'h' || button == 'H') && !state->completed) {
712 if (!compute_hint(state, &nx, &ny))
713 return NULL; /* shouldn't happen, since ^^we^^checked^^ */
715 return NULL; /* no move */
718 * Any click location should be equal to the gap location
719 * in _precisely_ one coordinate.
721 if ((cx == nx) ^ (cy == ny)) {
722 sprintf(buf, "M%d,%d", nx, ny);
729 static game_state *execute_move(const game_state *from, const char *move)
731 int gx, gy, dx, dy, ux, uy, up, p;
734 if (!strcmp(move, "S")) {
737 ret = dup_game(from);
740 * Simply replace the grid with a solved one. For this game,
741 * this isn't a useful operation for actually telling the user
742 * what they should have done, but it is useful for
743 * conveniently being able to get hold of a clean state from
744 * which to practise manoeuvres.
746 for (i = 0; i < ret->n; i++)
747 ret->tiles[i] = (i+1) % ret->n;
748 ret->gap_pos = ret->n-1;
749 ret->used_solve = TRUE;
750 ret->completed = ret->movecount = 1;
755 gx = X(from, from->gap_pos);
756 gy = Y(from, from->gap_pos);
758 if (move[0] != 'M' ||
759 sscanf(move+1, "%d,%d", &dx, &dy) != 2 ||
760 (dx == gx && dy == gy) || (dx != gx && dy != gy) ||
761 dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
765 * Find the unit displacement from the original gap
766 * position towards this one.
768 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
769 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
770 up = C(from, ux, uy);
772 ret = dup_game(from);
774 ret->gap_pos = C(from, dx, dy);
775 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
777 ret->tiles[ret->gap_pos] = 0;
779 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
780 assert(p >= 0 && p < from->n);
781 ret->tiles[p] = from->tiles[p + up];
786 * See if the game has been completed.
788 if (!ret->completed) {
789 ret->completed = ret->movecount;
790 for (p = 0; p < ret->n; p++)
791 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
798 /* ----------------------------------------------------------------------
802 static void game_compute_size(const game_params *params, int tilesize,
805 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
806 struct { int tilesize; } ads, *ds = &ads;
807 ads.tilesize = tilesize;
809 *x = TILE_SIZE * params->w + 2 * BORDER;
810 *y = TILE_SIZE * params->h + 2 * BORDER;
813 static void game_set_size(drawing *dr, game_drawstate *ds,
814 const game_params *params, int tilesize)
816 ds->tilesize = tilesize;
819 static float *game_colours(frontend *fe, int *ncolours)
821 float *ret = snewn(3 * NCOLOURS, float);
824 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
826 for (i = 0; i < 3; i++)
827 ret[COL_TEXT * 3 + i] = 0.0;
829 *ncolours = NCOLOURS;
833 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
835 struct game_drawstate *ds = snew(struct game_drawstate);
841 ds->bgcolour = COL_BACKGROUND;
842 ds->tiles = snewn(ds->w*ds->h, int);
843 ds->tilesize = 0; /* haven't decided yet */
844 for (i = 0; i < ds->w*ds->h; i++)
850 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
856 static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state,
857 int x, int y, int tile, int flash_colour)
860 draw_rect(dr, x, y, TILE_SIZE, TILE_SIZE,
866 coords[0] = x + TILE_SIZE - 1;
867 coords[1] = y + TILE_SIZE - 1;
868 coords[2] = x + TILE_SIZE - 1;
871 coords[5] = y + TILE_SIZE - 1;
872 draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
876 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
878 draw_rect(dr, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
879 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
882 sprintf(str, "%d", tile);
883 draw_text(dr, x + TILE_SIZE/2, y + TILE_SIZE/2,
884 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
887 draw_update(dr, x, y, TILE_SIZE, TILE_SIZE);
890 static void game_redraw(drawing *dr, game_drawstate *ds,
891 const game_state *oldstate, const game_state *state,
892 int dir, const game_ui *ui,
893 float animtime, float flashtime)
895 int i, pass, bgcolour;
898 int frame = (int)(flashtime / FLASH_FRAME);
899 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
901 bgcolour = COL_BACKGROUND;
907 TILE_SIZE * state->w + 2 * BORDER,
908 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
909 draw_update(dr, 0, 0,
910 TILE_SIZE * state->w + 2 * BORDER,
911 TILE_SIZE * state->h + 2 * BORDER);
914 * Recessed area containing the whole puzzle.
916 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
917 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
918 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
919 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
920 coords[4] = coords[2] - TILE_SIZE;
921 coords[5] = coords[3] + TILE_SIZE;
922 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
923 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
924 coords[6] = coords[8] + TILE_SIZE;
925 coords[7] = coords[9] - TILE_SIZE;
926 draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
928 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
929 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
930 draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
936 * Now draw each tile. We do this in two passes to make
939 for (pass = 0; pass < 2; pass++) {
940 for (i = 0; i < state->n; i++) {
943 * Figure out what should be displayed at this
944 * location. It's either a simple tile, or it's a
945 * transition between two tiles (in which case we say
946 * -1 because it must always be drawn).
949 if (oldstate && oldstate->tiles[i] != state->tiles[i])
956 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
957 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
961 * Figure out what to _actually_ draw, and where to
969 * On the first pass, just blank the tile.
972 x = COORD(X(state, i));
973 y = COORD(Y(state, i));
981 * Don't bother moving the gap; just don't
988 * Find the coordinates of this tile in the old and
991 x1 = COORD(X(state, i));
992 y1 = COORD(Y(state, i));
993 for (j = 0; j < oldstate->n; j++)
994 if (oldstate->tiles[j] == state->tiles[i])
996 assert(j < oldstate->n);
997 x0 = COORD(X(state, j));
998 y0 = COORD(Y(state, j));
1000 c = (animtime / ANIM_TIME);
1001 if (c < 0.0F) c = 0.0F;
1002 if (c > 1.0F) c = 1.0F;
1004 x = x0 + (int)(c * (x1 - x0));
1005 y = y0 + (int)(c * (y1 - y0));
1011 x = COORD(X(state, i));
1012 y = COORD(Y(state, i));
1015 draw_tile(dr, ds, state, x, y, t, bgcolour);
1020 ds->bgcolour = bgcolour;
1023 * Update the status bar.
1026 char statusbuf[256];
1029 * Don't show the new status until we're also showing the
1030 * new _state_ - after the game animation is complete.
1035 if (state->used_solve)
1036 sprintf(statusbuf, "Moves since auto-solve: %d",
1037 state->movecount - state->completed);
1039 sprintf(statusbuf, "%sMoves: %d",
1040 (state->completed ? "COMPLETED! " : ""),
1041 (state->completed ? state->completed : state->movecount));
1043 status_bar(dr, statusbuf);
1047 static float game_anim_length(const game_state *oldstate,
1048 const game_state *newstate, int dir, game_ui *ui)
1053 static float game_flash_length(const game_state *oldstate,
1054 const game_state *newstate, int dir, game_ui *ui)
1056 if (!oldstate->completed && newstate->completed &&
1057 !oldstate->used_solve && !newstate->used_solve)
1058 return 2 * FLASH_FRAME;
1063 static int game_status(const game_state *state)
1065 return state->completed ? +1 : 0;
1068 static int game_timing_state(const game_state *state, game_ui *ui)
1073 static void game_print_size(const game_params *params, float *x, float *y)
1077 static void game_print(drawing *dr, const game_state *state, int tilesize)
1082 #define thegame fifteen
1085 const struct game thegame = {
1086 "Fifteen", "games.fifteen", "fifteen",
1088 game_fetch_preset, NULL,
1093 TRUE, game_configure, custom_params,
1101 TRUE, game_can_format_as_text_now, game_text_format,
1109 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1112 game_free_drawstate,
1117 FALSE, FALSE, game_print_size, game_print,
1118 TRUE, /* wants_statusbar */
1119 FALSE, game_timing_state,
1123 #ifdef STANDALONE_SOLVER
1125 int main(int argc, char **argv)
1127 game_params *params;
1129 char *id = NULL, *desc;
1132 char *progname = argv[0];
1135 int limit, x, y, solvable;
1137 while (--argc > 0) {
1139 if (!strcmp(p, "-v")) {
1140 /* solver_show_working = TRUE; */
1141 } else if (!strcmp(p, "-g")) {
1143 } else if (*p == '-') {
1144 fprintf(stderr, "%s: unrecognised option `%s'\n", progname, p);
1152 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
1156 desc = strchr(id, ':');
1158 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
1163 params = default_params();
1164 decode_params(params, id);
1165 err = validate_desc(params, desc);
1167 free_params(params);
1168 fprintf(stderr, "%s: %s\n", argv[0], err);
1172 state = new_game(NULL, params, desc);
1173 free_params(params);
1175 solvable = (PARITY_S(state) == perm_parity(state->tiles, state->n));
1176 if (grade || !solvable) {
1178 fputs(solvable ? "Game is solvable" : "Game is unsolvable",
1179 grade ? stdout : stderr);
1183 for (limit = 5 * state->n * state->n * state->n; limit; --limit) {
1184 game_state *next_state;
1185 if (!compute_hint(state, &x, &y)) {
1186 fprintf(stderr, "couldn't compute next move while solving %s:%s",
1190 printf("Move the space to (%d, %d), moving %d into the space\n",
1191 x + 1, y + 1, state->tiles[C(state, x, y)]);
1192 sprintf(buf, "M%d,%d", x, y);
1193 next_state = execute_move(state, buf);
1197 fprintf(stderr, "invalid move when solving %s:%s\n", id, desc);
1201 if (next_state->completed) {
1208 fprintf(stderr, "ran out of moves for %s:%s\n", id, desc);