1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Making a Text Adventure Game in Ruby

By

Making a Text Adventure Game in Ruby
Making a Text Adventure Game in Ruby

Text adventure games (or "interactive fiction" games) were popular on minicomputers and microcomputers from the 1970's through the 1990's. With titles like "The Colossal Cave Adventure" and "Zork," they're a kind of interactive novel, thrusting the player into an unknown world with only the author's literary skill the player's imagination to fill in the rest. The game explains what's happening in plain English text (there are no graphics, these games come from an era before computer graphics were feasible for a game) and to play you simply type what you want to do in plain English.

Programming text adventure games is a relatively easy programming exercise. While it sounds hard at first, especially the part about interpreting English text from the user, it turns out that implementing such a game is quite easy. Like any game it will take quite a lot of work and some trial and error, but there there's only a single tree data structure at the heart of it all and most tasks are solved by searching for a node and moving it from one place to another.

But maybe you've never played any of these games before and you just don't know how it all fits together. Well, this is your lucky day, because the first task for you is going to be playing such a game before you try implementing one. I recommend the game "Zork," which can be played online here. The game starts out simply enough, you're standing in a field outside a white house with a boarded front door. There's also a small mailbox here. What do you want to do? Start off with some simple things like open mailbox or go west. But be prepared, Zork is a deep game with many puzzles, you probably won't finish it on your first try (or your fiftieth).

There is another related game genre that won't be covered here: the multi-user dungeon (MUD). It's like an interactive fiction game except other people are playing it at the same time. Hundreds of people can be playing in a MUD at a time. They were MMOs before MMOs existed. In fact, when the first few MMO games came out, people were still calling them "graphical MUDs." Text adventure games are related, but completely different from an architectural point of view and MUDs are much more difficult to implement.

Text Adventure Games in Ruby

So we need a plan first. How are we going to store the text adventure game data? More importantly, how are we going to define the game data? Once we have our data in place, how are we going to manipulate it and play the game?

  • One Tree To Rule Them All - If you're used to programming small scripts and web applications, you may not be familiar (or at least not think about) data structures on a regular basis. Choosing the right data structure is important. In this case, we'll be using a single tree structure.
  • Data Visualization - If I move the widget from the living room to the kitchen, did it really go to the kitchen? Visualizing tree data is hard, and by itself Ruby doesn't really give you anything to help you out. We'll discuss two ways of visualizing the game state tree, one that prints text to the console and another that uses the graph gem to create graphical representations. These won't be useful when playing the game, but invaluable when debugging the engine and developing the game.
  • Finding and Moving Nodes - When it comes right down to it, almost everything in a text adventure game involves finding a node or moving a node. In order to take the brass lantern, you must find the brass lantern and, if it's accessible to the player, move the brass lantern from the room node to the player node.
  • The Player - On to the "meat" of the game engine: the player. Until now, everything has been support code. You've been defining data and coming up with ways to search through it to find what you need but there's no game there. With the addition of the player with a few simple verbs, you'll be able to walk around and explore your world.
  • Scripting - A game with no scripting is not a fun game. You can walk around and move objects but everything is static. Add in a bit of scripting and putting the rabid weasel in the doghouse really will do something interesting.
  • Saving, Loading and Cleaning Up - One of the great things about the tree of hashes architecture we chose is that it's trivial to serialize and load using YAML with almost no extra code. We'll also clean a few things up and add room descriptions
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Text Adventure Games
  6. Making a Text Adventure Game in Ruby

©2014 About.com. All rights reserved.