迷宫游戏输入到2D矢量
Maze game inputting into a 2D vector
我正试图创建一个从文件导入的迷宫,然后将其放入一个包含布尔向量的向量中。
我的问题是,我已经从文件中获取了信息,但我不确定如何将其处理为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++;
}
相关文章:
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何在 c++ 中格式化和访问 2D 矢量数组中的数据
- 读取字符串文本输入以创建 2D 矢量
- 识别 2D 矢量C++中的位置
- Cython:无法分配给 2D 矢量
- 按列随机播放 2D 矢量
- 初始化空的 2D 矢量?
- 如何在 2d 矢量 c++ 中复制元素并将其放在原始元素旁边
- 如何访问具有地图的2D矢量?
- 通过功能进行2D矢量输入和显示
- 在C++中将对象添加到 2D 矢量
- 太空入侵者 – 2D 矢量运动算法
- 从 2 个 1D 矢量创建 2D 矢量
- 如何仅使用其中一个维度在 2D 矢量中插入值?
- 在 2D 矢量中获取多个输入时出现问题
- 2D 矢量循环 C++
- C++ 2D 矢量到 2D pybind11 数组
- 在 2D 矢量数组中的特定标头下添加数据
- 在C++中使用 2D 矢量时出现明显的内存泄漏
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中