Thursday, December 1, 2011

Exercise 31: Making Decisions: Learn Ruby the Hard Way: Practicum

As I have mentioned previously, the programs that we had made prior to the last couple of days had a limitation. They ran in a straight line. There are some benefits to doing that, but it's rather limiting in other ways, too.

The ability to make decisions and to allow the user make choices makes a program more useful and more interesting. The last couple of days also showed how to run a program where predefined choices were made. Now we get to interactively make choices that use if, elsif and else, along with the option to enter our choices directly.

This script asks the user questions. Decisions are made based on the answers.

A key point here is that you are now putting the if-statements inside if-statements as code that can run. This is very powerful and can be used to create "nested" decisions, where one branch leads to another and another.

What You Should See

It's basically a run through of the various decisions. See below for my output.

Extra Credit

Make new parts of the game and change what decisions people can make. Expand the game out as much as you can before it gets ridiculous.

[ I added a door number three so we could have a real "Let's Make a Deal", and I added a clown, because, well, clowns are creepy. We could enter even more depth if we wanted to and put nested ifs into the nested ifs if we wanted to. ]


This is the start of user choice and interactivity. It's still a bit linear, in that the steps are small branches that run in mostly a straight line, with a couple of diversions. Still, this shows that we can make a number of changes in the way the program operates, and that we can follow all of the paths easily. The issue still is that we can only build out the line so far, and it requires us to follow each step to the end. In future modules, we will see how to keep the story going, so to speak, through the use of looping... but we'll get there shortly ;).

No comments: