Detect contradictions in update()
This commit is contained in:
parent
49417b4840
commit
4e9a787e36
21
main.c
21
main.c
@ -19,6 +19,7 @@ typedef enum {
|
|||||||
FOUND_MINE,
|
FOUND_MINE,
|
||||||
FOUND_SAFE,
|
FOUND_SAFE,
|
||||||
FOUND_NOTHING,
|
FOUND_NOTHING,
|
||||||
|
FOUND_CONTRADICTION,
|
||||||
} update_res_t;
|
} update_res_t;
|
||||||
|
|
||||||
typedef struct {
|
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 y = 0; y < HEIGHT; ++y) {
|
||||||
for (int x = 0; x < WIDTH; ++x) {
|
for (int x = 0; x < WIDTH; ++x) {
|
||||||
if (state->field[x][y] == UNKNOWN || state->field[x][y] == MINE
|
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;
|
continue;
|
||||||
|
|
||||||
const int mines = countadj(state->field, x, y, MINE);
|
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);
|
const int unknowns = countadj(state->field, x, y, UNKNOWN);
|
||||||
if (unknowns == 0)
|
if (unknowns == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (mines + unknowns < state->field[x][y])
|
||||||
|
return FOUND_CONTRADICTION;
|
||||||
|
|
||||||
if (mines + unknowns == state->field[x][y]) {
|
if (mines + unknowns == state->field[x][y]) {
|
||||||
setadj(state->field, x, y, UNKNOWN, MINE);
|
setadj(state->field, x, y, UNKNOWN, MINE);
|
||||||
return FOUND_MINE;
|
return FOUND_MINE;
|
||||||
@ -110,10 +120,11 @@ static status_t solve(int *turns_out)
|
|||||||
&& (status = probe(x, y, state.field)) == DEAD)
|
&& (status = probe(x, y, state.field)) == DEAD)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
update_res_t update_res;
|
update_res_t res;
|
||||||
do
|
do {
|
||||||
update_res = update(&state, &x, &y);
|
res = update(&state, &x, &y);
|
||||||
while (update_res == FOUND_MINE);
|
assert(res != FOUND_CONTRADICTION);
|
||||||
|
} while (res == FOUND_MINE);
|
||||||
|
|
||||||
if (check(state.field) != OK) {
|
if (check(state.field) != OK) {
|
||||||
printf("Incorrect inference! State:\n");
|
printf("Incorrect inference! State:\n");
|
||||||
@ -122,7 +133,7 @@ static status_t solve(int *turns_out)
|
|||||||
return INCORRECT;
|
return INCORRECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (update_res == FOUND_NOTHING) {
|
if (res == FOUND_NOTHING) {
|
||||||
x = rand() % WIDTH;
|
x = rand() % WIDTH;
|
||||||
y = rand() % HEIGHT;
|
y = rand() % HEIGHT;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user