Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A simple computer game is to try to guess a number picked at random by a compute

ID: 3886387 • Letter: A

Question

A simple computer game is to try to guess a number picked at random by a computer. Assume the numbers must be integers between 1 and 1000, inclusive. Write a VBA program to play this game using IF statements. After each guess, the computer will indicate whether the number is correct or whether the guess is high or low. It will keep track of the number of guesses. The computer picks its number using the random number generator,which generates a number between 0 and 1. This number is then multiplied by 1000 and its integer value selected to produce the target number. Thus, g = Abs(Int(1000 * Rnd(seed))) where RND is the VBA random number generator. The computer can also play the game based on a number input by the player. Its method of attack is to find the midpoint of the eligible range. Thus, at the start, its first guess will be 500. If this is low, its next guess will be 750; if high;250, and so on.

Explanation / Answer

'The objective of the program is to guess the random number selected by the computer
'User wins when he selects a number equal to the number selected by the computer
'For every incorrect input, display if the user needs to select a bigger number or a smaller number
'Every user gets 5 chances to guess the correct number
'Allow user to play again or exit the game


'Start the program

Sub Computer_game()

'Code to generate Random number
'Declare the variable

Dim Random_number As Double
Codererun:
'Generate Random number between 0 and 1
Random_number = Rnd()

'Use the previously generated random number to generate target number
g = Abs(Int(1000 * Random_number))

'Declare i as loop counter and chances as a flag variable, to keep a count on number of chances left
Dim chances As Integer
Dim i As Integer

'Initialize i as 1 and chances as 6
i = 1
chances = 6

'Start the while loop and run it keep the max number of runs as 5

While (i < 6)

'Take user input in user_input variable and declare it as integer

Dim user_input As Integer
user_input = InputBox("Enter your guess!!! Only " & chances & " chances left")

'If the user's guess is correct then show success message and exit loop using Goto operator

If user_input = g Then
MsgBox ("Congrats!!!You have guessed the correct number")
GoTo play_again
  
'If the user's guess is higher then ask user to enter a smaller number

ElseIf user_input > g Then
MsgBox ("Try a lower number")
  
'If the user's guess is lower then ask user to enter a higher number
ElseIf user_input < g Then
MsgBox ("Try a bigger number")

'If user is entering an invalid number show this message

Else: MsgBox ("Enter a valid number")

'End the if loop

End If

'Increment the loop counter and decrement the chance varaiable to show how many chances left

i = i + 1
chances = chances - 1

'End the loop
Wend

'The other end of goto operator
play_again:

'Ask user if they want to continue

Dim answer As Integer

answer = MsgBox("Do you want to play again?", vbYesNo + vbQuestion, "Continue Game??")

'If answer comes out positive then jump back to code

If answer = vbYes Then
GoTo Codererun

'If answer comes out negative do nothing and let the program end
Else
'do nothing
End If

'End the program
End Sub

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote