A Text-Based Guessing Game

Now, let’s put together all of what you learned in this chapter and create your first guessing game! Basically, the users enter a number, and you will tell them if they are too high or too low. You will allow the users to guess until they figure it out. In order to make this game work, you will be using a loop in this game. If you cannot understand what the loop’s function is, it is explained in the next chapter.

First you need to create an initialization section. It will look something like this.

;demo02-11.bb - Try to guess the number
Print "Welcome to the Guessing Game!"
AppTitle "Guessing Game!"
;Seed the random generator...don't worry, it will be explained later
SeedRnd MilliSecs()

;Pick a number between 1 and 100
numbertoguess = Rand(1,100)

;The num of guesses the user has used
numofguesses = 0

The first line, after Print, calls the function AppTitle. This changes the name in the heading bar, so that instead of the program being named “Blitz Runtime Window,” it will be named “Guessing Game!”.

The randomizer works like this: numbertoguess is assigned to a random number, which is returned by Rand. Rand returns a number between what is given; here, it returns a number between 1 and 100. This section prints out introduction text, sets up the guessing number, and declares some variables.

Next you set up the loop and the test to make sure the player guessed a number between 1 and 100.

;set the beginning of loop label
.loopbegin
       ;Find out the user's guess
       guess = Input$("Guess a number ")

       ;If player guesses outside of range, tell him to guess again
       If guess > 100 Or guess < 1
              Print "Pick a number between 1 and 100, silly!"
              ;Go back to the beginning
              Goto loopbegin

       EndIf

The first line of this code sets up a label to go back to the loop later. Next, the loop begins, the player is asked for input, and the number is tested to see if it is within the correct range. If not, the player is sent back to the beginning of the loop.

Now, you insert the code to see if the player has guessed correctly.

;Add a guess to the guess counter
numofguesses = numofguesses + 1

;If the guess is too low, go back to beginning
If guess < numbertoguess Then

Print "The number was too low."
       Goto loopbegin
;If guess is too high, go back to the beginning
Else If guess > numbertoguess Then
       Print "The number was too high."
Goto loopbegin
EndIf

The first line adds one to the user’s number of guesses. Then, the code is tested to see if the user has guessed too high, too low, or just right. If the player has guessed just right, the code just continues through to the end of the program without going back to the beginning of the loop.

Finally, you enter the last section of code.

Print "You guessed the number " + numbertoguess + " in " + numofguesses   + " tries!"

;Wait five seconds
Delay 5000

This program can be run off the CD. It is named demo02-11.bb. Figure 2.14 shows the output of the complete Guessing Game.

Figure 2.14. The complete Guessing Game.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.7.240