From 78c0ad81e20290512667d877413a5dae5174ee26 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 6 Mar 2025 21:13:49 +0000 Subject: [PATCH] Implement probes --- puzz.c | 41 +++++++++++++++++++++++++++++++++++++++++ puzz.h | 2 ++ 2 files changed, 43 insertions(+) diff --git a/puzz.c b/puzz.c index f86dbb7..895e595 100644 --- a/puzz.c +++ b/puzz.c @@ -5,12 +5,16 @@ #include "puzz.h" +#include #include #include #include #include +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; +} diff --git a/puzz.h b/puzz.h index 4a639e3..aa615f4 100644 --- a/puzz.h +++ b/puzz.h @@ -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