Public Class Form2
Dim com1 As Integer
Dim com2 As Integer
Dim com3 As Integer
Dim play1 As Integer
Dim play2 As Integer
Dim play3 As Integer
Dim ace1 As Integer = 1 'lets player choose ace val
Dim ace2 As Integer = 11
Dim playScore As Integer = 0
Dim comScore As Integer = 0
Const MIN As Integer = 1
Const MAX As Integer = 13
Dim playTurn As Integer = 0
Dim comTurn As Integer = 0
Const SCORE As Integer = 21 'goal (equal or under)
Private Sub frm21_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize() 'randomize card values
com1 = Int((MAX - MIN + 1) * Rnd() * MIN)
com2 = Int((MAX - MIN + 1) * Rnd() * MIN)
com3 = Int((MAX - MIN + 1) * Rnd() * MIN)
play1 = Int((MAX - MIN + 1) * Rnd() * MIN)
play2 = Int((MAX - MIN + 1) * Rnd() * MIN)
play3 = Int((MAX - MIN + 1) * Rnd() * MIN)
btnAgain.Visible = False
btnAgain.Enabled = False
btnDraw.Visible = True
btnDraw.Enabled = True
btnCheck.Visible = False
btnCheck.Enabled = False
End Sub
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
playTurn += 1
comTurn += 1
Select Case comTurn
Case 1
lblPlayDeal.Visible = True
imgCard1.Visible = True
Case 2
imgCard1.Visible = True
imgCard2.Visible = True
lblChoose.Visible = True
btnCheck.Enabled = True
btnCheck.Visible = True
Case 3
imgCard1.Visible = True
imgCard2.Visible = True
imgCard3.Visible = True
lblChoose.Visible = False
btnDraw.Enabled = False
End Select
Select Case playTurn
Case 1
lblPlayDeal.Visible = True
imgCard4.Visible = True
Case 2
imgCard4.Visible = True
imgCard5.Visible = True
lblChoose.Visible = True
btnCheck.Enabled = True
btnCheck.Visible = True
Case 3
imgCard4.Visible = True
imgCard5.Visible = True
imgCard6.Visible = True
lblChoose.Visible = False
btnDraw.Enabled = False
End Select
End Sub
Private Function AddScore(ByVal p_Card As Integer, ByVal p_Image As Image) As Integer
Select Case p_Card
Case 1
p_Image = Blackjack_Fix.My.Resources.sa
If Form1.cmbAce.Text = "Play with ace as one" Then
Return 1
ElseIf Form1.cmbAce.Text = "Play with ace as eleven" Then
Return 11
End If
Case 2
p_Image = Blackjack_Fix.My.Resources.d_2
Return 2
Case 3
p_Image = Blackjack_Fix.My.Resources.club_3
Return 3
Case 4
p_Image = Blackjack_Fix.My.Resources.h4
Return 4
Case 5
p_Image = Blackjack_Fix.My.Resources.s5
Return 5
Case 6
p_Image = Blackjack_Fix.My.Resources.d_6
Return 6
Case 7
p_Image = Blackjack_Fix.My.Resources.club_7
Return 7
Case 8
p_Image = Blackjack_Fix.My.Resources.h8
Return 8
Case 9
p_Image = Blackjack_Fix.My.Resources.s9
Return 9
Case 10
p_Image = Blackjack_Fix.My.Resources.d_10
Return 10
Case 11
p_Image = Blackjack_Fix.My.Resources.jackdiamond
Return 11
Case 12
p_Image = Blackjack_Fix.My.Resources.queenheart
Return 12
Case 13
p_Image = Blackjack_Fix.My.Resources.kingheart
Return 13
End Select
End Function
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
lblChoose.Visible = False
Select Case playTurn
Case 2
playScore += AddScore(play1, imgCard4.Image)
playScore += AddScore(play2, imgCard5.Image)
imgCard6.Visible = False
Case 3
playScore += AddScore(play1, imgCard4.Image)
playScore += AddScore(play2, imgCard5.Image)
playScore += AddScore(play3, imgCard6.Image)
End Select
comScore += AddScore(com1, imgCard1.Image)
comScore += AddScore(com2, imgCard2.Image)
comScore += AddScore(com3, imgCard3.Image)
comScore += AddScore(com1, imgCard1.Image)
comScore += AddScore(com2, imgCard2.Image)
comScore += AddScore(com3, imgCard3.Image)
lblComScore.Text = comScore
lblPlayScore.Text = playScore
btnDraw.Enabled = False
btnDraw.Visible = False
btnAgain.Visible = True
btnAgain.Enabled = True
If playScore = comScore And comScore < SCORE Then 'scores are equal and under 21
MessageBox.Show("It’s a tie!")
End If
If comScore < playScore And playScore < SCORE Then 'player is over computer but less than 21
MessageBox.Show("You win!!!")
End If
If playScore > comScore And comScore > SCORE Then 'player is over computer and over 21
MessageBox.Show("You lose. :(")
End If
playScore = 0
comScore = 0
End Sub
Private Sub btnAgain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgain.Click
Me.Close()
Form1.Show()
Form1.cmbAce.Text = Nothing
End Sub
End Class