A dragon in its lair, lifted from
www.homeoflegolas.com

Assignment 5: Text Adventure Game

In this assignment, you will write an "engine" for a text adventure game. This is something like an interpreter for the simple language of game commands. Even if you do not write a lot of interpreters or compilers for true programming languages, you are likely to write interpreters for scripting languages, macro languages, graphic command languages, and so on.

Sample Run

The game is started with the call (play).
You are in the cave-entrance.
Exits: (north)
Your command? north
You are in the tunnel.
Exits: (north west south)
Monster: bat
Your command? east
You can't do that here.
You are in the tunnel.
Exits: (north west south)
Monster: bat
Your command? attack bat
You swing...a hit!
You killed it!
You are in the tunnel.
Exits: (north west south)
Your command? west
You are in the library.
Exits: (east north)
Treasure: book
Your command? get book
Done.
You are in the library.
Exits: (east north)
Your command? north
You are in the lair.
Exits: (south east)
Monster: dragon
Treasure: hoard
Your command? get hoard
You can't do that with unfriendlies about.
You are in the lair.
Exits: (south east)
Monster: dragon
Treasure: hoard
Your command? attack dragon
You swing...a hit!
It counterrattacks...a miss!
You are in the lair.
Exits: (south east)
Monster: dragon
Treasure: hoard
Your command? attack dragon
You swing...a hit!
It counterrattacks...a miss!
You are in the lair.
Exits: (south east)
Monster: dragon
Treasure: hoard
Your command? attack dragon
You swing...a miss!
It counterrattacks...a miss!
You are in the lair.
Exits: (south east)
Monster: dragon
Treasure: hoard
Your command? attack dragon
You swing...a hit!
You killed it!
You are in the lair.
Exits: (south east)
Treasure: hoard
Your command? get hoard
Done.
You are in the lair.
Exits: (south east)
Your command? get funky
There is no funky here.
You are in the lair.
Exits: (south east)
Your command? quit
Goodbye.

Rules of the Game

The rules should generally be obvious from common sense and the example above.

Every monster has a strength level -- the player's strength starts at 3. When the player attacks a monster (or vice versa), the attacker and target each roll a die and add it to their strength. If the attacker's total is greater, the defender loses a point of strength. When a creature's strength hits 0, it dies.

Specifying the Dungeon

The "dungeon" (maze, setting, world, whatever you want to call it) is stored in a variable. Here is the definition of the dungeon from the example above:
(define dungeon
  (build-dungeon
   (build-room 'cave-entrance
               '((north . 1))
               #f
               #f)
   (build-room 'tunnel
               '((north . 3) (west . 2) (south . 0))
               '(bat . 1)
               #f)
   (build-room 'library
               '((east . 1) (north . 4))
               #f
               'book)
   (build-room 'cathedral
               '((west . 4) (south . 1))
               #f
               'sword)
   (build-room 'lair
               '((south . 2) (east . 3))
               '(dragon . 3)
               'hoard)))
Since we've separated the data from the program, we could create a different adventure simply by specifying a different dungeon.

You will have to write the function build-room above. It takes four arguments: the name of the room, the exits from the room (an association list associating directions with indices of other rooms), the monster in the room (either #f or a dotted pair of the monster's name and strength), and the treature in the room (either #f or a symbol).

The function build-dungeon assembles all the rooms into some structure. A good choice is to:

(define build-dungeon vector)

Implementing the Engine

Obviously, you need a function play which takes no arguments. This should start up a "read-eval-print" loop (e.g., another recursive function) which reads a command from the user, handles the command, prints the result, and then recurs.
Last modified: Fri Feb 27 10:10:02 PST 2004