Week 12 Exercise: Choose Your Own Adventure

Chris Tralie, heavily inspired by Bill Mongan

Overview / Logistics

The purpose of this exercise is to give you practice with C++ STL Maps, as a warmup for when you create your own implementation of maps on homework 5. The idea for this exercise was inspired by an assignment that Bill Mongan ran in CS 173 a few years ago. This is basically the C++ version of his assignment.

To obtain the code for this exercise, type

git clone https://github.com/ursinus-cs174-f2022/Week12_MyAdventure.git

Learning Objectives

  • Use proper data structures as keys and values in C++ maps
  • Choose an appropriate loop structure for a given task

Background

Choose Your Own Adventure books are a series of books from the '80s and '90s in which the reader takes a more active than usual role. In particular, the books are written in the second person, and they turn over key decisions to the reader. Based on these decisions, the reader jumps to different pages to see the outcome of those decisions.

In this exercise, you'll be designing a Choose Your Own Adventure-style program to tell a dynamic story. There are four pieces of information that you should represent in your program

  1. A list of places you can be in your world
  2. A little blurb describing each place
  3. A list of places you can travel to next from each place
  4. Which place you start, and which place ends

Your Below is a very simple example to illustrate this idea, which pretty much sums up my life as an instructor during quarantine last year (thank goodness those days are over for now...). The game starts in the office, and it ends whenever someone reaches the outside.

A sample run through the program might look like this

  • You are in the Office. The place where you frantically try to get everything ready for class. Where would you like to go?

    Options: [Kitchen, Bathroom]

    Input: Kitchen

  • You are in the Kitchen. The place where you heat up frozen food and sometimes (but rarely) cook. Where would you like to go?

    Options: [Office, Upstairs Bedroom, Outside]

    Input: Upstairs Bedroom

  • You are in the Upstairs Bedroom. The place you go rarely when you've finished your work for the day. Where would you like to go?

    Options: [Kitchen, Upstairs Bathroom]

    Input: Kitchen

  • You are in the Kitchen. The place where you heat up frozen food and sometimes (but rarely) cook. Where would you like to go?

    Options: [Office, Upstairs Bedroom, Outside]

    Input: Upstairs Bedroom

  • ......

Programming Task

You should create a map called places whose keys are strings with the names of the different places you can go in your story. The values associated to different keys should be vectors of strings strings. The first element in this vector should be a description of the place, and the remaining elements in this vector should be the strings of the places you can reach from this place. Your program should go through a loop where in each iteration, it prints the place you are and its description, followed by a list of places you can go. Then, you should wait for input from the user to say which place to go. Finally, you should have a constant string which represents where the loop starts, and a constant string to represent when the loop should terminate.

The example I gave is pretty boring (because it mirrors my life!). But you should feel free to be as creative as you want. It is recommended that you sketch your world out of paper before you translate it into code. We will share some of your examples on the class web site. Have fun!