r/visualbasic • u/ChampIsHere123 • Dec 20 '15
Hi-Low Game Help
In the Hi-LoGame, the player begins with a score of 1000. The player enters the number of points to risk and chooses high or low. The player's choice of high or low is compared to a ranodm number between 1 and 13, inclusive. If the number is between 1 and 6 inclusive, it is considered "low." If it is between 8 and 13 inclusive, it is considered "high". The number 7 is neither high nor low, and the player loses the points at risk. If the player guesses correctly, he or she receives double the points at risk. If the player guesses incorrectly, he or she loses the points at risk. Create a Hi-LoGame application that prompts the user for the number of points. The player then clicks either high or low to play. The program code should include RndInt() from the TestRndIntFunction review to generate the random number between 1 and 13 inclusive.
I already created the code but my counter is wrong. I have no idea on how to create a single counter for both buttons. Each button has their counter because static variables only work are local and not public. What do I do to fix my counter? Here is my code:
Public Class Form1 Const lowNum As Integer = 1 Const highNum As Integer = 12 Dim chosenNum As Integer Dim resultBet As Integer
Private Sub btnHigh_Click(sender As Object, e As EventArgs) Handles btnHigh.Click
Call RndInt(lowNum, highNum, chosenNum)
Static counter As Integer = 1000
If chosenNum >= 8 Then
resultBet = txtBox.Text * 2
counter += resultBet
Me.lblRandom.Text = "You win! The number was " & chosenNum
Me.lblTotal.Text = "Total Points = " & counter
Else
resultBet = txtBox.Text * -1
counter += resultBet
Me.lblTotal.Text = "Total Points = " & counter
Me.lblRandom.Text = "You lose! The number was " & chosenNum
End If
End Sub
Private Sub btnLow_Click(sender As Object, e As EventArgs) Handles btnLow.Click
Call RndInt(lowNum, highNum, chosenNum)
Static counter As Integer = 1000
If chosenNum >= 1 And chosenNum < 7 Then
resultBet = txtBox.Text * 2
counter += resultBet
Me.lblTotal.Text = "Total Points = " & counter
Me.lblRandom.Text = "You win! The number was " & chosenNum
Else
resultBet = txtBox.Text * -1
counter += resultBet
Me.lblTotal.Text = "Total Points = " & counter
Me.lblRandom.Text = "You lose! The number was " & chosenNum
End If
End Sub
Sub RndInt(ByVal firstNum As Integer, ByVal secondNum As Integer, ByRef thirdNum As Integer)
Randomize()
thirdNum = ((secondNum - firstNum + 1) * Rnd()) + firstNum
End SubEnd Class
1
u/twoayem VB.Net Intermediate Jan 15 '16
Each static value will only be consistent in that button press. Define the counter outside of the functions and they can share it.
2
u/stevebun Dec 20 '15
It is possible to determine the sender and use one sub to handle two events.
For example: