Table of Contents

Corning Community College

CSCS2330 Discrete Structures

PROJECT: Black Jack Math continued, part 2 (BJM1)

OBJECTIVE

Take another week to polish your Blackjack implementation.

documentation page linked is the bjm0 one.

EDIT

You will want to go here to edit and fill in the various sections of the document:

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

Shuffle Cards and Values

In past documentation pages we implemented a way to shuffle cards but that way made it hard/impossible to assign values to your cards. This way fixes that. We are still using the same logic but are calling more arrays in the Fisher-Yates shuffle algorithm.

The following 2 arrays are needed:

// Values to be assigned to each card
int[52] cardValues = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
                      2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
 
 
// All cards before they are randomized
int[52] cardOrder = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
                     16,17,18,19,20,21,22,23,24,25,26,27,28,
                     29,30,31,32,33,34,35,36,37,38,39,40,41,
                     42,43,44,45,46,47,48,49,50,51,52};

Once you have created your arrays you will want to make the following for loop:

// Fisher-Yates shuffle algorithm
for (int i = 51; i > 0; i--)
{
    // Generate a random index between 0 and i
    int j = rand() % (i + 1);
 
    // Swaps cardOrder[i] and cardOrder[j], cardValues[i] and cardValues[j]
    int temp = cardOrder[i];
    int temp2 = cardValues[i];
    cardOrder[i] = cardOrder[j];
    cardValues[i] = cardValues[j];
    cardOrder[j] = temp;
    cardValues[j] = temp2;
}

In this version of the shuffle, we also shuffle the card values making it so they still line up and are assigned correctly. This version does not include suits but you can also do the same thing. Create an array of numbers 0-3. the numbers represent a suit so heart could be 0, diamond as 1, spade as 2, and clubs as 3.

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--;
            }
        }
Dealer Strategy

The dealer in blackjack always hits until hitting at least 17, and goes after the player does. A simple way of doing this is to out a check for if the player stands or busts towards the end of the loop, and set a flag if they do.

  if(stand == true || playerbust == true){}

Next you need a check to see if the dealer's cards already add up to 17 or greater, and if not then have them draw a card. You repeat this until the dealer has the correct value of cards, check if they bust or not, and if not then compare it to the player.

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

When dealing cards, make sure to decrement its point value from the probabilities array. (i.e. you draw the 5 of Spades, decrement deckProbabilities[4].) In the case of the house's face down card, do NOT decrement its probability! Since the player doesn't know what it is specifically, that card's value could still theoretically be pulled from the deck from the perspective of the player.

To calculate the player's bust chance, we'll need three more variables alongside the array:

int playerScore; // The current score of the player.
int bust;        // Counting number for the number of bust outcomes.
int total;       // Counting number for the number of total outcomes.

You'll need to loop through for each possible value stored in the deckProbabilities[] array. If the number stored added to the player's score is higher than 21, increment both bust and total. If the sum is still 21 or lower, only increment total. After every value has been checked, divide bust by total to get your percentage chance in decimal form.

 

SUBMISSION

To be successful in this project, the following criteria (or their equivalent) must be met:

Submit Tool Usage

Let's say you have completed work on the project, and are ready to submit, you would do the following:

lab46:~/src/SEMESTER/DESIG/PROJECT$ submit DESIG PROJECT file1 file2 file3 ... fileN

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

RUBRIC

I'll be evaluating the project based on the following criteria:

208:bjm1:final tally of results (208/208)
*:bjm1:functional blackjack game [104/104]
*:bjm1:probability or odds information displayed [104/104]

Pertaining to the collaborative authoring of project documentation

Additionally