+static int flip_cursor(int button)
+{
+ switch (button) {
+ case CURSOR_UP: return CURSOR_DOWN;
+ case CURSOR_DOWN: return CURSOR_UP;
+ case CURSOR_LEFT: return CURSOR_RIGHT;
+ case CURSOR_RIGHT: return CURSOR_LEFT;
+ }
+ return 0;
+}
+
+static void next_move_3x2(int ax, int ay, int bx, int by,
+ int gx, int gy, int *dx, int *dy)
+{
+ /* When w = 3 and h = 2 and the tile going in the top left corner
+ * is at (ax, ay) and the tile going in the bottom left corner is
+ * at (bx, by) and the blank tile is at (gx, gy), how do you move? */
+
+ /* Hard-coded shortest solutions. Sorry. */
+ static const unsigned char move[120] = {
+ 1,2,0,1,2,2,
+ 2,0,0,2,0,0,
+ 0,0,2,0,2,0,
+ 0,0,0,2,0,2,
+ 2,0,0,0,2,0,
+
+ 0,3,0,1,1,1,
+ 3,0,3,2,1,2,
+ 2,1,1,0,1,0,
+ 2,1,2,1,0,1,
+ 1,2,0,2,1,2,
+
+ 0,1,3,1,3,0,
+ 1,3,1,3,0,3,
+ 0,0,3,3,0,0,
+ 0,0,0,1,2,1,
+ 3,0,0,1,1,1,
+
+ 3,1,1,1,3,0,
+ 1,1,1,1,1,1,
+ 1,3,1,1,3,0,
+ 1,1,3,3,1,3,
+ 1,3,0,0,0,0
+ };
+ static const struct { int dx, dy; } d[4] = {{+1,0},{-1,0},{0,+1},{0,-1}};
+
+ int ea = 3*ay + ax, eb = 3*by + bx, eg = 3*gy + gx, v;
+ if (eb > ea) --eb;
+ if (eg > ea) --eg;
+ if (eg > eb) --eg;
+ v = move[ea + eb*6 + eg*5*6];
+ *dx = d[v].dx;
+ *dy = d[v].dy;
+}
+
+static void next_move(int nx, int ny, int ox, int oy, int gx, int gy,
+ int tx, int ty, int w, int *dx, int *dy)
+{
+ const int to_tile_x = (gx < nx ? +1 : -1);
+ const int to_goal_x = (gx < tx ? +1 : -1);
+ const int gap_x_on_goal_side = ((nx-tx) * (nx-gx) > 0);
+
+ assert (nx != tx || ny != ty); /* not already in place */
+ assert (nx != gx || ny != gy); /* not placing the gap */
+ assert (ty <= ny); /* because we're greedy (and flipping) */
+ assert (ty <= gy); /* because we're greedy (and flipping) */
+
+ /* TODO: define a termination function. Idea: 0 if solved, or
+ * the number of moves to solve the next piece plus the number of
+ * further unsolved pieces times an upper bound on the number of
+ * moves required to solve any piece. If such a function can be
+ * found, we have (termination && (termination => correctness)).
+ * The catch is our temporary disturbance of 2x3 corners. */
+
+ /* handles end-of-row, when 3 and 4 are in the top right 2x3 box */
+ if (tx == w - 2 &&
+ ny <= ty + 2 && (nx == tx || nx == tx + 1) &&
+ oy <= ty + 2 && (ox == tx || ox == tx + 1) &&
+ gy <= ty + 2 && (gx == tx || gx == tx + 1))
+ {
+ next_move_3x2(oy - ty, tx + 1 - ox,
+ ny - ty, tx + 1 - nx,
+ gy - ty, tx + 1 - gx, dy, dx);
+ *dx *= -1;
+ return;
+ }
+
+ if (tx == w - 1) {
+ if (ny <= ty + 2 && (nx == tx || nx == tx - 1) &&
+ gy <= ty + 2 && (gx == tx || gx == tx - 1)) {
+ next_move_3x2(ny - ty, tx - nx, 0, 1, gy - ty, tx - gx, dy, dx);
+ *dx *= -1;
+ } else if (gy == ty)
+ *dy = +1;
+ else if (nx != tx || ny != ty + 1) {
+ next_move((w - 1) - nx, ny, -1, -1, (w - 1) - gx, gy,
+ 0, ty + 1, -1, dx, dy);
+ *dx *= -1;
+ } else if (gx == nx)
+ *dy = -1;
+ else
+ *dx = +1;
+ return;
+ }
+
+ /* note that *dy = -1 is unsafe when gy = ty + 1 and gx < tx */
+ if (gy < ny)
+ if (nx == gx || (gy == ty && gx == tx))
+ *dy = +1;
+ else if (!gap_x_on_goal_side)
+ *dx = to_tile_x;
+ else if (ny - ty > abs(nx - tx))
+ *dx = to_tile_x;
+ else *dy = +1;
+
+ else if (gy == ny)
+ if (nx == tx) /* then we know ny > ty */
+ if (gx > nx || ny > ty + 1)
+ *dy = -1; /* ... so this is safe */
+ else
+ *dy = +1;
+ else if (gap_x_on_goal_side)
+ *dx = to_tile_x;
+ else if (gy == ty || (gy == ty + 1 && gx < tx))
+ *dy = +1;
+ else
+ *dy = -1;
+
+ else if (nx == tx) /* gy > ny */
+ if (gx > nx)
+ *dy = -1;
+ else
+ *dx = +1;
+ else if (gx == nx)
+ *dx = to_goal_x;
+ else if (gap_x_on_goal_side)
+ if (gy == ty + 1 && gx < tx)
+ *dx = to_tile_x;
+ else
+ *dy = -1;
+
+ else if (ny - ty > abs(nx - tx))
+ *dy = -1;
+ else
+ *dx = to_tile_x;
+}
+
+static int compute_hint(const game_state *state, int *out_x, int *out_y)