C++ C++;基于文本的游戏-“;“地图”;实施
我正在尝试创建一个基于文本的冒险游戏。我想我希望映射由不同的节点表示,其中每个节点对应于不同的位置,并且具有节点指针变量(左、前、右),这些变量应该指向各自方向上的另一个节点。我试图将其实现为一个链表,但使用这种数据结构,我只能让每个节点指向另一个节点。我希望每个节点指向其他三个节点。我可以使用哪种数据结构来实现这一点,或者这是可能的?您可以使用地图上的链接位置实现自定义链接数据结构,如下所示:C++ C++;基于文本的游戏-“;“地图”;实施,c++,data-structures,linked-list,C++,Data Structures,Linked List,我正在尝试创建一个基于文本的冒险游戏。我想我希望映射由不同的节点表示,其中每个节点对应于不同的位置,并且具有节点指针变量(左、前、右),这些变量应该指向各自方向上的另一个节点。我试图将其实现为一个链表,但使用这种数据结构,我只能让每个节点指向另一个节点。我希望每个节点指向其他三个节点。我可以使用哪种数据结构来实现这一点,或者这是可能的?您可以使用地图上的链接位置实现自定义链接数据结构,如下所示: struct Map_Node{ Map_Node *left; Map_Node
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
然后,您需要自己进行内存管理。例如,通过使用智能指针
std::shared_ptr<Map_Node> entry{ new MapNode };
std::shared_ptr<Map_Node> hallway{ new MapNode };
entry->forward = &*hallway;
//and so on
std::共享的ptr条目{newmapnode};
std::shared_ptr hallway{new MapNode};
进入->前进=&*走廊;
//等等
获取下一个文件更容易但效率更低的方法是std::map。如果每个位置
有其唯一的ID,例如字符串,您可以存储相邻字段的ID,并使用该ID在地图上自由移动
struct Map_Node{
std::string name;
std::string left;
std::string right;
std::string forward;
/* other needed field*/
};
std::map<std::string, Map_Node> map;
Map_Node entry;
entry.name = "entry";
map[entry.name] = entry;
Map_Node hallway;
hallway.name = "hallway";
map[hallway.name] = hallway;
//links between:
map["entry"].forward = "hallway";
struct-Map\u节点{
std::字符串名;
std::左字符串;
std::字符串右键;
std::字符串向前;
/*其他所需领域*/
};
地图;
地图节点条目;
entry.name=“entry”;
map[entry.name]=条目;
地图节点走廊;
hallway.name=“hallway”;
map[hallway.name]=走廊;
//以下各项之间的联系:
地图[“入口”]。前进=“走廊”;
您可以使用地图上的链接位置实现自定义链接数据结构,如下所示:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
然后,您需要自己进行内存管理。例如,通过使用智能指针
std::shared_ptr<Map_Node> entry{ new MapNode };
std::shared_ptr<Map_Node> hallway{ new MapNode };
entry->forward = &*hallway;
//and so on
std::共享的ptr条目{newmapnode};
std::shared_ptr hallway{new MapNode};
进入->前进=&*走廊;
//等等
获取下一个文件更容易但效率更低的方法是std::map。如果每个位置
有其唯一的ID,例如字符串,您可以存储相邻字段的ID,并使用该ID在地图上自由移动
struct Map_Node{
std::string name;
std::string left;
std::string right;
std::string forward;
/* other needed field*/
};
std::map<std::string, Map_Node> map;
Map_Node entry;
entry.name = "entry";
map[entry.name] = entry;
Map_Node hallway;
hallway.name = "hallway";
map[hallway.name] = hallway;
//links between:
map["entry"].forward = "hallway";
struct-Map\u节点{
std::字符串名;
std::左字符串;
std::字符串右键;
std::字符串向前;
/*其他所需领域*/
};
地图;
地图节点条目;
entry.name=“entry”;
map[entry.name]=条目;
地图节点走廊;
hallway.name=“hallway”;
map[hallway.name]=走廊;
//以下各项之间的联系:
地图[“入口”]。前进=“走廊”;
您可以使用地图上的链接位置实现自定义链接数据结构,如下所示:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
然后,您需要自己进行内存管理。例如,通过使用智能指针
std::shared_ptr<Map_Node> entry{ new MapNode };
std::shared_ptr<Map_Node> hallway{ new MapNode };
entry->forward = &*hallway;
//and so on
std::共享的ptr条目{newmapnode};
std::shared_ptr hallway{new MapNode};
进入->前进=&*走廊;
//等等
获取下一个文件更容易但效率更低的方法是std::map。如果每个位置
有其唯一的ID,例如字符串,您可以存储相邻字段的ID,并使用该ID在地图上自由移动
struct Map_Node{
std::string name;
std::string left;
std::string right;
std::string forward;
/* other needed field*/
};
std::map<std::string, Map_Node> map;
Map_Node entry;
entry.name = "entry";
map[entry.name] = entry;
Map_Node hallway;
hallway.name = "hallway";
map[hallway.name] = hallway;
//links between:
map["entry"].forward = "hallway";
struct-Map\u节点{
std::字符串名;
std::左字符串;
std::字符串右键;
std::字符串向前;
/*其他所需领域*/
};
地图;
地图节点条目;
entry.name=“entry”;
map[entry.name]=条目;
地图节点走廊;
hallway.name=“hallway”;
map[hallway.name]=走廊;
//以下各项之间的联系:
地图[“入口”]。前进=“走廊”;
您可以使用地图上的链接位置实现自定义链接数据结构,如下所示:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
然后,您需要自己进行内存管理。例如,通过使用智能指针
std::shared_ptr<Map_Node> entry{ new MapNode };
std::shared_ptr<Map_Node> hallway{ new MapNode };
entry->forward = &*hallway;
//and so on
std::共享的ptr条目{newmapnode};
std::shared_ptr hallway{new MapNode};
进入->前进=&*走廊;
//等等
获取下一个文件更容易但效率更低的方法是std::map。如果每个位置
有其唯一的ID,例如字符串,您可以存储相邻字段的ID,并使用该ID在地图上自由移动
struct Map_Node{
std::string name;
std::string left;
std::string right;
std::string forward;
/* other needed field*/
};
std::map<std::string, Map_Node> map;
Map_Node entry;
entry.name = "entry";
map[entry.name] = entry;
Map_Node hallway;
hallway.name = "hallway";
map[hallway.name] = hallway;
//links between:
map["entry"].forward = "hallway";
struct-Map\u节点{
std::字符串名;
std::左字符串;
std::字符串右键;
std::字符串向前;
/*其他所需领域*/
};
地图;
地图节点条目;
entry.name=“entry”;
map[entry.name]=条目;
地图节点走廊;
hallway.name=“hallway”;
map[hallway.name]=走廊;
//以下各项之间的联系:
地图[“入口”]。前进=“走廊”;
链接数据结构可以很好地满足您的需求:
例如:
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
}
我建议创建一个你可以读入的地图文件。可能使用如下模板:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
位置文件:
/*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;
加载地图非常简单,只需读取所有位置并将它们放入一个向量中进行存储,然后添加链接以连接它们。链接数据结构可以很好地完成您想要的工作: 例如:
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
}
我建议创建一个你可以读入的地图文件。可能使用如下模板:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
位置文件:
/*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;
加载地图非常简单,只需读取所有位置并将它们放入一个向量中进行存储,然后添加链接以连接它们。链接数据结构可以很好地完成您想要的工作: 例如:
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
}
我建议创建一个你可以读入的地图文件。可能使用如下模板:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
位置文件:
/*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;
加载地图非常简单,只需读取所有位置并将它们放入一个向量中进行存储,然后添加链接以连接它们。链接数据结构可以很好地完成您想要的工作: 例如:
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
}
我建议创建一个你可以读入的地图文件。可能使用如下模板:
struct Map_Node{
Map_Node *left;
Map_Node *right;
Map_Node *forward;
/* other needed field*/
};
位置文件:
/*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;
加载地图非常简单,只需读取所有位置并将其放入向量中存储,然后添加链接以连接它们。修改数据结构,使四个节点指针分别指向左、右、前和下一个。实现数据结构不需要名称