Search code examples
c++vectorminesweeper2d-vector

2D vector issue in c++


this is part of a larger basic minesweeper program. In the main cpp file of this program, I am loading all of the values of a matrix into a 2d vector of type int. Basically this matrix is filled with 1's and 0's which will be used to indicate whether a mine is there or not. I have confirmed through cout's that both the original vector and the referenced vector are outputting the matrices I loaded into them.

I have create a struct cell which contains 3 various conditions that it can be in. I have created a 2d vector of type cell which basically creates 3 vectors simultaneously. I am having trouble loading the referenced vector's values of 1's and 0's into minesweeperBoard[i][j].isMine.

Here is my code:

include <iostream>
include <vector>

using namespace std;

struct cell{
int state;                      //( 0 hidden, 1 revealed, 2 marked)
int value;                      //(-1 mine, 0 no surrounding, # > 0 number of surrounding mines)
bool isMine;
};


void minesweeper(int row, int col, int numOfMines, vector<vector<int> >&mineField) {

rowNum = row;
colNum = col;
minesNum = numOfMines;

    int temp;

for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < colNum; j++) {
        temp=mineField[i][j];
            if (temp ==0) {
                minesweeperBoard[i][j].isMine = false;  //will set minesweeper to false if the condition is met
            }
            else {
                minesweeperBoard[i][j].isMine = true;   //wil set minesweeper to true if the condition is met
            }
        }
    }
}

The program is compiling but throws an error once it reaches this point. What am I doing wrong is this not how I should transfer the states to the isMine vector.

In my main, I open the file and read several pieces of data, the rows, columns, and amount of mines. Then the file contains a giant matrix of said rows and columns which I feed into the vector. I then pass the vector. Keep in mind that the original void function is actually a class member hence the declaration in the main resembling that. I hope I'm not confusing you guys.

 file >> rows >> columns >> mines;

//declare mine field vector of int
vector<vector<int> > mineField(rows, vector<int> (columns));


//Set's the elements of the .txt matrix to an array
for (int i = 0; i < rows; i++){
    for (int j = 0; j < columns; j++){
        file_difficulty >> mineField[i][j];
    }
}

minesweeper:minesweeper play(columns, rows, mines, mineField);

Just realized that minesweeperBoard is not declared at all in either the constructor or the other function.


Solution

  • Let me guess: you have inverted the order of rows and columns in this declaration

    minesweeper:minesweeper play(columns, rows, mines, mineField);
    

    and should be

    minesweeper:minesweeper play(rows, columns, mines, mineField);