Jump to content

[VB.NET] Program Error (always yields the same answer)

Go to solution Solved by fizzlesticks,

When you read back the labels you get strings, you have to cast them to an int before you add them.

I am making a Blackjack program for my programming class (using numbers only from 1 through 10 and cannot use Global Variables, which reason is beyond me). Computer can only draw 3 cards while 2 are dealt to player and can hit for another 3.


 


Whenever, I press butHit (button Hit), it always gets the answer, "Bust : Tie" even when the values for both the computer and your own hand are clearly below 21.


This happens no matter what if you press butHit.


 


lblPlayV checks if the player has pressed butPlay before to get the random values and such.


lblCounter represents how many times butHit has been pressed.


 


I have no idea what could be wrong.


 


Here's the Code:



Public Class Form1

Private Sub butClose_Click(sender As System.Object, e As System.EventArgs) Handles butClose.Click
End
End Sub

Private Sub butPlay_Click(sender As System.Object, e As System.EventArgs) Handles butPlay.Click
Dim rand As New Random

Dim intM1 As Integer = rand.Next(1, 11)
Dim intM2 As Integer = rand.Next(1, 11)
Dim intM3 As Integer = rand.Next(1, 11)
Dim intM4 As Integer = rand.Next(1, 11)
Dim intM5 As Integer = rand.Next(1, 11)

Dim intOP1 As Integer = rand.Next(1, 11)
Dim intOP2 As Integer = rand.Next(1, 11)
Dim intOP3 As Integer = rand.Next(1, 11)

lblPlayV.Text = 1
lblCounter.Text = 1
butPlay.Text = "Replay"

lblM1.Text = intM1
lblM2.Text = intM2
lblM3.Text = intM3
lblM4.Text = intM4
lblM5.Text = intM5

lblOP1.Text = intOP1
lblOP2.Text = intOP2
lblOP3.Text = intOP3

lblM1.Visible = True
lblM2.Visible = True
End Sub

Private Sub butHit_Click(sender As System.Object, e As System.EventArgs) Handles butHit.Click
If lblPlayV.Text = 1 Then
'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 1 Then
lblM3.Visible = True

If lblM1.Text + lblM2.Text + lblM3.Text = 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If

ElseIf lblM1.Text + lblM2.Text + lblM3.Text > 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If

Else
lblCounter.Text = lblCounter.Text + 1
End If

End If

'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 2 Then
lblM4.Visible = True

If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text = 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If

ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text > 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If

Else
lblCounter.Text = lblCounter.Text + 1
End If

End If

'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 3 Then
lblM5.Visible = True

If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If

ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If

Else
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Draw")

ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")

Else
MsgBox("You Lose")
End If
End If
lblCounter.Text = 0
lblPlayV.Text = 0
End If

'<<<<<<<<<<<<<<<<<<<<<<'
End If
End Sub

Private Sub ButStand_Click(sender As System.Object, e As System.EventArgs) Handles ButStand.Click
If lblPlayV.Text = 1 Then
lblPlayV.Text = 0

If lblM3.Visible = False Then
If lblM1.Text + lblM2.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")

ElseIf lblM1.Text + lblM2.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If

If lblCounter.Text = 1 Then
If lblM1.Text + lblM2.Text + lblM3.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")

ElseIf lblM1.Text + lblM2.Text + lblM3.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If

If lblCounter.Text = 2 Then
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")

ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If

If lblCounter.Text = 3 Then
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")

ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If

End If
End Sub
End Class

 

 

Link to post
Share on other sites

When you read back the labels you get strings, you have to cast them to an int before you add them.

Thank you so much. I can't believe I made such a dumb mistake, I assumed in my brain that it was automatically cast to integer from text if they were only integers, don't know why.

 

And found my other mistake with the If Statements.

 

Case Closed lol.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×