+ /*
+ * Now construct a string which can be passed to execute_move()
+ * to transform the current grid into the solved one.
+ */
+ retsize = 256;
+ ret = snewn(retsize, char);
+ retlen = 0;
+ ret[retlen++] = 'S';
+
+ for (i = 0; i < state->width * state->height; i++) {
+ int from = currstate->tiles[i], to = tiles[i];
+ int ft = from & (R|L|U|D), tt = to & (R|L|U|D);
+ int x = i % state->width, y = i / state->width;
+ int chr = '\0';
+ char buf[80], *p = buf;
+
+ if (from == to)
+ continue; /* nothing needs doing at all */
+
+ /*
+ * To transform this tile into the desired tile: first
+ * unlock the tile if it's locked, then rotate it if
+ * necessary, then lock it if necessary.
+ */
+ if (from & LOCKED)
+ p += sprintf(p, ";L%d,%d", x, y);
+
+ if (tt == A(ft))
+ chr = 'A';
+ else if (tt == C(ft))
+ chr = 'C';
+ else if (tt == F(ft))
+ chr = 'F';
+ else {
+ assert(tt == ft);
+ chr = '\0';
+ }
+ if (chr)
+ p += sprintf(p, ";%c%d,%d", chr, x, y);
+
+ if (to & LOCKED)
+ p += sprintf(p, ";L%d,%d", x, y);
+
+ if (p > buf) {
+ if (retlen + (p - buf) >= retsize) {
+ retsize = retlen + (p - buf) + 512;
+ ret = sresize(ret, retsize, char);
+ }
+ memcpy(ret+retlen, buf, p - buf);
+ retlen += p - buf;
+ }
+ }
+
+ assert(retlen < retsize);
+ ret[retlen] = '\0';
+ ret = sresize(ret, retlen+1, char);
+
+ sfree(tiles);
+