Results 1 to 13 of 13

Thread: Help with my homework

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    15

    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:
    1. Private Sub GameForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         'verify that the user wants to exit the application
    3.  
    4.         Dim button As Integer
    5.         button = MessageBox.Show("Do you want to exit?", "Guessing Game", MessageBoxButtons.YesNo, _
    6.             MessageBoxIcon.Exclamation)
    7.  
    8.         'if the user selects the No button, don't close the form
    9.         If button = DialogResult.No Then
    10.             e.Cancel = True
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub uiExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiExitButton.Click
    15.         Me.Close()
    16.  
    17.     End Sub
    18.  
    19.     Private Sub uiCheckButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCheckButton.Click
    20.         Dim guessNum As Integer
    21.         Dim randomNum As Integer
    22.  
    23.         'display variables
    24.         guessNum = Integer.Parse(Me.uiGuessTextBox.Text)
    25.         randomNum = Integer.Parse(Me.uiGeneratedNumLabel.Text)
    26.         attemptNum = attemptNum + 1
    27.  
    28.  
    29.  
    30.         'display attemptNum label control
    31.         If attemptNum < 10 And randomNum <> guessNum Then
    32.             If attemptNum = 9 Then
    33.                 Me.uiAttemptTextLabel.Text = "You have 1 attempt left"
    34.             Else
    35.                 Me.uiAttemptTextLabel.Text = "You have " & (10 - (attemptNum)) & " attempts left"
    36.             End If
    37.  
    38.         ElseIf attemptNum = 10 AndAlso randomNum <> guessNum Then
    39.             Me.uiAttemptTextLabel.Text = "You lose!"
    40.         ElseIf attemptNum <= 10 AndAlso randomNum = attemptNum Then
    41.             Me.uiAttemptTextLabel.Text = "You Win!"
    42.         End If
    43.  
    44.  
    45.  
    46.  
    47.  
    48.         'determine if guess is greater than, less, than, or equal to the generated random number
    49.         If attemptNum < 10 AndAlso guessNum <> randomNum Then
    50.             Dim button As Integer
    51.             Do
    52.                 If guessNum < randomNum Then
    53.                     button = MessageBox.Show("Please choose a higher number", "Too Low", _
    54.                         MessageBoxButtons.OK, MessageBoxIcon.Hand)
    55.                 Else
    56.                     button = MessageBox.Show("Please choose a lower number", "Too High", _
    57.                         MessageBoxButtons.OK, MessageBoxIcon.Hand)
    58.                 End If
    59.                 Me.uiGuessTextBox.Focus()
    60.                 Me.uiGuessTextBox.SelectAll()
    61.             Loop Until button = DialogResult.OK
    62.  
    63.  
    64.         ElseIf guessNum = randomNum Then
    65.             Dim button As Integer
    66.             MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    67.                 "Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    68.             If Button = DialogResult.No Then
    69.                 Me.Close()
    70.             Else
    71.                 This is where I need help!
    72.             End If
    73.  
    74.         ElseIf attemptNum > 10 AndAlso guessNum <> randomNum Then
    75.             Dim button As Integer
    76.             MessageBox.Show("Sorry but the correct answer is " & Convert.ToString(randomNum) & "! Please try again", _
    77.                     "Try Again", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    78.             If Button = DialogResult.No Then
    79.                 Me.Close()
    80.             Else
    81.                 This is where I need help!
    82.  
    83.             End If
    84.         End If
    85.  
    86.     End Sub
    87.  
    88.     Private Sub GenerateRandomNumbers()
    89.         'generates a random number
    90.  
    91.         Dim randomNum As Integer
    92.         Dim randomGenerator As New Random
    93.  
    94.         'Generate the random number
    95.         randomNum = randomGenerator.Next(0, 101)
    96.  
    97.         'display random numbers----------------------------Make sure to remove this code, along with the box uiGeneratedNumLabel
    98.         Me.uiGeneratedNumLabel.Text = Convert.ToString(randomNum)
    99.  
    100.         Me.uiAttemptTextLabel.Text = "You have 10 attempts left"
    101.  
    102.  
    103.     End Sub
    104.  
    105.  
    106.     Private Sub GameForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    107.  
    108.         'generate the random number and populate the number of attempts on load
    109.         Call GenerateRandomNumbers()
    110.  
    111.     End Sub

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Help with my homework

    VB Code:
    1. Application.Restart()

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Help with my homework

    Quote Originally Posted by Fromethius
    VB Code:
    1. Application.Restart()
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    15

    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!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    15

    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:
    1. ElseIf guessNum = randomNum Then
    2.             Dim button As Integer
    3.             MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    4.                 "Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    5.             If button = DialogResult.No Then
    6.                 Me.Close()
    7.             Else
    8.                 Call GenerateRandomNumbers()
    9.                 attemptNum = 0
    10.                 Me.uiGuessTextBox.Text = ""
    11.             End If
    12.  
    13.         ElseIf attemptNum = 10 AndAlso guessNum <> randomNum Then
    14.             Dim button As Integer
    15.             MessageBox.Show("Sorry but the correct answer is " & Convert.ToString(randomNum) & "! Please try again", _
    16.                     "Try Again", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    17.             If Button = DialogResult.No Then
    18.                 Me.Close()
    19.             Else
    20.                 Call GenerateRandomNumbers()
    21.                 attemptNum = 0
    22.                 Me.uiGuessTextBox.Text = ""

  8. #8
    Frenzied Member
    Join Date
    Sep 2006
    Location
    UK / East Sussex
    Posts
    1,054

    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.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    15

    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:
    1. Dim button As DialogResult
    2.             MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    3.                 "Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    4.             If button = DialogResult.No Then
    5.                 Me.Close()
    6.             Else
    7.                 Call GenerateRandomNumbers()
    8.                 attemptNum = 0
    9.                 Me.uiGuessTextBox.Text = ""
    10.             End If
    11.  
    12.         ElseIf attemptNum = 10 AndAlso guessNum <> randomNum Then
    13.             Dim button As DialogResult
    14.             MessageBox.Show("Sorry but the correct answer is " & Convert.ToString(randomNum) & "! Please try again", _
    15.                     "Try Again", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    16.             If Button = DialogResult.No Then
    17.                 Me.Close()
    18.             Else
    19.                 Call GenerateRandomNumbers()
    20.                 attemptNum = 0
    21.                 Me.uiGuessTextBox.Text = ""

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Help with my homework

    You have this:

    VB Code:
    1. Dim button As DialogResult
    2.             MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    3.                 "Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)

    It should be this:

    VB Code:
    1. Dim button As DialogResult =
    2.             MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    3.                 "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

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    15

    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.

  13. #13
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Help with my homework

    You can change this
    VB Code:
    1. Dim button As DialogResult
    2.             MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    3.                 "Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
    4.             If button = DialogResult.No Then
    5.     Me.Close()
    6.             Else
    7.                 Call GenerateRandomNumbers()
    8.                 attemptNum = 0
    9.                 Me.uiGuessTextBox.Text = ""
    10.             End If

    To this
    VB Code:
    1. If MessageBox.Show("Congratulations!  You guessed the right answer!  Would you like to play again?", _
    2.                 "Correct!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) = DialogResult.No Then
    3.     Me.Dispose()
    4. Else
    5.     Call GenerateRandomNumbers()
    6.     attemptNum = 0
    7.     Me.uiGuessTextBox.Text = ""
    8. End If
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width