From cdf4ccc1a943c74c355566bd50284765b2101bf6 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 6 Mar 2025 21:37:58 +0000 Subject: [PATCH] Create countadj helper function --- puzz.c | 22 +++++++++++++++------- puzz.h | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/puzz.c b/puzz.c index 895e595..1778202 100644 --- a/puzz.c +++ b/puzz.c @@ -33,13 +33,7 @@ void gen(void) for (int x = 0; x < WIDTH; ++x) { if (soln[x][y] == MINE) continue; - 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) - continue; - soln[x][y] += soln[xp][yp] == MINE ? 1 : 0; - } - } + soln[x][y] = countadj(soln, x, y, MINE); } } } @@ -91,3 +85,17 @@ status_t probe(int x, int y, puzz_t out) return OK; } + +int countadj(puzz_t field, int x, int y, uint8_t val) +{ + int n = 0; + 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; + n += field[xp][yp] == val ? 1 : 0; + } + } + return n; +} diff --git a/puzz.h b/puzz.h index aa615f4..e31387f 100644 --- a/puzz.h +++ b/puzz.h @@ -20,5 +20,6 @@ typedef enum { DEAD, OK } status_t; void gen(void); void print(void); status_t probe(int x, int y, puzz_t out); +int countadj(puzz_t field, int x, int y, uint8_t val); #endif