Linking rooms in an adventure game.

So I have 4 rooms, all with unique names, descriptions and values. Now I wanna link them together through south, north, west and east. What would be the most efficient way to do this? In python I used dictionaries and that worked just fine but I am still a bit unfamiliar with C++. Any advice?
> In python I used dictionaries

A similar approach in C++ would use one of the associative containers: std::map<> or std::unordered_map<>.
http://en.cppreference.com/w/cpp/container/map
http://en.cppreference.com/w/cpp/container/unordered_map

Brief tutorial: https://www.cprogramming.com/tutorial/stl/stlmap.html
Thanks, I'll look into it! :)
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
enum class Direction
{
  East, South, West, North, Up, Down
};

class Room
{
public:
  void setExit(Direction dir, Room room);
  Room& getExit(Direction dir);
private:
  map<Direction, Room> exists;
};
Can you explain what you're doing at:

 
Room& getExit(Direction dir);


I am fairly new to this language and to me it looks like your creating an object reference. Is this right and why would you do that? O.o
Yes it returns a reference to a specified exit.
Is this right and why would you do that?
Assume you have a var called currentRoom and the player wants to go East.
In this case you assign currentRoom to currentRoom.getExit(Direction::East);
Of course you could also return a pointer in case there is no exit
the most efficient way would be to have 4 pointers, for each direction, that point to the destination room in that direction. But the map is way better. If you do it with 4 pointers, you save a couple of nanoseconds doing an extra lookup or two inside the <map> but you lose the flexibility of adding a NE exit later, or an up/down stairs component, etc. Having it in the map, you can add these with minimal effort so its the cleaner answer. Often speed and design are at odds, and the answer for 'most efficient' is quite often not the answer for 'good approach'. Comes down to picking your battles... find where the code is slow and needs a tweak, and you can do something odd there if needed, and keep to good design everywhere you can as much as possible.
Topic archived. No new replies allowed.