As an assignment for my C++ module, I have to build a text-adventure game. The problem I’m facing is a conceptual one; everyone is saying I should use a tree data structure to represent my game. What I don’t get is why.
Let’s say I have a house of 4 rooms. We can imagine this as an 2×2 array. In each room I have two objects. I want to display this data in such a way that I can easily move my character from 0x0 to 0x1 either way (directly – 1 step, or indirectly – 3 steps), while carrying one object with me.
Why is it better to use a tree to hold all data and how will my character move form one node to another? Or is the character a node as well? Shouldn’t my character be an object with a list as its inventory?
I’m a little confused about this. I’m not looking for any code, just a better understanding of data representation and manipulation.
The suggestion was for maps. But then I don’t understand how my character will “navigate” a map, either.
What you’re looking for isn’t a tree, but a graph.
The reason it’s preferable is that an array of “places” doesn’t necessarily represent what you want. There isn’t necessarily a door between all adjacent rooms, so you may not be able to go directly from one to another. At the time time, in a typical text adventure type of game, you may have some hallways (or whatever) that take you more or less directly from one room to another that’s not exactly (or even close to) adjacent. In addition, you may have one-way passages that will take you from one place to another, but you can’t turn around and go back. All of these are easy to represent with a directed graph, but difficult to represent with an array.