public static Grid CreateGameGrid(GameController controller, Cell[,] game) { Grid grid = new Grid(); int createdColumns = 0; for (int row = 0; row < game.GetLength(0); row++) { grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(25, GridUnitType.Pixel) }); for (int column = 0; column < game.GetLength(1); column++) { // Create columns only once if (createdColumns < game.GetLength(1)) { grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25, GridUnitType.Pixel) }); createdColumns++; } // Create cell controls CellControl cell = new CellControl(controller, game[row, column]); Grid.SetRow(cell, row); Grid.SetColumn(cell, column); grid.Children.Add(cell); } } return grid; }
public void MarkCell(Cell cell) { if (cell.IsToggled) { return; } cell.IsMarked = !cell.IsMarked; if (cell.IsMarked) { NumberOfMarkedCells++; } else { NumberOfMarkedCells--; } }
public static List<Cell> GetNeighbours(this Cell[,] cells, Cell cell) { List<Cell> neighbours = new List<Cell>(); int lowerRow = cell.Row - 1; int upperRow = cell.Row + 1; int lowerColumn = cell.Column - 1; int upperColumn = cell.Column + 1; for (int r = lowerRow; r <= upperRow; r++) { if (r >= cells.GetLength(0)) { continue; } for (int c = lowerColumn; c <= upperColumn; c++) { if (r < 0 || c < 0 || (r == cell.Row && c == cell.Column) || c >= cells.GetLength(1)) { continue; } neighbours.Add(cells[r, c]); } } return neighbours; }
public void OpenNeighbours(Cell cell) { if (!cell.IsToggled) { OpenCell(cell); } else { // Open cells and all depending cells var neighbours = Cells.GetNeighbours(cell); var markedNeighbours = neighbours.Count(n => n.IsMarked); if (markedNeighbours == cell.Number) { neighbours.ForEach(ToggleCore); CheckGameWon(); } } }
public void OpenCell(Cell cell) { ToggleCore(cell); CheckGameWon(); }
private void ToggleCore(Cell cell) { if (cell.IsToggled || cell.IsMarked) { return; } if (isFirstStep) { OnFirstStep(); } // Open cells and all depending cells cell.IsToggled = true; if (cell.Number == 0) { Cells.GetNeighbours(cell).ForEach(ToggleCore); } if (cell.Type == CellType.Mine) { OnGameOver(); isGameOver = true; ToggleAllCells(); } isFirstStep = false; }
private static Cell[,] CreateCells(int rows, int columns) { Cell[,] cells = new Cell[rows, columns]; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { cells[row, column] = new Cell(row, column); } } return cells; }