Home > Articles > Programming > Visual Basic

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:

  1. Start a new Standard EXE Visual Basic project.

  2. Set the form's properties to the values listed here:

    Name = CardForm

    AutoRedraw = True

    BackColor = Black

    Caption = "Blackjack"

    Height = 6015

    ScaleMode = Pixel

    Width = 8250

  3. 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

  4. 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.

Figure 8.6
The completed user interface.

Adding the Object Handlers

Next, you need to associate code with the various objects—the form, buttons, and timer—that make up the user interface. To accomplish this task, perform the following steps:

  1. 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.
  1. 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.
  1. 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:

  1. 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).
  1. 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
  1. 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.

  2. 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 2–10 points. All face cards count as 10 points, and an ace can count as either 1 or 11 points.)

Figure 8.7
The main blackjack screen.

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).

Figure 8.8
Winning at blackjack.

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020