Пример #1
0
 protected override bool Condition(Cell currentCell, Cell neighbourCell)
 {
     if (_field[neighbourCell.Row, neighbourCell.Column].IsMine)
         return true;
     else
         return false;
 }
Пример #2
0
 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();
     }
 }
Пример #3
0
 protected override bool NeedCheckNeighbors(Cell currentCell)
 {
     if (_field[currentCell.Row, currentCell.Column].Value == 0)
         return true;
     else
         return false;
 }
Пример #4
0
        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);
                    }
                }
            }
        }
Пример #5
0
 protected abstract void UnconditionalAction(Cell currentCell);
Пример #6
0
 protected abstract bool NeedCheckNeighbors(Cell currentCell);
Пример #7
0
 protected abstract void ConditionalAction(Cell currentCell, Cell neighbourCell);
Пример #8
0
 protected override void UnconditionalAction(Cell currentCell)
 {
     _field[currentCell.Row, currentCell.Column].IsOpened = true;
     OpenedCells.Add(currentCell);
 }
Пример #9
0
 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));
                     }
                 }
             }
         }
     }
 }
Пример #10
0
 protected override void UnconditionalAction(Cell currentCell)
 {
 }
Пример #11
0
 protected override void ConditionalAction(Cell currentCell, Cell neighbourCell)
 {
     ++_field[currentCell.Row, currentCell.Column].Value;
 }
Пример #12
0
 /// <summary>
 /// Add a cell to the board
 /// </summary>
 /// <param name="cell">Cell to be added</param>
 public void AddCell(Cell cell)
 {
     Cells.Add(cell);
 }
Пример #13
0
 protected override void ConditionalAction(Cell currentCell, Cell neighbourCell)
 {
     CellCalculation(neighbourCell);
 }
Пример #14
0
 protected abstract bool Condition(Cell currentCell, Cell neighbourCell);
Пример #15
0
 /// <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;
 }
Пример #16
0
 protected override void ConditionalAction(Cell currentCell, Cell neighbourCell)
 {
     CellCalculation(neighbourCell);
 }
Пример #17
0
 /// <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);
         }
     }
 }
Пример #18
0
 protected override void UnconditionalAction(Cell currentCell)
 {
     _field[currentCell.Row, currentCell.Column].IsOpened = true;
     OpenedCells.Add(currentCell);
 }
Пример #19
0
 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;
     }
 }