function computeNextCells() {
lastCells.set(cells);
let index = 0;
let neighborCount = 0;
for (let y = 0; y < gridHeight; y++) {
let up = y - 1;
let down = y + 1;
if (up < 0) up = gridHeight - 1;
else if (down >= gridHeight) down = 0;
up *= gridWidth;
down *= gridWidth;
const center = y * gridWidth;
for (let x = 0; x < gridWidth; x++) {
let left = x - 1;
let right = x + 1;
if (left < 0) left = gridWidth - 1;
else if (right >= gridWidth) right = 0;
neighborCount =
lastCells[up + left] +
lastCells[up + x] +
lastCells[up + right] +
lastCells[center + left] +
lastCells[center + right] +
lastCells[down + left] +
lastCells[down + x] +
lastCells[down + right];
if (neighborCount >= 6 && neighborCount <= 9) {
cells[index] = 0;
}
else if (neighborCount >= 10) {
cells[index] = 1;
}
else if (lastCells[index] > 0) {
if (neighborCount <= 3 || neighborCount >= 7) cells[index] = 0;
}
else {
if (neighborCount == 5) cells[index] = 1;
else if (neighborCount == 3) cells[index] = 2;
}
index++;
}
}
}