Esempio n. 1
0
 private bool InNextGeneration(Cell cell)
 {
     return _nextGeneration.Any(c => c.X == cell.X && c.Y == cell.Y);
 }
Esempio n. 2
0
 private IEnumerable<Cell> GetDeadNeighbours(Cell cell)
 {
     return GetNeighbours(cell).Where(c => !GetAliveNeighbours(cell).Contains(c));
 }
Esempio n. 3
0
 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;
 }
Esempio n. 4
0
 private IEnumerable<Cell> GetAliveNeighbours(Cell cell)
 {
     return _currentGeneration.Where(neighbour => IsNeighbour(cell, neighbour) && cell != neighbour);
 }
Esempio n. 5
0
        private ConsoleColor GetColorForRebornCell(Cell cell)
        {
            var aliveNeighbours = GetAliveNeighbours(cell);

            return aliveNeighbours.GroupBy(processedCell => processedCell.Color).OrderByDescending(x => x.Count()).Select(x => x.Key).First();
        }
Esempio n. 6
0
 private void CheckIfCellGoesToNextGeneration(Cell cell)
 {
     if (WillGoToNextGeneration(cell) && !InNextGeneration(cell))
         _nextGeneration.Add(cell);
 }
Esempio n. 7
0
 private bool WillGoToNextGeneration(Cell cell)
 {
     var aliveNeighbours = GetAliveNeighbours(cell).Count();
     return (aliveNeighbours >= MinNumberOfCellsForSurvival) && (aliveNeighbours <= MaxNumberOfCellsForSurvival);
 }
Esempio n. 8
0
 private bool WillGetResurected(Cell cell)
 {
     return (GetAliveNeighbours(cell).Count() == NumberOfCellsForRebirth);
 }
Esempio n. 9
0
 private void RebornDeadNeighbours(Cell cell)
 {
     foreach (var deadNeighbour in GetDeadNeighbours(cell))
         if (WillGetResurected(deadNeighbour) && !InNextGeneration(deadNeighbour))
         {
             deadNeighbour.Color = GetColorForRebornCell(deadNeighbour);
             _nextGeneration.Add(deadNeighbour);
         }
 }
Esempio n. 10
0
 private bool IsNeighbour(Cell cell, Cell neighbour)
 {
     return (Math.Abs(cell.X - neighbour.X) <= 1) && (Math.Abs(cell.Y - neighbour.Y) <= 1);
 }