Why use a tree data structure to represent data in a text adventure game?

As an assignment for my C ++ module, I need to create a text adventure game. The problem I am facing is conceptual; everyone says that I should use the tree data structure to represent my game. I do not understand why.

Say I have a 4-room house. We can represent this as a 2x2 array. In each room I have two objects. I want to display this data so that I can easily move my character from 0x0 to 0x1 in any case (directly by 1 step, or indirectly by 3 steps), carrying one object with me.

Why is it better to use a tree to store all the data and how does my character move from one node to another? Or a node symbol? Should my character be an item with a list as his inventory?

I am a little confused by this. I'm not looking for any code, I just better understand data representation and manipulation.

The offer was for cards. But then I do not understand how my character will "move" on the map.

+6
source share
3 answers

What you are looking for is not a tree, but a graph.

The reason this is preferable is because the array of "places" does not necessarily represent what you want. There is not necessarily a door between all the neighboring rooms, so you cannot go directly from one to the other. While you are playing a typical text adventure type of game, you may have several corridors (or something else) that will take you more or less directly from one room to another, which is not quite (or even close) to the neighborhood. In addition, you may have one-way passages that will take you from one place to another, but you cannot turn around and return. All of them are easily represented using a directed graph, but it is difficult to represent with an array.

+8
source

If your β€œhome” is a grid, and you can move from any grid cell to any other grid cell, the array is in order. I assume that your peers are hinting that you may not want to move from any room to any neighboring room (and also CANNOT move from 0.0 to 42.13).

However, with a tree structure, you still cannot imagine an arbitrary set of transitions between rooms.

A more flexible approach would be the Adjacency List , which is a specialized type of graph . Think of each room as a node and give each node a list of other rooms that you can go to. With this structure, you can even allow one-way transitions (think of a one-way door from many adventure games).

pseudo code

class Room { string Name; string Description List<Room> ConnectedRooms; } 

Then, introducing the symbol

 class Character { string Name; Room CurrentRoom; } 

To see where this character can go to:

 List<Room> availableRooms = myCharacter.CurrentRoom.ConnectedRooms; 
+9
source

I think wood is the best choice because you want to imagine possible transitions between rooms in addition to the rooms themselves.

Think of it as a graph: states are rooms, but certain events will cause transitions from one to another. It expresses the fact that the user cannot simply accidentally from one state to another.

Think of state machines and states.

0
source

Source: https://habr.com/ru/post/910448/


All Articles