11
\$\begingroup\$

Baccarat is a casino game where gamblers bet on whether one of two hands, the player or the banker, will have a sum closer to 9 (mod 10) or whether they will tie. It is played with four to eight standard decks of cards, without the jokers, shuffled together. Numbered cards count as their value, aces count as 1, and all face cards count as 10. A hand's total is the sum of the two or three cards within it, modulo ten. (So, a hand of [9 9 9] would have a total of 7, while a hand of [10 J Q] would have a total of 0.)

First the dealer deals two cards to each hand. Then play proceeds as follows:

  1. If the player has a total less than or equal to 5, they hit (draw a third card). Otherwise, they stand.
  2. If the player didn't draw a third card, the banker acts the same as the player: if the banker's total is less than or equal to 5, they hit. Otherwise, the banker stands.
  3. If the player did draw a third card, the banker acts according to the following table, based on their total and the card the player drew:
→ Player's third card →
↓ Banker total ↓
0 1 2 3 4 5 6 7 8 9
<3 H H H H H H H H H H
3 H H H H H H H H S H
4 S S H H H H H H S S
5 S S S H H H H H S S
6 S S S S S S H H S S
>6 S S S S S S S S S S

Your task is to write a program or function that implements that final step of the banker's logic in as few characters as possible.

You will be given two integers in the interval [0, 9] or [1, 10], whichever is more convenient for your language: the player's third card and the total of the banker's first two cards. If the entry in the above table says H, meaning that the banker should hit, output TRUE or equivalent. If the table says S, meaning that the banker should stand, output FALSE or equivalent.

\$\endgroup\$

8 Answers 8

8
\$\begingroup\$

JavaScript (ES6), 16 bytes

-2 bytes thanks to @Neil

Expects (b)(p) where \$b\in[0\dots9]\$ is the banker's total and \$p\in[0\dots9]\$ is the player's 3rd card. Returns a Boolean value.

b=>p=>~p%9<6-b*2

Try it online!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ b=>p=>-~p%9>b*2-6 works, I think? \$\endgroup\$
    – Neil
    Apr 7 at 23:56
  • \$\begingroup\$ @Neil Indeed. Nice one. \$\endgroup\$
    – Arnauld
    Apr 8 at 0:14
4
\$\begingroup\$

Google Sheets, 19 bytes

=mod(B1+1,9)>A1*2-6

Put the input \$[0...9]\$ in cells A1:B1 and the formula in cell C1. Returns true/false.

Used Arnauld's math, upgraded to Neil's.

baccarat.png

\$\endgroup\$
4
  • \$\begingroup\$ I think I was able to golf @Arnauld's answer, which might help you too. \$\endgroup\$
    – Neil
    Apr 7 at 23:57
  • \$\begingroup\$ @Neil thanks. In spreadsheets, bit manipulation isn't done by operators but functions with verbose names like bitlshift() so decimal math golfs better. \$\endgroup\$ Apr 8 at 6:51
  • \$\begingroup\$ -~p is just p+1 but written in a way that it has higher precedence than %. \$\endgroup\$
    – Neil
    Apr 8 at 6:54
  • \$\begingroup\$ @Neil d'oh. Upgraded. \$\endgroup\$ Apr 8 at 8:10
3
\$\begingroup\$

Charcoal, 9 bytes

›⁵⁻⊗NΣ⁺²N

Try it online! Link is to verbose version of code. Takes the banker's total as the first input and the player's third card as the second input and outputs a Charcoal boolean, i.e. - for hit, nothing for stand. Explanation:

 ⁵          Literal integer `5`
›           Is greater than
    N       Banker's total
   ⊗        Doubled
  ⁻         Subtract
       ²    Literal integer `2`
      ⁺     Plus
        N   Player's third card
     Σ      Digital sum
            Implicitly print

13 bytes to output the whole table:

Eχ⭆χ›⁵⁻⊗ιΣ⁺²λ

Try it online! Link is to verbose version of code.

\$\endgroup\$
2
  • \$\begingroup\$ The JavaScript answer has found that it can be done in just 10 characters, ~p%9<6-b*2. Would a Charcoal adaptation of that function be shorter? \$\endgroup\$
    – Purple P
    Apr 8 at 2:00
  • \$\begingroup\$ @PurpleP I think it would be ›⁶⁻⊗N﹪⊕N⁹ so the same length. \$\endgroup\$
    – Neil
    Apr 8 at 2:12
3
\$\begingroup\$

Jelly,  12 9  7 bytes

-3 thanks to Unrelated String (short-circuiting some of my arithmetic with DS)

+52DSH>

A dyadic Link that accepts the player's card value (0-9) on the left and the banker's current score (0-9) on the right and yields 1 for a hit and 0 for a stick.

Try it online! Or see the table.

How?

+52DSH> - Link: integer from [0-9], P; integer from [0-9], B
+52     - P plus fifty-two -> P+52
   D    - decimal digits -> [5 + (P>=8), (P+52) mod 10]
    S   - sum -> (P+52) mod 10 + 5 + (P>=8)
     H  - halve -> ((P+52) mod 10 + 5 + (P>=8)) / 2
      > - is greater than B?
\$\endgroup\$
10
  • 1
    \$\begingroup\$ -2 \$\endgroup\$ Apr 7 at 21:15
  • 1
    \$\begingroup\$ ...Actually, the d⁵ can just be D, for 9 bytes \$\endgroup\$ Apr 7 at 21:15
  • 1
    \$\begingroup\$ Smart, thanks @UnrelatedString! \$\endgroup\$ Apr 7 at 21:20
  • 1
    \$\begingroup\$ You're welcome! +2d⁵H‘SḞ> is a mildly interesting variation that ties 9, if there's anything more that can be done with that. \$\endgroup\$ Apr 7 at 21:23
  • \$\begingroup\$ You seem to have an off-by-1 error, did you mean to add 3 before comparing to B? Also your table should probably not have a copy of the (erroneous) output in its input area. \$\endgroup\$
    – Neil
    Apr 7 at 23:12
1
\$\begingroup\$

05AB1E, 8 7 bytes

52+SO;‹

Port of @JonathanAllan's Jelly answer.
(See edit history for three different 8-bytes approach; all ports of other answers.)

Inputs in the order \$p,b\$, both in the range \$[0,9]\$. Outputs 1/0 for H/S respectively.

Try it online or verify all test cases (as grid).

Explanation:

52+      # Increase the (implicit) input-integer `p` by 52
   SO    # Sum its digits together
     ;   # Halve it
      ‹  # Check whether the (implicit) input-integer `b` is smaller than it
         # (after which it is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 39 bytes

^.
$&$&
$
2
.
$*
.+$
$.&4
.
$*
^(.*)¶\1

Try it online! Takes input as banker's total and player's third card on separate lines but link is to test suite that generates the whole table (remove header and footer to run an individual test). Explanation:

^.
$&$&

"Double" the banker's total.

$
2

Append a 2 to the player's total.

.
$*

Convert to unary, taking the digital sum.

.+$
$.&4

Convert the player's total back to decimal and append a 4.

.
$*

Convert it to unary, taking the digital sum again.

^(.*)¶\1

Check that the first value does not exceed the second.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 20 bytes

->b,p{p+7-p/8*9>b*2}

Try it online!

Another twist of the same basic formula that almost everybody is using.

\$\endgroup\$
0
\$\begingroup\$

Forth (gforth), 28 bytes

Uses a variation of the same formula as most of the other answers.

Takes the top two stack arguments, where top is Player's third hit and second is Banker's total. Leaves answer on stack, -1 for true and 0 for false.

: f 1+ 9 mod swap 2* 6 - > ;

Try it online!

Explanation

: f     \ Start new word definition
  1+    \ Add 1 to top stack argument (p)
  9 mod \ Get the result modulo 9
  swap  \ swap the first (p) and second (b) stack arguments
  2*    \ multiple top stack argument (b) by 2
  6 -   \ subtract 6
  >     \ check whether 2nd stack argument is greater than first
;       \ End word definition
\$\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.