/// <summary> /// Decides the next cell to open without knowing the mine locations /// </summary> /// <returns>The next cell to open.</returns> public uint[] DecideNextCellToOpen() { // If a mine is found, search for a potentialy empty cell to click if (FlagClosedCellsIfPossible()) { for (int row = 0; row < m_fieldRows; row++) { for (int col = 0; col < m_fieldCols; col++) { // if the current cell is closed or empty it of no use here if (!m_minefield.GetField()[row, col].GetIsOpen() || m_minefield.GetField()[row, col].GetState() == CellState.Empty) { continue; } // For this cell, collect all nearby closed and flagged cells List <Cell> closedAndFlagged = new List <Cell>(); List <Cell> closedNotFlagged = new List <Cell>(); foreach (Cell cell in m_minefield.GetSurroundingCells(row, col)) { if (cell.GetIsOpen()) { continue; } if (cell.GetState() == CellState.Flagged) { closedAndFlagged.Add(cell); } else { closedNotFlagged.Add(cell); } } // If the amount of flagged cells are greater or = to the surrounding mine value // it means that any closed cells that are not flagged cannot be hiding a mine int mineCount = (int)Char.GetNumericValue((char)m_minefield.GetField()[row, col].GetState()); if (closedAndFlagged.Count >= mineCount && closedNotFlagged.Count > 0) { uint nextRow = (uint)closedNotFlagged[0].GetPosition()[0]; uint nextCol = (uint)closedNotFlagged[0].GetPosition()[1]; return(new uint[] { nextRow, nextCol }); } } } return(FindRandomCellToOpen()); } else { return(FindRandomCellToOpen()); } }
/// <summary> /// Initializes a new instance of the metal detector class. /// </summary> /// <param name="_mineField">Mine field.</param> public MetalDetector(Minefield _mineField) { m_minefield = _mineField; m_fieldRows = m_minefield.GetField().GetLength(0); m_fieldCols = m_minefield.GetField().GetLength(1); }