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”类的私人部分,有**开采; **透露; **标记; 为什么我的构造函数在创建这些数组之前崩溃(由于内存访问错误)?
非常感谢你的帮助,我只是不明白为什么它没有正确分配这个数组
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
这是从主,崩溃一旦被构造就被称为
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;
答案 0 :(得分:1)
在将width
和height
用作数组维度之前,您必须对其进行初始化。
MineSweeperBoard::MineSweeperBoard(int board_width, int board_height, int mine_count)
{
width = board_width;
height = board_height;
**mined = new int * [mine_count];
**revealed = new int * [width*height];
**flagged =new int * [width*height];
}
答案 1 :(得分:1)
必须初始化变量mined
,revealed
和flagged
才能运行。
他们必须指向一个准备好接受分配的指针:
int *p1;
mined = &p1;
**mined = new int * [mine_count];
或者用**
替换*
- 取决于这些变量的类型。