4 Replies - 767 Views - Last Post: 01 September 2013 - 09:26 AM Rate Topic: -----

#1 jace5869  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-August 13

Guess My Number program problems

Posted 31 August 2013 - 08:08 PM

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'll attach the instructions. I think I have it working mostly, but I just can't figure out the small issues I'm having. Maybe I'm declaring or assigning some variables in the wrong place or something..Just need some guidance.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Guess My Number program problems

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,529
  • Joined: 08-April 09

Re: Guess My Number program problems

Posted 01 September 2013 - 08:47 AM

Why do you have 3 instances of Random class? You only need 1 and you reuse that.
Here is some suggestions, when the guess counter is > 5, call your newGame method, that method should reset your counters and guesses, generate next random number(using the same Random instance ), and you go from there
Was This Post Helpful? 1
  • +
  • -

#3 jace5869  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-August 13

Re: Guess My Number program problems

Posted 01 September 2013 - 08:55 AM

I'm just a little confused...when you say use the same random instance what do you mean exactly? example?

I can tell by that you mean I should only have one Dim generateRandomNumber As Random, right? Is it okay to just call it in form load and not at the module level?
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,529
  • Joined: 08-April 09

Re: Guess My Number program problems

Posted 01 September 2013 - 09:05 AM

You have 3, at line 27, 87, 127.
When you start your program create 1 instance, do the 5 guess part and if the guess is > 5 reset the guess counter, i think that should do it.
Was This Post Helpful? 1
  • +
  • -

#5 jace5869  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-August 13

Re: Guess My Number program problems

Posted 01 September 2013 - 09:26 AM

Made some changes after given some advice. I didn't realize what I was doing wrong with Random class until it was pointed out , and I tried to clean up some things.

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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1