Codewise, this project is way simpler than our Consignment Shoppe Demo. Probably because the Demo was coded by a professional and this one was coded by me.
Today we've got a simple Mad Libs console application in C#. It uses random, switch case, an array, Console.ReadLine, Console.WriteLine, and a loop.
I enjoyed doing this from scratch and having a finished product in less than an hour!
Here's the Code:
Arrays and Switch Cases:
The main hurdles were making sure the array, switch case, and random variable worked together. I may have cheated a little, but in order for each of the switch cases to match up with the appropriate author in the array, I put in a dummy string "" for the first array index. Without this dummy string, the random.Next feature would pick, say, Virginia Woolf, who is Case 5, but because Arrays index starting from 0 instead of starting from 1, Virginia Woolf was actually considered Case 4, so the text was off by one.
There's probably a better way to do that, but it worked, and this is all learning curve right now.
Instead of just one determined writing sample that plays out the same each time, I wanted to create a relatively tidy index of random writing samples from famous authors: Willy Shakespeare, Jane Austen, Homer, Virginia Woolf, Herman Melville.
So I created a random variable, gave it an index, and plugged it into a switch case--all things that would still have sounded like mumbo-jumbo about a month ago.
Clean Up On Lines 17, 18, 19...
Reminded of the importance of spaces when dealing with multiple lines of strings. The things you take for granted!
It's still not perfect, but at least it reads a little more clearly!
Replay Or Exit:
Had a bit of a time with this one, and it's still not the clean exit I want. But I learned the importance of method parameters. I originally had everything in the Main method, then realized I couldn't easily ask the user if they wanted to play or exit. So I put the main game loop inside a method called Start and then allowed the player to either choose to play again or exit. The exit still goes to debug mode instead of closing down the console, but for some reason neither this.Close, Application.Exit, or Console.Close/Exit/Whatever seemed to be recognized.
But the point, this baby is finished, and I know what areas I need to work on.
The writing samples used in the application are from the following:
1. Shakespeare's "Much Ado About Nothing"
2. Jane Austen's "Emma"
3. Herman Melville's "Moby Dick"
4. Virginia Woolf's "Orlando"
5. Vince Lombardo's translation of Homer's "The Odyssey"
Glad you were here; till next time!
In Excelsis Deo.
K.W. writes novels, short stories, the occasional ode, game scripts, and (with actual evidence!), this here blog.