Create countadj helper function

This commit is contained in:
Camden Dixie O'Brien 2025-03-06 21:37:58 +00:00
parent 01d5376f31
commit cdf4ccc1a9
2 changed files with 16 additions and 7 deletions

22
puzz.c
View File

@ -33,13 +33,7 @@ void gen(void)
for (int x = 0; x < WIDTH; ++x) { for (int x = 0; x < WIDTH; ++x) {
if (soln[x][y] == MINE) if (soln[x][y] == MINE)
continue; continue;
for (int yp = y - 1; yp < y + 2; ++yp) { soln[x][y] = countadj(soln, x, y, MINE);
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;
}
}
} }
} }
} }
@ -91,3 +85,17 @@ status_t probe(int x, int y, puzz_t out)
return OK; 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;
}

1
puzz.h
View File

@ -20,5 +20,6 @@ typedef enum { DEAD, OK } status_t;
void gen(void); void gen(void);
void print(void); void print(void);
status_t probe(int x, int y, puzz_t out); status_t probe(int x, int y, puzz_t out);
int countadj(puzz_t field, int x, int y, uint8_t val);
#endif #endif