public void NextGeneration() { var modifiedCells = new List <Tuple <Tuple <int, int>, CellState> >(); for (var row = 0; row < ActiveGeneration.Rows; row++) { for (var column = 0; column < ActiveGeneration.Columns; column++) { var cell = ActiveGeneration.GetCell(row, column); var neighbours = GetNumberOfCellNeighbours(cell, ActiveGeneration); if (cell.State == CellState.Alive && (neighbours < UnderpopulationRule || neighbours > OverpopulationRule)) { modifiedCells.Add(new Tuple <Tuple <int, int>, CellState>(cell.Position, CellState.Dead)); } else if ((cell.State == CellState.Dead || cell.State == CellState.Empty) && neighbours == ReproductionRule) { modifiedCells.Add(new Tuple <Tuple <int, int>, CellState>(cell.Position, CellState.Alive)); } } } if (modifiedCells.Any()) { GenerationNumber++; Parallel.ForEach(modifiedCells, tuple => ActiveGeneration.SetCell(tuple.Item1.Item1, tuple.Item1.Item2, tuple.Item2)); } }
public Cell GetCell(int row, int column) { return(ActiveGeneration.GetCell(row, column)); }