- if (stop) {
- /*
- * Find the last two locations, and the last two pieces.
- */
- while (tiles[x] >= 0)
- x++;
- assert(x < n);
- x1 = x;
- x++;
- while (tiles[x] >= 0)
- x++;
- assert(x < n);
- x2 = x;
-
- for (i = 0; i < n; i++)
- if (!used[i])
- break;
- p1 = i;
- for (i = p1+1; i < n; i++)
- if (!used[i])
- break;
- p2 = i;
+ /*
+ * To at least _try_ to avoid boring cases, check
+ * that this move doesn't directly undo a previous
+ * one, or repeat it so many times as to turn it
+ * into fewer moves in the opposite direction. (For
+ * example, in a row of length 4, we're allowed to
+ * move it the same way twice, but not three
+ * times.)
+ *
+ * We track this for each individual row/column,
+ * and clear all the counters as soon as a
+ * perpendicular move is made. This isn't perfect
+ * (it _can't_ guaranteeably be perfect - there
+ * will always come a move count beyond which a
+ * shorter solution will be possible than the one
+ * which constructed the position) but it should
+ * sort out all the obvious cases.
+ */
+ if (offset == prevoffset) {
+ tmp = prevmoves[index] + direction;
+ if (abs(2*tmp) > len || abs(tmp) < abs(prevmoves[index]))
+ continue;
+ }
+
+ /* If we didn't `continue', we've found an OK move to make. */
+ if (offset != prevoffset) {
+ int i;
+ for (i = 0; i < max; i++)
+ prevmoves[i] = 0;
+ prevoffset = offset;
+ }
+ prevmoves[index] += direction;
+ break;
+ }