Guess and update in a loop

This commit is contained in:
Camden Dixie O'Brien 2025-03-22 13:33:38 +00:00
parent 7cdf4dcae6
commit 699e6b118d

20
main.c
View File

@ -5,6 +5,7 @@
#include "puzz.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -40,23 +41,36 @@ int main(void)
puzz_t field;
memset(field, UNKNOWN, sizeof(field));
bool solved = false;
do {
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
if (probe(x, y, field) == DEAD) {
field[x][y] = KILLER;
fprintf(stderr, "Dead\n");
} else {
break;
}
for (y = 0; y < HEIGHT; ++y) {
for (x = 0; x < WIDTH; ++x) {
if (field[x][y] == UNKNOWN || field[x][y] == MINE)
continue;
const int mines = countadj(field, x, y, MINE);
const int unknowns = countadj(field, x, y, UNKNOWN);
if (mines + unknowns == field[x][y])
if (unknowns > 0 && mines + unknowns == field[x][y])
setadj(field, x, y, UNKNOWN, MINE);
}
}
solved = true;
for (y = 0; y < HEIGHT; ++y) {
for (x = 0; x < WIDTH; ++x)
solved &= field[x][y] != UNKNOWN;
}
} while (!solved);
puts(solved ? "Solved" : "Dead");
putchar('\n');
for (int y = 0; y < HEIGHT; ++y) {