12.4 — Project: a text adventure game

📘 Chapter 12⏱ about 3 min read✍️ Beginner friendly
Read one box at a time. No rush.

Time to build something real! We'll make a tiny text adventure using things from the whole guide: structs, vectors, maps, functions, loops and switch.

The plan#

  • The world is a list of rooms.
  • Each room has a name, a description, and exits (like "north" → room 2).
  • The player types commands: go north, look, quit.

The full program#

C++▶ Run
#include <iostream>#include <map>#include <string>#include <vector> struct Room{    std::string name {};    std::string description {};    std::map<std::string, int> exits {};   // direction -> room index}; std::vector<Room> makeWorld(){    return {        { "Hall", "A dusty hall. Doors lead north and east.", { { "north", 1 }, { "east", 2 } } },        { "Library", "Books everywhere. A door leads south.", { { "south", 0 } } },        { "Kitchen", "It smells of old soup. Doors lead west and north.", { { "west", 0 }, { "north", 3 } } },        { "Garden", "Sunlight! You found the way out. You win!", {} },    };} void describe(const Room& room){    std::cout << "\n== " << room.name << " ==\n" << room.description << '\n';} int main(){    std::vector<Room> world { makeWorld() };    int here { 0 };    describe(world[here]);     while (true)    {        if (world[here].exits.empty())            break;   // no exits = the end         std::cout << "\n> ";        std::string command {};        std::cin >> command;         if (command == "quit")            break;        else if (command == "look")            describe(world[here]);        else if (command == "go")        {            std::string direction {};            std::cin >> direction;            auto exit { world[here].exits.find(direction) };            if (exit == world[here].exits.end())                std::cout << "You can't go " << direction << ".\n";            else            {                here = exit->second;                describe(world[here]);            }        }        else            std::cout << "Try: go <direction>, look, quit\n";    }    std::cout << "\nThanks for playing!\n";    return 0;}

How it works#

  • Room is a struct (10.2 — struct: a group of values). Its exits map links a direction to a room number (8.6 — map: look things up by name).
  • here is the number of the room the player is in.
  • exits.find(direction) looks up the direction. exit->second is the room number it leads to.
  • The loop keeps asking for commands until the player wins or types quit.

Ideas to extend it#

  • Add items: a std::vector<std::string> in each room, and take / inventory commands.
  • Lock a door until the player has a key.
  • Load the rooms from a file (11.5 — Reading and writing files).
Tip

Build it in small steps. First make two rooms and go work. Then add more. Compile after every few lines!

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.