2
\$\begingroup\$

After the success of my baccarat challenge, I figured we should try a similar challenge for another casino game, blackjack.

Blackjack is a game played with two to eight decks of cards shuffled together. The object is to reach a hand that totals as close to 21 as possible, without going over. Numbered cards count as their value, while jacks, queens, and kings are all counted as 10. Aces are unique: they are counted as either 11 or 1, whichever puts the hand closest to 21 without going over. For example, a hand of [A 9] would be counted as 11 + 9 = 20, but a hand of [A 6 5] would be counted as 1 + 6 + 5 = 12, because 11 + 6 + 5 = 22 > 21. A hand in which an ace is counted as 11 is called a "soft" hand, while a hand in which no ace is counted as 11 is called a "hard" hand.

The dealer first deals two cards to each player and two cards to themselves. Then the dealer reveals one of their cards. The player always has the following two options:

  1. "Hit": draw another card.
  2. "Stand": stop drawing cards and pass play to the dealer.

On their first move of a hand, the player may also have the following options:

  1. "Double down": double their bet, after which they must hit one more card and stand.
  2. "Split": if both the player's cards have the same value, they may bet the same amount as they did on the original hand and split their hand into two separate hands. Each of these hands hits a second card and is thereafter treated independently of the other.
  3. "Surrender": lose the hand, but get back half their bet. Because not all casinos allow players to surrender, I have chosen to exclude it from this challenge, as it is one less possibility that answerers will have to account for.

Once the player has stood, the dealer reveals their second card. If the dealer's total is less than 17, or their total is a "soft" 17 (an ace and other cards that total 6), they hit until their total is greater than or equal to 17. If the player's total is greater than the dealer's total at this point, or the dealer's total is greater than 21, the player wins. If the player and the dealer have equal totals, they "push" (tie). Otherwise, the dealer wins.

There is an optimal move for every situation, based on the player's total and the card the dealer is showing. The chart below is the table of optimal moves for the most commonly played variant: four to eight decks, dealer hits on soft 17. The rows with A,[0-9] at the header are the "soft" hands with an ace counted as 11; the optimal moves for a hand of [A 6] are the same as those for a hand of [A 3 3]. There is no row for A,10 because that totals 21 and is called a "blackjack", which wins instantly.

Blackjack has many rule variants, and some of these variants affect what moves are optimal. These include:

  • The game may be played with 2 decks, or 4 to 8 decks.
  • The dealer may stand instead of hitting if they have a soft 17.
  • Players may not be allowed to double down after splitting.

Optimal moves affected by rule variants are noted in the chart.

→ Dealer showing →
↓ Player hand ↓
2 3 4 5 6 7 8 9 10 A
<9 H H H H H H H H H H
9 H1 Dh Dh Dh Dh H H H H H
10 Dh Dh Dh Dh Dh Dh Dh Dh S S
11 Dh Dh Dh Dh Dh Dh Dh Dh Dh Dh2
12 H H S S S H H H H H
13 S S S S S H H H H H
14 S S S S S H H H H H
15 S S S S S H H H H H
16 S S S S S H H H H H
17 S S S S S S S S S S
>17 S S S S S S S S S S
A,2 H H H Dh Dh H H H H H
A,3 H H H3 Dh Dh H H H H H
A,4 H H Dh Dh Dh H H H H H
A,5 H H Dh Dh Dh H H H H H
A,6 H Dh Dh Dh Dh H H H H H
A,7 Ds4 Ds Ds Ds Ds S S H H H
A,8 S S S S Ds4 S S S S S
A,9 S S S S S S S S S S
2,2 Ph Ph P P P P H H H H
3,3 Ph Ph P P P P H H H H
4,4 H H H Ph Ph H H H H H
5,5 Dh Dh Dh Dh Dh Dh Dh Dh S S
6,6 Ph5 P P P P H6 H H H H
7,7 P P P P P P H6 H H H
8,8 P P P P P P P P P P
9,9 P P P P P S P P S S
10,10 S S S S S S S S S S
A,A P P P P P P P P P P

Key:

  • H = Hit
  • S = Stand
  • Dh = Double down if possible, otherwise hit
  • Ds = Double down if possible, otherwise stand
  • P = Split
  • Ph = Split if double after split is allowed, otherwise hit

Footnotes:

1 If the game is being played with 2 decks, double down instead of stand.

2 If the dealer stands on a soft 17, and the game is being played with 4 to 8 decks, hit instead of double down.

3 If the dealer hits on a soft 17, the game is being played with 2 decks, and the player can double down, double down instead of hitting.

4 If the dealer stands on a soft 17, stand instead of double down.

5 If the game is being played with 2 decks, always split, regardless of whether double after split is allowed.

6 If the game is being played with 2 decks, these are a Ph: split if double after split is allowed, otherwise hit.


For this challenge, you must choose a number of decks and a set of rules. Please state in your answer the number of decks and set of rules you have chosen. You will be given as input:

  • The value of the card that the dealer is showing, which will be either an integer from 2 to 10, or some other value appropriate to your language that indicates an ace.
  • One of the following:
  • A list of the player's cards, which will consist of integers from 2 to 10 and whatever value is most convenient for you to represent an ace; or
  • An integer indicating the sum of the player's cards, a boolean indicating whether the hand is soft or hard; and a boolean indicating whether the player can double down or split.

Your program or function must output some consistent representation of the optimal move in that situation, given the rules you have chosen. You may output in any format you choose: for example, you could output the words "Hit", "Split", "Double", "Stand"; or you could output simply the characters H, S, D, P; or you could output 1 for "Hit", 2 for "Stand", 500 for "surrender" — whatever is most convenient for you.

If the length of the hand you are given is just two cards, you may assume that the player can hit, double down and (if both of its cards are the same value) split. In reality, if double after split is not allowed, a player could get a 2-card hand that they could not double down on.

There has been a previous version of this challenge, but it went unanswered because it asked for a full interactive console, allowed for only one set of rules, and provided a low-resolution chart. I hope that the flexibility and simplicity of this version will draw more answers.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Charcoal, 118 bytes

NθNη≔&⁺⁶NI§§⪪⎇N”|“.MPητe4d↔_ςζWQG8XG60d⊞&≦δ‽”⎇N”|“.,oW¿²*₂¶‴ι6⁰γ;)J×XSiI=φO3””|'φΣ3Xζ⎚⦃″.I?¬4⟲↧\`cMN{|E#-⁶¹T”χηθεI∨&ε¹ε

Try it online! Link is to verbose version of code. Does not allow double down after splitting. Takes input in the order Dealer's card, Player total, Double down allowed, Hand is soft, Split allowed and outputs 0 for hit, 1 for double down, 2 for stand or 4 for split. Due to the use of cycling indexing, dealer's card may be either 1 or 11 for an ace, but an ace in the player total is assumed to be 11 if possible (two aces should have a total of 12). Explanation:

NθNη

Input the dealer's card and player total.

≔&⁺⁶NI§§⪪⎇N”...”⎇N”...””...”χηθε

Masking out the least significant bit unless double down is allowed, look up the player total in either the ace table, the pair table, or the general table, depending on whether the hand is soft or split is allowed.

I∨&ε¹ε

If double down is possible then double down otherwise hit, stand or split as appropriate.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.