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
Public Class Form1
	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
		Randomize()
		Dim randomNum As New Random
		answer = randomNum.Next(1, 10)

		MessageBox.Show(String.Format("CAN YOU GUESS MY NUMBER?{0}{0}I have a number between 1 and 10.{0}To try to guess my number, enter your guess in the box{0}and then click 'Check Guess'.{0}I will then tell you if you guessed my number or not.{0}You have 5 chances.{0}After your first guess, if you guess wrong,{0}I will tell you if you if you are getting warmer or colder.{0}If you're getting warmer, the box will turn red.{0}If you're getting colder, the box will turn blue.{0}If you want to see this information again at any time during the game,{0}just click 'About'.{0}Good Luck!", vbNewLine), "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information)
	End Sub

	Private Sub checkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkButton.Click

		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(String.Format("Sorry, you are out of guesses.{0}Click 'New Game' to try guessing a new number.", vbNewLine), "GAME OVER", MessageBoxButtons.OK, MessageBoxIcon.Stop)
			SetControls(True)
			Return
		End If

		If 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
			Else
				MessageBox.Show("Sorry.  Try again.  You're getting colder!", "Colder Guess", MessageBoxButtons.OK, MessageBoxIcon.Stop)
				guessBox.BackColor = Color.DodgerBlue
			End If

			lastGuess = guess
			totalAccumulator += 1
			SetControls(False)
			guessCounter += 1	 'increase the guess counter
			Return

		ElseIf guess = answer Then
			MessageBox.Show(String.Format("Congratulations!  You guessed my number!{0}Click 'New Game' to play again{0}or click 'Exit' to quit.", vbNewLine), "WINNER!!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			Dim randomNum As New Random
			answer = randomNum.Next(1, 11)
			guessCounter = 1
			totalAccumulator += 1
			correctAccumulator += 1
			lastGuess = 0
			SetControls(True)
		End If

	End Sub

	Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click
		MessageBox.Show(String.Format("I have a new number.{0}Can you guess it?{0}Enter your guess and click 'Check Guess' to try.{0}GOOD LUCK!", vbNewLine), "New Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
		Dim randomNum As New Random
		answer = randomNum.Next(1, 10)
		guessBox.BackColor = Color.White
		SetControls(False)
		guessCounter = 1
		guess = 0
		lastGuess = 0
	End Sub

	Private Sub SetControls(ByVal guessed As Boolean)
		guessBox.Clear()
		If guessed Then
			guessBox.BackColor = Color.White
			newGameButton.Focus()
			checkButton.Enabled = False
			guessBox.Enabled = False
			Return
		End If
		guessBox.Enabled = True
		checkButton.Enabled = True
		guessBox.Focus()
	End Sub
End Class
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.