2 Replies - 1967 Views - Last Post: 17 March 2010 - 06:37 PM Rate Topic: -----

#1 adam3187  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-March 10

vs.net Number guessing Game

Posted 17 March 2010 - 08:25 AM

Hi I have to do a number guessing game for school. I have almost all of it done except the part where if the user enters the same guess twice he/she should get a msg saying that they guessed that number already. And the guess don't count towards the max number of guesses(10). I think I would have to use an array. Here is my code so far. Can someone please help me figure out please. Also I want to allow only Numbers from 1-100 entered in the input textbox (txtinput) would it be better to do the validating of the text box in the keypress or at the time of the button click. Here is the code I got so far. Thanks a million for the help.
 Private Sub btnGuess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuess.Click
        Dim guess As Integer = txtinput.Text
        ReDim Preserve aryGuess(counter)
        aryGuess(counter) = (txtinput.Text)
        Dim n As Integer
        


        'For n = 0 To (aryGuess.Length - 1)
        '    If guess = aryGuess(n) Then
        '        lblResult.Text = "You already guessed that number please try again!"
        '    End If
        'Next
        If Not IsNumeric(guess) Or guess > 100 Or guess < 1 Then
            MsgBox("Please enter a number from 1 to 100")
            Exit Sub
        End If
        If guess > numbertoguess Then lblResult.Text = "The Number You Entered Is To High, Please Try Again"
        txtinput.Clear()
        If guess < numbertoguess Then lblResult.Text = "The Number You Entered Is To Low, Please Try Again"
        txtinput.Clear()
        counter = counter + 1


        lblNumGuesses.Text = counter

        If counter >= 10 Then
            lblResult.Text = "Sorry you used all your guesses. Please use the reset button to start again"
            btnGuess.Enabled = False
            Exit Sub

        Else
            MsgBox("Please Enter A Number From 1 to 100")
            txtinput.Clear()

        End If


    End Sub
 Private Sub txtinput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtinput.KeyPress
        If IsNumeric(txtinput.Text) = False Then
            txtinput.Clear()
            MsgBox("Please Do Not Enter Letters")
        End If
end sub



The keypress I have there works but the backspace won't work. Any help would be appreciated.
Adam

This post has been edited by adam3187: 17 March 2010 - 10:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: vs.net Number guessing Game

#2 BrainCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 23-August 09

Re: vs.net Number guessing Game

Posted 17 March 2010 - 04:41 PM

I tried to correct your code as far I could and should now yield desired outputs. The validating at KeyPress is actually not preferred because it keeps throwing your message even before the user has finished typing anything (could a number)so I put it at button press at the begining. If you do want to put it seperatly then try puting in the textbox Leave event and return focus to the textbox should anything be incorrect. Check out the code and compare it to yours and amend were necessary to your liking\situation.


Public Class Form1
    Dim aryGuess() As Integer
    Dim counter As Integer
    Dim numbertoguess As Integer
    Dim rndGen As Random
    Dim guess As Integer

    Private Sub btnGuess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuess.Click

        'If not numeric data then exit sub
        If IsNumeric(txtinput.Text) = False Then
            txtinput.Clear()
            MsgBox("Please Do Not Enter Letters")
            Exit Sub
        End If

        guess = Val(txtinput.Text)

        'if data is out of range then exit sub
        If guess > 100 Or guess < 1 Then
            MsgBox("Please enter a number between 1 and 100")
            Exit Sub
        End If



        ReDim Preserve aryGuess(counter)
        Dim n As Integer

        'if guess is already used then exit sub else add to used array
        For n = 0 To (aryGuess.Length - 1)

            If guess = aryGuess(n) Then

                lblResult.Text = "You already guessed that number please try again!"
                Exit Sub
            Else

                aryGuess(counter) = guess

            End If

        Next

        'too high
        If guess > numbertoguess Then
            lblResult.Text = "The Number You Entered Is Too High, Please Try Again" & "" & "Number: " & numbertoguess
            txtinput.Clear()
            'too low
        ElseIf guess < numbertoguess Then
            lblResult.Text = "The Number You Entered Is Too Low, Please Try Again" & "" & "Number: " & numbertoguess
            txtinput.Clear()
            'correct
        ElseIf guess = numbertoguess Then

            lblResult.Text = "Congratulations you won!! It took you " & counter & " guesses to win. Please use the reset button to start again "
            btnGuess.Enabled = False
            Exit Sub

        End If

        counter = counter + 1


        lblNumGuesses.Text = counter

        'if too many guesses then exit sub
        If counter >= 10 Then
            lblResult.Text = "Sorry you used all your guesses. Please use the reset button to start again"
            btnGuess.Enabled = False
            Exit Sub

        Else
            'prompt for new data
            MsgBox("Please Enter A Number From 1 to 100")
            txtinput.Clear()

        End If


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'get new random number between 1 & 100
        rndGen = New Random
        numbertoguess = rndGen.Next(1, 101)

    End Sub
End Class



Was This Post Helpful? 0
  • +
  • -

#3 adam3187  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-March 10

Re: vs.net Number guessing Game

Posted 17 March 2010 - 06:37 PM

Hi, thanks for the quick reply. I redid my code to what you told me to but I am having a little problem when I enter any number that is not the random number. I get the "MsgBox("Please enter a number between 1 and 100")" for all numbers except the "random" number. here is the code I have in there now. Any suggestions as to why this is happening? Thanks again for the help.Also if I just click ok when it tells me to enter a number between 1 to 100 and make another guess I can keep guessing but the counter is not counting, and I always gets the lblResult.Text = "You already guessed that number please try again!"
 Private Sub btnGuess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuess.Click


        'If not numeric data then exit sub
        If IsNumeric(txtinput.Text) = False Then
            txtinput.Clear()
            MsgBox("Please Do Not Enter Letters")
            Exit Sub
        End If

        guess = Val(txtinput.Text)

        'if data is out of range then exit sub
        If guess > 100 Or guess < 1 Then
            MsgBox("Please enter a number between 1 and 100")
            Exit Sub
        End If



        ReDim Preserve aryGuess(counter)
        Dim n As Integer

        'if guess is already used then exit sub else add to used array
        For n = 0 To (aryGuess.Length - 1)

            If guess = aryGuess(n) Then

                lblResult.Text = "You already guessed that number please try again!"
                Exit Sub
            Else

                aryGuess(counter) = guess

            End If

        Next

        'too high
        If guess > numbertoguess Then
            lblResult.Text = "The Number You Entered Is Too High, Please Try Again"
            txtinput.Clear()
            'too low
        ElseIf guess < numbertoguess Then
            lblResult.Text = "The Number You Entered Is Too Low, Please Try Again"
            txtinput.Clear()
            'correct
        ElseIf guess = numbertoguess Then

            lblResult.Text = "Congratulations you won!! It took you " & counter & " guesses to win. Please use the reset button to start again "
            btnGuess.Enabled = False
            Exit Sub

        End If

        counter = counter + 1


        lblNumGuesses.Text = counter

        'if too many guesses then exit sub
        If counter >= 10 Then
            lblResult.Text = "Sorry you used all your guesses. Please use the reset button to start again"
            btnGuess.Enabled = False
            Exit Sub

        Else
            'prompt for new data
            MsgBox("Please Enter A Number From 1 to 100")
            txtinput.Clear()

        End If
end sub


This post has been edited by adam3187: 17 March 2010 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1