Video Coming Soon...

Created by Zed A. Shaw Updated 2026-04-19 02:05:44

23: A Simple Adventure Game

There's a genre of game called "Interactive Fiction" that is most likely one of the earliest games. Games like Zork started as text only interactive narratives where you play a person wandering through a dungeon described only with text. Each location you travel is a new description, new things to interact with, and new scenarios to solve. These games eventually became Multi-User Dungeons, and Life is Strange to name a few.

What's great about these games is they're very easy to make. You only need to know how to handle some variables, call some functions, prompt the user, and write text descriptions. No graphics, physics, rendering, algorithms, or other complictions. Just you, your story, and some branching code.

Research

You should play the original Zork to get an idea of how an early interactive fiction game works. You can find fully playable MS-DOS versions of the game at these URLs:

If either of these don't work then you'll probably find another copy with a simple search.

You should also check out these games:

Also check out the Interactive Fiction Database for more specifically the more recent games list.

The Starter

I'm going to give a little "starter kit" but try to make a game like Zork with only 2 rooms and an end room on your own. If you get stuck then take a peek at my solution, or just go ahead and get this solution working for the next part of the exercise.

View my starter if you get stuckView Source file ex23.cpp Only

#include <fmt/core.h>
#include <iostream>
#include <functional>

using std::cin, fmt::println, fmt::print, std::string;

string prompt() {
  string buffer;
  print("> ");
  getline(cin, buffer);
  return buffer;
}

enum class RoomId {
  ROOM_A,
  ROOM_B,
  END
};

// this makes it so we don't type RoomId:: everywhere
using enum RoomId;

RoomId end() {
  fmt::println("You died...luser.");
  return END;
};

RoomId room_b() {
  println("There's a bear here. He hates you.");
  string cmd = prompt();
  
  if(cmd == "fight bear") {
    println("LOL, ok you're dead.");
    return END;
  } else if(cmd == "run") {
    println("Bears are fast, you're dead.");
    return END;
  } else {
    println("I don't understand that.");
    return ROOM_B;
  }
}

RoomId room_a() {
  println("You enter a room with a door to the east.");
  string cmd = prompt();

  if(cmd == "go east") {
    return ROOM_B;
  } else {
    println("I don't understand that.");
    return ROOM_A;
  }
}

int main(int argc, char *argv[]) {
  RoomId room=ROOM_A;

  while(room != END) {
    switch(room) {
      case ROOM_A:
        room = room_a();
        break;
      case ROOM_B:
        room = room_b();
        break;
      case END:
        room = end();
        break;
    }
  }
}

What is enum class?

The only new part of this code is the enum class RoomID. An enum stands for "enumerator" or "enumeration" and it's simply a way to associate a name like ROOM_A to a number. By default it starts numbering your names from 0, so ROOM_A==0 and END==2.

However, enum class style enums are more recent and also...annoying. In this code they work fine but if you ever want to print them you have to do int(room) to convert it to an int. Other than that, they should work like an int in most places you'd use them.

We use RoomID to create a path for the character and to know which room to run for the next step in the path.

The Requirements

Once you have this code working you are to do the following:

  1. Come up with a simple story for your game. It can be anything from a dungeon full of skeleton to a game about getting up and going to work.
  2. Draw a 8 room map using pencil (pen) and paper. You should be able to know how someone gets from room to room using North, South, East, West directions.
  3. Plan enemies and descriptions for each room.
  4. Plan the descriptions for doors.

Once you have your story, your map, and your plan you then need to turn that into code. I suggest you do it like this:

  1. Don't write up full descriptions of the rooms yet. First, get the flow (transitions) from room to room working correctly in the switch and enum class RoomID.
  2. Once you know you can move from room to room accurately, then write the text and prompts for one room at a time. Don't go crazy and write tons of code then test it once. Write a little, test a little.
  3. Finally add in any enemies, traps, etc. that you have in your game to make it interactive.

The Practice

Change It
You created the code, but see if you can add some additional feature I didn't require.
Break It
Try to find novel ways to break this code. Can you feed it bad inputs?
Recreate It
Try to recreate the basic structure of this code. It's not necessary to recreate all the text, just how you move from room to room.

Congratulations

You're at the end of the first module, and you now know enough C++ to start implementing little projects. In the next module you'll take the little C++ you've learned and create many little command line tools. If you've used Unix then you've probably used all of these tools multiple times.

Writing little command line tools is a great way to learn how to use any language. The tools are generally very simple, and almost always feature some aspect of systems programming. You could conceivably spend many months doing nothing but making a bunch of little utilities with the C++ you already know and learn a huge amount about programming.

Previous Lesson Back to Module

Register for Learn C++ the Hard Way

Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.