Your first game

I supposed that you read basic information about flow control commands and variables, it's need to understand this tutorial. The first game you will write is called Guess a number, the computer will ask to the player to guess a number between 0 and 100, and the computer will inform you if your number is higher or lower that the number to guess.

For this game we will use windows console, that have easy commands to make our game, it can write text on a console window or read a text of the user.

For this game we need a variable to allocate the number to guess, it will be called GuessNumber, and other to store the user number, it will be called UserNumber, both of byte type because we need numbers between 0 and 100.


 Declare GuessNumber.b, UserNumber.b

And then open a console window with:


 OpenConsole()

We get a random number between 0 and 100:


 GuessNumber = Random(100)

Now the actions to make the game are, show ask number message, read user number, compare it with computer number, and show a message to inform the user if the number is higher or lower, and repeat again.

Show ask number mesasage:


 PrintN("Guess a number (between 0 and 100):") 

Read user number, Input wait to a user typed text, and Val convert the text to a number:


 UserNumber = Val(Input()) 

Then compare with computer number, checking if it's the correct number, otherwise it will show a message to inform if number is higher or lower than computer number:


 If GuessNumber = UserNumber 
   PrintN("Congratulations, you win!!!") 
 Else
   If GuessNumber < UserNumber 
     PrintN("Your number is higher than mine, try again.") 
   Else
     PrintN("Your number is lower than mine, try again.") 
   EndIf
 EndIf 

But if you try this code it will ask just one time, you must use a Repeat - Until sequence that execute a block of code until Guest number is equal than User number:


 Repeat 

   PrintN("Guess a number (between 0 and 100):") 

   UserNumber = Val(Input()) 

   If GuessNumber = UserNumber 
     PrintN("Congratulations, you win!!!") 
   Else
     If GuessNumber < UserNumber 
       PrintN("Your number is higher than mine, try again.") 
     Else
       PrintN("Your number is lower than mine, try again.") 
     EndIf
   EndIf 

 Until GuessNumber = UserNumber

And wait to user press enter, then we close console window and end our program:


 PrintN("Press Enter to exit.") 
 Input() 

 CloseConsole() 

 EndProgram 

Exercise:
Add commands necesary to count number of times that user enter a number and show it when user guess the right number.