Hi everyone,
I've been debugging my program all night and I just can't quite come up with how to make my counter work. I keep feeling like I'm putting it, or maybe just part of it, in the wrong place so I've been moving it all over the place but haven't found anything that works yet. I'll paste my code below and it will be the code that seems to not stop when it reaches the end of my counter. Other ways I've tried it make it stop after one or two, depending on what I did to the code at them time. Anyway, if anyone out there sees where I'm messing up, could you please let me know? I've been going round in circles on this one and I think it's supposed to be easy. (Leave it to me to complicate things.LOL) Ok, here's what I have at the moment:
Public Class mainForm
Dim guessCounter As Integer
Dim correctAccumulator As Integer
Dim totalAccumulator As Integer
Dim answer As Integer
Dim guesses As Integer
Dim guess As Integer
Dim lastGuess As Integer
Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim randomNum As New Random
answer = randomNum.Next(1, 11)
MessageBox.Show("CAN YOU GUESS MY NUMBER?" & vbNewLine & vbNewLine & "I have a number between 1 and 10." & vbNewLine & "To try to guess my number, enter your guess in the box" & vbNewLine & "and then click 'Check Guess'." & vbNewLine & "I will then tell you if you guessed my number or not." & vbNewLine & "You have 5 chances." & vbNewLine & "After your first guess, if you guess wrong," & vbNewLine & "I will tell you if you if you are getting warmer or colder." & vbNewLine & "If you're getting warmer, the box will turn red." & vbNewLine & "If you're getting colder, the box will turn blue." & vbNewLine & "If you want to see this information again at any time during the game," & vbNewLine & "just click 'About'." & vbNewLine & "Good Luck!", "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End Sub
Private Sub checkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkButton.Click
lastGuess = guess
If Not Integer.TryParse(guessBox.Text, guess) Then
MessageBox.Show("You must enter a numeric value.", "Invalid Entry", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
guessBox.Focus()
Exit Sub
End If
If guessCounter > 5 Then
MessageBox.Show("Sorry, you are out of guesses." & vbNewLine & "Click 'New Game' to try guessing a new number.", "GAME OVER", MessageBoxButtons.OK, MessageBoxIcon.Stop)
guessBox.Clear()
guessBox.BackColor = Color.White
newGameButton.Focus()
checkButton.Enabled = False
guessBox.Enabled = False
Exit Sub
End If
For guessCounter = 1 To 5
If Not guess = answer Then
If (answer - guess) < (answer - lastGuess) Then
MessageBox.Show("Sorry. Try again. You're getting warmer!", "Warmer Guess", MessageBoxButtons.OK, MessageBoxIcon.Stop)
guessBox.BackColor = Color.Red
lastGuess = guess
totalAccumulator = totalAccumulator + 1
guessBox.Clear()
guessBox.Focus()
Else
MessageBox.Show("Sorry. Try again. You're getting colder!", "Colder Guess", MessageBoxButtons.OK, MessageBoxIcon.Stop)
guessBox.BackColor = Color.DodgerBlue
lastGuess = guess
totalAccumulator = totalAccumulator + 1
guessBox.Clear()
guessBox.Focus()
End If
Exit Sub
ElseIf guess = answer Then
MessageBox.Show("Congratulations! You guessed my number!" & vbNewLine & "Click 'New Game' to play again" & vbNewLine & "or click 'Exit' to quit.", "WINNER!!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Dim randomNum As New Random
answer = randomNum.Next(1, 11)
checkButton.Enabled = False
guessBox.Enabled = False
guessCounter = 1
guessBox.BackColor = Color.White
guessBox.Clear()
totalAccumulator = totalAccumulator + 1
correctAccumulator = correctAccumulator + 1
lastGuess = 0
End If
Exit Sub
Next guessCounter
End Sub
Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click
MessageBox.Show("I have a new number." & vbNewLine & "Can you guess it?" & vbNewLine & "Enter your guess and click 'Check Guess' to try." & vbNewLine & "GOOD LUCK!", "New Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim randomNum As New Random
answer = randomNum.Next(1, 11)
guessBox.BackColor = Color.White
guessBox.Clear()
guessBox.Focus()
guessBox.Enabled = True
checkButton.Enabled = True
guessCounter = 1
guess = 0
lastGuess = 0
Exit Sub
End Sub
Private Sub infoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles infoButton.Click
MessageBox.Show("CAN YOU GUESS MY NUMBER?" & vbNewLine & vbNewLine & "I have a number between 1 and 10." & vbNewLine & "To try to guess my number, enter your guess in the box" & vbNewLine & "and then click 'Check Guess'." & vbNewLine & "I will then tell you if you guessed my number or not." & vbNewLine & "You have 5 chances." & vbNewLine & "After your first guess, if you guess wrong," & vbNewLine & "I will tell you if you if you are getting warmer or colder." & vbNewLine & "If you're getting warmer, the box will turn red." & vbNewLine & "If you're getting colder, the box will turn blue." & vbNewLine & "If you want to see this information again at any time during the game," & vbNewLine & "just click 'About'." & vbNewLine & "Good Luck!", "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End Sub
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub guessBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guessBox.TextChanged
End Sub
End Class