Guessing game - QB64
I know the second language was supposed to be Java, but it's so much more complex than BASIC. I understand it's much more powerful, but it's a pain to use without a lot of experience.
Anyway, for this project I had to scrap step 5. I need to do a bit more research before I figure out that kind of loop. Outside of that, this has been a really fun language to revisit.
I used QB64 to write this. I love how passionate vintage computing fans are and they make some of the best tools. It's 2025 and I'm writing a BASIC program and running it in Windows 11!
On to the program:
The first thing I did was set up a variable to count the number of guesses the player took. They I had them input their name to a string ($) variable in one line. The random number part took a bit of reading to figure out. When BASIC generates a random number it needs a seed.
By using RANDOMIZE TIMER it generates a seed based on the computer clock so each time the player runs the program it generates a new number. Secondly, RANDOMIZE only generates a number between 0 and 1, so if I wanted a number between 1 and 20 I need to multiply it by 20. Finally, I want a whole number so I need to use the Int function to turn it into an integer and then add 1 to it so it generates from 1-20 instead of 0-19.
So, for example, if I wanted to simulate a dice roll I need to use dice = INT(RND * 6) + 1.
Then the DO loop is really straightforward. It runs a loop until a certain condition is met, testing it after each pass. Once the guess matches the random number it dumps and says the player won and told them how many guesses it took.
I enjoyed using BASIC. It was a fun time exploring a language I haven't used in over 20 years. If anyone wants to dig into the history of the language a bit, I recommend Endless Loop by Mark Jones Lorenzo. It's a fairly quick read that discussed this history of the language and its importance. I also loved revisiting the famous BASIC Computer Games books from the 1970s, which have been updated for modern times
The code:
num = 0
''' Player name input '''
Input "Enter your name:"; playerName$
Print
Print "Hello "; playerName$; ". I'm thinking of a number between 1 and 20"
Print
''' Generate Random number from 1 to 20 '''
Randomize Timer
answer = Rnd
answer = Int(20 * answer) + 1
''' Loop until the correct answer is guessed '''
Do
num = num + 1
Print "Enter guess"; num;
Input ": ", guess
If (guess < answer) Then Print "Your guess is too low"
If (guess > answer) Then Print "Your guess is too high"
Loop Until guess = answer
Print "Correct! That took"; num; "guesses"