迷宫游戏输入到2D矢量

Maze game inputting into a 2D vector

本文关键字:2D 矢量 输入 游戏 迷宫      更新时间:2023-10-16

我正试图创建一个从文件导入的迷宫,然后将其放入一个包含布尔向量的向量中。

我的问题是,我已经从文件中获取了信息,但我不确定如何将其处理为2D矢量。在迷宫中,任何带有"+"的坐标都是一条路径,而其他任何坐标(空格等)都是一堵墙。开始和结束位置是Location对象,但我还没有对此进行编码。

vector<vector<bool> > mazeSpec;
string buffer; //holds lines while they are read in
int length; //holds length of each line/# of columns
Location start, finish;
ifstream mazeFile("maze.txt");
if (!mazeFile) {
    cerr << "Unable to open filen";
    exit(1);
}
getline(mazeFile, buffer); // read in first line
cout << buffer << endl; //output first line
length = buffer.length(); //length now set so can be compared
while (getline(mazeFile, buffer)) {
    bool path = (buffer == "*");
    cout << buffer << endl;
}

您应该一个字符一个字符地填充它。对于文件中的每一行,向mazeSpec:添加一行

mazeSpec.resize(mazeSpec.size() + 1);

对于你阅读的每一个字符,在你正在处理的mazeSpec行中添加一列:

mazeSpec[i].push_back(buffer[j] == '*');

你会得到这样的东西:

int i, j;
vector<vector<bool> > mazeSpec;
string buffer; //holds lines while they are read in
int length; //holds length of each line/# of columns
Location start, finish;
ifstream mazeFile("maze.txt");
if (!mazeFile) {
    cerr << "Unable to open filen";
    exit(1);
}
getline(mazeFile, buffer); // read in first line
cout << buffer << endl; //output first line
length = buffer.length(); //length now set so can be compared
mazeSpec.resize(1);
for(j = 0; j < buffer.length(); j++) {
    mazeSpec[0].push_back(buffer[j] == '*');  // or perhaps '+'
}
i = 1;
while (!mazeFile.eof()) {   // read in maze til eof
    getline(mazeFile, buffer);
    mazeSpec.resize(mazeSpec.size() + 1);
    for(j = 0; j < buffer.length(); j++) {
        mazeSpec[i].push_back(buffer[j] == '*');  // or perhaps '+'
    }
     cout << buffer << endl;
     i++;
}