You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.7KB

5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ---
  2. title: Mimic the dealer
  3. ...
  4. # Mimic the dealer
  5. > Difficulty: 08/100
  6. This example implements a “mimic-the-dealer strategy,” i.e. hits if the hand totals less than seventeen and stands on eighteen or more. The player stands on hard seventeen but hits on soft seventeen.
  7. This time, the configuration file `blackjack.conf` is used. If a file with this name exists in the directory where `blackjack` is executed, it is read and parsed. The options should be fairly self descriptive. See the [configuration file] section of the manual for a detailed explanation of the variables and values that can be entered. In particular, we ask to play one hundred thousand hands at a six-deck game where the dealer hits soft seventeens. If the random seed is set to a fixed value so each execution will lead to the very same sequence of cards.
  8. Now, there are two options that tell Libre Blackjack how the player is going to talk to the backend: `player2dealer` and `dealer2player`. The first one sets the communication mechanism from the player to the dealer (by default is `blackjack`’s standard input), and the second one sets the mechanism from the dealer to the player (by default `blackjack`’s standard output). In this case, the configuration file reads:
  9. ```ini
  10. hands = 1e5
  11. decks = 6
  12. hit_soft_17 = 1
  13. # uncomment to obtain the same cards each time
  14. # rng_seed = 1
  15. player2dealer = fifo mimic_p2d
  16. dealer2player = fifo mimic_d2p
  17. buffered_fifo = 1
  18. ```
  19. This means that two FIFOs (a.k.a. named pipes) are to be used for communication, `player2dealer` from the player to the dealer and `dealer2player` for the dealer to the player. If these FIFOs do not exist, they are created by `blackjack` upon execution.
  20. The player this time is implemented as an awk script, whose input should be read from `dealer2player` and whose output should be written to `player2dealer`. To run the game, execute `blackjack` in one terminal making sure the current directory is where the `blackjack.conf` file exists. It should print a message telling that it is waiting for someone to be at the other side of the named pipes:
  21. ```terminal
  22. $ blackjack
  23. [...]
  24. waiting for dealer2player buffered fifo 'dealer2player'...
  25. ```
  26. In another terminal run the player
  27. ```terminal
  28. $ ./mimic-the-dealer.awk < dealer2player > player2dealer
  29. ```
  30. Both dealer and player may be run in the same terminal putting the first one on the background:
  31. ```terminal
  32. rm -f mimic_d2p mimic_p2d
  33. mkfifo mimic_d2p mimic_p2d
  34. blackjack &
  35. gawk -f mimic-the-dealer.awk < mimic_d2p > mimic_p2d
  36. ```
  37. To understand the decisions taken by the player, we have to remember that when Libre Blackjack receives the command `count` asking for the current player's count, it returns a positive number for hard hands and a negative number for soft hands. The instructions `fflush()` are needed in order to avoid deadlocks on the named pipes:
  38. ```awk
  39. #!/usr/bin/gawk -f
  40. function abs(x){return ( x >= 0 ) ? x : -x }
  41. /bet\?/ {
  42. print "1";
  43. fflush();
  44. }
  45. /insurance\?/ {
  46. print "no";
  47. fflush();
  48. }
  49. /play\?/ {
  50. count = $2
  51. # mimic the dealer: hit until 17 (hit soft 17)
  52. if (abs(count) < 17 || count == -17) { # soft hands are negative
  53. print "hit";
  54. } else {
  55. print "stand";
  56. }
  57. fflush();
  58. }
  59. /bye/ {
  60. exit;
  61. }
  62. ```
  63. ```yaml
  64. ---
  65. rules:
  66. decks: 6
  67. hands: 100000
  68. hit_soft_17: 1
  69. double_after_split: 1
  70. blackjack_pays: 1.5
  71. rng_seed: -1448949563
  72. number_of_burnt_cards: 0
  73. no_negative_bankroll: 0
  74. max_bet: 0
  75. penetration: 0.75
  76. penetration_sigma: 0.05
  77. cpu:
  78. user: 0.631905
  79. system: 1.19576
  80. wall: 2.15273
  81. second_per_hand: 2.2e-05
  82. hands_per_second: 4.6e+04
  83. player:
  84. wins: 0.41044
  85. pushes: 0.09695
  86. losses: 0.49261
  87. dealer_blackjacks: 0.04658
  88. player_blackjacks: 0.04663
  89. dealer_busts: 0.18984
  90. player_busts: 0.27268
  91. doubled_hands: 0
  92. doubled_wins: 0
  93. insured_hands: 0
  94. insured_wins: 0
  95. number_of_hands: 100000
  96. number_of_shuffles: 2326
  97. total_money_waged: 100000
  98. worst_bankroll: -5996.5
  99. final_bankroll: -5994.5
  100. return: -0.059945
  101. variance: 0.955017
  102. deviation: 0.97725
  103. error: 0.00309034
  104. result: "(-6.0 ± 0.6) %"
  105. ...
  106. ```
  107. > **Exercise:** modify the player and the configuration file so both the dealer and the player may stand on soft seventeen. Analyze the four combinations (player h17 - dealer h17, player h17 - dealer s17, player s17 - dealer h17, player s17 - dealer s17)
  108. -------
  109. :::{.text-center}
  110. [Previous](../05-no-bust) | [Index](../) | [Next](../20-basic-strategy)
  111. :::