I'm making a random number game. The user only gets three chances to guess. If the user guesses wrong all three times, a message box should appear asking if he/she wants to restart, if yes then reset the game and it's controls. I'm not sure if i'm suppose to use some sort of counter to do this.

Public Class Game
    Dim ranNum As Integer 'Declaring variable ranNum as an integer. This is a world level variable.

    'This code will clear text box.
    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtbxEnterNum.Clear()
    End Sub
    'This code will show your guess number and the random number the computer generated in
    'a messagebox.show giving you the option to restart or quit.
    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim intCounter As Integer = 3
        Dim intEnterNum As Integer = 0 ' Declaring intEnterNum as an integer
        intEnterNum = CInt(txtbxEnterNum.Text) 'Using the convertion type Cint 

        If (intEnterNum <> ranNum) Then

            intEnterNum = (MessageBox.Show("Your guess number is:  " & CStr(txtbxEnterNum.Text) & " and the correct guess is:  " & CStr(ranNum)))
        Else

            If MessageBox.Show("Correct! Do you wish to restart?", "Please Confirm Action", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                txtbxEnterNum.Clear()
            ElseIf Windows.Forms.DialogResult.No Then
                Me.Close()

            End If
            End If
    End Sub
    'This code will close the the program.
    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub
    'This code will generate a random number between 1-10
    Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
        ranNum = CInt(Rnd() * 10)
    End Sub
End C

Recommended Answers

All 4 Replies

You have to declare a counter variable at world level as you call it and augment it every time you click the play button until 3 times an reset it if needed.

This is what I have so far. I'm thinking I should use a for...next loop to give the user three chances to guess, after the three chances a messagebox should appear asking the user if he wants to retry oc quit. I'm having trouble using the for...next loop.

Public Class Game
    Dim ranNum As Integer 'Declaring variable ranNum as an integer. This is a world level variable.
    Dim intCounter As Integer

    'This code will clear text box.
    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtbxEnterNum.Clear()
    End Sub
    'This code will show your guess number and the random number the computer generated in
    'a messagebox.show giving you the option to restart or quit.
    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim intCounter As Integer
        Dim intEnterNum As Integer ' Declaring intEnterNum as an integer
        intEnterNum = CInt(txtbxEnterNum.Text) 'Using the convertion type Cint 

        For intCounter = 0 To 3




            If (intEnterNum <> ranNum) Then

                intEnterNum = (MessageBox.Show("Your guess number is:  " & CStr(txtbxEnterNum.Text) & " and the correct guess is:  " & CStr(ranNum)))

            Else


                If MessageBox.Show("Correct! Do you wish to restart?", "Please Confirm Action", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    txtbxEnterNum.Clear()
                ElseIf Windows.Forms.DialogResult.No Then
                    Me.Close()

                End If

            End If
        Next

    End Sub
    'This code will close the the program.
    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub
    'This code will generate a random number between 1-10
    Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
        ranNum = CInt(Rnd() * 10)
    End Sub
End Class

No need for a for-next loop here.
Incremenent the counter in the click handler
Just use an if statement to check if counter is greater than 3 and act accordingly.

well first you should first globalize the count it won't count the way you want it if you put it under the private sub and also try to use a timer you to rendomise you will need to start and stop the timer by giving it the interval that if the timer = this interval then it should stop counting and the number that the timer stoped on will be you guest ID but then if you have more then 1 guest then you should verify that the ID is not in used by ather user so if the ID is already in used the the timer should start again to get the free ID as you say it up to 10 that means you will only handle up to ten guest so don't forget to prevent the error there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.