// Constants
// Cell initialization
function setup() {
// Setup boilerplate
cells[cellIndex(5, 5)] = 1;
let neighbors = neighborCells(5, 5);
for (let i=0;i<neighbors.length;i++) {
cells[neighbors[i]] = 1;
}
renderCells();
}
function northIndex(x, y) {
if (y===0) { return -1; }
return cellIndex(x, y-1);
}
function southIndex(x, y) {
if (y===ROWS-1) { return -1; }
return cellIndex(x, y+1);
}
function eastIndex(x, y) {
if (x===COLUMNS-1) { return -1; }
return cellIndex(x+1, y);
}
function westIndex(x, y) {
if (x===0) { return -1; }
return cellIndex(x-1, y);
}
function neighborCells(x, y) {
let neighbors = [];
neighbors.push(northIndex(x, y));
neighbors.push(southIndex(x, y));
neighbors.push(eastIndex(x, y));
neighbors.push(westIndex(x, y));
return neighbors.filter(n => n !== -1);
}
// cellIndex()
// renderCells()
Prevents attempting to return neighbor cells that are "off-grid".