public bool WillItLive(Cell cell, List<Cell> neighbours) { var liveNeighbours = neighbours.Count(c => c.IsAlive); if(cell.IsAlive && liveNeighbours < 2) return false; if(cell.IsAlive && liveNeighbours > 3) return false; if(cell.IsAlive && (liveNeighbours == 2 || liveNeighbours == 3)) return true; if(!cell.IsAlive && liveNeighbours == 3) return true; return false; }
public List<Cell> GetNeighbours(Cell cell) { var northwestXY = new { Y = cell.Y - 1, X = cell.X - 1 }; var northXY = new { Y = cell.Y - 1, X = cell.X }; var northeastXY = new { Y = cell.Y - 1, X = cell.X + 1 }; var westXY = new { Y = cell.Y, X = cell.X - 1 }; var eastXY = new { Y = cell.Y, X = cell.X + 1 }; var southwestXY = new { Y = cell.Y + 1, X = cell.X - 1 }; var southXY = new { Y = cell.Y + 1, X = cell.X }; var southeastXY = new { Y = cell.Y + 1, X = cell.X + 1 }; var neighbours = new List<Cell> { getCellFromBoard(northwestXY.X, northwestXY.Y), getCellFromBoard(northXY.X, northXY.Y), getCellFromBoard(northeastXY.X, northeastXY.Y), getCellFromBoard(westXY.X, westXY.Y), getCellFromBoard(eastXY.X, eastXY.Y), getCellFromBoard(southwestXY.X, southwestXY.Y), getCellFromBoard(southXY.X, southXY.Y), getCellFromBoard(southeastXY.X, southeastXY.Y) }; return neighbours; }
private string formatColumn(Cell cell) { return string.Format(" {0}", cell.IsAlive ? "+" : " ");; }