Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ---
  2. title: No-bust strategy
  3. ...
  4. # No-bust strategy
  5. > Difficulty: 05/100
  6. 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):
  7. ```terminal
  8. mkfifo fifo
  9. ```
  10. Then we execute `blackjack`, piping its output to the player (say `no-bust.pl`) and reading the standard input from `fifo`, whilst at the same time we redirect the player's standard output to `fifo`:
  11. ```terminal
  12. rm -f fifo; mkfifo fifo
  13. blackjack -n1e5 < fifo | ./no-bust.pl > fifo
  14. ```
  15. 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:
  16. ```perl
  17. #!/usr/bin/perl
  18. # this is needed to avoid deadlock with the fifo
  19. STDOUT->autoflush(1);
  20. while ($command ne "bye") {
  21. # do not play more than a number of commands
  22. # if the argument -n was not passed to blackjack
  23. if ($i++ == 1234567) {
  24. print "quit\n";
  25. }
  26. # read and process the commands
  27. chomp($command = <STDIN>);
  28. if ($command eq "bet?") {
  29. print "1\n";
  30. } elsif ($command eq "insurance?") {
  31. print "no\n";
  32. } elsif ($comm eq "play?") {
  33. @tokens = split(/ /, $command);
  34. if ($tokens[1] < 12) {
  35. print "hit\n";
  36. } else {
  37. print "stand\n";
  38. }
  39. }
  40. }
  41. ```
  42. The very same player may be implemented as a shell script:
  43. ```bash
  44. #!/bin/sh
  45. i=0
  46. while read command
  47. do
  48. i=$((i+1))
  49. if test ${i} -ge 12345; then
  50. echo "quit"
  51. elif test "${command}" = 'bye'; then
  52. exit
  53. elif test "${command}" = 'bet?'; then
  54. echo 1
  55. elif test "${command}" = 'insurance?'; then
  56. echo "no"
  57. elif test "$(echo ${command} | cut -c-5)" = 'play?'; then
  58. count=$(echo ${command} | cut -f2 -d" ")
  59. if test ${count} -lt 12; then
  60. echo "hit"
  61. else
  62. echo "stand"
  63. fi
  64. fi
  65. done
  66. ```
  67. To check these two players give the same results, make them play against Libre Blackjack with the same seed (say one) and send the YAML report to two different files:
  68. ```terminal
  69. blackjack -n1e5 --rng_seed=1 --report_file_path=perl.yml < fifo | ./no-bust.pl > fifo
  70. blackjack -n1e5 --rng_seed=1 --report_file_path=shell.yml < fifo | ./no-bust.awk > fifo
  71. diff perl.yml shell.yml
  72. ```
  73. 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.
  74. > **Exercise:** modify the players so they always insure aces and see if it improves or degrades the result.
  75. -------
  76. :::{.text-center}
  77. [Previous](../02-always-stand) | [Index](../) | [Next](../08-mimic-the-dealer)
  78. :::