Guess and update in a loop

This commit is contained in:
Camden Dixie O'Brien 2025-03-06 21:54:21 +00:00
parent eeea09eaa2
commit e36c1c6502

30
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));
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
if (probe(x, y, field) == DEAD) {
field[x][y] = KILLER;
fprintf(stderr, "Dead\n");
} else {
bool solved = false;
do {
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
if (probe(x, y, field) == DEAD) {
field[x][y] = KILLER;
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) {