commit 520828a3467da9dddaa901e27670b0a5c9cb0338 parent 88590b291d4cb0e3534f4e2c50a4e00bc818ae98 Author: Ryan Jeffrey <ryan@ryanmj.xyz> Date: Tue, 20 Apr 2021 14:13:31 -0700 Get the total number of alive neighbors. Diffstat:
M | conway.js | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/conway.js b/conway.js @@ -62,6 +62,29 @@ function init() { } +// get the total number of neighboring cells that are alive. +function totalAliveNeighbors() +{ + let sum = 0; + let newX = 0; + let newY = 0; + for(let i = -1; i < 2; i++) + { + for(let j = -1; j < 2; j++) + { + newX = (x + i + rows) % rows; + newY = (y + j + cols) % cols; + if(cells[newX][newY] == lifeState.alive) + sum++; + } + } + + if(cells[x][y] == lifeState.alive) + sum--; + + return sum; +} + function startAnimating(fps) { fpsInterval = 1000 / fps;