Question

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];
    }

}

in the private section of the class "MineSweeperBoard" there is **mined; **revealed; **flagged; why is my constructor crashing (due to memory access error) before it is creating these arrays?

Thank you so much for your help, I just don't understand why it's not correctly allocating this array

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

this is what is called from main, crashes as soon as it is constructed

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;
Was it helpful?

Solution

You must initialize width and height before utilizing them as the array dimensions.

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];
}

OTHER TIPS

The variables mined, revealed, and flagged must be initialized before this can be run.

They must point a pointer ready to accept the allocations:

int *p1;
mined = &p1;
**mined = new int * [mine_count];

Either that, or replace ** with *—depending on the types of these variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top