C++ 迷宫游戏输入到一个二维向量
我试图创建一个迷宫,它从一个文件导入,然后放入一个包含布尔向量的向量中 我的问题是,我从文件中获取了信息,但我不确定如何将其处理为2D向量。在迷宫中,任何带有“+”的坐标都是路径,而其他坐标(空格等)都是墙。开始和结束位置是C++ 迷宫游戏输入到一个二维向量,c++,vector,maze,C++,Vector,Maze,我试图创建一个迷宫,它从一个文件导入,然后放入一个包含布尔向量的向量中 我的问题是,我从文件中获取了信息,但我不确定如何将其处理为2D向量。在迷宫中,任何带有“+”的坐标都是路径,而其他坐标(空格等)都是墙。开始和结束位置是Location对象,但我还没有对其进行编码 vector<vector<bool> > mazeSpec; string buffer; //holds lines while they are read in int length; //holds
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 file\n";
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;
}
vectormazespec;
字符串缓冲区//在读入行时保留行
整数长度//保存每行/每列的长度
位置开始、结束;
ifstream mazeFile(“maze.txt”);
如果(!mazeFile){
cerr你应该一个字符一个字符地填充它。
对于文件中的每一行,向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 file\n";
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++;
}
inti,j;
向量mazeSpec;
字符串缓冲区;//在读取行时保存行
int length;//保存每行/#列的长度
位置开始、结束;
ifstream mazeFile(“maze.txt”);
如果(!mazeFile){
cerr您需要提供一个完整的小样本文件和所需的2D数组。也不清楚为什么要读取第一行并存储其长度以供以后比较——将其与什么进行比较?您在说明中提到了+
,但在代码中使用了*
。