VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



game of 21

by John (4 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 17th May 2007
Date Added: Mon 8th February 2021
Rating: (1 Votes)

game of 21

Rate game of 21



Private Const strPlayer As String = "Player"
Private Const strComputer As String = "Computer"
Private Const strDraw As String = "Draw"

Private Sub cmdDone_Click()
    Unload Me
End Sub

Private Sub cmdPlayGame_Click()
    Static intPlayerScore As Integer, intCompScore As Integer, intDrawsScore As Integer
    Dim intPlayerTotal As Integer, intCompTotal As Integer
    
    'Deal 3 cards to Player
    Call DealCard(imgPlayerCard1, intPlayerTotal)
    Call DealCard(imgPlayerCard2, intPlayerTotal)
    Call DealCard(imgPlayerCard3, intPlayerTotal)
    
    'Deal 3 cards to Computer
    Call DealCard(imgCompCard1, intCompTotal)
    Call DealCard(imgCompCard2, intCompTotal)
    Call DealCard(imgCompCard3, intCompTotal)
    
    If Winner(intPlayerTotal, intCompTotal) = strPlayer Then
        lblWinner.Caption = "You won!"
        Call UpdateScore(intPlayerScore)
        Call ShowScore(lblScore, intPlayerScore, intCompScore, intDrawsScore)
    ElseIf Winner(intPlayerTotal, intCompTotal) = strComputer Then
        lblWinner.Caption = "Computer won"
        Call UpdateScore(intPlayerScore)
        Call ShowScore(lblScore, intPlayerScore, intCompScore, intDrawsScore)
    Else
        lblWinner.Caption = "It's a draw!"
        Call UpdateScore(intDrawsScore)
        Call ShowScore(lblScore, intPlayerScore, intCompScore, intDrawsScore)
    End If
End Sub

Sub DealCard(ByRef imgCard As Image, ByRef intPlayerTotal As Integer)
    Dim intCardNum As Integer
    
    intCardNum = Int(10 * Rnd + 1)
    If intCardNum = 1 Then
        imgCard.Picture = LoadPicture("Card1.wmf")
    ElseIf intCardNum = 2 Then
        imgCard.Picture = LoadPicture("Card2.wmf")
    ElseIf intCardNum = 3 Then
        imgCard.Picture = LoadPicture("Card3.wmf")
    ElseIf intCardNum = 4 Then
        imgCard.Picture = LoadPicture("Card4.wmf")
    ElseIf intCardNum = 5 Then
        imgCard.Picture = LoadPicture("Card5.wmf")
    ElseIf intCardNum = 6 Then
        imgCard.Picture = LoadPicture("Card6.wmf")
    ElseIf intCardNum = 7 Then
        imgCard.Picture = LoadPicture("Card7.wmf")
    ElseIf intCardNum = 8 Then
        imgCard.Picture = LoadPicture("Card7.wmf")
    ElseIf intCardNum = 9 Then
        imgCard.Picture = LoadPicture("Card9.wmf")
    ElseIf intCardNum = 10 Then
        imgCard.Picture = LoadPicture("Card10.wmf")
    End If
    intPlayerTotal = intPlayerTotal + intCardNum
End Sub

Function Winner(ByVal intPlayerTotal As Integer, ByVal intCompTotal As Integer) As String
    Const intLimit As Integer = 21
    
    If (intPlayerTotal = intCompTotal) Or _
       (intPlayerTotal > intLimit And intCompTotal > intLimit) Then
       Winner = strDraw
    ElseIf (intCompTotal > intLimit) Or _
       (intPlayerTotal > intLimit And intCompTotal > intLimit) Then
       Winner = strPlayer
    Else
      Winner = strComputer
    End If
End Function

Sub UpdateScore(ByRef intWinner As Integer)
    Const intWinPoints As Integer = 1
    
    intWinner = intWinner + intWinPoints
End Sub

Sub ShowScore(ByRef lblLabel As Label, ByVal intPlayerScore As Integer, _
ByVal intCompScore As Integer, ByVal intDrawsScore As Integer)
    lblScore.Caption = "You: " & intPlayerScore & vbCrLf _
        & "Computer: " & intCompScore & vbCrLf _
        & "Draws: " & intDrawsScore
End Sub
Private Sub Form_Load()
    Randomize
End Sub


Download this snippet    Add to My Saved Code

game of 21 Comments

No comments have been posted about game of 21. Why not be the first to post a comment about game of 21.

Post your comment

Subject:
Message:
0/1000 characters