Learning to Program Through the Use of Writing Games: Part Two

Douglas Hutsell
In part one we used the old memory game as our basis for our program, and broke it down into smaller parts. I normally use Perl for my quick coding projects, however most web-severs use Perl and will actually try to execute the code ... instead of displaying as we would like. So, for this example we will use our own pseudo-code so that we are able to understand what's going on without giving the web-server a heart attack. For all those interested in working code I have a working version written in Perl located at: http://docs.google.com/Doc?id=df7fc6xh_75nnf8jn5f

Let's look at the steps we took to create our memory game.

1. Generate a number
2. Show number
3. Hide number
4. Prompt for user's guess
5. Check to see if user is correct

Now all we need to do it follow the same steps we took for our game logic and apply it to our programming logic. We will take each game step and add the appropriate pseudo-code to go along with it. These code examples are not language specific. So do not worry if you are using Perl, ruby, C++ etc.

1. Generate a number - Our first task was to generate a random number that we would need to try to memorize. This would be done by using your programming languages random functions and assigning it to a variable.

>NUMBER = (random number between 1 and 100000)

2. Show number - Now that we have a generated number we need to let the player see it. We do this by simply printing the variable out to STD-OUT which by default is the screen.

> print ("NUMBER")

3. Hide number - In our third task we are going to combine three steps, because they flow logically together. Now that we have our number on the screen we are going to wait 5 seconds by using your languages pause or wait command. After this 5 second pause, the program with then clear the screen so the number is no longer showing. After another 5 seconds the player will be prompted to see if he or she remembers the number that was just shown. This time can be adjusted if a higher or lower difficultly is desired.

> wait (5 seconds)
> system "clear screen"
> wait (5 seconds)

4. Prompt for user's guess - We now get the players guess by pulling their answer from standard in. Don't forget to chop or chomp the newline off if you start getting unexpected behaviors. IE you know you got the number right even though it tells you it was the wrong answer.

> get answer from STD-IN

5. Check to see if user is correct - Our last step is to take the players answer and compare it to the actual number. In our example below we respond back to the screen whether we were right or wrong.

> if ANSWER == NUMBER { print ("you got it!")}
> else { print ("better luck next time")}

This example may seem really simple, and it is. Even though it is short and sweet, all it would take to run is to plug in the syntax of your particular programming language. As mentioned before I have made available a version of this simple game in a working Perl script. If I find people find this helpful or interesting I will write a part three that will develop this game even further, by adding a saved score feature and perhaps a scaling difficulty.

Published by Douglas Hutsell

Young, opinionated and technical with way too much time on his hands.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.