Guess The Number game

Show Index

Now I would like to show you how to write a 'Guess the number' game.  This game will allow you to guess a number from 0 to 9.  It will then tell you if you guessed the number correctly, or incorrectly.

Later, I will show you how to modify this game so that if you guess incorrectly, the program will tell you if you guessed too high or too low.  If you guessed too high or too low, it will give you a chance to guess again.

 

First, I would like the game to repeat over and over until I ask it to quit.  So the first thing I need is a loop.  A Repeat-Until should do it.  This time however, I'm going to change things a little bit (for learning purposes) and request that the user type in 'Y' to play again, or 'N' to quit.  Actually I'm only going to bother checking for Y.  If the player doesn't enter a Y, then the program will end.

One small note before I show you the code; Since Waitkey returns the ASCII value of the key pressed, the value returned could be 89 for an uppercase 'Y' if the player is holding the SHIFT key or if their CAPSLOCK key is on, but normally it would just be a 121 for a lowercase 'y'.  Since it could be either one, we need to check for both, so I decided to combine the two checks into one statement using the Or command with the If command (If keypressed = 89 Or keypressed = 121 Then playagain = True).

Repeat

  ;game code will be inserted here...

  Print "Would you like to play again?"
  Print "Press 'Y' to play again or 'N' to quit."
  playagain = False
  keypressed = WaitKey()
  If keypressed = 89 Or keypressed = 121 Then playagain = True
Until playagain = False
End
The use of the semicolon before the text ;game code will be inserted here... makes everything on that line after the semicolon a comment.   Blitz ignores all comments.  You can use comments in your code any where you like by typing a semicolon to make notes to your self.

Did you notice that I created a variable named 'playagain' and set it to 'False' before I checked what key was pressed?  In Blitz, False = 0 and True = 1.  I could have written 'playagain = 0' but I chose to write 'playagain = False' because using True and False is more readable.

So, as you can see, our program will loop over and over until playagain equals False (Until playagain = False).  Every loop of the program, I purposely set playagain to equal False.   BUT, if the player presses the 'Y' key, then playagain will be set to True before the computer reaches the Until statement.  If playagain equals True when the computer reaches the Until statement, it will cause the computer to go all of the way back up to the corresponding Repeat statement and start over.

 

So, now we need to actually add some code in there to get our game working.

Replace the comment line above (;game code will be inserted here...) with the following code:

  numbertoguess = Rand(0, 9)
  Print
  Print "I am thinking of a number between 0 and 9."
  Print "Guess the number by pressing a key between 0 and 9."
  Print
  Repeat
    keypressed = WaitKey()
  Until keypressed > 47 And keypressed < 58
  keypressed = keypressed - 48
  If keypressed = numbertoguess
    Print "Correct! The number I was thinking of was " + numbertoguess
  Else
    Print "Sorry, but " + keypressed + " is not the number I was thinking of."
  EndIf

First off, I hope you noticed that all of the above code is indented from the left.   Please copy/type it in that way.  Indenting the code between the Repeat-Until  statements helps us to easily see the programs structure.  We can quickly see which parts of the program are part of the Repeat-Until loop because everything in between is indented to the right.

I'll explain how this section of code works.  The first thing this code does is chooses a random number between 0 and 9 using the Rand command and then saves that value in the variable 'numbertoguess'.  Next, it asks you to guess the number.

You'll notice that there is then one more Repeat-Until pair.  This loop will repeat itself over and over until the player presses a key between 47 and 58.  Key 48 is ASCII 0 and key 57 is ASCII 9.   So, a key between 47 and 58 will be a number from 0 to 9.  This value is stored in the variable 'keypressed'.

On the following line (keypressed = keypressed - 48), the value stored in keypressed is reduced by 48.  This is so that we can compare keypressed to numbertoguess.  For instance, if the key pressed was the number 3 key, then the value returned from the Waitkey() command would be 51 (the ASCII value associated with the number 3).  So the variable keypressed would first be assigned the value of 51 but then reduced by 48 to finally equal 3.  This is how we know which key was pressed. (For your information, there are other ways to find out which keys have been pressed, but this is the method we are using in this example game.)

And finally, depending on if keypressed equals numbertoguess or not, the program informs you if you were correct or not.

 

So, the entire program pieced together should look like this:

Repeat

  numbertoguess = Rand(0, 9)
  Print
  Print "I am thinking of a number between 0 and 9."
  Print "Guess the number by pressing a key between 0 and 9."
  Print
  Repeat
    keypressed = WaitKey()
  Until keypressed > 47 And keypressed < 58
  keypressed = keypressed - 48
  If keypressed = numbertoguess
    Print "Correct! The number I was thinking of was " + numbertoguess
  Else
    Print "Sorry, but " + keypressed + " is not the number I was thinking of."
  EndIf

  Print "Would you like to play again?"
  Print "Press 'Y' to play again or 'N' to quit."
  playagain = False
  keypressed = WaitKey()
  If keypressed = 89 Or keypressed = 121 Then playagain = True

Until playagain = False
End

 

Play a few games and see how it works.  Try changing a few things if you dare, such as making it only guess a number between 1 and 5.

P.S. If you've played a few games and have happened to notice that the numbers chosen by the computer aren't exactly random, don't worry about it.  I'll address that issue at the end of the next section.

Continue on to Improved Guess The Number Game.
Goto next lesson

 

If you've reached this page and there's no index on the left, Click here to show the Index Page