User Tools

Site Tools


notes:discrete:fall2023:projects:bjm0

This is an old revision of the document!


Table of Contents

BJM0

RULES

The primary objective of blackjack is to beat the dealer by having a hand value closer to 21 without exceeding it. If your hand goes over 21, you “bust” and lose the game. If the dealer busts and you don't, you win.

  • Number cards (2-10) are worth their face value.
  • Face cards (Jack, Queen, King) are worth 10 points each.
  • Aces can be worth 1 or 11 points, if an 11 would cause your hand to bust; it is counted as a 1, if not it is counted as a 11.

Here is how the game is played:

  1. Each player, including the dealer, is initially dealt two cards. (the dealer has one faced down and the other up)
  2. Players can choose to “hit” (take another card) or “stand” (keep their current hand).
  3. Players can continue to hit until they decide to stand, or until they bust by exceeding 21.
  4. After all players have finished their turns, the dealer reveals their facedown card.
  5. The dealer must hit until their hand reaches at least 17 points.
  6. If the dealer busts, all remaining players win.
  7. If the dealer doesn't bust, then the hands of all remaining players are compared to the dealer's hand, and the players with a hand larger than the dealer but lower than 22 wins.

GAMEPLAY

Ace Logic

One of the unique aspects of Black Jack is that an Ace can either be 11 or 1 depending on what the player decides. Here is an example of a hand involving an Ace.

Example 1: The player is dealt a 5 and an Ace. they then draw and get a King. If the player decided to keep the Ace an 11 then they would bust and the dealer would win but you can switch the Ace to 1 and not bust.

The challenging aspect of this is how would you implement this so the game automatically handles this for the player.

To start you need to create two int variables, one to keep track of the current score of the player and one to keep track of how many aces are dealt to the player. Note: Ace is originally set to 11 because starting with the highest value the ace can be is easier and requires less testing.

int PlayerScore = 0;
int NumberOfAces = 0;

Next, inside your button logic for asking for another card from the dealer use the following if statements:

        // If X is pressed than next card is shown and the value of that card is added to PlayerScore
        if (hit) {
            PlayerScore+= card->value; // Random card that is displayed is added to PlayerScore
 
            // Logic for handling what value ace should be. if it should be 1 or 11
            if (card->value == 11)
            {
                 NumberOfAces++;
            }
 
            if (PlayerScore > 21 && NumberOfAces > 0)
            {
                 PlayerScore -= 10;
                 NumberOfAces--;
            }
        }

CHANCES

In a standard 52 card deck, there are

  • 4 aces
  • 16 cards with the value of 10
  • 32 cards with values from 2-9.

The probability of getting Blackjack for the first 2 cards dealt accounts for all the possible combinations of 2 cards and the cases which the 2 cards add to 21.

Calculating Bust Probabilities:

To calculate probabilities for a bust, between both the player and the house, you'll need a way to compare against all possible outcomes. You'll need a way to store these potential outcomes that isn't the deck itself. Either an array or list should work, but for this example we'll use an array.

Each slot in the array will represent a drawn card's potential point value. Since Aces can be either 1 or 11, but we're calculating the probability of a bust, we only need to count Aces as 1s. So in total we'll have 10 array slots, and the number stored in each slot is the number of cards with that point value theoretically stored in the deck.

int[ 10 ] deckProbabilities

After

notes/discrete/fall2023/projects/bjm0.1698245441.txt.gz · Last modified: 2023/10/25 14:50 by walley