X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/b5ba72bc4698800049947cdf7696cb44f11096bb..HEAD:/singles.c diff --git a/singles.c b/singles.c index ec5db73..32449b6 100644 --- a/singles.c +++ b/singles.c @@ -324,7 +324,7 @@ static char n2c(int num) { } static int c2n(char c) { - if (isdigit(c)) + if (isdigit((unsigned char)c)) return (int)(c - '0'); else if (c >= 'a' && c <= 'z') return (int)(c - 'a' + 10); @@ -450,7 +450,10 @@ static void connect_dsf(game_state *state, int *dsf) } } -static int check_rowcol(game_state *state, int starti, int di, int sz, int mark_errors) +#define CC_MARK_ERRORS 1 +#define CC_MUST_FILL 2 + +static int check_rowcol(game_state *state, int starti, int di, int sz, unsigned flags) { int nerr = 0, n, m, i, j; @@ -466,7 +469,7 @@ static int check_rowcol(game_state *state, int starti, int di, int sz, int mark_ if (state->nums[i] != state->nums[j]) continue; nerr++; /* ok, we have two numbers the same in a row. */ - if (!mark_errors) continue; + if (!(flags & CC_MARK_ERRORS)) continue; /* If we have two circles in the same row around * two identical numbers, they are _both_ wrong. */ @@ -491,17 +494,27 @@ static int check_rowcol(game_state *state, int starti, int di, int sz, int mark_ return nerr; } -static int check_complete(game_state *state, int mark_errors) +static int check_complete(game_state *state, unsigned flags) { int *dsf = snewn(state->n, int); int x, y, i, error = 0, nwhite, w = state->w, h = state->h; - if (mark_errors) { + if (flags & CC_MARK_ERRORS) { for (i = 0; i < state->n; i++) state->flags[i] &= ~F_ERROR; } connect_dsf(state, dsf); + /* If we're the solver we need the grid all to be definitively + * black or definitively white (i.e. circled) otherwise the solver + * has found an ambiguous grid. */ + if (flags & CC_MUST_FILL) { + for (i = 0; i < state->n; i++) { + if (!(state->flags[i] & F_BLACK) && !(state->flags[i] & F_CIRCLE)) + error += 1; + } + } + /* Mark any black squares in groups of >1 as errors. * Count number of white squares. */ nwhite = 0; @@ -509,7 +522,7 @@ static int check_complete(game_state *state, int mark_errors) if (state->flags[i] & F_BLACK) { if (dsf_size(dsf, i) > 1) { error += 1; - if (mark_errors) + if (flags & CC_MARK_ERRORS) state->flags[i] |= F_ERROR; } } else @@ -518,9 +531,9 @@ static int check_complete(game_state *state, int mark_errors) /* Check attributes of white squares, row- and column-wise. */ for (x = 0; x < w; x++) /* check cols from (x,0) */ - error += check_rowcol(state, x, w, h, mark_errors); + error += check_rowcol(state, x, w, h, flags); for (y = 0; y < h; y++) /* check rows from (0,y) */ - error += check_rowcol(state, y*w, 1, w, mark_errors); + error += check_rowcol(state, y*w, 1, w, flags); /* mark (all) white regions as an error if there is more than one. * may want to make this less in-your-face (by only marking @@ -530,7 +543,7 @@ static int check_complete(game_state *state, int mark_errors) if (!(state->flags[i] & F_BLACK) && dsf_size(dsf, i) < nwhite) { error += 1; - if (mark_errors) + if (flags & CC_MARK_ERRORS) state->flags[i] |= F_ERROR; } } @@ -617,7 +630,7 @@ static void solver_op_add(struct solver_state *ss, int x, int y, int op, const c } sop = &(ss->ops[ss->n_ops++]); sop->x = x; sop->y = y; sop->op = op; sop->desc = desc; - debug(("added solver op %s ('%s') at (%d,%d)", + debug(("added solver op %s ('%s') at (%d,%d)\n", op == BLACK ? "BLACK" : "CIRCLE", desc, x, y)); } @@ -628,7 +641,7 @@ static void solver_op_circle(game_state *state, struct solver_state *ss, if (!INGRID(state, x, y)) return; if (state->flags[i] & F_BLACK) { - debug(("... solver wants to add auto-circle on black (%d,%d)", x, y)); + debug(("... solver wants to add auto-circle on black (%d,%d)\n", x, y)); state->impossible = 1; return; } @@ -646,7 +659,7 @@ static void solver_op_blacken(game_state *state, struct solver_state *ss, if (!INGRID(state, x, y)) return; if (state->nums[i] != num) return; if (state->flags[i] & F_CIRCLE) { - debug(("... solver wants to add auto-black on circled(%d,%d)", x, y)); + debug(("... solver wants to add auto-black on circled(%d,%d)\n", x, y)); state->impossible = 1; return; } @@ -670,12 +683,12 @@ static int solver_ops_do(game_state *state, struct solver_state *ss) if (op.op == BLACK) { if (state->flags[i] & F_CIRCLE) { - debug(("Solver wants to blacken circled square (%d,%d)!", op.x, op.y)); + debug(("Solver wants to blacken circled square (%d,%d)!\n", op.x, op.y)); state->impossible = 1; return n_ops; } if (!(state->flags[i] & F_BLACK)) { - debug(("... solver adding black at (%d,%d): %s", op.x, op.y, op.desc)); + debug(("... solver adding black at (%d,%d): %s\n", op.x, op.y, op.desc)); #ifdef STANDALONE_SOLVER if (verbose) printf("Adding black at (%d,%d): %s\n", op.x, op.y, op.desc); @@ -690,12 +703,12 @@ static int solver_ops_do(game_state *state, struct solver_state *ss) } } else { if (state->flags[i] & F_BLACK) { - debug(("Solver wants to circle blackened square (%d,%d)!", op.x, op.y)); + debug(("Solver wants to circle blackened square (%d,%d)!\n", op.x, op.y)); state->impossible = 1; return n_ops; } if (!(state->flags[i] & F_CIRCLE)) { - debug(("... solver adding circle at (%d,%d): %s", op.x, op.y, op.desc)); + debug(("... solver adding circle at (%d,%d): %s\n", op.x, op.y, op.desc)); #ifdef STANDALONE_SOLVER if (verbose) printf("Adding circle at (%d,%d): %s\n", op.x, op.y, op.desc); @@ -822,7 +835,7 @@ static int solve_allblackbutone(game_state *state, struct solver_state *ss) solver_op_add(ss, ifree%state->w, ifree/state->w, CIRCLE, "CC/CE/QM: white cell with single non-black around it"); else { - debug(("White cell with no escape at (%d,%d)", x, y)); + debug(("White cell with no escape at (%d,%d)\n", x, y)); state->impossible = 1; return 0; } @@ -928,9 +941,9 @@ static void solve_offsetpair_pair(game_state *state, struct solver_state *ss, if (an == dn) { /* We have a match; so (WLOG) the 'A' marked above are at * (x1,y1) and (x2,y2), and the 'B' are at (ax,ay) and (dx,dy). */ - debug(("Found offset-pair: %d at (%d,%d) and (%d,%d)", + debug(("Found offset-pair: %d at (%d,%d) and (%d,%d)\n", state->nums[y1*w + x1], x1, y1, x2, y2)); - debug((" and: %d at (%d,%d) and (%d,%d)", + debug((" and: %d at (%d,%d) and (%d,%d)\n", an, ax, ay, dx[d], dy[d])); xd = dx[d] - x2; yd = dy[d] - y2; @@ -984,7 +997,7 @@ static int solve_hassinglewhiteregion(game_state *state, struct solver_state *ss state->flags[i] &= ~F_SCRATCH; } if (lwhite == -1) { - debug(("solve_hassinglewhite: no white squares found!")); + debug(("solve_hassinglewhite: no white squares found!\n")); state->impossible = 1; return 0; } @@ -1043,7 +1056,7 @@ static int solve_removesplits(game_state *state, struct solver_state *ss) int i, x, y, n_ops = ss->n_ops; if (!solve_hassinglewhiteregion(state, ss)) { - debug(("solve_removesplits: white region is not contiguous at start!")); + debug(("solve_removesplits: white region is not contiguous at start!\n")); state->impossible = 1; return 0; } @@ -1155,7 +1168,7 @@ static int solve_specific(game_state *state, int diff, int sneaky) } solver_state_free(ss); - return state->impossible ? -1 : check_complete(state, 0); + return state->impossible ? -1 : check_complete(state, CC_MUST_FILL); } static char *solve_game(game_state *state, game_state *currstate, @@ -1164,11 +1177,11 @@ static char *solve_game(game_state *state, game_state *currstate, game_state *solved = dup_game(currstate); char *move = NULL; - if (solve_specific(solved, DIFF_ANY, 0)) goto solved; + if (solve_specific(solved, DIFF_ANY, 0) > 0) goto solved; free_game(solved); solved = dup_game(state); - if (solve_specific(solved, DIFF_ANY, 0)) goto solved; + if (solve_specific(solved, DIFF_ANY, 0) > 0) goto solved; free_game(solved); *error = "Unable to solve puzzle."; @@ -1229,7 +1242,7 @@ static int new_game_is_good(game_params *params, } if (sret <= 0 || sret_easy > 0) { - debug(("Generated puzzle %s at chosen difficulty %s", + debug(("Generated puzzle %s at chosen difficulty %s\n", sret <= 0 ? "insoluble" : "too easy", singles_diffnames[params->diff])); return 0; @@ -1256,7 +1269,7 @@ static int best_black_col(game_state *state, random_state *rs, int *scratch, for (i = 0; i < o; i++) { j = scratch[i] + 1; if (rownums[y*o + j-1] == 1 && colnums[x*o + j-1] == 1) - return j; + goto found; } /* Then try each number in turn returning the first one that's @@ -1264,10 +1277,16 @@ static int best_black_col(game_state *state, random_state *rs, int *scratch, for (i = 0; i < o; i++) { j = scratch[i] + 1; if (rownums[y*o + j-1] != 0 || colnums[x*o + j-1] != 0) - return j; + goto found; } assert(!"unable to place number under black cell."); return 0; + +found: + /* Update column and row counts assuming this number will be placed. */ + rownums[y*o + j-1] += 1; + colnums[x*o + j-1] += 1; + return j; } static char *new_game_desc(game_params *params, random_state *rs, @@ -1287,7 +1306,7 @@ static char *new_game_desc(game_params *params, random_state *rs, generate: ss->n_ops = 0; - debug(("Starting game generation, size %dx%d", w, h)); + debug(("Starting game generation, size %dx%d\n", w, h)); memset(state->flags, 0, state->n*sizeof(unsigned int)); @@ -1307,7 +1326,7 @@ generate: for (j = 0; j < state->n; j++) { i = scratch[j]; if ((state->flags[i] & F_CIRCLE) || (state->flags[i] & F_BLACK)) { - debug(("generator skipping (%d,%d): %s", i%w, i/w, + debug(("generator skipping (%d,%d): %s\n", i%w, i/w, (state->flags[i] & F_CIRCLE) ? "CIRCLE" : "BLACK")); continue; /* solver knows this must be one or the other already. */ } @@ -1324,7 +1343,7 @@ generate: solver_ops_do(state, ss); if (state->impossible) { - debug(("generator made impossible, restarting...")); + debug(("generator made impossible, restarting...\n")); goto generate; } } @@ -1363,10 +1382,10 @@ randomise: !new_game_is_good(params, state, tosolve)) { ntries++; if (ntries > MAXTRIES) { - debug(("Ran out of randomisation attempts, re-generating.")); + debug(("Ran out of randomisation attempts, re-generating.\n")); goto generate; } - debug(("Re-randomising numbers under black squares.")); + debug(("Re-randomising numbers under black squares.\n")); goto randomise; } @@ -1452,7 +1471,7 @@ struct game_drawstate { unsigned int *flags; }; -static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, +static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds, int mx, int my, int button) { char buf[80], c; @@ -1508,7 +1527,7 @@ static game_state *execute_move(game_state *state, char *move) game_state *ret = dup_game(state); int x, y, i, n; - debug(("move: %s", move)); + debug(("move: %s\n", move)); while (*move) { char c = *move; @@ -1536,7 +1555,7 @@ static game_state *execute_move(game_state *state, char *move) else if (*move) goto badmove; } - if (check_complete(ret, 1)) ret->completed = 1; + if (check_complete(ret, CC_MARK_ERRORS)) ret->completed = 1; return ret; badmove: @@ -1716,6 +1735,11 @@ static float game_flash_length(game_state *oldstate, game_state *newstate, return 0.0F; } +static int game_status(game_state *state) +{ + return state->completed ? +1 : 0; +} + static int game_timing_state(game_state *state, game_ui *ui) { return TRUE; @@ -1802,6 +1826,7 @@ const struct game thegame = { game_redraw, game_anim_length, game_flash_length, + game_status, TRUE, FALSE, game_print_size, game_print, FALSE, /* wants_statusbar */ FALSE, game_timing_state, @@ -1935,13 +1960,13 @@ int main(int argc, char **argv) if (verbose) { tgame = game_text_format(s); - printf(tgame); + fputs(tgame, stdout); sfree(tgame); } soln = solve_specific(s, DIFF_ANY, 0); tgame = game_text_format(s); - printf(tgame); + fputs(tgame, stdout); sfree(tgame); printf("Game was %s.\n\n", soln < 0 ? "impossible" : soln > 0 ? "solved" : "not solved");