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