Hello, I'm working on making a minesweeper game using C++ but I'm having trouble re sizing a vector.
Here's what I have:
A vector of ints:
A vector of structs:Code:vector<vector<int> > mineField;
The vector of cell is located in a separate .cpp file of a minesweeper class.Code:struct cell{ int value; //(-1 mine, 0 no surrounding, # > 0 number of surrounding mines) int state; //( 0 hidden, 1 revealed, 2 marked) bool isMine; }; vector<vector<cell> > mineField;
What I want to do is re size the vector of cell to have the same dimensions as the vector of ints.
and initialize the struct variable *value* to the values in the vector of ints and the struct
variable *state* to 0.
This is what I have tried so far:
When attempt to run this I am only able to have the dimensions 5 rows x 5 columns (I cannot figure out why). AnyCode:this->mineField.resize(rowNum, vector<cell>(colNum)); for(int i = 0; i < rowNum; i++){ for(int j = 0; j < colNum; j++){ this->mineField[i][j].state = 0; this->mineField[i][j].value = mineField[i][j]; } }
other dimensions exits the program and netbeans tells me the run has failed.
I have also tried:
When trying to resize this way, nothing works.Code:this->mineField.clear(); for (int i = 0; i < rowNum; i++){ this->mineField.push_back(vector<cell>(colNum, 0)); } for(int i = 0; i < rowNum; i++){ for(int j = 0; j < colNum; j++){ this->mineField[i][j].state = 0; this->mineField[i][j].value = mineField[i][j]; } }
And this way:
Attempting this lets the program run but doesn't work for any dimensions combination.Code:this->mineField.resize(rowNum); for(int i = 0; i < rowNum; i++){ this->mineField[i].resize(colNum); } for(int i = 0; i < rowNum; i++){ for(int j = 0; j < colNum; j++){ this->mineField[i][j].state = 0; this->mineField[i][j].value = mineField[i][j]; } }
Any help is greatly appreciated, thank you.


Reply With Quote
