300-Level Challenge: Escape Room
You've finished the 300-level and built programs that can read input, maintain state, update an interactive Display, and organize larger jobs into subroutines.
Now it's your turn to put those pieces together.
The Challenge
Build a small command-driven escape room.
The player should be able to enter commands such as:
NORTH
EAST
GET KEY
WEST
SOUTH
OPEN DOOR
Your program reads each command, decides what it means in the player's current situation, changes the state of the world when appropriate, and waits for another command.
Exactly what that world looks like is up to you.
Your game should have:
- at least four locations the player can move between;
- at least one object the player can acquire;
- at least one obstacle that requires something the player has found;
- a definite way to escape and win;
- a response when the player enters a command that doesn't make sense;
- subroutines for behavior that occurs in more than one place.
Most importantly:
Something the player does in one location must change what is possible in another.
Finding a key might let the player open a locked door somewhere else.
Pressing a switch might activate a machine in another room.
Picking up a tool might let the player repair something that was previously broken.
The details are yours.
Design the World First
Before writing the program, decide what locations exist and how the player can move between them.
Then think about what can change while the game is running.
The player's current location changes when they move.
An object can go from not collected to collected.
A locked door can become unlocked.
Those changing facts are the state of your game.
Your program needs to remember enough of them to decide what happens when the player enters the next command.
Talking to the Game
Your game should accept complete commands from the Keyboard.
For example:
You are standing in a narrow hallway.
> EAST
You enter the storage room.
> GET KEY
You take the key.
> WEST
You return to the hallway.
The Display can be as simple or elaborate as you like.
You might let text scroll naturally as the player explores, or build a fixed interface showing the current location, description, and command prompt.
What matters is that the player can understand where they are and what happened after each command.
Give It a Try
Start by answering a few questions about your game:
- Where does the player begin?
- Where can they go from there?
- What do they need to find?
- What prevents them from escaping immediately?
- What facts about the world can change?
Then start building.
Don't worry about making a general-purpose adventure game engine.
You're making one small game.
Stuck?
The hints below become progressively more specific. Start at the top and only open as many as you need.
How should I represent the rooms?
However you like.
One option is to number them:
0 = Hall
1 = Storage
2 = Workshop
3 = Lobby
4 = Exit
Another is to use their names directly:
"HALL"
"STORAGE"
"WORKSHOP"
"LOBBY"
"EXIT"
Numbers are compact and convenient.
Names can make the program easier to read and debug, although they're also easier to mistype.
Pick whichever representation makes sense for your game.
Still stuck?
Give the current room its own memory location.
Using names, you might begin with:
#room $000
S.MOVE #room "HALL"
When the player moves somewhere else, change #room:
S.MOVE #room "STORAGE"
Your program can then use #room to decide what commands are possible.
What else should I remember?
Think about facts about the world that can change.
If something the player does now can affect what happens later, your program needs some way to remember it.
Still stuck?
Suppose the game contains a key and a locked door.
You could give each fact its own memory location:
#has_key $001
#door_open $002
MOVE #has_key 0
MOVE #door_open 0
Taking the key changes one fact:
MOVE #has_key 1
Unlocking the door changes another:
MOVE #door_open 1
Later commands can check those values before deciding what happens.
Do I need to parse the commands?
Not unless you want to.
It's perfectly fine to treat each command as one complete string.
As far as this challenge is concerned, "GET KEY" can simply be the name of a command.
If you want to split commands into verbs and objects and build something more flexible, go for it. The challenge doesn't require it.
Still stuck?
Store the completed command in #command, then compare the whole thing:
S.BEQ :north #command "NORTH"
S.BEQ :south #command "SOUTH"
S.BEQ :get_key #command "GET KEY"
S.BEQ :open_door #command "OPEN DOOR"
That's enough to build the complete challenge.
How do I read a complete command?
Think back to NCL 302: Building User Input.
The Keyboard still gives you one key at a time.
Collect ordinary characters into a buffer. Handle Backspace by removing a character. When Enter arrives, the buffer contains the command you need to process.
Still stuck?
The beginning of your input loop can look very familiar:
:input
SYS.AKEY #key
S.LEN #length #key
BGT :command_key #length 1
S.JOIN #command #command #key
D.CHR #key
JUMP :input
:command_key
S.BEQ :enter #key "ENTER"
S.BEQ :backspace #key "BS"
JUMP :input
When Enter arrives, branch to the part of the program that interprets #command.
How do I decide what a command means?
A command only has meaning in the player's current situation.
One way to organize that is to first decide which room the player is in, then let that room decide which commands are valid there.
Each room becomes a small state machine with its own exits and actions.
Still stuck?
Suppose the player is in Storage.
Storage might recognize only:
:storage
S.BEQ :storage_west #command "WEST"
S.BEQ :storage_get #command "GET KEY"
JUMP :invalid
WEST leaves the room.
GET KEY interacts with something in the room.
Everything else is invalid there.
Another room can recognize a completely different set of commands.
How do I organize repeated behavior?
Look for jobs that several parts of the game need to perform.
Reading another command, describing the current room, and reporting an invalid command are all things that may happen repeatedly.
Those are good candidates for subroutines or shared paths.
Don't worry if the room logic itself becomes repetitive. You're working with the tools you have so far.
Still stuck?
You might have a subroutine that describes whichever room is currently stored in #room:
:describe
S.BEQ :describe_hall #room "HALL"
S.BEQ :describe_storage #room "STORAGE"
S.BEQ :describe_workshop #room "WORKSHOP"
S.BEQ :describe_lobby #room "LOBBY"
RET
Each room can call the same subroutine after movement:
S.MOVE #room "STORAGE"
CALL :describe
JUMP :prompt
Show me one solution
Here's one possible escape room.
The player begins in the Hall.
East leads to Storage, where the Workshop key can be found.
North from the Hall leads through a locked door to the Workshop, where the Exit key can be found.
South from the Hall leads to the Lobby.
South from the Lobby leads through another locked door to the Exit.
This version uses room names directly and lets each room decide which commands are valid there.
-- Escape Room
#room $000
#command $001
#key $002
#length $003
#has_workshop_key $004
#workshop_open $005
#has_exit_key $006
#exit_open $007
-- Initialize the world.
S.MOVE #room "HALL"
S.MOVE #command ""
MOVE #has_workshop_key 0
MOVE #workshop_open 0
MOVE #has_exit_key 0
MOVE #exit_open 0
D.FIL ""
CALL :describe
D.BLT
-- Begin another command.
:prompt
S.MOVE #command ""
D.CHR "\r\n> "
-- Read one key at a time.
:input
SYS.AKEY #key
S.LEN #length #key
BGT :command_key #length 1
S.JOIN #command #command #key
D.CHR #key
JUMP :input
-- Handle Keyboard function keys.
:command_key
S.BEQ :enter #key "ENTER"
S.BEQ :backspace #key "BS"
JUMP :input
:backspace
S.LEN #length #command
BEQ :input #length 0
S.DEL #command #command -1 -1
D.CHR "\b\d"
JUMP :input
-- Dispatch the completed command to the current room.
:enter
D.CHR "\r\n"
S.UPR #command #command
S.BEQ :hall #room "HALL"
S.BEQ :storage #room "STORAGE"
S.BEQ :workshop #room "WORKSHOP"
S.BEQ :lobby #room "LOBBY"
JUMP :invalid
-- Hall
:hall
S.BEQ :hall_north #command "NORTH"
S.BEQ :hall_east #command "EAST"
S.BEQ :hall_south #command "SOUTH"
S.BEQ :hall_open #command "OPEN DOOR"
S.BEQ :look #command "LOOK"
JUMP :invalid
:hall_north
BEQ :hall_enter_workshop #workshop_open 1
D.TXT "THE WORKSHOP DOOR IS LOCKED.\r\n"
D.BLT
JUMP :prompt
:hall_enter_workshop
S.MOVE #room "WORKSHOP"
CALL :describe
D.BLT
JUMP :prompt
:hall_east
S.MOVE #room "STORAGE"
CALL :describe
D.BLT
JUMP :prompt
:hall_south
S.MOVE #room "LOBBY"
CALL :describe
D.BLT
JUMP :prompt
:hall_open
BEQ :already_open #workshop_open 1
BEQ :hall_unlock #has_workshop_key 1
D.TXT "YOU DON'T HAVE THE KEY.\r\n"
D.BLT
JUMP :prompt
:hall_unlock
MOVE #workshop_open 1
D.TXT "YOU UNLOCK THE WORKSHOP DOOR.\r\n"
D.BLT
JUMP :prompt
-- Storage
:storage
S.BEQ :storage_west #command "WEST"
S.BEQ :storage_get #command "GET KEY"
S.BEQ :look #command "LOOK"
JUMP :invalid
:storage_west
S.MOVE #room "HALL"
CALL :describe
D.BLT
JUMP :prompt
:storage_get
BEQ :already_taken #has_workshop_key 1
MOVE #has_workshop_key 1
D.TXT "YOU TAKE THE WORKSHOP KEY.\r\n"
D.BLT
JUMP :prompt
-- Workshop
:workshop
S.BEQ :workshop_south #command "SOUTH"
S.BEQ :workshop_get #command "GET KEY"
S.BEQ :look #command "LOOK"
JUMP :invalid
:workshop_south
S.MOVE #room "HALL"
CALL :describe
D.BLT
JUMP :prompt
:workshop_get
BEQ :already_taken #has_exit_key 1
MOVE #has_exit_key 1
D.TXT "YOU TAKE THE EXIT KEY.\r\n"
D.BLT
JUMP :prompt
-- Lobby
:lobby
S.BEQ :lobby_north #command "NORTH"
S.BEQ :lobby_south #command "SOUTH"
S.BEQ :lobby_open #command "OPEN DOOR"
S.BEQ :look #command "LOOK"
JUMP :invalid
:lobby_north
S.MOVE #room "HALL"
CALL :describe
D.BLT
JUMP :prompt
:lobby_south
BEQ :escape #exit_open 1
D.TXT "THE EXIT DOOR IS LOCKED.\r\n"
D.BLT
JUMP :prompt
:lobby_open
BEQ :already_open #exit_open 1
BEQ :lobby_unlock #has_exit_key 1
D.TXT "YOU DON'T HAVE THE KEY.\r\n"
D.BLT
JUMP :prompt
:lobby_unlock
MOVE #exit_open 1
D.TXT "YOU UNLOCK THE EXIT DOOR.\r\n"
D.BLT
JUMP :prompt
-- Shared command paths.
:look
CALL :describe
D.BLT
JUMP :prompt
:invalid
D.TXT "YOU CAN'T DO THAT HERE.\r\n"
D.BLT
JUMP :prompt
:already_open
D.TXT "THE DOOR IS ALREADY UNLOCKED.\r\n"
D.BLT
JUMP :prompt
:already_taken
D.TXT "YOU ALREADY TOOK IT.\r\n"
D.BLT
JUMP :prompt
-- Describe the current room.
:describe
S.BEQ :describe_hall #room "HALL"
S.BEQ :describe_storage #room "STORAGE"
S.BEQ :describe_workshop #room "WORKSHOP"
S.BEQ :describe_lobby #room "LOBBY"
RET
:describe_hall
D.TXT "\r\nHALL\r\n"
D.TXT "STORAGE IS EAST.\r\n"
D.TXT "THE WORKSHOP IS NORTH.\r\n"
D.TXT "THE LOBBY IS SOUTH.\r\n"
RET
:describe_storage
D.TXT "\r\nSTORAGE\r\n"
D.TXT "THE HALL IS WEST.\r\n"
BEQ :describe_storage_empty #has_workshop_key 1
D.TXT "A SMALL KEY IS ON A SHELF.\r\n"
:describe_storage_empty
RET
:describe_workshop
D.TXT "\r\nWORKSHOP\r\n"
D.TXT "THE HALL IS SOUTH.\r\n"
BEQ :describe_workshop_empty #has_exit_key 1
D.TXT "A KEY HANGS BESIDE THE BENCH.\r\n"
:describe_workshop_empty
RET
:describe_lobby
D.TXT "\r\nLOBBY\r\n"
D.TXT "THE HALL IS NORTH.\r\n"
D.TXT "THE EXIT IS SOUTH.\r\n"
RET
-- Win.
:escape
S.MOVE #room "EXIT"
D.TXT "\r\nTHE DOOR SWINGS OPEN.\r\n\r\n"
D.TXT "YOU ESCAPED!\r\n"
D.BLT
One successful route is:
EAST
GET KEY
WEST
OPEN DOOR
NORTH
GET KEY
SOUTH
SOUTH
OPEN DOOR
SOUTH
But the player can explore in other orders.
They can visit the Lobby before finding the Exit key. They can try the Workshop before finding its key. They can return to rooms they've already visited.
The important part is that the program remembers what has happened.
#room remembers where the player is.
#has_workshop_key and #has_exit_key remember what they've found.
#workshop_open and #exit_open remember permanent changes to the world.
Each room then interprets commands according to that state.
Your program doesn't need to look like this one.
You might use numbers for rooms, arrange your command handling differently, add more locations, or build a more sophisticated command parser.
If your world behaves consistently, remembers what the player has done, and eventually lets them escape, you've solved the challenge.
That's the end of the 300-level.
We've gone from reading our first keypress to building programs that accept complete commands, maintain state, update interactive displays, respond to selections, and organize larger programs with reusable subroutines.
In this challenge, we combined those ideas into a small interactive world where what the player can do depends on where they are, what they've found, and what they've already changed.
Ready to continue? In NCL 401: The Value Stack, we'll learn another way to put values aside when we only need them for a moment.