Time to Move

Now that we have different rooms in place, the next logical step is to connect them and allow the player to walk from one to the other. What we need to get started is something to represent an Exit. But what is an exit in game terms?

Since we’ll be making this a class, the first thing we have to ask ourselves, what do all exits have in common? The answer is surprisingly simple.

Exit
↳ Direction
↳ Target

For the time being, that is really it. We need to know, where in the room the exit is (Direction) and which room it leads to (Target). It could not be simpler. So let’s implement it.

This will allow us to create instances of exits right in the room initialization like I do here.

As you can see, the first room now has an exit leading south into the second room, and the second room has an exit leading north back into the first room. At some point, I may use named indices for the rooms to make it more readable and less error-prone, but for now, using indices like 0 and 1 will do to identify the rooms.

It’s no use having these exits in the room, however, if the player is not aware of them. What we need now is a method for a room object that will list all of its exits.

This may look a little confusing at first, but it’s really quite simple. I am running through all the exits in a room and count them. I am also creating a string variable that contains each exit listed and separated by the word “and.” If there are multiple exits, it would say something like “North and East and South,” for example. Because this sort of listing is ugly, in the end, I then replace all but the last of the ands with a comma. That’s what this line does.

When the loop is done, I am simply checking how many exits there were and create grammatically correct responses that I then print to the screen. The result is then something like

Exits are leading North, East and South.

But where do the words North, East and South actually come from? You will notice that I am calling a Decribe() method on each one of the exits, which I expect to return that word.

However, since the name is not part of the exit itself, where does it come from? I realized I needed a way to look up a token and find its actually command word… a reverse lookup of what the parser does, in essence.

This may not be elegant, I know, but it’s the best I could come up with so far. Perhaps in the future, as I learn more about some of Python’s more obscure language features I’ll be able to find a more efficient implementation. Since it’s in a separate function it’ll be an easy improvement to make.

So now, my exit’s Describe() function can simply do this…

With exit listings in place, now it is time to turn my attention towards moving between rooms. The first thing that is needed is an expanded vocabulary, so I am adding these word definitions to it.

I’ve introduced a new word type also, called WordType.Direction which the parser will feed into a variable called theDirection so that it won’t interfere with any verbs or nouns. After all, I want the player to be able to also say something like Run north or Go East or much later perhaps even Close the door to the North. In order to do that I need to preserve the verbs and nouns and treat directions as their own command entity.

Once that is all done, it is time for the room’s Evaluate() function to respond to my directional commands.

Believe it or now, but that’s it. This will allow the player to simply walk from one room to the next. No special checks, not a lot of code, just a few lines that re-assign a few variables. I am starting to get a sense that this is the true power of object-oriented programming because now that we have “activated” a new object as the current room, all logic will run through that room object. Mighty impressive stuff… especially for so little code.

Leave a Reply

Your email address will not be published. Required fields are marked *