/// <summary> /// Checks if 2 cells are neighbours /// </summary> /// <param name="cell1">Cell 1</param> /// <param name="cell2">Cell 2</param> /// <returns>True if cells are neighbours, else false</returns> private bool AreNeighbours(Cell cell1, Cell cell2) { if (cell1 == cell2) return false; if (Math.Abs(cell1.XPos - cell2.XPos) > 1) return false; if (Math.Abs(cell1.YPos - cell2.YPos) > 1) return false; return true; }
/// <summary> /// Check if cell should come to life /// </summary> /// <param name="cell">Cell</param> /// <returns>true if cell should come to life, else false if cell should remain dead</returns> private bool ShouldAwake(Cell cell) { if (cell.IsAlive) return false; var noOfLiveNeighbours = GetCellNeighbours(cell).Count(n => n.IsAlive); return (noOfLiveNeighbours == 3); }
/// <summary> /// Gets all neighbours of a given cell /// </summary> /// <param name="cell">Cell</param> /// <returns>All neighbors of a cell</returns> private IEnumerable<Cell> GetCellNeighbours(Cell cell) { return _cells.Where(c => AreNeighbours(c, cell)); }
/// <summary> /// Check if a cell should die /// </summary> /// <param name="cell">Cell</param> /// <returns>true if cell should die,else false if cell should live</returns> private bool ShouldDie(Cell cell) { if (!cell.IsAlive) return false; var noOfLiveNeighbours = GetCellNeighbours(cell).Count(n => n.IsAlive); return (noOfLiveNeighbours < 2 || noOfLiveNeighbours > 3); }