Blackjack Game

Divider line

The basic premise of the game is that you want to have a hand value that is closer to 21 than that of the dealer, without going over 21. Other players at the table are of no concern. Your hand is strictly played out against the hand of the dealer.
In blackjack, the cards are valued as follows:

  1. An Ace can count as either 1 or 11 (so as to avoid going over 21)
  2. The cards from 2 through 9 are valued at their face value.
  3. The 10, Jack, Queen, and King are all valued at 10
You can implement the dealer in two ways:
  1. The dealer is essentially a second player in your app, with the same type of controls as player 1
  2. The dealer is the computer (all actions are automatic)
You should create a simple version of the game, i.e. one deck of cards, closest to 21 wins. No other rules are important for this assignment (though you may wish to add extra functionality). Once someone wins, the game should be reset.

Exercise

Divider line

Given the rules of the game, I set out to design a program that would be more realistic to the game, while maintaining the basic rules given. The project was built on a step-by-step progress; prior to starting the game, I draw a list of things that needed to be accomplished and then a list of items that could be nice to have.
Given that I needed to build a Blackjack game, I created the following list;

The List;

  1. Given that I needed to build a Blackjack game, I created the following list;
  2. Create the playing screen
  3. Create a playing deck of cards and shuffle the values
  4. Play the first hand (2 cards for dealer, one face down and 2 for player)
  5. Create a player
  6. Create a dealer
  7. Calculate values (figures are 10, ACE is 1 or 11, depending on total value)
  8. Allow for Hit (one more card) and Stand (dealer play cards)
  9. Understand values and select a winner for the game
  10. Allow for a Double bet
  11. Allow for a Split bet
  12. Add sounds to the application

Create The Playing Screen;

The screen was designed using a top-down approach and all the buttons to play the game have been located at the lower part of the screen. This approach allows the user to use one hand to control the phone and to use the thumb as the finger to click the buttons. The buttons and the display information is presented on two columns an two rows, thus the important information is all kept to the left of the screen with other information presented on the right of the screen.

Create a Playing Deck of Cards and Shuffle the Values

I downloaded the card deck from the internet1 and I selected the file HUGE collection of playing cards2 ; from the selection, I decided cardset-gdkcard-bonded was the best choice and I used back101 as the background. To create the playing deck, I created 4 arrays, once per suit, containing all the values; then I shuffled each suite, add the suites to the playing deck and finally shuffle the entire deck.

Play the first hand
(2 cards for dealer, first face down, and 2 cards for player)

A special class was created to play the first hand; this was done because on the game, the dealer keeps the first card down. The program is able to draw a card from the deck, store the value and the card and present only the back to the user, the function then pulls 3 more cards. At this point we check the user hand, if an ACE and a figure were drawn, then a Blackjack message and music are presented and the game ends; if the user has an ACE and a 10, then a Win message and music are presented and the game ends.

Create a Player

The player instance keeps track of the cards given to the player and the values of the cards. The player actions are controlled with the buttons on the screen; the player can click on Hit to get one more card; on Stand, to stop playing the hand; on Double, to receive on more card and stand or on Split to play two hands when an identical pair3 of cards is drawn on the first hand.

Create a Dealer

The dealer instance plays the dealer hand and uses the option “Dealer Stands at 17”; the dealer will draw a card unless the value on the table is 17 or higher. Calculate values (figures are 10, ACE is 1 or 11, depending on total value)

Allow for Hit (one more card) and Stand (dealer play cards)

The Hit button is very simple; it takes a card from the deck, displays the card on the screen and adds the value to the total, then it compares to make sure the total value is below 21. If the value is greater than 21and the hand has an Ace, it downgrades de Ace to a value of 1 and re-adjusts the total value; if there is no Ace cards on the hand, the system ends the game with the player loosing. The Stand button does most of what the Hit button does, but this time is the dealer’s hand the one being played; the system is configure to play on the rule Dealer stands on all 17’s4 This part of the code analyses the value for both players to find who wins, if the dealer’s total value is over 21, then the player wins, if both the dealer and the player are under 21, then the closes to 21 wins and if there is a tie on the values, the player wins.

Allow for Double

The Double option allows the player to double the initial bet but only drawing one more card from the deck. Unlike the normal hand, when the player can press on Hit as many times as needed; on this bet, the code simply pulls one card from the deck, check the player is not over 21 and then “soft” clicks on the Stand button to call this code.

Allow for a Split bet

The Split button allows the user to split one hand and play two hands, as long as there 2 initial cards have the same value. All the figures were counted as having the same value, but a 10 and a figure are not good for a split. The code looks at the cards and if allowed, it will split the hands, then the code uses the defined buttons of Hit and Stand to play the initial hand, once the initial hand is lots or the customer stands, the program displays a message and allows the user to play the “split” hand. The “split” hand then is played as a normal hand using the Hit and Stand buttons. The system them compares the results of the 3 hands, and displays messages on regards of the winner. The player can win the Original hand, the Split hand or both hands; the dealer only wins if the player loses both hands.

Add sounds to the application

The last step to complete the game experience was to add sounds; there are four sounds added to the game; when the cards are shuffled, when the player wins, when the dealer wins and when the player gets a Blackjack.

Notes

1http://freeware.esoterica.free.fr/html/freecards.html
2http://ftp.debian.org/debian/pool/main/p/pysol-cardsets/pysol-cardsets_4.40.orig.tar.gz
3An identical pair means all figures are the same, so J-Q, J-K, Q-K can be split; 10 and a figure can’t be split
4http://www.blackjackinfo.com/blackjack-rules.php