Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Projects and Practice

Project: A Text Adventure Engine

Model rooms, items, a player and commands as collaborating objects, the Smalltalk way. Build a runnable text adventure engine in JavaScript with polymorphic commands, a world graph and tests.

Text adventures are a classic Smalltalk-style project because they are all about objects sending messages: a player moves through rooms, picks up items, and uses them. There is no framework to learn, so the whole lesson is design: who owns which knowledge, and which object answers which message. You will build the engine in small runnable pieces. The Smalltalk shape of each class is shown read-only, and the JavaScript code is the version you can run and change.

This project uses ideas from earlier lessons: polymorphism and design patterns for commands, and tests to protect behaviour as the world grows.

The object model

Four kinds of objects do the work:

  • Room: a description, exits to other rooms, and items lying in it.
  • Item: a name, a description, and whether it can be carried.
  • Player: the current room and an inventory.
  • Command: one class per verb (go, take, look, drop, inventory), each knowing how to run itself.

In Smalltalk the central classes would be written like this (read-only):

SMALLTALK
Object subclass: #Room
    instanceVariableNames: 'name description exits items'
    package: 'Adventure'.

Room >> exitTo: aDirection
    ^ exits at: aDirection ifAbsent: [ nil ]

Room >> connect: aDirection to: aRoom
    exits at: aDirection put: aRoom

Object subclass: #Player
    instanceVariableNames: 'room inventory'
    package: 'Adventure'.

Player >> go: aDirection
    (room exitTo: aDirection)
        ifNil: [ ^ 'You cannot go that way.' ]
        ifNotNil: [ :next | room := next. ^ room describe ]

Step 1: rooms and items

JS Step 1: the world

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The world is a graph: each room knows its neighbours. connect can create the return path with one call, so you cannot forget it.

Step 2: the player and commands

Instead of a long if chain on the verb, every command is an object with the same protocol: matches(verb) and run(game, argument). This is the Command pattern and it means a new verb is a new class, with no change to existing code.

JS Step 2: player and polymorphic commands

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Every verb is a tiny class. Game.input asks "who matches this verb?" and sends run. Compare this with doesNotUnderstand:: unknown verbs are handled by one fallback message.

Step 3: puzzles with locked exits

Add a rule: the gate in the garden opens only if the player carries the key. Put the rule in the exit, not in the Go command, so every future lock works the same way.

JS Step 3: locked exits

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Moving the rule into Exit keeps Go unchanged. Later you can subclass Exit as HiddenExit (appears after you read a note) or OneWayExit without touching other classes.

Step 4: tests protect the world

A growing world breaks easily. Write small tests on the model, with no console printing, as in Test-Driven Development in the Debugger.

JS Step 4: tests

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Extension ideas

  1. Add use <item>, open, and drop commands.
  2. Let rooms have light levels, so look in the dark returns "It is pitch black."
  3. Track score and a move counter, and announce events with an Announcer.
  4. Save the game state to JSON, as in persistence.
  5. Read the world from a data file so non-programmers can write adventures.
flowchart LR
  I["input line"] --> G["Game.input"]
  G -->|"find matching command"| C["Command.run"]
  C --> P["Player"]
  C --> R["Room / Exit / Item"]
  C --> O["response text"]

Each verb is one class; the game only dispatches.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is each verb a Command object instead of a long if chain?

    Polymorphism lets you extend behaviour without editing the dispatcher.
  2. 2. Where should the rule about needing a key live?

    The object that owns the knowledge should answer canPass.
  3. 3. What data structure best models the world of rooms?

    Rooms and exits form a graph; each room knows its neighbours.
  4. 4. Why test the model instead of the printed text?

    Assertions on state survive changes to the description text.

Summary

  • Model the domain as collaborating objects: rooms, items, player, commands.
  • One class per verb keeps the engine open for extension.
  • Put rules, such as locks, where the knowledge lives (the exit).
  • Test the model, not the output text.
  • The Smalltalk snippets are read-only; the JavaScript engine is a runnable stand-in.

Next: Project: A Smalltalk Message Interpreter