c++ - 如何为扫雷 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# - TLS 1.2 和 .NET 4.7.2 根据客户端
- css - CSS动画只在谷歌浏览器中工作?
- c# - 如何正确使用 deep-pro 翻译服务?
- mysql - 如何选择一个特定的行
- python - 更快的方法来大量求解由具有多个变量参数的多个非线性方程控制的多个参数?
- node.js - 带有 NodeJS 的 Morphcast SDK
- javascript - 如何返回 navigator.geolocation.getCurrentPosition 的值以在我的代码中的其他地方使用?
- python - 使用 Selenium 从 Facebook 获取照片的点赞和评论
- python - 在 Flask 上显示线性回归模型时出错
- ibm-midrange - 如何将 AS400 .mac 宏转换为 PCOMM (vbs) 宏?