4 Replies - 1163 Views - Last Post: 03 December 2012 - 09:25 AM Rate Topic: -----

#1 mitchstokes225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-November 09

Help with Random Number Guessing Game Timer and Image Display.

Posted 02 December 2012 - 09:38 PM

I am creating a random number guessing game. I have 6 options. 3 Game playing levels and 3 timer levels.

I have my form display with 6 radio buttons:
radRookie
radVeteran
radPro

radThirty
radFourtyFive
radSixty

You select your level and time.

Then you have a text box to enter your guess, then click guess.

I want to display an image above or below the text box to guess higher or guess lower.
I can get the images hidden until button click, but only one image displays and doesn't work properly.

I also cannot get the darn timer to work and count down while guessing. Any help please!?!

Public Class Form1

    Dim intGuess As Integer
    Dim aRandom As New Random
    Dim holdNumber As Integer
    Dim timercount As Integer

    Private Sub radRookie_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRookie.CheckedChanged
        holdNumber = aRandom.Next(0, 10)
    End Sub

    Private Sub radVeteran_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radVeteran.CheckedChanged
        holdNumber = aRandom.Next(0, 100)
    End Sub

    Private Sub radPro_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radPro.CheckedChanged
        holdNumber = aRandom.Next(0, 1000)
    End Sub

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

        If Integer.TryParse(txtNumber.Text, intGuess) Then
            If intGuess > -1 Then
                If radRookie.Checked = True Then
                    If txtNumber.Text = holdNumber Then
                        imgHigher.Visible = False
                        imgLower.Visible = False
                        MessageBox.Show("You Win! The Number was " & holdNumber)
                    ElseIf txtNumber.Text < holdNumber Then
                        imgHigher.Visible = True
                    ElseIf txtNumber.Text > holdNumber Then
                        imgHigher.Visible = True
                    End If


                ElseIf radVeteran.Checked = True Then
                    If txtNumber.Text = holdNumber Then
                        imgHigher.Visible = False
                        imgLower.Visible = False
                        MessageBox.Show("You Win! The Number was " & holdNumber)
                    ElseIf txtNumber.Text < holdNumber Then
                        imgHigher.Visible = True
                    ElseIf txtNumber.Text > holdNumber Then
                        imgHigher.Visible = True
                    End If


                ElseIf radPro.Checked = True Then
                    If txtNumber.Text = holdNumber Then
                        imgHigher.Visible = False
                        imgLower.Visible = False
                        MessageBox.Show("You Win! The Number was " & holdNumber)
                    ElseIf txtNumber.Text < holdNumber Then
                        imgHigher.Visible = True
                    ElseIf txtNumber.Text > holdNumber Then
                        imgHigher.Visible = True
                    End If
                End If
            Else
                MessageBox.Show("Must enter a Valid Guess!")
            End If
        End If
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        radRookie.Checked = False
        radVeteran.Checked = False
        radPro.Checked = False

        radThirty.Checked = False
        radFourtyFive.Checked = False
        radSixty.Checked = False

        lblTimeRemain.Text = String.Empty

        txtNumber.Clear()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If radThirty.Checked = True Then
            Timer1.Interval = 1000
            Timer1.Enabled = True
            timercount = 30
            lblTimeRemain.Text = timercount.ToString()
            If timercount = 0 Then
                Timer1.Enabled = False
                lblTimeRemain.Text = "Done"

            Else
                timercount -= 1
            End If
        ElseIf radFourtyFive.Checked = True Then
            Timer1.Interval = 1000
            Timer1.Enabled = True
            timercount = 45
            lblTimeRemain.Text = timercount.ToString()
            If timercount = 0 Then
                Timer1.Enabled = False
                lblTimeRemain.Text = "Done"

            Else
                timercount -= 1
            End If
        ElseIf radSixty.Checked = True Then
            Timer1.Interval = 1000
            Timer1.Enabled = True
            timercount = 60
            lblTimeRemain.Text = timercount.ToString()
            If timercount = 0 Then
                Timer1.Enabled = False
                lblTimeRemain.Text = "Done"

            Else
                timercount -= 1
            End If

        End If
    End Sub
End Class



Attached is my gui

Attached image(s)

  • Attached Image

This post has been edited by mitchstokes225: 02 December 2012 - 09:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Random Number Guessing Game Timer and Image Display.

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Help with Random Number Guessing Game Timer and Image Display.

Posted 02 December 2012 - 10:23 PM

View Postmitchstokes225, on 02 December 2012 - 10:38 PM, said:

I also cannot get the darn timer to work and count down while guessing. Any help please!?!
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If radThirty.Checked = True Then
            Timer1.Interval = 1000
            Timer1.Enabled = True
            timercount = 30
            lblTimeRemain.Text = timercount.ToString()
            If timercount = 0 Then
                Timer1.Enabled = False
                lblTimeRemain.Text = "Done"
            Else
                timercount -= 1


When you leave the Timer handler, you decrement timercount, but the next time through, you set it back to 30 (or 45, or 60, depending on which radioButton is checked).

              If radRookie.Checked = True Then
                   If txtNumber.Text = holdNumber Then
                       imgHigher.Visible = False
                       imgLower.Visible = False
                       MessageBox.Show("You Win! The Number was " & holdNumber)
                   ElseIf txtNumber.Text < holdNumber Then
                       imgHigher.Visible = True
                   ElseIf txtNumber.Text > holdNumber Then
                       imgHigher.Visible = True
                   End If


When you are having problems, it pays to walk through the code in your mind., so let's see what I see when I look at your code above.

If the user guesses the number, turn off both images.
If the user's guess is too low, turn on the higher image
If the user's guess is too high, turn on the higher image
Hmmm....

Ok, but once you fix that, walk through it again, at least five times. You might ask "Why five times?". Well, you might not, but I'm going to pretend you did.

Make holdNumber 50.
The first time through, choose 78.
The second time through, choose 36.
The third time through, choose 67.
The fourth time through, choose 46.
OK, now you can choose 50, to clean everything up.

Did you see anything wrong in your walkthrough?
If not, you might want to try running it and choosing those same values.

In what way does the image "not work properly? If it's fixed up after you read the above, then I guess that was what you meant.

You should also read up on debugging skills in our VB.Net tutorials section.
Was This Post Helpful? 1
  • +
  • -

#3 mitchstokes225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-November 09

Re: Help with Random Number Guessing Game Timer and Image Display.

Posted 03 December 2012 - 07:31 AM

Aw man.....Thanks. I see the issue with the image. I knew I was going crazy, I had it working once. I wonder how the heck I changed it. I guess that's what I get for working on it when I should have been in bed.

Now for that darn timer.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Help with Random Number Guessing Game Timer and Image Display.

Posted 03 December 2012 - 09:22 AM

View Postmitchstokes225, on 03 December 2012 - 08:31 AM, said:

Aw man.....Thanks. I see the issue with the image. I knew I was going crazy, I had it working once. I wonder how the heck I changed it. I guess that's what I get for working on it when I should have been in bed.

Now for that darn timer.

Shoulda been in bed... been there, done that.

As for the timer, it's easy enough to solve, and you can choose one of a couple of ways.

1. Make timerCount global (Dim outside any Sub or Function)
2. Make timerCount a Static variable, within the code block where it's used.

If you use method 2, don't forget to reset it if the user guesses the number before timerCount reaches 0.
Was This Post Helpful? 1
  • +
  • -

#5 mitchstokes225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-November 09

Re: Help with Random Number Guessing Game Timer and Image Display.

Posted 03 December 2012 - 09:25 AM

I just ended up doing a simple timer instead of trying to display a countdown. It's due here shortly and I don't want to get dinged for trying to go above and beyond what I was suppose to do.


So far so good in my runs.



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

        If radThirty.Checked = True Then
            Timer1.Interval = 30000 '30 Seconds
            Timer1.Enabled = True
        ElseIf radFourtyFive.Checked = True Then
            Timer1.Interval = 45000 '45 Seconds
            Timer1.Enabled = True
        ElseIf radSixty.Checked = True Then
            Timer1.Interval = 60000 '60 Seconds
            Timer1.Enabled = True
        End If

        If Integer.TryParse(txtNumber.Text, intGuess) Then
            If intGuess > -1 Then
                If radRookie.Checked = True Then 'If rookie is selected the number entered will check against the held random number.  Displaying a pop up message that you win with the winning number
                    If txtNumber.Text = holdNumber Then
                        imgLower.Visible = False
                        imgHigher.Visible = False
                        MessageBox.Show("You Win! The Number was " & holdNumber)
                    ElseIf txtNumber.Text < holdNumber Then  'Or will show you an image to guess higher or lower.
                        imgHigher.Visible = True
                        imgLower.Visible = False
                    ElseIf txtNumber.Text > holdNumber Then
                        imgLower.Visible = True
                        imgHigher.Visible = False
                    End If


                ElseIf radVeteran.Checked = True Then 'If Veteran is selected the number entered will check against the held random number.  Displaying a pop up message that you win with the winning number
                    If txtNumber.Text = holdNumber Then
                        imgLower.Visible = False
                        imgHigher.Visible = False
                        MessageBox.Show("You Win! The Number was " & holdNumber)
                    ElseIf txtNumber.Text < holdNumber Then  'Or will show you an image to guess higher or lower.
                        imgHigher.Visible = True
                        imgLower.Visible = False
                    ElseIf txtNumber.Text > holdNumber Then
                        imgLower.Visible = True
                        imgHigher.Visible = False
                    End If


                ElseIf radPro.Checked = True Then 'If Professional is selected the number entered will check against the held random number.  Displaying a pop up message that you win with the winning number
                    If txtNumber.Text = holdNumber Then
                        imgLower.Visible = False
                        imgHigher.Visible = False
                        MessageBox.Show("You Win! The Number was " & holdNumber)
                    ElseIf txtNumber.Text < holdNumber Then  'Or will show you an image to guess higher or lower.
                        imgHigher.Visible = True
                        imgLower.Visible = False
                    ElseIf txtNumber.Text > holdNumber Then
                        imgLower.Visible = True
                        imgHigher.Visible = False
                    End If
                End If
            Else
                MessageBox.Show("Must enter a Valid Guess!")
            End If
        End If
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ' Setup to clear the fields to play again.
        radRookie.Checked = False
        radVeteran.Checked = False
        radPro.Checked = False

        radThirty.Checked = False
        radFourtyFive.Checked = False
        radSixty.Checked = False

        imgLower.Visible = False
        imgHigher.Visible = False

        txtNumber.Clear()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        MsgBox("Times up!")
      
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1