-1

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.

  • what is `minesweeperBoard` and where do you declare/initialize it? – 463035818_is_not_an_ai May 14 '16 at 09:25
  • 1
    there are more errors, that wont make this code compile. Please provide a [mcve](http://stackoverflow.com/help/mcve) – 463035818_is_not_an_ai May 14 '16 at 09:27
  • sorry, the code above is just a snippet of the project. minesweeper is actually a class and this is one of the functions. minesweeperBoard is a private member and is declared as vector > minesweeperBoard; – Sharyar Khan May 14 '16 at 09:36
  • I don't want to make it super complicated which is why I left out a lot of code. The main basically asks a user which minesweeper file they would like to use, loads the file, reads from it the actual board which it stores into a vector. I have created an object of type minesweeper and i send the rows, columns, mines, and the vector of mine locations to the code you see above. – Sharyar Khan May 14 '16 at 09:39
  • 1
    it is supercomplicated when we cannot reproduce the error. You dont have to post the original code, but you should post code that allows others to reproduce the error. – 463035818_is_not_an_ai May 14 '16 at 09:40
  • @sharyar - tobi is being nice when asking if `minesweeperBoard` and `mineField` are initialized to the correct sizes. Otherwise all the `[i][j]` indexing might be out-of-bounds. That's about what can go wrong in the code you have shown. So perhaps the real error is somewhere else? – Bo Persson May 14 '16 at 09:40
  • @BoPersson Thats what I think is incorrect. I will update the question momentarily. – Sharyar Khan May 14 '16 at 09:43
  • Why do you require the user to give you the size as parameters; I guess that mineField has not the same size given by the user (test it!), but in principal you should give more information as said by the others. – overseas May 14 '16 at 10:05

1 Answers1

0

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);
max66
  • 65,235
  • 10
  • 71
  • 111