Comments
This commit is contained in:
parent
ecccca869d
commit
e85b72191f
@ -129,6 +129,7 @@ int gridAliveNeighbors(Grid<Cell> &grid, int row, int col) {
|
|||||||
|
|
||||||
return alive;
|
return alive;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run one Game of Life tick.
|
* Run one Game of Life tick.
|
||||||
* Cells (X) live and die by the following rules:
|
* 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++) {
|
for (int col = 0; col < grid.numCols(); col++) {
|
||||||
int alive = gridAliveNeighbors(grid, row, col);
|
int alive = gridAliveNeighbors(grid, row, col);
|
||||||
|
|
||||||
|
// A cell with 1 or fewer neighbours dies.
|
||||||
if (alive == 1)
|
if (alive == 1)
|
||||||
nextGrid.set(row, col, Cell::DEAD);
|
nextGrid.set(row, col, Cell::DEAD);
|
||||||
|
// Locations with 2 neighbours remain stable.
|
||||||
else if (alive == 2)
|
else if (alive == 2)
|
||||||
nextGrid.set(row, col, grid.get(row, col));
|
nextGrid.set(row, col, grid.get(row, col));
|
||||||
|
// Locations with 3 neighbours will create life.
|
||||||
else if (alive == 3)
|
else if (alive == 3)
|
||||||
nextGrid.set(row, col, Cell::ALIVE);
|
nextGrid.set(row, col, Cell::ALIVE);
|
||||||
|
// A cell with 4 or more neighbours dies.
|
||||||
else if (alive == 4)
|
else if (alive == 4)
|
||||||
nextGrid.set(row, col, Cell::DEAD);
|
nextGrid.set(row, col, Cell::DEAD);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user