Strangely Consistent

Theory, practice, and languages, braided together

July 3 2012 — testing the adventure game, looking around

Let's write an adventure game. Again. ☺

We can start by writing a command loop. It's a reduced version of the loop we wrote yesterday for the Hanoi game.

$ bin/crypt 
CRYPT
=====

You've heard there's supposed to be an ancient hidden crypt in these
woods. One containing a priceless treasure. Well, there's only one way
to find out...

> look 
Sorry, I did not understand that.

> quit

Neat. But the game doesn't understand look yet. We need to do something about that. Time to write our first test.

We implement looking. See how easy it is. (Yes, we are hardcoding things when we can get away with it. But that's OK; then we'll need to write tests to make sure they can change.)

Now we hook looking into the command loop.

Now this happens:

$ bin/crypt 
CRYPT
=====

You've heard there's supposed to be an ancient hidden crypt in these
woods. One containing a priceless treasure. Well, there's only one way
to find out...

> look
<clearing>

> quit

Well, it works, but... we have some presentation issues. Last year we added a file descriptions with text descriptions of everything. Let's do that now, and plug it into the PlayerLooked event.

Now things look like this:

$ bin/crypt 
CRYPT
=====

You've heard there's supposed to be an ancient hidden crypt in these
woods. One containing a priceless treasure. Well, there's only one way
to find out...

> look
The forest road stops here, and the gaps between the trees widen into a
patch of un-forest. The sky above is clear apart from a few harmless clouds.

> quit

We're off to a good start.

Some observations: notice how we're following a model-view kind of thinking here. The Crypt::Game class is the model, and our MAIN() method is a view. We don't bother the model with the actual descriptions of things; these end up in the view.

I was a bit hesitant in making PlayerLooked an event. It felt like looking might be a "pure" operation without side effects. Then I remembered that sometimes in the game looking does have side effects. So it definitely belongs there. If looking didn't have side effects, we might handle looking entirely in the MAIN() method. But I think I prefer it this way.

Ok, that's it for today. Tomorrow we'll add walking around to our repertoire.