This is a question I couldent get very far on using Micrsoft Visual Basic reardi
ID: 3765892 • Letter: T
Question
This is a question I couldent get very far on using Micrsoft Visual Basic rearding Arrays. Plasea answer in English, Thank you.
1. Create a VB Form with a single button to start the game. When the user clicks the button, do the following:
2. Create an array of 10 random integers, each between 0 and 30, inclusive. Hints: use a loop, and it is OK if some of the elements of the array may hold the same number.
3. Enter the 'Game loop':
(The user has 3 attempts to guess a winning number.)
Hint: the condition for your loop needs to include both a counter for the number of attempts and a boolean for whether the user has made a correct guess or not
a. Ask the user for their guess using an InputBox
b. Call a function to search the array you created in step 2 for the number the user gave.
c. If your search function returns true, tell the user they won the game and stop looping, otherwise, continue looping until the user either runs out of guesses or wins the game
3. Your search function must take two parameters, the array holding the random numbers and the user's guess (an integer) and must do the following:
a. Look through each element of the array
b. if the array element matches the users guess return true
c. if you go clear through the loop and do not get a match, return false
Explanation / Answer
open visual basic application
and select the vb form and name it as Guess in the properties window
Drag a button on the Guess form and settext as "Start Game" , name as “ startButton” in text properties window,
double click on start button and enter the following code
Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
'declaring necessary variables
Dim randomNums(10) As Integer
Dim userGuess As Integer
Dim numAttempts As Integer
Dim madeCorrectGuess As Boolean
Dim myValue As String
Dim RandomClass As New Random() 'for generating random number
startButton.Visible = False
'generating 10 random numbers between 0 to 30
For i = 0 To 9
randomNums(i) = RandomClass.Next(0, 30)
Next
'initialize the varaibles
numAttempts = 0
madeCorrectGuess = False
'eneter into Game loop
While numAttempts < 3 And madeCorrectGuess = False
myValue = InputBox("Guess a number(0-30): number of attempts available:" + Convert.ToString(3 - numAttempts), "Guess ", "-1")
userGuess = Convert.ToInt16(myValue)
'each time call search function
If search(userGuess, randomNums) Then
madeCorrectGuess = True
End If
numAttempts = numAttempts + 1
End While
'display the result
If madeCorrectGuess = True Then
MessageBox.Show("Congrats! You win", "Guess Game Result")
End If
If numAttempts >= 3 Then
MessageBox.Show("Sorry! try again", "Guess Game Result")
End If
End Sub
9. now create a user defined function in the class Guess
Public Function search(ByVal guess As Integer, ByVal randoms() As Integer)
For i = 0 To 9
If randoms(i) = guess Then
Return True
End If
Next
Return False
End Function
Program:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.