This commit is contained in:
Love 2024-09-04 22:24:05 +02:00
parent 6ed9035713
commit 86025a8836

View File

@ -129,6 +129,7 @@ int gridAliveNeighbors(Grid<Cell> &grid, int row, int col) {
return alive;
}
/**
* Run one Game of Life tick.
* Cells (X) live and die by the following rules:
@ -144,12 +145,16 @@ void gridTick(Grid<Cell> &grid) {
for (int col = 0; col < grid.numCols(); col++) {
int alive = gridAliveNeighbors(grid, row, col);
// A cell with 1 or fewer neighbours dies.
if (alive == 1)
nextGrid.set(row, col, Cell::DEAD);
// Locations with 2 neighbours remain stable.
else if (alive == 2)
nextGrid.set(row, col, grid.get(row, col));
// Locations with 3 neighbours will create life.
else if (alive == 3)
nextGrid.set(row, col, Cell::ALIVE);
// A cell with 4 or more neighbours dies.
else if (alive == 4)
nextGrid.set(row, col, Cell::DEAD);
}