Implement probes

This commit is contained in:
Camden Dixie O'Brien 2025-03-06 21:13:49 +00:00
parent cf84ae2017
commit 78c0ad81e2
2 changed files with 43 additions and 0 deletions

41
puzz.c
View File

@ -5,12 +5,16 @@
#include "puzz.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { NO = 0, YES = 1 };
static puzz_t soln;
static puzz_t scanned;
void gen(void)
{
@ -50,3 +54,40 @@ void print(void)
}
putchar('\n');
}
static void scan_copy(int x, int y, puzz_t out)
{
assert(x >= 0 && x < WIDTH);
assert(y >= 0 && y < HEIGHT);
out[x][y] = soln[x][y];
scanned[x][y] = YES;
if (soln[x][y] != 0)
return;
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
|| scanned[xp][yp] == YES)
continue;
scan_copy(xp, yp, out);
}
}
}
status_t probe(int x, int y, puzz_t out)
{
printf("Probing: %d,%d\n", x, y);
assert(x >= 0 && x < WIDTH);
assert(y >= 0 && y < HEIGHT);
if (soln[x][y] == MINE)
return DEAD;
memset(scanned, NO, sizeof(scanned));
scan_copy(x, y, out);
return OK;
}

2
puzz.h
View File

@ -15,8 +15,10 @@
enum { MINE = 0xff };
typedef uint8_t puzz_t[WIDTH][HEIGHT];
typedef enum { DEAD, OK } status_t;
void gen(void);
void print(void);
status_t probe(int x, int y, puzz_t out);
#endif