如何为扫雷 C++ 程序声明一个工作变量?
How do I declare a working variable for minesweeper C++ program?
我目前正在解决一个扫雷 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
您将游戏板传递给这些函数中的每一个,但从未使用过参数
好的,你至少有两个问题。
最大的一个:
您正在设置变量 currentCell
,但您的实际意思是设置 面板中的一个单元格!
更改对此的所有分配和引用:
board[row][column]
第二题:
打印函数不打印换行符。只需将 cout << endl;
添加到嵌套 for 循环的末尾即可修复它。
第三题:
这实际上不是问题,但它让我的眼睛受伤;-)
您有:
for (int row = 0; row < MAX_ROWS; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
嵌套 if 没有意义,因为它 总是 计算为 true
。
我目前正在解决一个扫雷 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
您将游戏板传递给这些函数中的每一个,但从未使用过参数
好的,你至少有两个问题。
最大的一个:
您正在设置变量 currentCell
,但您的实际意思是设置 面板中的一个单元格!
更改对此的所有分配和引用:
board[row][column]
第二题:
打印函数不打印换行符。只需将 cout << endl;
添加到嵌套 for 循环的末尾即可修复它。
第三题:
这实际上不是问题,但它让我的眼睛受伤;-)
您有:
for (int row = 0; row < MAX_ROWS; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
嵌套 if 没有意义,因为它 总是 计算为 true
。