This is an old revision of the document!
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.
Here is how the game is played:
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--; } }
In a standard 52 card deck, there are
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.
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.