SUNY Geneseo Department of Computer Science


Project 3—Mad Libs

CSci 120, Spring 2014

Prof. Doug Baldwin

Due Thursday, May 1

Purpose

This exercise reinforces your ability to work with data structures, notably cell arrays and strings, in Matlab. It also reinforces your general ability to design and write Matlab programs.

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.

You are likely to want to work with vectors of strings in this project. However, ordinary vectors of strings don’t work very well in Matlab: a string is itself really a vector of characters, and so a “vector” of strings is really a matrix of characters. Since all rows (or columns) in a matrix have to be the same length, this in turn implies that all strings in a “vector” of strings have to be the same length. And in real life, the strings in a set of related strings are almost never all the same length. The usual Matlab solution to this problem is to use one-dimensional cell arrays, not ordinary vectors, when you want a vector of strings.

Exercise

Write a Matlab script that manages a game of Mad Libs with its user. In particular, this script 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 in some reasonably readable way.

The cleanest way to write the script would be as a function that receives the Mad Lib it is to manage through it parameter(s). This function will have no explicit return variable, since it produces its result by printing the completed Mad Lib.

The central problem in solving this exercise will probably be to think of a way to represent a Mad Lib as Matlab data. As a hint, 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.  

There are several ways you could represent such an alternating sequence in Matlab.

In addition to the functional requirement of managing a Mad Libs game, your script should also be clear and readable, including being well-commented. It also has to be distinct from other people’s solutions and reflect your own understanding of how to solve the problem; sources you use from outside this course (e.g., examples from the Internet, books other than our textbook) must be acknowledged.

Bonus. Store one or more Mad Libs in “.mat” files so that you can easily save them for reuse or sharing with others.

Follow-Up

When you finish this exercise, email the “.m” file containing your script to Samir (saf8@geneseo.edu). Please make the subject line of your email be

Project 3 - YourName

where YourName should be replaced with your own first and last name. Your email should be sent by 11:59 on the due date above.