1,105,380 Community Members

Guess My Number program

Member Avatar
jason.craft.5439
Newbie Poster
2 posts since Aug 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So I have this assignment for school and I've got the program working, but some minor issues (maybe major issues with cleanilness of code, but working on it). I'm having to make a Guess My Number program and it seems to work, but when I start a new game it wants to check the guess against the old answer instead of the new random number. I will supply what my code is and the instructions of the project.

Public Class Form1

    'module-level scope variables
    Dim guessLimit As Single = 0
    Dim correctGuesses As Single = 0
    Dim totalGuesses As Single = 0
    Dim randomNumber As Single


    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

        Dim averageCorrect As Single

        'calculate users avg of correct guesses
        averageCorrect = (correctGuesses / totalGuesses)

        MessageBox.Show("You had " & correctGuesses & " correct guesses out of 5 guesses, for a win percentage of " _
                        & averageCorrect.ToString("P"))


        'exit application
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim generateRandomNumber As New Random


        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 10)


    End Sub

    Private Sub checkGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkGuessButton.Click

        'users guess
        Dim guess As Single
        'Dim answer As Single

        'answer = randomNumber


        If Not Single.TryParse(guessTextBox.Text, guess) Then
            MessageBox.Show("You must enter a numeric value here.", "Bad value", _
                             MessageBoxButtons.OK, MessageBoxIcon.Information)

            'setup textbox
            setup()

            Exit Sub
        End If

        'Single.TryParse(guessTextBox.Text, guess)

        If guess = randomNumber Then
            MessageBox.Show("Congratulations! You guessed my number! Click on Start New Game to play again.", "Winner", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            'increment total and correct guesses on A CORRECT GUESS
            correctGuesses += 1
            totalGuesses += 1

        Else

            If totalGuesses < 5 Then
                MessageBox.Show("Sorry! Try another guess.", "Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                guessLimit += 1
                totalGuesses += 1
                setup()


            ElseIf totalGuesses > 5 Then
                MessageBox.Show("Sorry! You have used all 5 chances. I have a new number. Try to guess it.", "New game", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                'clear the textbox
                setup()
                'reset guess count limitor
                guessLimit = 0



                'Generate a new random number
                Dim generateRandomNumber As New Random
                Dim randomNumber As Single



                'Generate a random number for user to guess
                randomNumber = generateRandomNumber.Next(1, 10)

                'For testing purposes ONLY remove or comment out
                guessTextBox.Text = randomNumber.ToString()

            End If


        End If


    End Sub

    Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click

        newGame()

    End Sub

    'sub routine to clear textbox and set it to focus
    Sub setup()
        guessTextBox.Clear()
        guessTextBox.Focus()
    End Sub


    Sub newGame()

        'clear the textbox
        setup()
        'reset guess count limitor
        guessLimit = 0

        'Generate a new random number
        Dim generateRandomNumber As New Random
        Dim randomNumber As Single



        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 10)

    End Sub

End Class

I will paste the instructions as a reply to this..

Thank you...I've been banging my head against the wall on this.

Member Avatar
kRod
Junior Poster
139 posts since May 2012
Reputation Points: 49 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 6 [?]
 
0
 

In your Sub newGame() You are Dim(ing) a variable named randomNumber instead of just reassigning your global variable new value. Since it's scope is local to only the Sub newGame it will have no effect on the global variable randomNumber and it will be whatever you assigned it in the beginning.

Member Avatar
jason.craft.5439
Newbie Poster
2 posts since Aug 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I've submitted the code to teach and what I had changed is below.. Seems to be working smoothly.

Public Class Form1

    'module-level scope variables
    Dim guessLimit As Single = 0
    Dim correctGuesses As Single = 0
    Dim totalGuesses As Single = 0
    Dim randomNumber As Single
    Dim generateRandomNumber As New Random


    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

        Dim averageCorrect As Single

        'calculate users avg of correct guesses
        averageCorrect = (correctGuesses / totalGuesses)

        MessageBox.Show("You had " & correctGuesses & " correct guesses out of 5 guesses, for a win percentage of " _
                        & averageCorrect.ToString("P"))


        'exit application
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 10)

    End Sub

    Private Sub checkGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkGuessButton.Click

        'users guess
        Dim guess As Single


        If Not Single.TryParse(guessTextBox.Text, guess) Then
            MessageBox.Show("You must enter a numeric value here.", "Bad value", _
                             MessageBoxButtons.OK, MessageBoxIcon.Information)

            'setup textbox
            setup()

            Exit Sub
        End If


        If guess = randomNumber Then
            MessageBox.Show("Congratulations! You guessed my number! Click on Start New Game to play again.", "Winner", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            'increment total and correct guesses on A CORRECT GUESS
            correctGuesses += 1
            totalGuesses += 1

        Else

            If guessLimit < 5 Then
                MessageBox.Show("Sorry! Try another guess.", "Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                guessLimit += 1
                totalGuesses += 1
                setup()

            Else
                MessageBox.Show("Sorry! You have used all 5 chances. I have a new number. Try to guess it.", "New game", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                newGame()

            End If

        End If

    End Sub

    Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click

        newGame()

    End Sub

    'sub routine to clear textbox and set it to focus
    Sub setup()
        guessTextBox.Clear()
        guessTextBox.Focus()
    End Sub


    Sub newGame()

        guessTextBox.Clear()
        guessTextBox.Focus()

        'reset guess count limitor
        guessLimit = 0


        'Generate a random number for user to guess
        randomNumber = generateRandomNumber.Next(1, 11)

    End Sub

End Class
Member Avatar
kRod
Junior Poster
139 posts since May 2012
Reputation Points: 49 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 6 [?]
 
0
 

If your question has been answered satisfactorily please mark the thread as solved.

Question Answered as of 7 Months Ago by kRod
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: