Qbasic Tutorial for beginners and children (Under Construction)


Writing a guess game

Writing a guess game in BASIC is one of the easiest game you could write for a start.

The concepts that you should know to understand this game are:

You should

Algorithm

Clear the screen

Define variables

Assign a random value to variable tobefound

Set variable counter to zero

Write "Try to guess the correct number" on screen
DO
  Ask user to enter his choice

  Store choice in variable choice

  Increment the counter by 1

  Check whether variable tobefound is less than variable choice. If so say "number is smaller"

  Check whether variable tobefound is greater than variable choice. If so say "number is greater"

LOOP UNTIL choice = tobefound
Tell user how much moves were needed to succeed

 

Program Code

'Clearing the screen
CLS
'Randomize is used to generate number
RANDOMIZE TIMER
'------------------------
'Defining the variables
DIM tobefound AS INTEGER
DIM choice AS INTEGER
DIM counter AS INTEGER
'------------------------
'Generating a random number between 1 and 100

tobefound = INT(RND * 100) + 1
'Setting the counter to 0. The variable counter is used to monitor the number of
'tries of the player.

counter = 0
PRINT "Try to guess the correct number"
'Using a loop to get user input until the correct number is found.
DO

  INPUT "Enter your choice: ", choice
  counter = counter + 1
  'If the correct number is smaller than the guess then say "I am lower"
  IF tobefound < choice THEN
    PRINT "I am lower"
  END IF
  'If the correct number is greater than the guess then say "I am higher"
  IF tobefound > choice THEN
    PRINT "I am higher"
  END IF
LOOP UNTIL choice = tobefound
PRINT "You succeeded in"; counter; "moves."

 


pages viewed since January  19 2002