# vb number of guesses and stats in guessing game

Page 1 of 1

## 6 Replies - 1717 Views - Last Post: 05 March 2013 - 03:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314187&amp;s=112bfdafdaceff24468c95f15f7e14d9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jsheehan

Reputation: 0
• Posts: 26
• Joined: 14-February 13

# vb number of guesses and stats in guessing game

Posted 03 March 2013 - 01:34 PM

Hi,i am working on a guessing game in vb. I need to show the number of guesses and also the win loss record. Here is the code so far.

```Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim inputNumber As Integer
Dim number As Integer
Dim i As Integer
Dim Count As Integer = 0

Randomize()
number = CInt(Rnd() * 25) + 1

inputNumber = CInt(txtBoxGuess.Text)

If inputNumber < number Then
txtHint.Text = "please enter a higher number"
ElseIf inputNumber > number Then
txtHint.Text = "please enter a lower number"
ElseIf inputNumber = number Then
Count += 1
LabelNumGuess.Text = "number of guesses: " + Str(Count)
txtGuess.Text = "you win in" + Str(Count) + "Guesses"
End If

End Sub

Any help would be much appreciated! Thanks

```

Is This A Good Question/Topic? 0

## Replies To: vb number of guesses and stats in guessing game

### #2 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: vb number of guesses and stats in guessing game

Posted 03 March 2013 - 02:34 PM

Ok. Good luck with that.

If you come up with a QUESTION let us know.

### #3 jsheehan

Reputation: 0
• Posts: 26
• Joined: 14-February 13

## Re: vb number of guesses and stats in guessing game

Posted 04 March 2013 - 02:16 AM

Sorry, still trying to get used to the forum.

What I meant to ask was how can i get the statistics from the games played from the input or code? Also, is there a way to only get the number of guesses taken to win only once, as it's repeating on me?

### #4 IronRazer

• Custom Control Freak

Reputation: 1505
• Posts: 3,806
• Joined: 01-February 13

## Re: vb number of guesses and stats in guessing game

Posted 04 March 2013 - 02:50 PM

hello jsheehan,
The way it is set up you are resetting your variables and picking a new number every time the button is pressed. You could fix this in several ways but, i made this example and broke the game up into 2 buttons. 1 to start the game and 1 to check your guess. I added comments in the code so it should explain what is going on in the program and why.
```Public Class Form1
'Declared variables here to make them Global so they can be seen in all of the events
Dim number As Integer = 0
Dim gamesWon As Integer = 0
Dim Guesses As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When the game is opened set all needed settings
btnGuess.Enabled = False
btnStart.Enabled = True
End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Randomize()
number = CInt(Rnd() * 25) + 1
Guesses = 0 'Make sure the number of guesses is set to 0 when starting new game.

'Disable Start button so a new number can`t be picked again until you win
btnStart.Enabled = False
'Enable the Guess button so you can start guessing numbers
btnGuess.Enabled = True
End Sub

Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
'Make sure the textbox has a number in it before continuing
If IsNumeric(txtBoxGuess.Text) Then

'Declare this variable here because it is only used inside this event
Dim inputNumber As Integer = 0
inputNumber = CInt(txtBoxGuess.Text)

'Move this here so it counts a try each time you press the guess button
Guesses += 1
LabelNumGuess.Text = "number of guesses: " + Str(Guesses)

If inputNumber < number Then
txtHint.Text = "please enter a higher number"
ElseIf inputNumber > number Then
txtHint.Text = "please enter a lower number"
ElseIf inputNumber = number Then
txtGuess.Text = "you win in" + Str(Guesses) + " Guesses"

gamesWon += 1 'Add 1 to the number of games won and dislay it.
Label_GamesWon.Text = "You have played and won " & CStr(gamesWon) & " game(s)."

'Disable Guess button because we won and will need to start a new game
btnGuess.Enabled = False
'Enable the Start button so we can start a new game
btnStart.Enabled = True
End If

End If
End Sub

End Class

```

### #5 Norman_Bates

Reputation: 0
• Posts: 18
• Joined: 26-February 13

## Re: vb number of guesses and stats in guessing game

Posted 04 March 2013 - 03:01 PM

First of all your code doesn't actually let you guess properly because every time you press btnStart it changes the number and also resets you Count variable to 0

You will need to add an extra button and move some of your code around.

```Randomize()
Count = 0
number= CInt(Rnd() * 25) + 1

```

The next bit is your btnStart_Click event:
```inputnumber = CInt(txtBoxGuess.text)

count += 1

labelNumGuess.Text = "Number of guesses: " & Str(Count)

if inputnumber < number then
txthint.text = "Please enter a higher number"
EsleIf inputnumber > number then
txthint.text = "Please enter a lower number"
ElseIf inputnumber = number then
txtguess.text = "You win in " & Str(Count) & " guesses"
End If

```

Now if you want to reset it and generate a new number so that the user can have another go you need to add another button and in its event put the following:
```Count = 0
number = CInt(Rnd() * 25) + 1

labelNumGuess.Text = "Number of guesses: " & Str(Count)
txtHint.Text = ""
txtGuess.text = ""
txtBoxGuess.Text = ""

```

Also, declare your variables outside of the events. It would look like:

```Public Class Form1

Dim inputnumber as Integer
Dim number as Integer
Dim i as integer
Dim Count as Integer

'btnStart click event

'btnReset click event

End Class

```

### #6 jsheehan

Reputation: 0
• Posts: 26
• Joined: 14-February 13

## Re: vb number of guesses and stats in guessing game

Posted 05 March 2013 - 02:49 PM

thanks ironrazer, it worked. one more thing, is it possible to limit or restrict the ammount of guesses to 10 using guess?

### #7 IronRazer

• Custom Control Freak

Reputation: 1505
• Posts: 3,806
• Joined: 01-February 13

## Re: vb number of guesses and stats in guessing game

Posted 05 March 2013 - 03:04 PM

If you added another elseif statement at the bottom of the (btnGuess) button event such as (elseif Guesses = 10 then) you could have it reset the buttons and variables and tell the user they lost.

### #8 IronRazer

• Custom Control Freak

Reputation: 1505
• Posts: 3,806
• Joined: 01-February 13

## Re: vb number of guesses and stats in guessing game

Posted 05 March 2013 - 03:25 PM

Sorry, I just noticed that will not work unless you re-order the (if elseif) statements like this.
```            If inputNumber = number Then
txtGuess.Text = "you win in" + Str(Guesses) + " Guesses"

gamesWon += 1 'Add 1 to the number of games won and dislay it.
Label_GamesWon.Text = "You have played and won " & CStr(gamesWon) & " game(s)."

'Disable Guess button because we won and will need to start a new game
btnGuess.Enabled = False
'Enable the Start button so we can start a new game
btnStart.Enabled = True
ElseIf Guesses = 3 Then
txtHint.Text = "YOU LOOSE !!!"

'Here you could add 1 to another global variable to keep track of games lost

'Disable Guess button because we lost and will need to start a new game
btnGuess.Enabled = False
'Enable the Start button so we can start a new game
btnStart.Enabled = True

ElseIf inputNumber < number Then
txtHint.Text = "please enter a higher number"
ElseIf inputNumber > number Then
txtHint.Text = "please enter a lower number"
End If

```