CS 1520 / COE 1520: Programming Languages for Web Applications (Fall 2005)

Department of Computer Science, University of Pittsburgh

Assignment 3: PHP: Online Mad Libs

(Due 11:59pm, Monday, November 21st)


Goal

You will use PHP to create an online game similar to Mad Libs, a popular word game for young children.

Introduction

In a Mad Lib, the player is given a list of blanks to fill in, with labels such as "noun," "verb, "name", "something round", etc. Once all of the blanks are filled in, the player turns the page and substitutes these words into the corresponding blanks of a story. The end result is usually a very funny story because the player had no idea what the context would be for the words he/she filled in.

Specifications

You will create three pages in PHP that allow a user to select a story, choose words to be substituted into that story, and view the finished story. You will also provide buttons for the user to return to a previous page if he/she wants to change something.

(i) Story files

Your program must be able to read in a "story file," which contains a mix of story text and special tags that identify what should be substituted into the story. An example story file would be:
One day, I was walking through the <# adjective #> part of
Pittsburgh when I ran into a huge stack of <# plural noun #>!
It was the most <# adjective #> sight I had ever seen. I tried
to call my best friend, <# proper noun #>, to tell <# him/her #>
about it but all of the phone circuits were busy.
As demonstrated by the above story, your program must be able to identify tags of the form "<# label #>". These tags can be of two types:

(ii) Required pages

Your program must consist of three pages: story selection, word selection, and story display.

(a) Story selection
This is the first page of your program. On this page, the user will be presented with a pulldown menu listing all of the stories available to be used in the game. The user will select a story from this list and hit a submit button, which will take the user to the word selection page.

Your program will give the user a choice between three story files: story1.txt, story2.txt, story3.txt.

(b) Word selection
Using the story selected in the previous page, your program must read through the story file and extract all word tags from it.

At the bottom of your word selection page there should be a "Submit" button and a "Go back" button. "Submit" will move the user to the "story display" page, while "Go back" will return the user to the first page to select a new story.

A word selection page using our example story might look like this.

Note: If the user attempts to access the word selection page directly, without first selecting a story from the story selection page, your program should print an error message and provide a link for the user to go select a story first.

(c) Story display
On this page, you must show the user the story he/she created. Your program should use HTML to print the story, and all word tags in the story must be replaced by the words that were chosen on the word selection page. These words should be in bold text so they can be easily noticed. For example, the story from earlier might be displayed as:
One day, I was walking through the laziest part of Pittsburgh
when I ran into a huge stack of pigs! It was the most colorful
sight I had ever seen. I tried to call my best friend, Nebraska,
to tell her about it but all of the phone circuits were busy.
In addition, you must also show the user his/her word selections at the top of the page. Each word should be labeled with a number (starting with 1) and should show the user both the word type (e.g. "adjective") and the word chosen (e.g. "laziest"). For multiple choice selections, display all available choices (e.g. "him/her") as well as the choice selected (e.g. "her"). For example, using the story above, the top of your page should show:
1) adjective: laziest
2) plural noun: pigs
3) adjective: colorful
4) proper noun: Nebraska
5) him/her: her
Finally, your story display page must include a "Go back" button that will return the user to the word selection page, where he/she may select new words to put into the story. When using the "Go back" button in this case, the word selection form should be filled with all the previously filled-in options.

A story display page using our example story might look like this.

Note: If the user attempts to access the story display page directly, without first selecting a story from the story selection page or selecting words from the word selection page, your program should print an error message and provide a link for the user to go select a story first.

Submitting your assignment

Notes on the submission process


Back to the class web page