Qbasic Tutorial for beginners and children (Under Construction)


Writing a Hangman game

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

You should:

Algorithm

Clear the screen

Define variables

Create a wordlist

Use a loop to

choose a random word from the list

Set the number of tries to 0

Reset the array that monitors the variables to 0

Print the program title , messages

Print the number of chances left

Set the counter that monitors the number of occurrences of the letter pressed in word to 0

Use a loop

Set variable foundletter to 0

Wait for user to press a letter

Change the letter pressed to uppercase

Check the key pressed

If a letter has not been pressed yet

Check whether the letter is found in the word

if so

increment counter by 1

Display letter on screen

Set variable foundletter to TRUE

end if

If not found

increment variable numberoftries by 1

Display number of chances left

Set the array that monitors the variables to -1

end if

loop until escape key is pressed or word found or no more chances left

 

Say whether user has won or lost

Ask if user want to play again

loop until user presses letter n

 

 

DIM SHARED wordlist(30) AS STRING
DIM wordtoguess AS STRING
DIM again AS STRING
DIM letter(26)
DIM numberoftries AS INTEGER
DIM foundletter AS INTEGER
CONST false = 0
CONST TRUE = -1

CALL createwordlist

DO
  RANDOMIZE TIMER
  wordtoguess = wordlist(INT(RND * 30) + 1)
  numberoftries = 0
  FOR x = 1 TO 26
     letter(x) = 0 
  NEXT
  CLS
  LOCATE 1, 30
  PRINT "Hangman"
  LOCATE 3, 30
  PRINT "Find the missing word"
  LOCATE 5, 30
  FOR x = 1 TO LEN(wordtoguess)
    PRINT "-";
  NEXT
  LOCATE 15, 30
  PRINT "You have 10 chances"
  counter = 0
  DO
     foundletter = false
     DO
         a$ = INKEY$
     LOOP UNTIL a$ <> ""
     a$ = UCASE$(a$)

     SELECT CASE a$
         CASE "A" TO "Z"
              IF letter(ASC(a$) - 64) = 0 THEN
                  FOR x = 1 TO LEN(wordtoguess)
                      LOCATE 5, 9 + x
                      IF UCASE$(MID$(wordtoguess, x, 1)) = a$ THEN
                         PRINT MID$(wordtoguess, x, 1)
                         counter = counter + 1
                         foundletter = TRUE
                      END IF
                  NEXT
                  IF foundletter = false THEN
                      numberoftries = numberoftries + 1
                      LOCATE 15, 30
                      PRINT "You have only"; (10 - numberoftries); "chances "
                  END IF
                  letter(ASC(a$) - 64) = -1
              END IF
        END SELECT

    LOOP UNTIL a$ = CHR$(27) OR counter = LEN(wordtoguess) OR numberoftries = 10
    IF counter = LEN(wordtoguess) THEN
       LOCATE 8, 30
       PRINT "You won"
    ELSE
       LOCATE 8, 30
       PRINT "You lost"
    END IF
    LOCATE 9, 30
    PRINT "Do you want to play again (y/n)";
    DO
       again = INKEY$
    LOOP UNTIL UCASE$(again) = "Y" OR UCASE$(again) = "N"
 LOOP UNTIL a$ = CHR$(27) OR UCASE$(again) = "N"

SUB createwordlist

wordlist(1) = "computer"
wordlist(2) = "keyboard"
wordlist(3) = "mouse"
wordlist(4) = "joystick"
wordlist(5) = "Video"
wordlist(6) = "Television"
wordlist(7) = "Radio"
wordlist(8) = "Monitor"
wordlist(9) = "Screen"
wordlist(10) = "Baby"
wordlist(11) = "Boy"
wordlist(12) = "Girl"
wordlist(13) = "Father"
wordlist(14) = "Mother"
wordlist(15) = "Uncle"
wordlist(16) = "Aunt"
wordlist(17) = "Dog"
wordlist(18) = "Cat"
wordlist(19) = "Horse"
wordlist(20) = "Stag"
wordlist(21) = "Wolf"
wordlist(22) = "Monkey"
wordlist(23) = "Piano"
wordlist(24) = "Guitar"
wordlist(25) = "Star"
wordlist(26) = "Harmonica"
wordlist(27) = "Wonderful"
wordlist(28) = "Perfect"
wordlist(29) = "Satisfactory"
wordlist(30) = "Great"

END SUB

 

pages viewed since January  19 2002