| Liberty BASIC - Help Online |
Let's Write a Program
HILO.BAS
Now we will write a simple game using all of the concepts described in this tutorial. These include:
input statement
print statement
let statement
variables
goto statement
conditional branching with if . . . then
functions
documenting
THE GAME - HILO.BAS
Hi-Lo is a simple guessing game. The computer will pick a number between 1 and 100. Our job is to guess the number in as few guesses as we can. When we guess, the computer will tell us to guess higher or to guess lower depending on whether we guessed too high or too low. When we finally get it, the computer will tell us how many guesses it took.
Let's outline how our program will work before we begin to write code:
(1) Pick a number between 1 and 100
(2) Print program title and give some instructions
(3) Ask for the user to guess number
(4) Tally the guess
(5) If the guess is right go to step (9)
(6) If the guess is too low tell the user to guess higher
(7) If the guess is too high tell the user to guess lower
(8) Go back to step (3)
(9) Beep and tell the user how many guess it took to win
(10) Ask the user whether to play again
(11) If the user answers yes then clear the guess tally and goto step (1)
(12) Give the user instructions on how to close the game window
(13) End the program
When we write an outline for a computer program like we did here, the resulting outline is often called pseudocode, which is a fancy name for false code. This can be a useful tool for planning out software before it is written, and it can be very helpful in developing ideas before code is actually written.
Now we are going to take each of the steps above and write BASIC code for each step. We will document the code to explain its purpose:
(1) Pick a number between 1 and 100
' Here is an interactive HI-LO
' Program
[start]
guessMe = int(rnd(1)*100) + 1
The first couple of lines are just ' remark statements to give a brief desciption for the program. The [start] branch label is equivalent to calling this part of the program step .
Now we have the code that picks the number. We use two functions here to accomplish this task:
The rnd( ) function is the key to this line of code. It picks a random (or nearly random) number greater than 0 and less than 1 (for example 0.3256). Then we multiply this by 100 with the * operator to get a number between greater than 0 but less then 100 (0.3256 times 100 would be 32.56) ;
The int( ) function removes the fractional part to leave only the integer part of the number (the 0.56 part of 32.56 would be removed to leave only 32).
Then we add 1 to this. This is necessary because we want to pick a number as small as 1 and as large as 100. The rnd( ) function only gives us a number as large as 0.9999999 and not as large as 1. If you multiply 0.9999999 by 100, the biggest number you can get is 99.99999, and this is not big enough so we add one.
When we have picked the number, we assign its value to the variable guessMe.
2) Print program title and give some instructions
' Clear the screen and print the title and instructions
cls
print "HI-LO"
print
print "I have decided on a number between one"
print "and a hundred, and I want you to guess"
print "what it is. I will tell you to guess"
print "higher or lower, and we'll count up"
print "the number of guesses you use."
print
This very simple part of the program wipes the window clean and prints the title HI-LO and some instructions. Notice the use of blank print statements to add space between the title and the instructions and after the instructions also.
3) Ask for the user to guess number
[ask]
' Ask the user to guess the number and tally the guess
input "OK. What is your guess"; guess
Here the branch label [ask] will let us go back here later if the user needs to be asked to guess again. Then we use the input statement to ask the user for a guess. The user's guess is then placed in the guess (what else?) variable.
4) Tally the guess
' Now add one to the count variable to count the guesses
let count = count + 1
Now we take the value of count and add one to it. Each time this code is performed, count's value will increase by one.
5) If the guess is right go to step (9)
' check to see if the guess is right
if guess = guessMe then goto [win]
This line compares the variable guess with the variable guessMe. If they are equal, then we goto [win], which is equivalent to . . . go to step (9) in our outline.
6) If the guess is too low tell the user to guess higher
' check to see if the guess is too low
if guess < guessMe then print "Guess higher."
This line compares the variable guess with the variable guessMe. If guess is less than guessMe, then display the text "Guess higher."
7) If the guess is too high tell the user to guess lower
' check to see if the guess is too high
if guess > guessMe then print "Guess lower."
This line compares the variable guess with the variable guessMe. If guess is greater than guessMe, then display the text "Guess lower."
8) Go back to step 3
' go back and ask again
goto [ask]
9) Beep and tell the user how many guess it took to win
[win]
' beep once and tell how many guesses it took to win
beep
print "You win! It took"; count; "guesses."
' reset the count variable to zero for the next game
let count = 0
This is the code our game executes when the player wins. The beep statement rings the terminal bell once. Then the print statement says that the game is won and how many guesses it took. Finally, the let statement resets the count variable to zero for the next game.
10) Ask the user whether to play again
' ask to play again
input "Play again (Y/N)"; play$
This input statement asks whether or not to play again. The resulting string is stored in the string variable called play$.
11) If the user answers yes then goto step 1
if instr("YESyes", play$) > 0 then goto [start]
This if . . . then statement uses the instr( ) function to determine whether the player answered "Y", "y", "YES", or "yes". The instr( ) function checks to see if the string in play$ is found anywhere in the string literal "YESyes". If it is, then instr( ) returns the position, which is then compared with 0 using the > operator. If the contents of play$ are found in "YESyes", then the value returned from instr( ) will be greater than zero, so that goto [start] will be executed, and the game will be restarted.
12) Give the user instructions on how to close the game window
print "Press ALT-F4 to close this window."
Since the player did not wish to play again, we will display instructions on how to close the game window.
13) End the program
end
It is good practice place the end statement as the end of your BASIC programs. It can also be used at any place where you want the program to stop running.
Copyright (C) 2005 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/