Skip to content
C++ project #15Intermediate

Text Adventure Engine

A small data-driven adventure engine with rooms, exits, locked doors, portable and fixed items, hidden items revealed by using objects, and a win condition.

Estimated time
~5 hours to build
Files
5 files · 388 lines
Technologies
C++17, <map>, <vector>, <set>, <algorithm>

Introduction

Interactive fiction is a fun way to learn to separate data from logic. The Game class knows how to move, take, drop, examine and use things, but the story — rooms, locks, puzzles — lives entirely in build_world(). Replace that function and you have a different game.

Problem statement

Implement an engine for a room-based adventure. The player can look, move in four directions (with short forms), take and drop items, examine them, list the inventory and use an item, which may reveal a hidden item in a particular room. Some exits need an item to pass. Some items can't be carried. Reaching the goal room ends the game and reports the number of moves.

Objectives

  • Represent a world as data with nested maps and vectors
  • Write a tolerant command parser (verbs, multi-word objects, abbreviations)
  • Track game state in a class
  • Use std::set to remember one-time events

Features

  • Rooms with descriptions, items and named exits
  • Locked exits that need a key item
  • Puzzles: using an item in a room reveals a hidden item
  • Move counter and a win condition

Concepts demonstrated

  • Data-driven design
  • State machines
  • Command parsing
  • std::map / std::set
  • Encapsulation

System architecture

World is pure data: a map of room ids to Room structs (title, description, exits by direction, items), plus uses (item + room → revealed item) and the hidden items themselves. Game holds the world, the current room, the inventory and a set of puzzle steps already done. command dispatches each verb to a member function. go checks Exit::needs against the inventory. main lower-cases the verb and joins the remaining words into the object name, so take rusty key works.

Folder structure

TextProject tree
text-adventure-engine/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
World data types, build_world (the story), the Game engine with its verbs, and the input loop.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
THE DUNGEON ESCAPE
(Type commands such as 'look', 'take spoon', 'use spoon', 'go north'.)
== Damp Cell ==
Stone walls drip with water. A heavy door stands to the north.
You see: [straw bed] [spoon]
Exits: north
> The cell door is locked.
> The straw bed can't be carried.
> Taken: spoon
> A bent metal spoon.
> You dig at a loose stone with the spoon and find a rusty key behind it.
> Taken: rusty key
> You carry: [spoon] [rusty key]
> You unlock the way with the rusty key.
== Torch-lit Corridor ==
A long corridor. Passages lead east and west; the cell is south.
You see: [torch]
Exits: east south west
> I don't understand 'dance'. Try: look, go <dir>, take, drop, use, examine, inventory
> Taken: torch
> == Iron Gate ==
A gate of iron bars blocks the way to the courtyard to the north.
Exits: east north
> The gate is locked with a large padlock.
> == Torch-lit Corridor ==
A long corridor. Passages lead east and west; the cell is south.
Exits: east south west
> == Old Armoury ==
Empty weapon racks line the walls. It is too dark to see the corners.
Exits: west
> The torchlight reveals a key hanging on a hook in the corner.
> Nothing happens.
> Taken: gate key
> == Torch-lit Corridor ==
A long corridor. Passages lead east and west; the cell is south.
Exits: east south west
> == Iron Gate ==
A gate of iron bars blocks the way to the courtyard to the north.
Exits: east north
> Dropped: spoon
> You unlock the way with the gate key.
== Courtyard ==
Fresh air at last! You have escaped.
*** You escaped in 20 moves! ***
Input
go north
take straw bed
take spoon
examine spoon
use spoon
take rusty key
inventory
n
dance
take torch
go west
go north
e
e
use torch
use torch
take gate key
w
w
drop spoon
n

Testing

  • Compare ./app < tests/sample-input.txt with tests/expected-output.txt: the scripted walkthrough escapes in 20 moves.
  • The cell door stays locked until the spoon reveals the rusty key; the gate needs the key revealed by the torch.
  • take straw bed fails because it isn't portable; using the torch a second time does nothing.

Common errors

"take rusty key" says there is no rusty

Join all remaining words into the object name instead of reading only one word.

Items vanish after dropping

Push the item into the current room's vector before erasing it from the inventory.

Possible improvements

  • Load the world from a text file
  • Add NPCs with simple dialogue
  • Add save/load of the game state

Additional challenges

  1. 1Write a second world without changing the engine.
  2. 2Add a score for discovering secrets.
  3. 3Add a map command that prints visited rooms.

Built and tested it yourself? Mark it to update your dashboard.