Building a Number Guessing Game in QBasic
Creating a simple number guessing game in QBasic is not only fun, but it also serves as a fantastic way to get hands-on experience with user input and control flow in programming. In this project, we will build a game where the computer selects a random number, and the player has to guess it. The game will give feedback on whether the guess is too high, too low, or correct. Let’s dive in!
Step 1: Setting Up Your Environment
Before we start coding, ensure you have QBasic installed on your system. If you don’t have it installed yet, you can download it from various online sources that provide versions compatible with modern operating systems or use DOSBox for older versions. Once you have QBasic ready, open a new file to start writing your game.
Step 2: Generate a Random Number
The first part of our game will involve the computer selecting a random number. In QBasic, we can use the RANDOMIZE statement followed by INT function to generate a random number within a specific range. Below is the code to initialize our random number.
RANDOMIZE TIMER
targetNumber = INT(RND * 100) + 1
In this code snippet, RANDOMIZE TIMER initializes the random number generator based on the current time. Then, RND generates a random number between 0 and 1, which we multiply by 100 and round down using INT to get an integer between 1 and 100.
Step 3: Gathering User Input
Next, we need to gather input from the user. We will prompt the player to enter their guess and store it in a variable. Here’s how you can do this:
DIM userGuess AS INTEGER
PRINT "Welcome to the Number Guessing Game!"
PRINT "I have chosen a number between 1 and 100."
PRINT "Can you guess what it is?"
INPUT "Enter your guess: ", userGuess
This block of code uses the PRINT statement to give the user instructions and the INPUT statement to capture their guess.
Step 4: Implementing Control Flow
Now that we have the random number and user input, it’s crucial to implement control flow to determine if the user's guess is too high, too low, or correct. We will use a DO...LOOP structure to keep the game running until the user guesses correctly.
DO
INPUT "Enter your guess: ", userGuess
IF userGuess < targetNumber THEN
PRINT "Too low! Try again."
ELSEIF userGuess > targetNumber THEN
PRINT "Too high! Try again."
ELSE
PRINT "Congratulations! You've guessed the correct number: "; targetNumber
END IF
LOOP UNTIL userGuess = targetNumber
Explanation:
- We use a
DO...LOOPwhich continues looping until the user guesses the correct number. - The
IF...ELSEIF...ELSEstructure checks the value ofuserGuessagainsttargetNumber:- If the guess is lower than the target, it prompts “Too low! Try again.”
- If it’s higher, it responds with “Too high! Try again.”
- When they guess correctly, it congratulates them and exits the loop.
Step 5: Enhancements
While the basic game works, we can add more features to enhance the user experience. Here are a few ideas:
Keeping Track of Attempts
We can keep track of how many attempts the player takes to guess the number. Let’s introduce a variable to count the number of guesses:
DIM attempts AS INTEGER
attempts = 0
We will then increment this variable every time the player makes a guess:
attempts = attempts + 1
Full Code
Combining all the parts and adding the attempts logic, our complete code would look like this:
RANDOMIZE TIMER
targetNumber = INT(RND * 100) + 1
DIM userGuess AS INTEGER
DIM attempts AS INTEGER
attempts = 0
PRINT "Welcome to the Number Guessing Game!"
PRINT "I have chosen a number between 1 and 100."
PRINT "Can you guess what it is?"
DO
INPUT "Enter your guess: ", userGuess
attempts = attempts + 1
IF userGuess < targetNumber THEN
PRINT "Too low! Try again."
ELSEIF userGuess > targetNumber THEN
PRINT "Too high! Try again."
ELSE
PRINT "Congratulations! You've guessed the correct number: "; targetNumber
PRINT "It took you "; attempts; " attempts."
END IF
LOOP UNTIL userGuess = targetNumber
Step 6: Adding Replay Functionality
To make the game even more engaging, we can give players the option to play again after they successfully guess the number. We can wrap our game logic in a WHILE loop that checks if the player wants to continue.
DIM playAgain AS STRING
playAgain = "Y"
WHILE UCASE$(playAgain) = "Y"
RANDOMIZE TIMER
targetNumber = INT(RND * 100) + 1
attempts = 0
PRINT "Welcome to the Number Guessing Game!"
PRINT "I have chosen a number between 1 and 100."
PRINT "Can you guess what it is?"
DO
INPUT "Enter your guess: ", userGuess
attempts = attempts + 1
IF userGuess < targetNumber THEN
PRINT "Too low! Try again."
ELSEIF userGuess > targetNumber THEN
PRINT "Too high! Try again."
ELSE
PRINT "Congratulations! You've guessed the correct number: "; targetNumber
PRINT "It took you "; attempts; " attempts."
END IF
LOOP UNTIL userGuess = targetNumber
INPUT "Do you want to play again? (Y/N): ", playAgain
WEND
Conclusion
Congratulations! You’ve created a simple number guessing game in QBasic. Through this project, we’ve explored user input, control flow, and loops. These are fundamental concepts that will serve you well in your programming journey. Feel free to expand on this project by introducing features like a scoring system, time limits, or higher difficulty levels!
Enjoy coding, and happy guessing!