|
-
Oct 4th, 2006, 05:58 PM
#1
Thread Starter
New Member
Help with my homework
Ok, I was on here a while back, and you all helped me out a lot! I'm hoping you can help me one more time...
My assignment is to create a guessing game that gives the user 10 attempts to guess a random generated number. After the 10th wrong attempt, or if user guesses correctly, I'm giving a message asking the user if they want to play again. If they choose no, the game exits, and if they click yes, the game should reload. The only problem is, I have no idea how to make the game reload. I'll warn in advance that I'm really new at this, so if anybody is able to help me, you would probably have to break it way down into laymen's terms, because I have no clue what I'm doing. Anyhow, my code is below:
VB Code:
Private Sub GameForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'verify that the user wants to exit the application
Dim button As Integer
button = MessageBox.Show("Do you want to exit?", "Guessing Game", MessageBoxButtons.YesNo, _
MessageBoxIcon.Exclamation)
'if the user selects the No button, don't close the form
If button = DialogResult.No Then
e.Cancel = True
End If
End Sub
Private Sub uiExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiExitButton.Click
Me.Close()
End Sub
Private Sub uiCheckButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCheckButton.Click
Dim guessNum As Integer
Dim randomNum As Integer
'display variables
guessNum = Integer.Parse(Me.uiGuessTextBox.Text)
randomNum = Integer.Parse(Me.uiGeneratedNumLabel.Text)
attemptNum = attemptNum + 1
'display attemptNum label control
If attemptNum < 10 And randomNum <> guessNum Then
If attemptNum = 9 Then
Me.uiAttemptTextLabel.Text = "You have 1 attempt left"
Else
Me.uiAttemptTextLabel.Text = "You have " & (10 - (attemptNum)) & " attempts left"
End If
ElseIf attemptNum = 10 AndAlso randomNum <> guessNum Then
Me.uiAttemptTextLabel.Text = "You lose!"
ElseIf attemptNum <= 10 AndAlso randomNum = attemptNum Then
Me.uiAttemptTextLabel.Text = "You Win!"
End If
'determine if guess is greater than, less, than, or equal to the generated random number
If attemptNum < 10 AndAlso guessNum <> randomNum Then
Dim button As Integer
Do
If guessNum < randomNum Then
button = MessageBox.Show("Please choose a higher number", "Too Low", _
MessageBoxButtons.OK, MessageBoxIcon.Hand)
Else
button = MessageBox.Show("Please choose a lower number", "Too High", _
MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If
Me.uiGuessTextBox.Focus()
Me.uiGuessTextBox.SelectAll()
Loop Until button = DialogResult.OK
ElseIf guessNum = randomNum Then
Dim button As Integer
MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If Button = DialogResult.No Then
Me.Close()
Else
This is where I need help!
End If
ElseIf attemptNum > 10 AndAlso guessNum <> randomNum Then
Dim button As Integer
MessageBox.Show("Sorry but the correct answer is " & Convert.ToString(randomNum) & "! Please try again", _
"Try Again", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If Button = DialogResult.No Then
Me.Close()
Else
This is where I need help!
End If
End If
End Sub
Private Sub GenerateRandomNumbers()
'generates a random number
Dim randomNum As Integer
Dim randomGenerator As New Random
'Generate the random number
randomNum = randomGenerator.Next(0, 101)
'display random numbers----------------------------Make sure to remove this code, along with the box uiGeneratedNumLabel
Me.uiGeneratedNumLabel.Text = Convert.ToString(randomNum)
Me.uiAttemptTextLabel.Text = "You have 10 attempts left"
End Sub
Private Sub GameForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'generate the random number and populate the number of attempts on load
Call GenerateRandomNumbers()
End Sub
-
Oct 4th, 2006, 06:37 PM
#2
Frenzied Member
Re: Help with my homework
-
Oct 4th, 2006, 06:42 PM
#3
Re: Help with my homework
Man, you don't need MUCH help, that's REALLY close.
In fact, pretty much all you need to do is set attemptNum back to 0, and call GenerateRandomNumbers(). It looks like everything else will work.
Having said that, let me suggest a change to GenerateRandomNumbers(). In that function, you are getting the random number the person is guessing at, then putting it in a label. Then in the click event, you are getting it back out of the label. While this works, it is not a good way to do things, because pretty much ANYTHING you do with a string is slow. You won't see that slowdown in 99.9% of programs because computers are fast, but you should keep in mind that numbers are fast and strings are slow, so as a general rule, you don't want to be doing that kind of conversion very often.
In this case, the better solution would be to promote the randomNum variable from being a function level variable to being a class level variable. Basically, change Dim to Private, and move it out of the function (but leave it in the class). Then the function will be filling the variable, but the click event can see the variable, too. This means that you can get rid of the line where you parse the label to get the target number.
To get back to the main question, once you have made this change to the GenerateRandomNumbers() function, the effect of that function is to pick a new target number. The only other thing that needs to happen is to tell the program that you are back to the first attempt, which looks like it is as simple as setting attemptNum back to 0.....or did it start at 1...no, should be 0....well, you probably know that, but it looks like 0 is where it should be.
My usual boring signature: Nothing
 
-
Oct 4th, 2006, 06:43 PM
#4
Re: Help with my homework
 Originally Posted by Fromethius
I can't agree with that. It might work, but the overhead for that one simple line has got to be vastly greater than what is really needed.
My usual boring signature: Nothing
 
-
Oct 4th, 2006, 06:55 PM
#5
Thread Starter
New Member
Re: Help with my homework
You guys rock! It works now. By the way, I went with calling GenerateRandomNumbers and changing the attemptNum to 0. It works!
-
Oct 4th, 2006, 07:40 PM
#6
Re: Help with my homework
Many people ask for help with homework here, but most don't show any effort. You had it kicked. Good job.
My usual boring signature: Nothing
 
-
Oct 7th, 2006, 10:39 AM
#7
Thread Starter
New Member
Re: Help with my homework
Alright, I need more help... I did get the form to reset on the "Yes" option, but it doesn't close on the "No" option. Here's how I formatted it. Any help?
VB Code:
ElseIf guessNum = randomNum Then
Dim button As Integer
MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If button = DialogResult.No Then
Me.Close()
Else
Call GenerateRandomNumbers()
attemptNum = 0
Me.uiGuessTextBox.Text = ""
End If
ElseIf attemptNum = 10 AndAlso guessNum <> randomNum Then
Dim button As Integer
MessageBox.Show("Sorry but the correct answer is " & Convert.ToString(randomNum) & "! Please try again", _
"Try Again", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If Button = DialogResult.No Then
Me.Close()
Else
Call GenerateRandomNumbers()
attemptNum = 0
Me.uiGuessTextBox.Text = ""
-
Oct 7th, 2006, 11:20 AM
#8
Frenzied Member
Re: Help with my homework
Hi, I am not great in vb but for something like that I would use a dialogue like this:
if msgbox ("Your message", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
'commands for playing again
else
end
end if
If you are closing the program if they dont want to play again then you want end instead of me.hide as that just hides the form but the program still works. or if you do want to hide the form there is a way of ending the form
me.dispose()
which I has to recently use in by Stock Control program as I couldn't use formname.show() for some off reason and had to use it open through a vaible but it could open mutli forms of the same form so me.hide didnt work.
-
Oct 7th, 2006, 01:48 PM
#9
Re: Help with my homework
Me.Hide certainly won't work, but I don't actually see that in the code.
I also don't see an "=" sign.
You are checking if button is DialogResult.No. Frankly, I assumed that DialogResult was a structure, and button is an integer, so this shouldn't work.....unless you don't have Option Strict set to On. You should do this, because it will catch a number of subtle errors, and may produce faster code because you will have to explicitly convert types, rather than implicitly converting them.
Anyhow, you don't set Button to be the return from the messagebox, there is no '=' sign. Therefore, the button variable is always 0, and if that is not DialogReslt.No, then the else will always fire. In any event, the result of the messagebox is ignored.
Button should be of type DialogResult, rather than integer.
My usual boring signature: Nothing
 
-
Oct 8th, 2006, 10:30 AM
#10
Thread Starter
New Member
Re: Help with my homework
I'm confused... I realize this is probably about as simple as it gets, but for some reason I'm just not getting this... I changed the button from an integer to a DialogResult (which I thought is what you were saying above), and it still restarts the game, regardless of the dialog result... Any help?
VB Code:
Dim button As DialogResult
MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If button = DialogResult.No Then
Me.Close()
Else
Call GenerateRandomNumbers()
attemptNum = 0
Me.uiGuessTextBox.Text = ""
End If
ElseIf attemptNum = 10 AndAlso guessNum <> randomNum Then
Dim button As DialogResult
MessageBox.Show("Sorry but the correct answer is " & Convert.ToString(randomNum) & "! Please try again", _
"Try Again", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If Button = DialogResult.No Then
Me.Close()
Else
Call GenerateRandomNumbers()
attemptNum = 0
Me.uiGuessTextBox.Text = ""
-
Oct 8th, 2006, 01:55 PM
#11
Re: Help with my homework
You have this:
VB Code:
Dim button As DialogResult
MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
It should be this:
VB Code:
Dim button As DialogResult =
MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
See the difference? You create the variable called button, but you never actually assigned anything to it, so it was always 0. The return from the messagebox was simply being ignored, because it was never assigned to the variable.
My usual boring signature: Nothing
 
-
Oct 8th, 2006, 06:49 PM
#12
Thread Starter
New Member
Re: Help with my homework
When I do this, it says there is an expression expected after the = sign. I then tried formattign as
Dim Button as DialogResult = DialogResult.No
But it still is not working. Sorry if I'm being a pain.
-
Oct 8th, 2006, 08:04 PM
#13
Re: Help with my homework
You can change this
VB Code:
Dim button As DialogResult
MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
If button = DialogResult.No Then
Me.Close()
Else
Call GenerateRandomNumbers()
attemptNum = 0
Me.uiGuessTextBox.Text = ""
End If
To this
VB Code:
If MessageBox.Show("Congratulations! You guessed the right answer! Would you like to play again?", _
"Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) = DialogResult.No Then
Me.Dispose()
Else
Call GenerateRandomNumbers()
attemptNum = 0
Me.uiGuessTextBox.Text = ""
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|