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.