Short assignment: Adventure

Your goal in this short assignment is to write a program that lets the user read and play a simple "choose-your-own-adventure" style book. (There is a long history of such interactive fiction or text adventure games. Perhaps my favorite short one is suveh nux. Enjoy, but do the assignment first.)

Part 1: Write your novel

The first order of business is to write the story, and save it in a text file that your program can interpret. The basic structure of the story will be a graph. The data in each vertex will be the text of the story and the list of choices. You will also need to specify how the vertices in the graph are connected.

Here is a sample story. You will need to write your own, but may use this one for testing. (Your story may be less Grimm.)

START| LESS_TRAVELED, BRIDGE| You are walking through the woods on a snowy evening.  You come to a fork in the road.  Do you a) take the road less-traveled or b) take the road more-traveled?

LESS_TRAVELED|| You are eaten by a bear.  The end.

BRIDGE|SLIPPED, EATEN_BY_TROLL| You walk along the road for a while.  As you come to a bridge, a troll jumps out and demands a gold coin as a toll.  Do you a) pay the troll's toll or b) refuse to pay?

SLIPPED|| The troll happily wanders off with his gold coin, but as you step onto the bridge,
your foot slips on the wet wood.  You fall off of the bridge and into the icy water.  The end.

EATEN_BY_TROLL|| The troll eats you.  The end.

Let's look at the first line of the file:

START| LESS_TRAVELED, BRIDGE| You are walking through the woods on a snowy evening.  You come to a fork in the road.  Do you a) take the road less-traveled or b) take the road more-traveled?

It's a single line of text; the only invisible '' carriage-return character occurs at the end of the line (in this example, after the question mark).

The first capital word, before the vertical bar (shift-colon on a US keyboard), is the name of the vertex. The comma-separated words between the vertical bars are a list of names of vertices that can be reached from this vertex, by taking options a, b, c, etc. The text after the second bar is the data for the vertex, a text description that you'd see on the page of a choose-your-own-adventure novel.

You'll need to write a Vertex class. A Vertex contains reference to a Python list of strings describing the connected vertices, and some data. For the example vertex, the adjacency list would be ["LESS_TRAVELED", "BRIDGE"], and the data would be "You are walking...more-traveled?"

Where are vertices stored? We'll use a dictionary of vertices. The vertex names will be the keys into the dictionary. For our example, you'd like something like this: vertices["START"] = Vertex(...) (That's just a hint. Your code won't look exactly like this.)

Here's some nearly-complete code to get you started with loading the file in and parsing it. You'll need to finish the load_story function by creating graph vertices and adding them to the dictionary.

Finally, write a function containing a while loop that allows you to play the game. Grab the vertex "START" from the dictionary, get the list of choices, and allow the user to input a value like a, b, or c using the Python input function, which you may look up online. (If you are using Python 2, you should use the function raw_input instead.) Based on that choice (remember you can convert a char to a number using ord), grab the next vertex from the dictionary, and repeat until you reach a vertex with no outgoing links.

Your code for should call the function to start game play after loading the story data into the graph.

Writing your story

You will need to write your own story for the assignment; mine is too simple to adequately test your program.

Here are the ground rules for your story. It should have at least twelve vertices (mine only has five). At least one of the vertices should have three adjacent vertices, and at least three vertices should have two adjacent vertices. You will not be graded on the quality of the content of your story, but please keep it PG-rated!

How to create the text file

Do not use Word to write your story. Word documents contain information about formatting (like whether a word is in bold or italics) in the file. Such formatting information would deeply confuse your program.

In order to create a plain text document, the easiest thing is to go to the "New file" choice in PyCharm, and create a file with the .txt extension, such as nanowrimo.txt. Do not press return as you are typing the text line.

Notice that there is a tradeoff here between simplicity and generality. How can you have a multi-paragraph description of a location (page) in your story, if you aren't able to press the return key?

I will give 2 SA extra credit points if you allow return keys to separate paragraphs in your file. You'll need to improve the file parser to handle that, and you'll need to change the rules of how your file is written. The tricky thing is for the parser to know when it is done reading in the description and should now work on the next vertex. You could use an extra return key to indicate that a description is complete.

What to turn in

  1. Your text file with the story. I called mine "story.txt".
  2., containing the required functions, and code that calls those functions to load and play the story.
  3. A screenshot of a story in progress.

Honor Code

The consequences of violating the Honor Code can be severe. Please always keep in mind the word and spirit of the Honor Code.