Detect contradictions in update()

This commit is contained in:
Camden Dixie O'Brien 2025-03-22 13:33:38 +00:00
parent 49417b4840
commit 4e9a787e36

21
main.c
View File

@ -19,6 +19,7 @@ typedef enum {
FOUND_MINE,
FOUND_SAFE,
FOUND_NOTHING,
FOUND_CONTRADICTION,
} update_res_t;
typedef struct {
@ -66,6 +67,9 @@ static update_res_t update(state_t *state, int *x_out, int *y_out)
}
}
if (state->mines > NMINES || state->mines + state->unknown < NMINES)
return FOUND_CONTRADICTION;
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (state->field[x][y] == UNKNOWN || state->field[x][y] == MINE
@ -73,10 +77,16 @@ static update_res_t update(state_t *state, int *x_out, int *y_out)
continue;
const int mines = countadj(state->field, x, y, MINE);
if (mines > state->field[x][y])
return FOUND_CONTRADICTION;
const int unknowns = countadj(state->field, x, y, UNKNOWN);
if (unknowns == 0)
continue;
if (mines + unknowns < state->field[x][y])
return FOUND_CONTRADICTION;
if (mines + unknowns == state->field[x][y]) {
setadj(state->field, x, y, UNKNOWN, MINE);
return FOUND_MINE;
@ -110,10 +120,11 @@ static status_t solve(int *turns_out)
&& (status = probe(x, y, state.field)) == DEAD)
break;
update_res_t update_res;
do
update_res = update(&state, &x, &y);
while (update_res == FOUND_MINE);
update_res_t res;
do {
res = update(&state, &x, &y);
assert(res != FOUND_CONTRADICTION);
} while (res == FOUND_MINE);
if (check(state.field) != OK) {
printf("Incorrect inference! State:\n");
@ -122,7 +133,7 @@ static status_t solve(int *turns_out)
return INCORRECT;
}
if (update_res == FOUND_NOTHING) {
if (res == FOUND_NOTHING) {
x = rand() % WIDTH;
y = rand() % HEIGHT;
}