MADLibs.NET and C# and ASP.NET

Introduction

When I was taking my much needed vacation with my girlfriend, I found a Brookstone game that brought back childhood camp memories of one my favorite belly-laughing pasttimes, MadLIBS.  I still get a kick out of the game after all these years and decided to put together a version for the web.  For those who are unfamiliar with the game, it goes something like this.  You are prompted for a word by another person (or in this case a computer) in a grammar category.  For example you may be asked for a noun or verb or name.  You simply name anything in that particular category and it is recorded inside the story in a blanked out word space.  You will continue to be prompted for a word fitting a particular grammar until all spots are filled.  Then the resulting story is read. As you might imagine, the story takes on an almost mad construction as you have blindly seeded the story with substituted irrelevant words. 

Figure 1 - The Results of a Mad Libs Children's Tale

MadLibs.jpg

MadLibs.NET

My version of mad libs utilizes a text file that is a template for the story.  All information for the game is contained in this file.  The file consists of two types of placeholders.  One place holder is bracketed with < > . These brackets contain the grammar prompt for all the unfilled spots (e.g. <plural noun>).  The other place holder is a set of square brackets [] containing a number (e.g. [0]) .  This is a new feature I added to the game to allow the user to repeat a chosen bracketed <> word in another part of the story.  The number in the square brackets refers to the order of the <> grammar word.  Below is the MadLibs text file for the three little pigs:

Listing 1 - Mad Libs Template File

Once upon a time there were three little <animal>s. They each lived in three houses. One made of <plural noun>, one made of <plural noun> and one made of <plural noun>. One day the <adjective> [0]s were sitting in their houses, when along came a big <adjective> <animal>. He huffed and he <verb - past tense> and he blew down the house made of [1]. The [0] was so <emotion> that he yelled <exclamation> and ran to the house made of [2]. The [6] <verb - past tense> to the house made of [2] and blew that house to <place>. Both [0]s <verb-past tense> and ran to the house made of [3]. The [6] followed the [0]s to the house made of [3]. He huffed and he puffed, but he could not seem to make the house <action verb>. So the [6] gave up and ran to <place>. The [0]s were very <adjective>.

The MadLib.NET game will continue to prompt the user with each grammar only in the <> brackets.  When the programming engine reaches the end of the template, the story is displayed.  Word needed for the [] square brackets are automatically substituted by the computer based on the sequence of the <> fields.

Design

The design of the MadLibs game consists of a web page and a parser.  The web page displays all the grammar prompts and the final story.  The parser does all the parsing of the template file shown in Listing 1.

MadLibsUML.jpg

Figure 2 - MadLibs.NET Game Reverse Engineered with WithClass UML Tool

Getting the template file

Initially, the template file is read into the session and the first prompt is read in from the template and placed in the grammar prompt label.  Also we keep a pointer in the session pointing to the last position of the <field> in the template that we read into the prompt.

Listing 2 - Initial reading of the template

// open a new excel spreadsheet
if (this.IsPostBack)
{
...
}
else
{
// get the madlib template and store it in the session
string libText = ReadMadLibFromFile("MadFile.txt");
Session["Counter"] = 0;
// get the first label and place it in the grammar prompt
MadLibParser parser = new MadLibParser();
int nextIndex = 0;
string grammar = parser.ParseNext(0, libText, ref nextIndex);
Session["NextIndex"] = nextIndex;
lblMadQuestion.Text = String.Format("{0}: Enter a {1}.", 1, grammar);
}

After each subsequent push of the button, we parse the next piece of grammar out of the template in the post back and assign the label text.  If the end of the template is reached in the post back, we display the final story as shown in Listing 3

Listing 3 - Reading in grammar after the post back

if (this.IsPostBack)
{
int counter = (int)Session["Counter"]; // get the current counter, that counts the template field
counter++;
// get the next grammar field
MadLibParser parser = new MadLibParser();
int index = (int)Session["NextIndex"];
int nextIndex = index;
string madText = (string)Session["MadLibForm"];
string grammar = parser.ParseNext(index, madText, ref nextIndex);
if (nextIndex == -1) // we have reached the end of the template, display the story
{
RecordNextWord(); // make sure we get the last entered word and put it into the session
string story = parser.ConstructStory(madText, Session);
txtStory.Text = story;
Button1.Enabled = false;
}
else // set the prompt for the next grammar field
{
Session["NextIndex"] = nextIndex;
lblMadQuestion.Text = String.Format("{0}: Enter a {1}.", counter+1, grammar);
}
}
else
{
... // see listing 2
}

The button event handler saves the word that the player entered into the Session State.  Here we increment a count on each grammar field, and use this to make a unique index into the session state table as shown in listing 4.

Listing 4 - Recording the player's entered word

private void RecordNextWord()
{
int count = (int)Session["Counter"];
Session["Entry" + count.ToString()] = txtGrammarEntry.Text;
count++;
Session["Counter"] = count; // store it back in the session
} 

#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
// store the next entry into the session
RecordNextWord();
}

After all words are entered, the story is constructed from the template.  The program goes back through all the players words recorded in the session and places them consecutively inside the template in the appropriate places.  All construction is performed in the ConstructStory method of the MadLibParser class.  The ConstructStory method first goes through <> fields and replaces them one after another with the player's word entries.  It then finds each square bracket field, looks at the number contained in them, and places the appropriate player entry corresponding to the sequence in which the player entered the word. For example, if the square brackets contain "[0]", the program will replace this field with the first entry the player entered in the game.

Listing 5 - Constructing the story from the player words and the madlib template

public string ConstructStory(string madlibFile, HttpSessionState session)
{
string story = "";
int count = 0;
int nextIndex = 0;
int index = 0;
// loop through all player entries until there are no more entries
while (session["Entry" + count.ToString()] != null)
{
// replace the template with the next player entry
madlibFile = ReplaceNext(madlibFile, (string)session["Entry" + count.ToString()], "<", ">");
count++;
}
// replace square brackets as well
// loop through all square brackets, and replace with the player word
// corresponding to the particular sequence number contained in the brackets
while (madlibFile.IndexOf("[") >= 0)
{
// parse out sequence number from square brackets
string bracketNumber = GetNextNumber(madlibFile);
replace template string bracket field with player entry corresponding to the sequence
madlibFile = ReplaceNext(madlibFile, (string)session["Entry" + bracketNumber], "[", "]");
}
return madlibFile;
}

Conclusion

MadLIBS has given me hours of fun as a kid and still makes me chuckle when I read the wacky stories constructed from the game.  It's my pleasure to pass on some of that wackiness to you as you stretch your creativity in this entertaining game of words now living on the Web courtesy of  C# and .NET.