No-bust strategy

No-bust strategy

Difficulty: 05/100

This directory shows how to play a “no-bust” strategy, i.e. not hitting any hand higher or equal to hard twelve with Libre Blackjack. The communication between the player and the back end is through standard input and output. The player reads from its standard input Libre Blackjack’s commands and writes to its standard output the playing commands. In order to do this a FIFO (a.k.a. named pipe) is needed. So first, we create it (if it is not already created):

mkfifo fifo

Then we execute blackjack, piping its output to the player (say and reading the standard input from fifo, whilst at the same time we redirect the player’s standard output to fifo:

if test ! -e fifo; then
 mkfifo fifo
blackjack -n1e5 < fifo | ./ > fifo

As this time the player is coded in an interpreted langauge, it is far smarter than the previous yes-based player. So the player can handle bets and insurances, and there is not need to pass the options --flat_bet nor --no_insurance (though they can be passed anyway). Let us take a look at the Perl implementation:

# this is needed to avoid deadlock with the fifo

while ($command ne "bye") {
  # do not play more than a number of commands
  # if the argument -n was not passed to libreblackjack
  if ($i++ == 123456789) {
    print "quit\n";
  # read and process the commands
  chomp($command = <STDIN>);
  if ($command eq "bet?") {
    print "1\n";
  } elsif ($command eq "insurance?") {
    print "no\n";
  } elsif ($comm eq "play?") {
    print "count\n";
    chomp($count = <STDIN>); # the count
    chomp($play = <STDIN>);  # again the "play?" query
    if ($count < 12) {
      print "hit\n";
    } else {
      print "stand\n";

The very same player may be implemented as a shell script:


while read command
  if test "${command}" = 'bye'; then
  elif test "${command}" = 'bet?'; then
    echo 1  
  elif test "${command}" = 'insurance?'; then
    echo "no"
  elif test "`echo ${command} | cut -c-5`" = 'play?'; then
    echo "count"
    read count
    read play      # libreblackjack will ask again for 'play?'
    if test ${count} -lt 12; then
      echo "hit"
      echo "stand"

To check these two players give the same results, make them play agains libreblackjack with the same seed (say one) and send the YAML report to two different files:

blackjack -n1e3 --rng_seed=1 --yaml_report=perl.yml \
    < fifo | ./ > fifo
blackjack -n1e3 --rng_seed=1 --yaml_report=shell.yml \
    < fifo | ./ > fifo
diff perl.yml shell.yml 

<   user:             0
<   system:           0.022603
<   wall:             0.034317
<   second_per_hand:  3.4e-05
<   hands_per_second: 2.9e+04
>   user:             0.06838
>   system:           0.13676
>   wall:             11.1446
>   second_per_hand:  1.1e-02
>   hands_per_second: 9.0e+01

As expected, the reports are the same. They just differ in the speed because the shell script is orders of magnitude slower than its Perl-based counterpart.

Exercise: modifiy the players so they always insure aces and see if it improves or degrades the result.