Text Adventure
Text adventures (Zork, Colossal Cave) were among the first computer games, and they are still a perfect exercise: a world made of rooms, a parser that turns typed commands into actions, items that change what's possible, and a win condition. The lesson behind it is one of the most important in game programming: put the content in data, keep the code generic.
The world is data
Each room has a description, exits and items. The code never says "if the player is in the kitchen"; it looks things up:
using System;
using System.Collections.Generic;
var rooms = new Dictionary<string, Room>
{
["hall"] = new("A dusty hall. Stairs lead up.", new() { ["north"] = "library", ["up"] = "tower" }, new() { "lamp" }),
["library"] = new("Shelves of rotting books.", new() { ["south"] = "hall" }, new() { "key" }),
["tower"] = new("Wind howls through the tower.", new() { ["down"] = "hall" }, new()),
};
foreach (var (id, room) in rooms)
{
Console.WriteLine(quot;[{id}] {room.Description}");
Console.WriteLine(quot; exits: {string.Join(", ", room.Exits.Keys)}; items: {(room.Items.Count > 0 ? string.Join(", ", room.Items) : "none")}");
}
record Room(string Description, Dictionary<string, string> Exits, List<string> Items);[hall] A dusty hall. Stairs lead up.
exits: north, up; items: lamp
[library] Shelves of rotting books.
exits: south; items: key
[tower] Wind howls through the tower.
exits: down; items: none
Adding a room is adding an entry, not writing code. Real games take this further and load rooms from JSON or a level editor, so writers and designers can add content without a programmer.
The full game
Now add a player, a command parser and rules: the vault door is locked until you carry the key, and taking the gem out of the vault wins the game. The player's commands come from a script, so the run is deterministic; in a real console game they'd come from Console.ReadLine().
using System;
using System.Collections.Generic;
using System.Linq;
var rooms = new Dictionary<string, Room>
{
["hall"] = new("Hall", "A dusty hall. A heavy door is to the east.",
new() { ["north"] = "library", ["east"] = "vault" }, new() { "lamp" }),
["library"] = new("Library", "Shelves of rotting books.",
new() { ["south"] = "hall" }, new() { "key" }),
["vault"] = new("Vault", "Gold glitters everywhere.",
new() { ["west"] = "hall" }, new() { "gem" }),
};
var game = new Game(rooms, start: "hall");
string[] script = { "look", "east", "take sword", "north", "take key", "inventory", "dance", "south", "east", "take gem" };
foreach (string command in script)
{
Console.WriteLine(quot;> {command}");
Console.WriteLine(game.Handle(command));
if (game.Won) { Console.WriteLine("*** You escape with the gem. You win! ***"); break; }
}
record Room(string Name, string Description, Dictionary<string, string> Exits, List<string> Items);
class Game
{
readonly Dictionary<string, Room> rooms;
readonly List<string> inventory = new();
string current;
public bool Won { get; private set; }
public Game(Dictionary<string, Room> rooms, string start) { this.rooms = rooms; current = start; }
public string Handle(string input)
{
string[] words = input.Trim().ToLowerInvariant().Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
string verb = words.Length > 0 ? words[0] : "";
string noun = words.Length > 1 ? words[1] : "";
return verb switch
{
"look" => Describe(),
"north" or "south" or "east" or "west" => Go(verb),
"take" => Take(noun),
"inventory" => inventory.Count == 0 ? "You carry nothing." : "You carry: " + string.Join(", ", inventory),
_ => quot;I don't know how to '{verb}'.",
};
}
string Describe()
{
Room r = rooms[current];
string items = r.Items.Count > 0 ? quot; You see: {string.Join(", ", r.Items)}." : "";
return quot;{r.Name}: {r.Description}{items} Exits: {string.Join(", ", r.Exits.Keys)}.";
}
string Go(string direction)
{
if (!rooms[current].Exits.TryGetValue(direction, out string next)) return "You can't go that way.";
if (next == "vault" && !inventory.Contains("key")) return "The door is locked.";
current = next;
return Describe();
}
string Take(string item)
{
Room r = rooms[current];
if (!r.Items.Remove(item)) return quot;There is no {item} here.";
inventory.Add(item);
if (item == "gem") Won = true;
return quot;Taken: {item}.";
}
}> look
Hall: A dusty hall. A heavy door is to the east. You see: lamp. Exits: north, east.
> east
The door is locked.
> take sword
There is no sword here.
> north
Library: Shelves of rotting books. You see: key. Exits: south.
> take key
Taken: key.
> inventory
You carry: key
> dance
I don't know how to 'dance'.
> south
Hall: A dusty hall. A heavy door is to the east. You see: lamp. Exits: north, east.
> east
Vault: Gold glitters everywhere. You see: gem. Exits: west.
> take gem
Taken: gem.
*** You escape with the gem. You win! ***
How it's built
| Part | Job | Why it matters in bigger games |
|---|---|---|
rooms dictionary |
the content | level data, loaded from files or an editor |
Handle |
turns text into a verb and a noun, then dispatches | the same "input → command" step as mapping keys to actions |
Go, Take |
the rules; each returns a message | gameplay rules live in small functions you can test |
Won |
the end state | every game needs explicit win and lose states |
Notice that the game never trusts the input: unknown verbs, missing items and blocked exits all produce a message instead of an exception. The lock is a rule in Go, not a special room type, so another locked door is a one-line change.
Tip: Because
Handletakes a string and returns a string, it is trivial to test: feed it a script and compare the output, exactly as this page does. Separating game logic from input and output pays off the same way in engine games.
Make it yours
- Add a
dropcommand and ago <direction>synonym ("go"→ use the noun as the direction). - Make the library dark: you can only see the key if you carry the lamp.
- Load the rooms from a JSON string with
System.Text.Json(see Save & Load with JSON). - Add a turn counter and a lose condition: the lamp runs out after 15 moves.
Test yourself
What does 'put the content in data' mean in the text adventure?
- Save the game to a database
- Rooms, exits and items are entries in a dictionary, not code branches
- Use a data structure course
- Compress the strings
Where is the locked-door rule implemented?
- In the Room record
- In the Go method, which checks the inventory for the key
- In the parser
- In Main
What happens when the player types an unknown command like 'dance'?
- The game crashes
- The game prints a message and continues
- The command is retried
- The player loses