SUNY Geneseo Department of Mathematics

Project 2—Mad Libs

Math 230 01
Fall 2015
Prof. Doug Baldwin

Complete by Monday, December 14
Grade by Tuesday, December 22

Purpose

This exercise helps develop your ability to design data structures to represent complex abstract concepts. It also gives you some practice working with Matlab strings, string input and output, and files.

Background

Mad Libs is a word game involving a short story or other piece of text with some words missing. Players suggest words to insert for the missing ones, but without knowing the story. The result is an often-funny mangled story. See http://www.madlibs.com/ for more information. Your goal in this project is to write a Matlab program that manages a game of Mad Libs.

Your solution to this exercise will probably rely heavily on strings (i.e., vectors of characters). We will talk about strings in lecture on December 7, and you can find a discussion in chapter 8 of our textbook.

One relevant fact about Matlab strings is that you can’t easily make ordinary vectors of them. Cell arrays are the usual way around this problem. They were discussed in the December 2 and December 4 classes, and in sections 15.2 through 15.4 of our textbook.

In order to play an interactive game of Mad Libs, your program will need to input strings from the user, and output strings to the user. Matlab’s input and fprintf functions will be helpful for this.

Finally, you will need to save Mad Libs to files and load them from files. Our textbook talks about such file operations in section 6.5.

Activity

Write a Matlab function that takes a representation of a Mad Lib as its argument, and that “plays” that Mad Lib with its user. In particular, your function should…

  1. Prompt its user for each word needed to complete the Mad Lib, showing the cue (e.g., “noun,” “color,” etc.) for the word as part of the prompt
  2. After all words have been collected, print the filled-in Mad Lib.

The central problem in this exercise is to think of a “representation” of a Mad Lib as Matlab data. Your Mad-Lib-playing function must have an argument that is such a representation of a Mad Lib, and the function must be able to play whatever Mad Lib that argument describes. If you end up with a function that only plays one Mad Lib you are missing an important part of this project.

To help you start thinking about what a “representation” of a Mad Lib might be, consider that a Mab Lib is basically an alternating sequence of text fragments and blanks. Each blank has a cue for the kind of word that should fill that blank. The sequence starts and ends with text fragments. For example…

Text Fragments Cues for Blanks
Once upon a time there lived a  
  Noun
who was very  
  Adjective
But luckily, he/she/it met a  
  Noun
and lived happily ever after.  

A “representation” therefore has to be some sort of Matlab value that can store such an alternating sequence. There are several ways you can do this.

Note that your Mad-Lib-playing function will have no explicit return variable, since it produces its result by printing the completed Mad Lib.

In addition to the function that plays a Mad Lib with the user, you should also be able to save a Matlab representation of a Mad Lib into a file, and load it back from its file. Note that this may be more a matter of knowing single Matlab commands for saving and loading relevant variables than of writing substantial amounts of Matlab code.

Follow-Up

I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring a written solution to the exercise to your meeting, as that will speed the process along.

Sign up for a meeting via Google calendar. If you worked in a group on this exercise, the whole group should schedule a single meeting with me. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.