/// <summary> /// Main gameplay loop /// </summary> public void Run() { // initial click is random Random rand = new Random(); uint randRow = (uint)rand.Next(0, (int)m_minefield.GetRows()); uint randCol = (uint)rand.Next(0, (int)m_minefield.GetCols()); uint[] nextCellToOpen = new uint[] { randRow, randCol }; // main gameplay loop while (m_gameState == GameState.Playing) { // Prompt user Console.Write("Press Enter for next step!\n"); // Wait for enter to proceed Console.ReadKey(); // Display the cell opened next Console.Write("Opened Row: " + (int)nextCellToOpen[0] + ", Col: " + (int)nextCellToOpen[1] + '\n'); // Open the cell if failed, game over m_minefield.OpenCell(nextCellToOpen[0], nextCellToOpen[1]); // Figure out the next cell to open if the game isn't over if (m_gameState == GameState.Playing) { nextCellToOpen = m_metalDetector.DecideNextCellToOpen(); } // Display updated minefield m_minefield.Draw(); } // End game switch (m_gameState) { case GameState.GameOver: Console.WriteLine("Game Over"); break; case GameState.GameWin: Console.WriteLine("You Win!"); break; default: Console.WriteLine("Something went wrong..."); break; } }
/// <summary> /// Finds the random closed cell to open. /// </summary> /// <returns>The random cell to open.</returns> public uint[] FindRandomCellToOpen() { bool found = false; uint randRow = 0; uint randCol = 0; while (!found) { Random rand = new Random(); randRow = (uint)rand.Next(0, (int)m_minefield.GetRows()); randCol = (uint)rand.Next(0, (int)m_minefield.GetCols()); // Checking the random cell is a closed one and not flagged if (!m_minefield.GetField()[randRow, randCol].GetIsOpen() && m_minefield.GetField()[randRow, randCol].GetState() != CellState.Flagged) { found = true; } } return(new uint[] { randRow, randCol }); }