Building a madlib is a great way to learn fundamental programming concepts. The application will ask a player to enter in words, it will store those words, and then replace words in a quote, story, or other short piece of text that will be written to the console window. The random words are often humorous when incorporated, so it can be fun to play after it is built.
We'll start our Madlib as a console application so that we can focus primarily on programming concepts, and the basics of C#. By the end of the series we'll have a GUI (Graphical User Interface) version.
New Project
Launch Visual Studio and start a new project.
Visual Studio has several ways to start a new project. Three easy options are:
- At the top left of the application choose the File drop down menu and select File >New > Project...
- or use the keyboard shortcut ctrl-shift-N
- or select the option from the start page
You can use one of Visual Studio's templates to start. On the left side of the New Project dialog box choose Visual C#. On the right side choose Console Application.
Keep your project name as one word - don't include spaces.
Madlib Text
We'll use a public domain excerpt from The Hound of the Baskervilles by A. Conan Doyle:
They all agreed that it was a huge creature, luminous, ghastly, and spectral. I have cross-examined these men, one of them a hard-headed countryman, one a farrier, and one a moorland farmer, who all tell the same story of this dreadful apparition, exactly corresponding to the hell-hound of the legend.
There are several elements within these sentences that we could use as prompts for the player; here is a list of ten words:
- creature
- luminous
- ghastly
- spectral
- countryman
- farrier
- farmer
- dreadful
- apparition
- hell-hound
C# Madlib Framework
At the top of the template that Visual Studio generated there are many lines that start with the word 'using'. The only one we need for this application is:
using System;
You can delete the rest of the using directives if you like to simplify your code, or keep them. My code examples will only have the one using directive that we need.
Inside of the Main() function set up comments that will serve as our 'to do' list of what code to create.
using System; namespace Madlib { class Program { static void Main() { //declare variables //write out a header //ask player to enter words //write out finished story //keep window open and prompt for exit } } }
Variables
A general guideline for naming your variables is to have the name be meaningful. If someone new looks at the code, ideally it is immediately apparent what the variable is for.
The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent. [Robert C. Martin
For this walk through we'll name our variables as the words they are replacing. For example, the variable to hold what the player types in for the first noun we'll call "Creature".
We can also add another variable to store our story after we add in the words the player has entered.
using System; namespace Madlib { class Program { static void Main() { //declare variables string Creature; string Luminous; string Ghastly; string Spectral; string Countryman; string Farrier; string Farmer; string Dreadful; string Apparition; string Hound; string Story; //write out a header //ask player to enter words //write out finished story //keep window open and prompt for exit } } }
Output
At the top of our console window we'll want to have some type of introduction to the game.
To write out to the console window we'll use some built-in code. We'll use the WriteLine function of the Console class to write a line of text out.
Example:
//write header and instructions Console.WriteLine("--------"); Console.WriteLine("Madlib!"); Console.WriteLine("-------");
Input
Next we will want to prompt our player to enter a word. We'll use the ReadLine function of the Console class to read in a line of text.
At the same time we'll store what the player has typed into the appropriate variable.
Example:
Console.Write("Please enter a noun: "); Creature = Console.ReadLine();
We can use this same pattern for all of our variables - just changing the prompt when needed (noun, adjective, occupation) and changing the variable name that the information will be stored into.
For this simple example we'll assume that the player is typing in the information we ask for, and we won't be performing any checks on the data entered.
Concatenation
We can build up our story with string concatenation. This combines variables and strings; the variable's value will show where the variable name is.
Example:
//write out story Story = "They all agreed that it was a huge " + Creature + ", " + Luminous + ", " + Ghastly + ", and " + Spectral + ". I have cross-examined these men, one of them a hard-headed " + Countryman + ", one a " + Farrier + ", and one a moorland " + Farmer + ", who all tell the same story of this " + Dreadful + " " + Apparition + ", exactly corresponding to the " + Hound + " of the legend."; Console.WriteLine(Story);
(Another way of writing out variable values is composite formatting.)
Exit Prompt
We want the console window to stay open for the player to read the finished story. We can use another prompt for input using either Console.Read(), Console.ReadLine() or Console.ReadKey(). If we don't have the prompt, the application will run, write out the results to the window, and then close quickly - probably before the player can even see that a story was generated. We'll add another line of text to let the player know that if they press the enter key the application will exit.
//keep window open and prompt for exit Console.WriteLine("Press enter to exit"); Console.ReadKey();
First Draft
With these steps we've gotten a first draft of a Madlib. It works, but there are some things we could do to improve it both on the interface side and on the programming side. In Part Two we'll look at some options to improve our Madlib application.
Show full code example
using System; namespace Madlib { class Program { static void Main() { //declare variables string Creature; string Luminous; string Ghastly; string Spectral; string Countryman; string Farrier; string Farmer; string Dreadful; string Apparition; string Hound; string Story; //write header and instructions Console.WriteLine("-------"); Console.WriteLine("Madlib!"); Console.WriteLine("-------"); //ask player to enter words Console.Write("Please enter a noun: "); Creature = Console.ReadLine(); Console.Write("Please enter an adjective: "); Luminous = Console.ReadLine(); Console.Write("Please enter an adjective: "); Ghastly = Console.ReadLine(); Console.Write("Please enter an adjective: "); Spectral = Console.ReadLine(); Console.Write("Please enter an occupation: "); Countryman = Console.ReadLine(); Console.Write("Please enter an occupation: "); Farrier = Console.ReadLine(); Console.Write("Please enter an occupation: "); Farmer = Console.ReadLine(); Console.Write("Please enter an adjective: "); Dreadful = Console.ReadLine(); Console.Write("Please enter a noun: "); Apparition = Console.ReadLine(); Console.Write("Please enter a noun: "); Hound = Console.ReadLine(); //write out story Story = "They all agreed that it was a huge " + Creature + ", " + Luminous + ", " + Ghastly + ", and " + Spectral + ". I have cross-examined these men, one of them a hard-headed " + Countryman + ", one a " + Farrier + ", and one a moorland " + Farmer + ", who all tell the same story of this " + Dreadful + " " + Apparition + ", exactly corresponding to the " + Hound + " of the legend."; Console.WriteLine(Story); //keep window open and prompt for exit Console.WriteLine("Press enter to exit"); Console.ReadKey(); } } }
Make the next iteration of your Madlib: Simple C# Madlib (Part Two)