+/* ----------------------------------------------------------------------
+ * Error-checking during gameplay.
+ */
+
+/*
+ * The difficulty in error-checking Pattern is to make the error check
+ * _weak_ enough. The most obvious way would be to check each row and
+ * column by calling (a modified form of) do_row() to recursively
+ * analyse the row contents against the clue set and see if the
+ * GRID_UNKNOWNs could be filled in in any way that would end up
+ * correct. However, this turns out to be such a strong error check as
+ * to constitute a spoiler in many situations: you make a typo while
+ * trying to fill in one row, and not only does the row light up to
+ * indicate an error, but several columns crossed by the move also
+ * light up and draw your attention to deductions you hadn't even
+ * noticed you could make.
+ *
+ * So instead I restrict error-checking to 'complete runs' within a
+ * row, by which I mean contiguous sequences of GRID_FULL bounded at
+ * both ends by either GRID_EMPTY or the ends of the row. We identify
+ * all the complete runs in a row, and verify that _those_ are
+ * consistent with the row's clue list. Sequences of complete runs
+ * separated by solid GRID_EMPTY are required to match contiguous
+ * sequences in the clue list, whereas if there's at least one
+ * GRID_UNKNOWN between any two complete runs then those two need not
+ * be contiguous in the clue list.
+ *
+ * To simplify the edge cases, I pretend that the clue list for the
+ * row is extended with a 0 at each end, and I also pretend that the
+ * grid data for the row is extended with a GRID_EMPTY and a
+ * zero-length run at each end. This permits the contiguity checker to
+ * handle the fiddly end effects (e.g. if the first contiguous
+ * sequence of complete runs in the grid matches _something_ in the
+ * clue list but not at the beginning, this is allowable iff there's a
+ * GRID_UNKNOWN before the first one) with minimal faff, since the end
+ * effects just drop out as special cases of the normal inter-run
+ * handling (in this code the above case is not 'at the end of the
+ * clue list' at all, but between the implicit initial zero run and
+ * the first nonzero one).
+ *
+ * We must also be a little careful about how we search for a
+ * contiguous sequence of runs. In the clue list (1 1 2 1 2 3),
+ * suppose we see a GRID_UNKNOWN and then a length-1 run. We search
+ * for 1 in the clue list and find it at the very beginning. But now
+ * suppose we find a length-2 run with no GRID_UNKNOWN before it. We
+ * can't naively look at the next clue from the 1 we found, because
+ * that'll be the second 1 and won't match. Instead, we must backtrack
+ * by observing that the 2 we've just found must be contiguous with
+ * the 1 we've already seen, so we search for the sequence (1 2) and
+ * find it starting at the second 1. Now if we see a 3, we must
+ * rethink again and search for (1 2 3).
+ */
+
+struct errcheck_state {
+ /*
+ * rowdata and rowlen point at the clue data for this row in the
+ * game state.
+ */
+ int *rowdata;
+ int rowlen;
+ /*
+ * rowpos indicates the lowest position where it would be valid to
+ * see our next run length. It might be equal to rowlen,
+ * indicating that the next run would have to be the terminating 0.
+ */
+ int rowpos;
+ /*
+ * ncontig indicates how many runs we've seen in a contiguous
+ * block. This is taken into account when searching for the next
+ * run we find, unless ncontig is zeroed out first by encountering
+ * a GRID_UNKNOWN.
+ */
+ int ncontig;
+};
+
+static int errcheck_found_run(struct errcheck_state *es, int r)
+{
+/* Macro to handle the pretence that rowdata has a 0 at each end */
+#define ROWDATA(k) ((k)<0 || (k)>=es->rowlen ? 0 : es->rowdata[(k)])
+
+ /*
+ * See if we can find this new run length at a position where it
+ * also matches the last 'ncontig' runs we've seen.
+ */
+ int i, newpos;
+ for (newpos = es->rowpos; newpos <= es->rowlen; newpos++) {
+
+ if (ROWDATA(newpos) != r)
+ goto notfound;
+
+ for (i = 1; i <= es->ncontig; i++)
+ if (ROWDATA(newpos - i) != ROWDATA(es->rowpos - i))
+ goto notfound;
+
+ es->rowpos = newpos+1;
+ es->ncontig++;
+ return TRUE;
+
+ notfound:;
+ }
+
+ return FALSE;
+
+#undef ROWDATA
+}
+
+static int check_errors(const game_state *state, int i)
+{
+ int start, step, end, j;
+ int val, runlen;
+ struct errcheck_state aes, *es = &aes;
+
+ es->rowlen = state->common->rowlen[i];
+ es->rowdata = state->common->rowdata + state->common->rowsize * i;
+ /* Pretend that we've already encountered the initial zero run */
+ es->ncontig = 1;
+ es->rowpos = 0;
+
+ if (i < state->common->w) {
+ start = i;
+ step = state->common->w;
+ end = start + step * state->common->h;
+ } else {
+ start = (i - state->common->w) * state->common->w;
+ step = 1;
+ end = start + step * state->common->w;
+ }
+
+ runlen = -1;
+ for (j = start - step; j <= end; j += step) {
+ if (j < start || j == end)
+ val = GRID_EMPTY;
+ else
+ val = state->grid[j];
+
+ if (val == GRID_UNKNOWN) {
+ runlen = -1;
+ es->ncontig = 0;
+ } else if (val == GRID_FULL) {
+ if (runlen >= 0)
+ runlen++;
+ } else if (val == GRID_EMPTY) {
+ if (runlen > 0) {
+ if (!errcheck_found_run(es, runlen))
+ return TRUE; /* error! */
+ }
+ runlen = 0;
+ }
+ }
+
+ /* Signal end-of-row by sending errcheck_found_run the terminating
+ * zero run, which will be marked as contiguous with the previous
+ * run if and only if there hasn't been a GRID_UNKNOWN before. */
+ if (!errcheck_found_run(es, 0))
+ return TRUE; /* error at the last minute! */
+
+ return FALSE; /* no error */
+}
+