Improved Guess The Number game

Show Index

Now I will show you what code to add to make the 'Guess the number' game tell you if you are guessing too high or too low.

Building larger programs is often easier if we start with something simple first and then we just keep adding to it.  This is what we will do here as well.

So, starting with the original Guessing game code:

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

We will add code to not only tell the player if he/she guessed too high or too low, but we will also keep track of how many tries it took them to guess the number.

The first thing we will do is add a loop to keep the player guessing until they guess the correct number.  I think a While-Wend  loop will work fine here:

Repeat

  numbertoguess = Rand(0, 9)
  keepguessing = True
  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

  While keepguessing = True
    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
      keepguessing = False
    Else
      Print "Sorry, but " + keypressed + " is not the number I am thinking of."
    EndIf
  Wend

  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

Notice the While keepguessing = True loop that I added around the mid section of the code? (Note also, that I indented all of the code in between the While-Wend for readability...)  I also had to add two other lines of code to make it work.   The first one is at the top, keepguessing = True, and the second line is in the middle, keepguessing = False.   The keepguessing = True line at the top sets up our While loop to keep looping until the player guesses correctly.   The keepguessing = False line is executed if the player guesses the correct number and will allow the While loop to stop looping, thereby allowing the game to end if the player so wishes

You can actually run the above code and it will work, but it's not very informative yet.  But it does allow you to keep guessing until you guess the correct number.

Now lets look at adding some code to actually make the computer tell us if we guessed too high or too low.  Add the following few lines immediately after the Print "Sorry, but "... line.

      If keypressed > numbertoguess
        clue$ = "lower."
      Else
        clue$ = "higher."
      EndIf
      Print "The number I am thinking of is " + clue$
      Print "Try again by pressing a different key."
      Print

This will tell us to guess lower if the key we pressed was higher than the number or it will tell us to guess higher if the key we pressed was lower.  The above code should be fairly self-explanatory.

If you run the code in this state, it will now work the way we want it to.  Play a few games and see.

 

The last thing for us to add is the ability for the computer to keep track of how many tries it took the player to guess the correct number.

Three lines of code in well placed areas is all it takes to do that.  We'll set the number of tries taken to equal zero at the beginning of the program.  Then we'll increment the number of tries taken by 1 every time the player guesses a number (presses a key).  Lastly, we'll let the player know how many tries it took when they finally do guess the correct number.

And the code should look like this:

Repeat

  numbertoguess = Rand(0, 9)
  keepguessing = True
  numberoftries = 0
  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

  While keepguessing = True
    Repeat
      keypressed = WaitKey()
    Until keypressed > 47 And keypressed < 58
    keypressed = keypressed - 48
    numberoftries = numberoftries + 1
    If keypressed = numbertoguess
      Print "Correct! The number I was thinking of was " + numbertoguess
      Print "It took you " + numberoftries + " tries to guess the number."
      Print
      keepguessing = False
    Else
      Print "Sorry, but " + keypressed + " is not the number I am thinking of."
      If keypressed > numbertoguess
        clue$ = "lower."
      Else
        clue$ = "higher."
      EndIf
      Print "The number I am thinking of is " + clue$
      Print "Try again by pressing a different key."
      Print
    EndIf
  Wend

  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

Can you see where I added those three lines of code?

 

And that's it.  Our Improved Guess The Number game is now completely functional.   It should tell you if you are right/wrong, whether to guess higher/lower, and how many tries it took you to finally guess the number.

 

One last thing.  By now, if you've played a few games of Guess The Number, you will probably have noticed that the first number the program 'randomly' chooses is always the same number.  And every number after that is always the same ones too.  This is because most computers can't actually pick a truly random number.  That is not how they work.  They can only really choose a pseudo-random number.

Every time the program starts, it starts choosing the same sequence of random numbers.   We can change that though by using the SeedRnd command.  This command will start the pseudo-random number sequence in a different spot.  The only thing is, how do we start it at a spot we don't know?  A great way to do this is to tell the SeedRnd command to use the system clock as the spot to start at.  The MilliSecs() command returns the number of milliseconds (thousandths of a second) that have elapsed since the computer was turned on.  Since it would be dang near impossible for you to start your Guess A Number game at the exact same millisecond that you started it at the last time you had the computer on, it pretty much guarantees a random number sequence that you will not know.

So, to get completely random numbers, just add SeedRnd MilliSecs() to the very beginning of your program.

SeedRnd MilliSecs()
Repeat

  numbertoguess = Rand(0, 9)
  keepguessing = True
.
.
.

 

And that's all for now...

to be continued...

Continue on to *NoLink*.

 

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