首页 > 解决方案 > 如何为扫雷 C++ 程序声明一个工作变量?

问题描述

我目前正在解决一个扫雷 C++ 程序,当程序完成时我无法接收到正确的输出。我知道这是一个逻辑错误,很可能来自“currentCell”变量,但我不知道如何使它工作。我的输出最终是这样的:____________________________________________________________________________________________________ ----

#include <iostream>
#include <iomanip>
#include <time.h>

using namespace std;

// Global Constants
const int MAX_ROWS = 10;
const int MAX_COLUMNS = 10;
const int EMPTY_SQUARE_DIGIT = 0;
const char EMPTY_SQUARE_SYMBOL = '_'; 
const int BOMB_DIGIT = -1;
const char BOMB_SYMBOL = '#';
char currentCell = 0;

// Function Prototypes
void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);
void insertTheMineClues(int board[MAX_ROWS][MAX_COLUMNS]);
void incrementTheNeighborSquares(int board[MAX_ROWS][MAX_COLUMNS], int row, int column);


// ############################################
int main(void)
{
   int gameBoard[MAX_ROWS][MAX_COLUMNS];

   srand(time(NULL));
   fillTheGameBoard(gameBoard);
   insertTheMineClues(gameBoard);
   displayTheGameBoard(gameBoard);
   return 0;
} // End main


// ############################################
void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
   for (int row = 0; row < MAX_ROWS; row++)
   {
      if ( (row >= 0) && (row < MAX_ROWS) )
      {
         for (int column = 0; column < MAX_COLUMNS; column++)
         {
            if (rand() % (MAX_ROWS - 3) == 0)
            {
               currentCell = BOMB_DIGIT;
            }
            else
            {
               currentCell = EMPTY_SQUARE_DIGIT;
            }
         }
      }
   }                    
} // End fillTheGameBoard


// ############################################
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
   for (int row = 0; row < MAX_ROWS; row++)
   {
      if ( (row >= 0) && (row < MAX_ROWS) )
      {
         for (int column = 0; column < MAX_COLUMNS; column++)
         {
            if (currentCell == BOMB_DIGIT)
            {
               cout << BOMB_SYMBOL;
            }
            else if (currentCell == EMPTY_SQUARE_DIGIT)
            {
               cout << EMPTY_SQUARE_SYMBOL;
            }
            else
            {
               cout << currentCell;
            }
         }
      }
   }      
} // End displayTheGameBoard


// ############################################
void insertTheMineClues(int board[MAX_ROWS][MAX_COLUMNS])
{
   for (int row = 0; row < MAX_ROWS; row++)
   {
      if ( (row >= 0) && (row < MAX_ROWS) )
      {
         for (int column = 0; column < MAX_COLUMNS; column++)
         {
            if (currentCell == BOMB_DIGIT)
            {
               incrementTheNeighborSquares(board, row, column);
            }
         }
      }   
   }                  
} // End insertTheMineClues


// ############################################
// The function definition below is finished.  Make no changes to it.
void incrementTheNeighborSquares(int board[MAX_ROWS][MAX_COLUMNS], int bombRow, int 
bombColumn)
{
   for (int row = bombRow - 1; row <= bombRow + 1; row++)
   {
      if ( (row >= 0) && (row < MAX_ROWS) )     
      {   
         for (int column = bombColumn - 1; column <= bombColumn + 1; column++)
         {
            if ( (column >= 0) && (column < MAX_COLUMNS) && (board[row][column] != BOMB_DIGIT) )      
               board[row][column]++;            
         } // End for column       
      } // End if   
   } // End for row
} // incrementTheNeighborSquares

标签: c++minesweeper

解决方案


您将游戏板传递给这些函数中的每一个,但从未使用过该参数


推荐阅读