private IGameOfLifeGrid ConvertSerializedGridToGameGrid(Grid grid) { int columnCount = grid.Rows.Max(r => r.Cells.Count); GameOfLifeGrid g = new GameOfLifeGrid(columnCount, grid.Rows.Count); for (int rowIndex = 0; rowIndex < grid.Rows.Count; rowIndex++) { for (int columnIndex = 0; columnIndex < grid.Rows[rowIndex].Cells.Count; columnIndex++) { g[rowIndex, columnIndex] = new Cell(grid.Rows[rowIndex].Cells[columnIndex].On ? CellState.Live : CellState.Dead, columnIndex, rowIndex); } } return g; }
private CellCollection GetNeighbors(Cell[,] cells, int rowIndex, int columnIndex) { CellCollection cellCollection = new CellCollection(); int previousRowIndex = rowIndex - 1; int previousColumnIndex = columnIndex - 1; int nextRowIndex = rowIndex + 1; int nextColumnIndex = columnIndex + 1; if (previousRowIndex >= 0) { if (previousColumnIndex >= 0) cellCollection.Add(cells[previousRowIndex, previousColumnIndex]); cellCollection.Add(cells[previousRowIndex, columnIndex]); if (nextColumnIndex < this.Size.Width) cellCollection.Add(cells[previousRowIndex, nextColumnIndex]); } if (nextRowIndex < this.Size.Height) { if (previousColumnIndex >= 0) cellCollection.Add(cells[nextRowIndex, previousColumnIndex]); cellCollection.Add(cells[nextRowIndex, columnIndex]); if (nextColumnIndex < this.Size.Width) cellCollection.Add(cells[nextRowIndex, nextColumnIndex]); } if (previousColumnIndex >= 0) cellCollection.Add(cells[rowIndex, previousColumnIndex]); if (nextColumnIndex < this.Size.Width) cellCollection.Add(cells[rowIndex, nextColumnIndex]); return cellCollection; }