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 (download and unzip it) 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 adventure.py should call the function to start game play after loading the story data into the graph.

Word wrap

You might notice that the nice long lines of text you wrote to describe locations extend beyond the edge of the screen when you display them.

Wrapping a long line of text into shorter lines so that they fit on the screen is is called “word wrap”. There are two types: “hard wrap” (no relation to the music), in which return characters are added to the original lines of text in the original file, and “soft wrap”, in which return characters are added as the text is being displayed.

Soft wrap is the more sophisticated choice because a) it doesn’t interfere with parsing the text file, and b) it allows the text to be displayed nicely on devices of different widths. If I hard-wrapped the lecture notes, you couldn’t read them on a phone.

The easiest way to get soft wraps is in PyCharm. Go to “preferences–>editor–>general” and choose “use soft wraps in console”. You’re set.

Or, for the adventurous, write your display code so that it figures out, based on some global constant describing how wide the screen is, where to wrap the text, and breaks the text into new strings of the correct length, using print to display the shorter strings. (This was an exam question a few years ago.) This is what Pycharm is doing internally when you choose the “use soft wraps” option.

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 one point of extra credit 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. adventure.py, 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.