Probe known-safe tiles

This commit is contained in:
Camden Dixie O'Brien 2025-03-06 22:05:03 +00:00
parent e36c1c6502
commit 6394dcb819

28
main.c
View File

@ -5,6 +5,7 @@
#include "puzz.h" #include "puzz.h"
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -26,6 +27,23 @@ static void setadj(puzz_t field, int x, int y, uint8_t from, uint8_t to)
} }
} }
static void getadj(puzz_t field, int *x, int *y, uint8_t val)
{
for (int yp = *y - 1; yp < *y + 2; ++yp) {
for (int xp = *x - 1; xp < *x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| (xp == *x && yp == *y))
continue;
if (field[xp][yp] == val) {
*x = xp;
*y = yp;
return;
}
}
}
assert(false);
}
int main(void) int main(void)
{ {
struct timeval tv; struct timeval tv;
@ -46,6 +64,7 @@ int main(void)
int x = rand() % WIDTH; int x = rand() % WIDTH;
int y = rand() % HEIGHT; int y = rand() % HEIGHT;
probe:
if (probe(x, y, field) == DEAD) { if (probe(x, y, field) == DEAD) {
field[x][y] = KILLER; field[x][y] = KILLER;
break; break;
@ -58,9 +77,16 @@ int main(void)
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 (unknowns == 0)
continue;
if (unknowns > 0 && mines + unknowns == field[x][y]) if (mines + unknowns == field[x][y])
setadj(field, x, y, UNKNOWN, MINE); setadj(field, x, y, UNKNOWN, MINE);
if (mines == field[x][y]) {
getadj(field, &x, &y, UNKNOWN);
goto probe;
}
} }
} }