简体   繁体   中英

Some questions regarding 2d Vectors, C++

This 2d vector is being used to hold a game-board for minesweeper. I want to create a 2d vector of struct cell, which has several "state" variables all holding information needed to construct the game board (I am creating a basic minesweeper game to run on the command line, very rudimentary, just want to get a better grasp of classes). First of all, what am I doing wrong when trying to pass the vector to the void function? And then how would I be able to access the separate variables to read and write to them? I know this may be unusual (could solve using arrays) but I'd like to do it a little differently. I have looked through various forums but people don't seem to use this approach. Thanks guys.

EDIT: What I'm trying to accomplish with the vector of cell's is basically 3 vectors in 1 so that I can simultaneously use the information in the different states to check whether various conditions have been met when a player makes a move (ie check whether there is a mine there, or whether that spot has already been opened/marked/unmarked etc.) Please let me know if the code below doesn't allow for what I want to accomplish.

code:

#include <iostream>
#include <vector>

using namespace std;
void gameboard(vector<vector<int>> &stateboard)

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

void gameboard(vector<vector<int>> &stateboard)
{

}

int main()
{
    int columns = 10;
    int rows = 10;

    vector <vector<cell> > gameboard(rows, vector<cell>(columns));
    gameboard(&gameboard);


    return 0;

}

sorry guys, this piece of code doesn't even begin to resemble the outline I have in Xcode, I was just trying to present the question in an easier to follow manner and threw this together.

new 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
    bool isMine;
};

void game_state(vector<vector<cell>> &stateboard)
{

}

int main()
{
    int columns = 10;
    int rows = 10;

    vector <vector<cell> > gameboard(rows, vector<cell>(columns));
    game_state(gameboard);


    return 0;

}

I guess having the same name for a function and vector was throwing Xcode off, which is why I made game board a reference originally but now I see why that was stupid. Now that this works, how can i specifically read and write to just the bool isMine variable? I'm not asking for you to do it completely but a basic line of code showing me how to access that specific part would be a greatly help me. Am I conceptualizing this incorrectly?

hope it helps you:

#include <iostream>
#include <vector>

// your columns and rows are equal,
//and they should no change, so i think better to do them const
const int BOARD_SIZE = 10;

struct cell {

    int state;
    int value;
    bool isMine;
};

void game_state(std::vector < std::vector <cell > > &stateboard) {


}

int main (){


    std::vector < std::vector <cell > > gameboard;

    //I give more preference to initialize matrix like this
    gameboard.resize(BOARD_SIZE);
    for (int x = 0; x < BOARD_SIZE; x++) {
        gameboard[x].resize(BOARD_SIZE);
        for (int y = 0; y < BOARD_SIZE; y++) {

            // and this is an example how to use bool is mine
            // here all cells of 10x10 matrix is false
            // if you want place mine in a first cell just change it
            // to gameboard[0][0].isMine = true;

            gameboard[x][y].isMine = false;
        }
    }

    game_state(gameboard);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM