Blackjack, Anyone?
The sample program shows how to call many of the clsDeck methods, but it doesn't show them in action in a real game. In the next chapter you'll design a complete card game called Poker Squares, but for now, something a little simpler is in order. It's time to create a bare-bones version of blackjack.
Creating Blackjack's User Interface
The first step is to create the game's user interface. Perform the following steps:
Start a new Standard EXE Visual Basic project.
Set the form's properties to the values listed here:
Name = CardForm
AutoRedraw = True
BackColor = Black
Caption = "Blackjack"
Height = 6015
ScaleMode = Pixel
Width = 8250
Add three CommandButton controls to the form, giving them the property values listed here:
CommandButton #1
Name = cmdStartGame
Caption = "&Start Game"
Height = 33
Left = 19
Top = 320
Width = 89
CommandButton #2
Name = cmdHit
Caption = "&Hit"
Height = 33
Left = 341
Top = 320
Width = 89
CommandButton #3
Name = cmdStay
Caption = "S&tay"
Height = 33
Left = 443
Top = 320
Width = 89
Add a Timer control to the form.
You've now completed blackjack's user interface. Figure 8.6 shows what your main form will look like at this point. In the next section, you'll add handlers for the program's various controls.
Adding the Object Handlers
Next, you need to associate code with the various objectsthe form, buttons, and timerthat make up the user interface. To accomplish this task, perform the following steps:
Double-click the form to bring up the code window, and add the following form handlers to it. You can either type the code or copy it from the BlackJack1.txt file, which you can find in the Chap08\Code directory of this book's CD-ROM.
Listing 8.8 The Form Handlers
1: Private Sub Form_Load() 2: cmdHit.Enabled = False 3: cmdStay.Enabled = False 4: cmdStartGame_Click 5: End Sub 6: 7: Private Sub Form_Unload(Cancel As Integer) 8: Unload frmCards 9: End Sub
Analysis - The Form_Load subroutine, which Visual Basic calls when the user starts the program, disables the Hit and Stay buttons (Lines 2 and 4) and the starts a new game by simulating a click on the Start Game button (Line 4). Line 8 in the Form_Unload subroutine removes the frmCards form from memory at the same time the main form closes.
-
Add to the code window the CommandButton handlers in Listing 8.9. You can either type the code or copy it from the BlackJack2.txt file, which you can find in the Chap08\Code directory of this book's CD-ROM.
Listing 8.9 The CommandButton Handlers
1: Private Sub cmdHit_Click() 2: PlayerCardCount = PlayerCardCount + 1 3: Deck.Deal 1, Player, _ 4: PlayerCardCount * 80 + 20, 220, 0, FaceUp 5: PlayerTotal = GetCardTotal(Player) 6: If PlayerTotal > 21 Then 7: MsgBox "You busted" 8: DealerTotal = GetCardTotal(Dealer) 9: EndGame 10: End If 11: End Sub 12: 13: Private Sub cmdStartGame_Click() 14: Cls 15: cmdStartGame.Enabled = False 16: cmdHit.Enabled = True 17: cmdStay.Enabled = True 18: Set Deck = New clsDeck 19: Deck.Shuffle 20: DealerCardCount = 1 21: PlayerCardCount = 1 22: CurrentX = 20 23: CurrentY = 20 24: Print "DEALER'S HAND" 25: Deck.Deal 1, Dealer, 20, 60, 0, FaceDown 26: Deck.Deal 1, Dealer, 100, 60, 0, FaceUp 27: CurrentX = 20 28: CurrentY = 180 29: Print "PLAYER'S HAND" 30: Deck.Deal 2, Player, 20, 220, 24, FaceUp 31: End Sub 32: 33: Private Sub cmdStay_Click() 34: cmdHit.Enabled = False 35: cmdStay.Enabled = False 36: Timer1.Interval = 1000 37: End Sub
Analysis - The cmdHit_Click subroutine responds to the Hit button. Line 2 increases the player's card count, and Line 3 deals another card into the player's hand. A call to GetCardTotal (Line 5) gets the player's current score, and if the total is over 21 (Line 6), the game is over (Lines 7 to 9).
Analysis - The cmdStartGame_Click subroutine responds to the Start Game button. Line 14 clears the screen, and Lines 16 and 17 enable the Hit and Stay buttons. Then, Lines 18 and 19 create a new Deck object and shuffle it. Lines 20 and 21 initialize the card counts, and Lines 22 to 24 print the "DEALER'S HAND" label. Lines 25 and 26 deal two cards to the dealer, one of them face down, while Line 30 does the same thing for the player's hand, except this time both cards are dealt face-up.
Analysis - The cmdStay_Click subroutine responds to the Stay button. Lines 34 and 35 disable the Hit and Stay buttons in preparation for the dealer's turn. Line 36 turns on the timer, which gets the dealer's turn going.
-
Add to the code window the Timer handler shown in Listing 8.10. You can either type the code or copy it from the BlackJack3.txt file, which you can find in the Chap08\Code directory of this book's CD-ROM.
Listing 8.10 The Timer Handler
1: Private Sub Timer1_Timer() 2: DealerTotal = GetCardTotal(Dealer) 3: If DealerTotal > 21 Then 4: MsgBox "Dealer busts" 5: Timer1.Interval = 0 6: EndGame 7: ElseIf DealerTotal > 16 Then 8: MsgBox "Dealer stays" 9: Timer1.Interval = 0 10: EndGame 11: Else 12: DealerCardCount = DealerCardCount + 1 13: Deck.Deal 1, Dealer, _ 14: DealerCardCount * 80 + 20, 60, 0, FaceUp 15: End If 16: End Sub
Analysis - The Timer1_Timer subroutine implements the computer player and gets called for each timer event. Line 2 gets the dealer's current card total. If the total is greater than 21, Line 4 notifies the player that the dealer has busted and Line 5 turns off the timer. If the dealer's total is greater than 16, the dealer stays (Lines 8 and 9) and the current game ends (Line 10). Finally, if the dealer's total is less than or equal to 16, Lines 12 to 14 add a card to the dealer's hand.
Completing the Game
Almost there! After you add the general game subroutines and the required modules, you'll be ready to play blackjack. Here are the final steps:
Add to the code window the general game subroutines shown in Listing 8.11. You can either type the code or copy it from the BlackJack4.txt file, which you can find in the Chap08\Code directory of this book's CD-ROM.
Listing 8.11 The General Subroutines
1: Function GetCardTotal(plyer As Integer) As Integer 2: Dim value As Integer 3: Dim total As Integer 4: Dim AceCount As Integer 5: Dim CardCount As Integer 6: Dim i As Integer 7: 8: total = 0 9: AceCount = 0 10: CardCount = Deck.NumCardsInHand(plyer) 11: For i = 0 To CardCount - 1 12: value = Deck.GetCardValue(plyer, i) Mod 13 13: If value > Ten Then 14: value = Ten 15: ElseIf value = Ace Then 16: AceCount = AceCount + 1 17: value = 10 18: End If 19: total = total + value + 1 20: Next i 21: 22: If total > 21 And AceCount > 0 Then _ 23: total = total - AceCount * 10 24: GetCardTotal = total 25: End Function 26: 27: Sub EndGame() 28: Dim msg As String 29: 30: Deck.ShowHandCard Dealer, 0, FaceUp 31: DealerTotal = GetCardTotal(Dealer) 32: PlayerTotal = GetCardTotal(Player) 33: Set Deck = Nothing 34: cmdStartGame.Enabled = True 35: cmdHit.Enabled = False 36: cmdStay.Enabled = False 37: msg = "Dealer: " + CStr(DealerTotal) + _ 38: vbCrLf + "Player: " + CStr(PlayerTotal) 39: If PlayerTotal > 21 Or _ 40: (PlayerTotal < DealerTotal And DealerTotal < 22) Then 41: msg = msg + vbCrLf + vbCrLf + "You lose." 42: Else 43: msg = msg + vbCrLf + vbCrLf + "You win." 44: End If 45: MsgBox msg 46: End Sub
Analysis - The GetCardTotal function calculates the card total for the player or dealer, depending upon the value of the plyer parameter. You'll study this function in detail later in this chapter. The EndGame subroutine shows the dealer's hand (Line 30), gets the player's and dealer's card totals (Lines 31 and 32), deletes the deck (Line 33), sets the game's buttons (Lines 34 to 36), and displays a message telling the player who won (Lines 37 to 45).
-
Add to the top of the code window the variable declarations and enumerations in Listing 8.12. You can either type the code or copy it from the BlackJack5.txt file, which you can find in the Chap08\Code directory of this book's CD-ROM.
Listing 8.12 The Declarations
1: Option Explicit 2: 3: Private Enum HandIDs 4: Dealer 5: Player 6: End Enum 7: 8: Dim DealerCardCount As Integer 9: Dim PlayerCardCount As Integer 10: Dim Deck As clsDeck 11: Dim PlayerTotal As Integer 12: Dim DealerTotal As Integer
Add the Cards.frm form and the clsCard.cls, clsDeck.cls, and Cards.bas modules to the project, just as you did with the previous demo program.
Save the game's main form as CardForm.frm and the project file as BlackJack.vbp.
You've now completed the blackjack program.
Playing Blackjack
When you run the program, you see the screen shown in Figure 8.7. The dealer's hand is at the top of the screen, and the player's hand is at the bottom. The objective of the game is to get as close to 21 as you can without going over. (The cards 2 through 10 are worth 210 points. All face cards count as 10 points, and an ace can count as either 1 or 11 points.)
To draw a card, press Enter or click the Hit button. Continue to draw until you're ready to stop, and then click the Stay button. If you haven't gone over 21, the dealer then begins to draw cards. The dealer must continue to draw until it reaches 17 or better. The winning hand is the one that's closest to 21 without going over (see Figure 8.8).
Programming Blackjack
Obviously, this program isn't a complete blackjack game. Many of the game's details are ignored (like doubling-down and insurance), there's no betting, and each game is only a single hand. However, the program does demonstrate how you can use the clsDeck and clsCard classes when programming an actual game. Much of the code in the program needs no explanation. However, one function, GetCardTotal, is the heart of the game and worthy of close examination.
GetCardTotal analyzes a blackjack hand and comes up with a total. This might seem like a trivial task until you recall that an ace can count as either 1 or 11 points. Moreover, a hand might have as many as four aces, further complicating the point-counting process.
To keep this task simple, GetCardTotal assumes that it will count all aces in a hand as the same value. The point value that the program chooses depends on the hand's point total. (Obviously, the program will never use 11 as an ace point value if the hand has more than one ace, because two 11-point aces will bring the hand to over 21.)
First, the program determines how many cards are in the hand by calling NumCardsInHand:
CardCount = Deck.NumCardsInHand(plyer)
This clsDeck method takes as its single parameter the number of the hand to check. The program uses the value returned from NumCardsInHand to set up a For loop that looks at each card in the hand. In the loop, the program first calculates the value of the current card:
value = Deck.GetCardValue(plyer, i) Mod 13
This calculation results in a value from 0 to 12 (ace to king). If the card's value is greater than 10, indicating a face card (jack, queen, or king), the program sets the card's value to Ten:
If value > Ten Then value = Ten
(The constants range from Ace, which equals 0, to King, which equals 12. Therefore, Ten is actually the integer value 9, not 10 as you might think.)
If the card turns out to be an ace, the program increments the number of aces in the hand and sets value to 10:
ElseIf value = Ace Then AceCount = AceCount + 1 value = 10 End If
The program first assumes that it will treat the ace as a high card that is worth one point more than the face cards.
Next, the program adds the value of the current card to the total so far:
total = total + value + 1
Because the card values range from 0 to 12, the added point value is actually value+1.
After totaling the values of all cards in the hand, the program checks whether the hand is over 21. If it is, and it contains aces, the program subtracts 10 for each ace in the hand so that the values of the aces all change to 1:
If total > 21 And AceCount > 0 Then _ total = total - AceCount * 10
The function then returns the total to the calling method:
GetCardTotal = total
That's all there is to analyzing a blackjack hand (although this is a simplified version of the game). Now you're ready to move on to more challenging card games.