An implementation of Conway's Game of Life using the p5js.
Basic implementation with support for periodic boundary conditions.
/* Returns the neighbors for the given cell, wrapping around to
opposite side if at boundary */
function getNeighbors(row, col, cells) {
const n = row > 0 ? row - 1 : cells.length - 1;
const s = row == cells.length -1 ? 0 : row + 1;
const e = col == cells[0].length - 1 ? 0 : col + 1;
const w = col > 0 ? col - 1 : cells[0].length - 1;
return [
[n, w], [n, col], [n, e], [row, e],
[s, e], [s, col], [s, w], [row, w]
];
}
See source code.