繁体   English   中英

关于2d向量,C ++的一些问题

[英]Some questions regarding 2d Vectors, C++

这个2d矢量用于为扫雷者举行游戏板。 我想创建一个结构单元格的2d向量,它有几个“状态”变量,都包含构建游戏板所需的信息(我正在创建一个基本的扫雷游戏,在命令行上运行,非常简陋,只是想得到一个更好地掌握课程)。 首先,在尝试将向量传递给void函数时,我做错了什么? 然后,我将如何访问单独的变量来读取和写入它们? 我知道这可能是不寻常的(可以解决使用数组)但我想做的有点不同。 我浏览了各种论坛,但人们似乎并没有使用这种方法。 多谢你们。

编辑:我正在尝试用单元格的向量基本上完成3个向量,这样我就可以同时使用不同状态的信息来检查玩家移动时是否满足各种条件(即检查是否那里有一个矿井,或者该地点是否已经打开/标记/未标记等。如果下面的代码不允许我想要完成的任务,请告诉我。

码:

#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;

}

对不起,这段代码甚至没有开始像我在Xcode中的轮廓,我只是试图以一种更容易理解的方式呈现问题并将它们放在一起。

新代码:

#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;

}

我想函数和矢量的名称相同,就是抛弃Xcode,这就是为什么我最初把游戏板作为参考,但现在我明白为什么那是愚蠢的。 既然这样可行,我怎样才能专门读取和写入bool isMine变量? 我不是要求你完全做到这一点,但是基本的代码行显示我如何访问该特定部分将对我有很大帮助。 我是否错误地概念化了这一点?

希望它可以帮助你:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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