我正在尝试创建一个基于文本的冒险游戏。我在想我希望 map 由不同的节点表示,其中每个节点对应一个不同的位置并且具有节点指针变量(左、前和右)应该指向各自方向上的另一个节点。我试图将它实现为一个链表,但使用这种数据结构,我只能让每个节点指向另一个节点。我希望每个节点都指向其他三个节点。我可以使用哪种数据结构来实现这一点,或者这是否可能?
最佳答案
链接数据结构可以很好地完成您想要的工作:
例子:
class location
{
std::string loc_name;
std::vector<std::pair<std::string,location*>> connections;
std::string description;
public:
bool add_link(location* loc, std::string dicription_to, std::string dicription_from);
//other parameters + functions to manage class
}
这将允许您创建位置,例如:
location* loc = new location("graveyard");
loc->description = "A spooky graveyard on a hill, a cool mist floats amongst the gravestones and monuments";
loc->add_link(crypt /*previously defined*/,
"An imposing mausoleum with an open door, steps inside lead down into darkness",
"Moonlight filters down from the top of some steps, a way out?");
loc.add_link(spooky_house /*previously defined*/,
"The North gate of the graveyard",
"The entrance to the house's spooky graveyard");
我建议创建一个您可以阅读的 map 文件。可能使用这样的模板:
位置文件:
/*locations, format = "name; description"*/
Spooky House; house_description
Crypt; crypt_description
Graveyard; A spooky graveyard on a hill, a cool mist floats amongst the gravestones and monuments
链接文件:
/*links, format = "index # (from); index # (to); description (from->to); description (to->from)"*/
3;2;An imposing mausoleum with an open door, steps inside lead down into darkness; Moonlight filters down from the top of some steps, a way out?
3;1;The North gate of the graveyard;The entrance to the house's spooky graveyard;
加载 map 就像读取所有位置并将它们插入 vector 进行存储一样简单,然后添加链接以连接它们。
关于C++ 基于文本的游戏 - "Map"实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26627096/