1,105,456 Community Members

Need Help with Random Number Guessing Game Timer and Image Display

Member Avatar
mitchstokes225
Newbie Poster
11 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Here is my gui
guess_game

Attachments
Member Avatar
mitchstokes225
Newbie Poster
11 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I see the issue with my image...but my timer is still jacked. I just don't know how to get the darn thing to work.

Member Avatar
Nutster
Junior Poster
146 posts since Oct 2006
Reputation Points: 52 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 3 [?]
 
0
 

First of all, in your CheckChanged handlers, check to make sure the option button/radio button has a value of 1. If 0, then this indicates that the current button is losing a previous selection. If you don't do this, you will generate unnecessary random numbers and may set the random number to the wrong value, depending on what order the events happen.

In your btnGuess handler, you are duplicating code. You are doing the exact same thing no matter which level option is selected. You may want to check earlier in the sub that one of the option buttons is selected, if not give a MessageBox.Show letting the user know to pick a level first. Maybe leave the Guess button disabled until one of the level options is selected.

You assigned intGuess in TryParse. Comparing an Integer to another number is a lot faster and easier than comparing a string, like txtNumber.Text. Use intGuess instead. Also, intGuess is used only inside btnGuess_Click. Make the variable local to the function.

When you make one of the images visible, also make the other image invisible.

If intGuess < holdNumber Then
    imgHigher.Visible = True
    imgLower.Visible = False
ElseIf intGuess > holdNumber Then
    imgHigher.Visible = False
    imgLower.Visible = True
Else
    rem The guess is not less than, nor greater than.  Must be equal.
    Debug.Assert(intGuess = holdNumber)
    rem This happens least often, so put it in Else.
    imgHigher.Visible = False
    imgLower.Visible = False
    MessageBox.Show("You Win! The Number was " & holdNumber)
End If

Timer_Tick is called when the timer hits its event time, as defined by Interval. You are using this function to set up the timer instead. Move this type of code into new handlers for the timer options. Each one should setup the timer's interval, set the starting time (30, 45, 60 seconds) and start the timer, or you could start the timer when the user makes his/her first guess.

The kind of thing that should be in the Timer_Tick handler should be counting down the counter on the form and stopping the game when the user runs out of time.

Member Avatar
Luc001
Posting Pro in Training
455 posts since Mar 2010
Reputation Points: 77 [?]
Q&As Helped to Solve: 117 [?]
Skill Endorsements: 3 [?]
 
0
 

You can try this to let your timer working:

 Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If timercount > 0 Then
            timercount = timercount - 1
            lblTimeRemain.Text = timercount.ToString()
        Else
            Timer1.Enabled = False
            MessageBox.Show("Time Up")

        End If


    End Sub

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

        If timercount > 0 Then
            timercount = timercount
            Timer1.Enabled = True

        End If

    End Sub

    Private Sub RadThirty_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadThirty.CheckedChanged
        If RadThirty.Checked = True Then
            lblTimeRemain.Text = "30"
            timercount = 30

        End If
    End Sub

    Private Sub RadFourtyfive_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadFourtyfive.CheckedChanged
        If RadFourtyfive.Checked = True Then
            lblTimeRemain.Text = "45"
            timercount = 45

        End If
    End Sub

    Private Sub RadSixty_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadSixty.CheckedChanged
        If RadSixty.Checked = True Then
            lblTimeRemain.Text = "60"
            timercount = 60

        End If
    End Sub
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article