gpt4 book ai didi

c++从文件中读取以构建基于指针的迷宫

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:21 24 4
gpt4 key购买 nike

我正在尝试阅读“基于指针”的迷宫。每行的第一个字母对应房间的字母……然后后面的字母分别是北节点、东节点、南节点和西节点。星号表示空房间或无效选项。

所以这将是一个示例输入:

A E B * *
B * * * A
C G D * *
D * * * C
E I F A *
F J G * E
G K H C F
H L * * G
I * J E *
J * K F I
K * * G J
L * * H *

注释说:

We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.

这是我目前所拥有的:

void Maze::read_maze(string FileName) {
string line;
ifstream inStream;
inStream.open(FileName.c_str());
int test = inStream.peek();
int i = 0;
if (!(inStream.fail())) {
while (!inStream.eof() && test != EOF) {
getline(inStream, line);
Node n(line[0]);
i++;
rooms[i] = n;
if (!(line[2] == '*')) {
Node North = find_Node(line[2]);
(find_Node(line[0])).set_North(&North);
}
if (!(line[4] == '*')) {
Node East = find_Node(line[4]);
(find_Node(line[0])).set_East(&East);
}
if (!(line[6] == '*')) {
Node South = find_Node(line[6]);
(find_Node(line[0])).set_South(&South);
}
if (!(line[8] == '*')) {
Node West = find_Node(line[8]);
(find_Node(line[0])).set_West(&West);
}
}
currentRoom = find_Node('A');
} else {
cout << "Could not open the file, please choose another.\n" << endl;
exit(1);
}
}

当我开始解析文件时,我的程序会调用我拥有的函数来检查文件是否为空。所以我认为我读入的文件不正确。

最佳答案

您不能在程序运行时使用调试器来检查这些值吗?如果没有,那就试试

cost << line <<  endl; 

在getline之后,验证每行读入的实际数据。

关于c++从文件中读取以构建基于指针的迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com