private bool InNextGeneration(Cell cell) { return _nextGeneration.Any(c => c.X == cell.X && c.Y == cell.Y); }
private IEnumerable<Cell> GetDeadNeighbours(Cell cell) { return GetNeighbours(cell).Where(c => !GetAliveNeighbours(cell).Contains(c)); }
private IEnumerable<Cell> GetNeighbours(Cell cell) { var neighbours = new List<Cell>(); for (var x = -1; x <= 1; x++) for (var y = -1; y <= 1; y++) neighbours.Add(new Cell(cell.X + x, cell.Y + y, cell.Color)); neighbours.Remove(cell); return neighbours; }
private IEnumerable<Cell> GetAliveNeighbours(Cell cell) { return _currentGeneration.Where(neighbour => IsNeighbour(cell, neighbour) && cell != neighbour); }
private ConsoleColor GetColorForRebornCell(Cell cell) { var aliveNeighbours = GetAliveNeighbours(cell); return aliveNeighbours.GroupBy(processedCell => processedCell.Color).OrderByDescending(x => x.Count()).Select(x => x.Key).First(); }
private void CheckIfCellGoesToNextGeneration(Cell cell) { if (WillGoToNextGeneration(cell) && !InNextGeneration(cell)) _nextGeneration.Add(cell); }
private bool WillGoToNextGeneration(Cell cell) { var aliveNeighbours = GetAliveNeighbours(cell).Count(); return (aliveNeighbours >= MinNumberOfCellsForSurvival) && (aliveNeighbours <= MaxNumberOfCellsForSurvival); }
private bool WillGetResurected(Cell cell) { return (GetAliveNeighbours(cell).Count() == NumberOfCellsForRebirth); }
private void RebornDeadNeighbours(Cell cell) { foreach (var deadNeighbour in GetDeadNeighbours(cell)) if (WillGetResurected(deadNeighbour) && !InNextGeneration(deadNeighbour)) { deadNeighbour.Color = GetColorForRebornCell(deadNeighbour); _nextGeneration.Add(deadNeighbour); } }
private bool IsNeighbour(Cell cell, Cell neighbour) { return (Math.Abs(cell.X - neighbour.X) <= 1) && (Math.Abs(cell.Y - neighbour.Y) <= 1); }