【发布时间】:2014-04-30 00:07:58
【问题描述】:
MineSweeperBoard::MineSweeperBoard(int board_width, int board_height, int mine_count)
{
width = board_width;
height = board_height;
mined = new bool*[board_width];
for (int i = 0; i < board_width; i++){
mined[i] = new bool[board_height];
}
flagged = new bool*[board_width];
for (int i = 0; i < board_width; i++){
flagged[i] = new bool[board_height];
}
revealed = new bool*[board_width];
for (int i = 0; i < board_width; i++){
revealed[i] = new bool[board_height];
}
}
在“MineSweeperBoard”类的私有部分有 **mined; **揭晓; **标记; 为什么我的构造函数在创建这些数组之前崩溃(由于内存访问错误)?
非常感谢您的帮助,我只是不明白为什么它没有正确分配这个数组
class MineSweeperBoard
{
public:
// Initialize a board with a given width and height, containing the
// given number of randomly-placed mines. Nothing should be revealed
// or flagged. If there are more mines than spaces for them, fill
// the entire board with mines.
MineSweeperBoard(int board_width, int board_height, int mine_count);
// Clean up the board, freeing any dynamically allocated memory.
//~MineSweeperBoard();
// Get the size of the board.
int get_width() const;
int get_height() const;
// Reveal a square.
// If this square wasn't already revealed, and if the number of
// adjacent mines equals the number of adjacent flags, recursively
// reveal all the surrounding squares that aren't flagged.
void reveal(Position p);
// Put a flag on a square, or remove a flag that is
// already there. Returns true if we placed a flag, false if
// we removed one.
bool flag(Position p);
// Return the appearance of the square (what will be
// displayed to the player) as a single character.
char appearance(Position p) const;
// Display the entire board to an output stream. Prints
// a header with the column numbers, and prints the row
// number on each line. For example,
// | 0 1 2 3 4
// ---+---------------
// 0 | 1 / . . .
// 1 | 1 2 . . .
// 2 | 0 1 . . .
// 3 | 0 1 2 1 1
// 4 | 0 0 0 0 0
void display() const;
// Returns true if the player won (every square with a mine
// is flagged, and every cell without a mine is revealed).
bool won() const;
// Returns true if the player lost (there is a revealed
// mine).
bool lost() const;
// Reveal everything (losing the game in the process)
void give_up();
private:
// Returns a list of all the positions adjacent to p. If p
// is in the middle of the board, it has eight neighbors,
// but if it is on an edge or corner it will have fewer.
PositionList adjacent(Position p) const;
// Return the number of mines or flags adjacent to a square.
int adjacent_mines(Position p) const;
int adjacent_flags(Position p) const;
// Size of the board.
int width;
int height;
// Dynamically allocated 2D arrays indicating which squares are
// revealed, which are mined, and which are flagged.
bool **revealed;
bool **mined;
bool **flagged;
};
#endif
这是从main调用的,一构造就崩溃
int main()
{
srand(static_cast<unsigned int>(time(NULL)));
int width;
int height;
int mines;
cout << "Please enter a board height: 5-20 ";
cin >> height;
cin.clear();
cout << endl << "Please enter the board width: (5-20) ";
cin >> width;
cin.clear();
cout << endl << "Please enter the number of mines: ";
cin >> mines;
cin.clear();
MineSweeperBoard board(width, height, mines);
Position p;
【问题讨论】:
-
您确定需要单独的地雷、旗帜和启示数组吗? AFAICS,单个字节
unsigned char有足够的空间。使用低 3 位表示相邻的地雷,1 表示已开采,1 表示已显示,哇,还剩 3 位。 -
我更新了代码,但是我的构造函数仍然崩溃,我定义了 bool mined; bool **显示;和 bool标记在 MineSweeperBoard 的私有部分
-
请显示类定义。
-
展示你如何构造这个类的对象
-
好的,我添加了来自 main 的调用。
标签: c++ arrays pointers dynamic constructor