commit 532da220dbb29fb0d09cdce2473634074c487812
parent d0b08002961ba6d30a87003747cbc56f70161908
Author: aabacchus <ben@bvnf.space>
Date: Tue, 28 Sep 2021 12:19:24 +0100
make the board wrap around like a torus
Diffstat:
M | rules.c | | | 22 | ++++++++-------------- |
1 file changed, 8 insertions(+), 14 deletions(-)
diff --git a/rules.c b/rules.c
@@ -40,8 +40,14 @@ evolve(short *cells, int nx, int ny) {
int
alive(int x, int y, short *cells, int nx, int ny) {
- if (y < 0 || y >= ny || x < 0 || x >= nx)
- return WHATS_OUTSIDE;
+ if (y < 0)
+ y += ny;
+ else if (y >= ny)
+ y -= ny;
+ if (x < 0)
+ x += nx;
+ else if (x >= nx)
+ x -= nx;
return (int) *(cells + y * nx + x);
}
@@ -50,19 +56,7 @@ neighbours(int x, int y, short *cells, int nx, int ny) {
int n = 0;
int x0, y0;
for (y0 = y - 1; y0 <= y + 1; y0++) {
- /*
- if (y0 < 0 || y0 >= ny) {
- n += WHATS_OUTSIDE;
- continue;
- }
- */
for (x0 = x - 1; x0 <= x + 1; x0++) {
- /*
- if (x0 < 0 || x0 >= nx) {
- n += WHATS_OUTSIDE;
- continue;
- }
- */
if (alive(x0, y0, cells, nx, ny))
n++;
}