protected override bool Condition(Cell currentCell, Cell neighbourCell) { if (_field[neighbourCell.Row, neighbourCell.Column].IsMine) return true; else return false; }
static void PrintBoard(Cell[,] boardCells, int height, int width) { for (var r = 0; r < height; r++) { for (var c = 0; c < width; c++) { var cell = boardCells[r, c]; if (cell.IsExposed) { if (cell.IsBomb && cell.IsFlipped) { Console.Write("B"); } else if (cell.IsFlipped) { Console.Write("."); } else { Console.Write(cell.Number); } } else { Console.Write("?"); } Console.Write(" "); } Console.WriteLine(); } }
protected override bool NeedCheckNeighbors(Cell currentCell) { if (_field[currentCell.Row, currentCell.Column].Value == 0) return true; else return false; }
public void CellCalculation(Cell currentCell) { UnconditionalAction(currentCell); if (NeedCheckNeighbors(currentCell)) { Cell neighbourCell; if (currentCell.Row > 0) { neighbourCell.Row = currentCell.Row - 1; if (currentCell.Column > 0) { neighbourCell.Column = currentCell.Column - 1; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); } neighbourCell.Column = currentCell.Column; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); if (currentCell.Column + 1 < Width) { neighbourCell.Column = currentCell.Column + 1; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); } } neighbourCell.Row = currentCell.Row; if (currentCell.Column > 0) { neighbourCell.Column = currentCell.Column - 1; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); } if (currentCell.Column + 1 < Width) { neighbourCell.Column = currentCell.Column + 1; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); } if (currentCell.Row + 1 < Height) { neighbourCell.Row = currentCell.Row + 1; if (currentCell.Column > 0) { neighbourCell.Column = currentCell.Column - 1; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); } neighbourCell.Column = currentCell.Column; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); if (currentCell.Column + 1 < Width) { neighbourCell.Column = currentCell.Column + 1; if (Condition(currentCell, neighbourCell)) ConditionalAction(currentCell, neighbourCell); } } } }
protected abstract void UnconditionalAction(Cell currentCell);
protected abstract bool NeedCheckNeighbors(Cell currentCell);
protected abstract void ConditionalAction(Cell currentCell, Cell neighbourCell);
protected override void UnconditionalAction(Cell currentCell) { _field[currentCell.Row, currentCell.Column].IsOpened = true; OpenedCells.Add(currentCell); }
public void cascade(Cell btn) { foreach (Control x in tableroporquemevalemadreelingles.Controls) { if (x is Cell) { if ((btn.id - columnCount) > 0) { if (((btn.id - columnCount - 1) == ((Cell)x).id) && (btn.id % columnCount) != 1) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } if ((btn.id - columnCount) == ((Cell)x).id) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } if (((btn.id - columnCount + 1) == ((Cell)x).id) && (btn.id % columnCount) != 0) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } } if (((btn.id - 1) == ((Cell)x).id) && (btn.id % columnCount) != 1) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } if (((btn.id + 1) == ((Cell)x).id) && (btn.id % columnCount) != 0) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } if ((btn.id + columnCount) <= cellCount) { if (((btn.id + columnCount - 1) == ((Cell)x).id) && (btn.id % columnCount) != 1) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } if ((btn.id + columnCount) == ((Cell)x).id) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } if (((btn.id + columnCount + 1) == ((Cell)x).id) && (btn.id % columnCount) != 0) { if ((((Cell)x).isBlank && !((Cell)x).isRevealed) || (!((Cell)x).isMine && !((Cell)x).isRevealed)) { //((Cell)x).Text = " "; //((Cell)x).ForeColor = Color.Purple; //((Cell)x).isFlagged = false; //((Cell)x).isRevealed = true; //cascade(((Cell)x)); revealCells(((Cell)x)); } } } } } }
protected override void UnconditionalAction(Cell currentCell) { }
protected override void ConditionalAction(Cell currentCell, Cell neighbourCell) { ++_field[currentCell.Row, currentCell.Column].Value; }
/// <summary> /// Add a cell to the board /// </summary> /// <param name="cell">Cell to be added</param> public void AddCell(Cell cell) { Cells.Add(cell); }
protected override void ConditionalAction(Cell currentCell, Cell neighbourCell) { CellCalculation(neighbourCell); }
protected abstract bool Condition(Cell currentCell, Cell neighbourCell);
/// <summary> /// Convert the 1d board to 2d board object /// </summary> /// <returns>Cell[,] representation of the current board</returns> public Cell[,] GetBoardAs2DArray() { var board = new Cell[_gameBoard.Width, _gameBoard.Height]; for (var r = 0; r < _gameBoard.Height; r++) { for (var c = 0; c < _gameBoard.Width; c++) { board[c, r] = _gameBoard.GetCell(r, c); } } return board; }
/// <summary> /// BFS algorithm to reveal all empty cells from a given cell. Exposes "edge" cells as well. /// </summary> /// <param name="cell">starting cell</param> private void RevealEmptyCells(Cell cell) { var queue = new Queue<Cell>(); queue.Enqueue(cell); while (queue.Count > 0) { var currCell = queue.Dequeue(); currCell.IsFlipped = true; _numFlips += 1; if (currCell.Number != 0) continue; var adjCells = GetAdjacentCells(currCell.Row, currCell.Col); foreach (var adjCell in adjCells.Where(adjCell => !adjCell.IsBomb && !adjCell.IsFlipped)) { queue.Enqueue(adjCell); } } }
private void RefreshButton(Cell location) { int buttonIndex = location.Row * _gameSettings.GameFieldHeight + location.Column; if (_game.Field[location.Row, location.Column].IsOpened) { _gameButtons[buttonIndex].Enabled = false; _gameButtons[buttonIndex].BackColor = Color.Silver; } }