Object

Note it looks like Excel rounds the values of these parameters to the nearest 0.25. So to calculate A, I would always round down to the closest.25. For example, with Calibri 11, Ratio(200.00) = 1053.75/200=5.27 - rounded down to 5.25. Blackjack is a comparing card game between a player and dealer and played with one or more French decks of 52 cards. The player is dealt an initial two card hand with the option of drawing cards to bring the total value to 21 or less without exceeding it, so that the dealer will lose by having a lesser hand than the player or by exceeding 21.

Frames

Image

Combo Box

Combo Box

Code In Excel Vba

Command Button

Command Button

Labels

Property

Name

BackColor Caption

StartUpPosition

BorderStyle

Name

Caption

BorderStyle

Name

AutoSize

BorderStyle

Name

Style

Value/Text

Name

Style

Value/Text

Name

Caption

Enabled

Name

Caption

Enabled

Name

Caption

BorderStyle

ForeColor

TextAlign

Tutorials

Vba Excel Tutorials

Value frmTable Green

'Blackjack' CenterScreen fmBorderStyleNone frmDealer and frmPlayer 'Dealer' and 'Player' fmBorderStyleSingle imgDlrl through imgDlr5 and imgPlayerl through imgPlayer5

False fmBorderStyleSingle cmbNumDecks fmStyleDropDownList 'l'

cmbBet fmStyleDropDownCombo '$2' cmdHit 'Hit' False cmdDeal 'Begin' True lblPlayerScore and lblDealerScore

Empty String fmBorderStyleNone

White fmTextAlignCenter

Excel Vba Programming Pdf

Table 6.6 Select Properties of the Blackjack Form (continued)

Object

Property

Value

Label

Name

lblResult

ForeColor

Red

BorderStyle

fmBorderStyleNone

TextAlign

fmTextAlignCenter

Label

Name

lblEarnings

Caption

'$0'

ForeColor

Blue

BorderStyle

fmBorderStyleNone

TextAlign

fmTextAlignCenter

To set the size of the Image controls, I first set the AutoSize property of one Image control to true. Then, I loaded an image of a card into the control at Design Time via its Picture property. The Image control automatically adjusts its Width and Height properties to fit the image exactly. Finally, I removed the image from the Image control by deleting the path from its Picture property and set the Width and Height properties of all other Image controls to match.

Combo Box controls.

Label controls

The form design for the Blackjack game.

Combo Box controls.

Label controls

The form design for the Blackjack game.

Image controls

Frame controls

Command Button controls

Image controls

Frame controls

Command Button controls

In addition to the Blackjack form, a second form is added to the project to serve as a splash screen to distract the player as the code that simulates the shuffling of the deck executes. The code doesn't really take that long to run, but the delay in the game is a nice distraction that doesn't require the player to do anything, and it serves to inform the player that the end of the deck was reached and must be reshuffled. Figure 6.17 shows the deck shuffling form with two Label controls.

The code module for the Shuffling form contains the code for initializing and shuffling the deck.

The last part of the interface for the Blackjack game is the worksheet that shows the form and stores the results of each hand. Figure 6.18 shows the worksheet for the Blackjack game. It contains two Command Button controls: one for showing the Blackjack form, and the second for clearing the content of the first three columns that store the result of each hand.

Program inputs include bitmap image files, Wave Form Audio (.wav) files, the number of decks in the game, an amount to bet on each hand and numerous mouse clicks. The image files represent a deck of cards, and are displayed in the Image controls on the Blackjack form. A total of fifty-three images are needed to represent the deck (52 for the faces and 1 for the card back). You can create simple images such as these shown using just about any drawing program (I used MS Paint). These images are loaded into the Image controls when a new hand is dealt and when the player or dealer draws additional cards. The .wav files are played whenever the deck is shuffled or cards are dealt. Combo Box controls on the Blackjack form allows the player to choose the number of decks and select an amount to bet on each hand.

Program outputs include the results of each hand and the playing of the .wav sound files. The results of each hand include the player's score, the dealer's score, and the player's new balance to columns A, B, and C of the worksheet, respectively. The sound files are played such that program execution is continuous (i.e., the program does not pause while the sound file plays).

The Shuffling

form.

The Blackjack worksheet.

The Blackjack worksheet.

The outline of program execution follows:

• The game starts from the Command Button control on the worksheet labeled Blackjack. This displays the Blackjack form. A public procedure in a standard code module is connected to the Command Button (this button is from the Forms toolbar) and its code shows the form.

The Initialize() event procedure of the Blackjack form should contain code that initializes its ActiveX controls.

• A single Command Button control on the form begins the game, deals each hand, and offers the player the choice to stand or hit on the current hand.

• The Caption property of this Command Button control starts with 'Begin' and changes between 'Deal' and 'Stand' as the game proceeds.

The Click() event of the Command Button control contains code that initializes the game (shuffles the cards and sets the Caption property to 'Deal') when the Caption property is 'Begin'. If the Caption property is 'Deal', then the code should clear the Blackjack form of card images, and simulate the dealing of two cards each to the dealer and player. If the Caption property is 'Stand', then the code should display the dealer's hidden card and start the dealer's turn at drawing cards before ending the hand. At a minimum, custom sub procedures should be used to handle shuffling, clearing the form of images, dealing the hand, and drawing the dealer's cards. More custom procedures may be added to handle these tasks when the program is written.

• When the player changes the number of decks, the Change() event of the Combo Box control is triggered and the program forces an immediate shuffling of the deck.

• The code that simulates shuffling the deck is entered in the code module for the Shuffling form.

The deck of cards is simulated using an array. The length of the array depends on the number of decks selected by the player in the Combo Box control. The deck array variable must be global as it must also be accessed by the code in the Blackjack form module.

The array must store the value of each card, its suit, and the file path to the image representing the card. To handle these different data types, the deck should be constructed from a custom data type.

The Activate() event procedure of the UserForm object contains the code for initializing and shuffling the deck. It should also play the shuffling .wav file and hide the form after a short delay.

The deck is shuffled randomly by generating integer random numbers between 0 and the number of elements in the array. Next, two elements in the array (chosen randomly) representing two cards in the deck are swapped. The process of choosing two random numbers and swapping two elements in the deck array is contained within a loop such that it may be repeated. Figure 6.19 illustrates the process of swapping two cards in an array. When this process is repeated many times, the deck is effectively shuffled.

Swapping two cards in the deck.

• Four cards are dealt (two to the dealer and two to the player) with each new hand. The procedure that handles this task must loop through the image controls to find the correct control for displaying the card image, load the image of the card, store the value of each card for scoring, increment to the next card, play the draw card .wav file, and test if the deck needs shuffling. The first card drawn to the dealer must be displayed face down. Card information is stored in an array, so an array index must increment by one after each draw. The index representing the dealer's face-down card will have to be stored in a variable so its image and value can be called upon when the hand is over.

• A second Command Button control on the Blackjack form allows the player to draw more cards. This control must be initially disabled and then enabled when the player is allowed to draw more cards.

The Click() event of this Command Button control should simulate drawing a single card to the player's hand. The code will have to display the card image in the appropriate Image control, play a .wav file, score the player's hand after each draw, and test for a bust.

The code cannot allow the player to draw more than three cards while the player's score is less than twenty-one.

If the player busts, then the dealer's cards are shown and score calculated before the hand is ended.

The program must test if the deck needs reshuffling after each draw.

• After the player stands on a score of twenty-one or less, then the dealer draws cards until its score is sixteen or higher. The procedure that handles this task will have to load the card images, play a .wav file, score the dealer's hand, increment the deck array variable, and test if the deck must be shuffled after each draw. The dealer cannot draw more than three cards.

• A hand ends when either the player busts or the dealer finishes drawing cards. When the hand ends the program must test to see who won or if the hand is a push, then output the result to a Label control. The player's new balance is written to a Label control (win or lose) and the results of the hand are written to the worksheet.

• Results are cleared when the public procedure attached to the Command Button control labeled 'Clear' (located on the worksheet) is executed.

Excel vba functions

• The game ends when the player closes the Blackjack form. This triggers the QueryClose() event of the UserForm object where the program removes the forms from system memory and ends the program.

• A majority of the code is entered in the code module for the Blackjack form. The remaining code is contained in the code module for the Shuffling form and two standard code modules.

Was this article helpful?