+ freetree234(barriertree);
+ }
+
+ /*
+ * Finally, encode the grid into a string game description.
+ *
+ * My syntax is extremely simple: each square is encoded as a
+ * hex digit in which bit 0 means a connection on the right,
+ * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
+ * encoding as used internally). Each digit is followed by
+ * optional barrier indicators: `v' means a vertical barrier to
+ * the right of it, and `h' means a horizontal barrier below
+ * it.
+ */
+ desc = snewn(w * h * 3 + 1, char);
+ p = desc;
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ *p++ = "0123456789abcdef"[index(params, tiles, x, y)];
+ if ((params->wrapping || x < w-1) &&
+ (index(params, barriers, x, y) & R))
+ *p++ = 'v';
+ if ((params->wrapping || y < h-1) &&
+ (index(params, barriers, x, y) & D))
+ *p++ = 'h';
+ }
+ }
+ assert(p - desc <= w*h*3);
+ *p = '\0';
+
+ sfree(tiles);
+ sfree(barriers);
+
+ return desc;
+}
+
+static char *validate_desc(game_params *params, char *desc)
+{
+ int w = params->width, h = params->height;
+ int i;
+
+ for (i = 0; i < w*h; i++) {
+ if (*desc >= '0' && *desc <= '9')
+ /* OK */;
+ else if (*desc >= 'a' && *desc <= 'f')
+ /* OK */;
+ else if (*desc >= 'A' && *desc <= 'F')
+ /* OK */;
+ else if (!*desc)
+ return "Game description shorter than expected";
+ else
+ return "Game description contained unexpected character";
+ desc++;
+ while (*desc == 'h' || *desc == 'v')
+ desc++;
+ }
+ if (*desc)
+ return "Game description longer than expected";
+
+ return NULL;
+}
+
+/* ----------------------------------------------------------------------
+ * Construct an initial game state, given a description and parameters.
+ */
+
+static game_state *new_game(midend_data *me, game_params *params, char *desc)
+{
+ game_state *state;
+ int w, h, x, y;
+
+ assert(params->width > 0 && params->height > 0);
+ assert(params->width > 1 || params->height > 1);
+
+ /*
+ * Create a blank game state.
+ */
+ state = snew(game_state);
+ w = state->width = params->width;
+ h = state->height = params->height;
+ state->cx = state->width / 2;
+ state->cy = state->height / 2;
+ state->wrapping = params->wrapping;
+ state->movetarget = params->movetarget;
+ state->completed = 0;
+ state->used_solve = state->just_used_solve = FALSE;
+ state->move_count = 0;
+ state->last_move_row = -1;
+ state->last_move_col = -1;
+ state->last_move_dir = 0;
+ state->tiles = snewn(state->width * state->height, unsigned char);
+ memset(state->tiles, 0, state->width * state->height);
+ state->barriers = snewn(state->width * state->height, unsigned char);
+ memset(state->barriers, 0, state->width * state->height);
+
+
+ /*
+ * Parse the game description into the grid.
+ */
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ if (*desc >= '0' && *desc <= '9')
+ tile(state, x, y) = *desc - '0';
+ else if (*desc >= 'a' && *desc <= 'f')
+ tile(state, x, y) = *desc - 'a' + 10;
+ else if (*desc >= 'A' && *desc <= 'F')
+ tile(state, x, y) = *desc - 'A' + 10;
+ if (*desc)
+ desc++;
+ while (*desc == 'h' || *desc == 'v') {
+ int x2, y2, d1, d2;
+ if (*desc == 'v')
+ d1 = R;
+ else
+ d1 = D;
+
+ OFFSET(x2, y2, x, y, d1, state);
+ d2 = F(d1);
+
+ barrier(state, x, y) |= d1;
+ barrier(state, x2, y2) |= d2;
+
+ desc++;
+ }
+ }
+ }
+
+ /*
+ * Set up border barriers if this is a non-wrapping game.
+ */
+ if (!state->wrapping) {
+ for (x = 0; x < state->width; x++) {
+ barrier(state, x, 0) |= U;
+ barrier(state, x, state->height-1) |= D;
+ }
+ for (y = 0; y < state->height; y++) {
+ barrier(state, 0, y) |= L;
+ barrier(state, state->width-1, y) |= R;
+ }