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.

386 line
10KB

  1. #!/bin/bash
  2. for i in grep cut bc awk; do
  3. if [ -z "$(which $i)" ]; then
  4. echo "error: $i not installed"
  5. exit 1
  6. fi
  7. done
  8. debug=0
  9. declare -A strategy
  10. declare -A ev
  11. declare -A min
  12. min["hard"]=4 # from 20 to 4 in hards
  13. min["soft"]=12 # from 20 to 12 in softs
  14. rm -f hard.html soft.html pair.html
  15. # --------------------------------------------------------------
  16. # start with standing
  17. cp hard-stand.txt hard.txt
  18. cp soft-stand.txt soft.txt
  19. cat << EOF > table.md
  20. | Hand | \$n\$ | Stand | Double | Hit |
  21. | ---- | ----- | ----- | ------ | --- |
  22. EOF
  23. for type in hard soft; do
  24. for hand in $(seq 20 -1 ${min[${type}]}); do
  25. # choose two random cards that make up the player's assumed total
  26. if [ ${type} = "hard" ]; then
  27. t="h"
  28. card1=11
  29. card2=11
  30. while test $card1 -gt 10 -o $card2 -gt 10; do
  31. card1=$((${RANDOM} % (${hand}-3) + 2))
  32. card2=$((${hand} - ${card1}))
  33. done
  34. elif [ ${type} = "soft" ]; then
  35. t="s"
  36. # one card is an ace
  37. card1=1
  38. card2=$((${hand} - 10 - ${card1}))
  39. fi
  40. cat << EOF >> ${type}.html
  41. <tr>
  42. <td>${t}${hand}</td>
  43. <td>
  44. <div class="text-right">s<span class="d-none d-lg-inline">tand</span></div>
  45. <div class="text-right">h<span class="d-none d-lg-inline">it</span></div>
  46. <div class="text-right">d<span class="d-none d-lg-inline">ouble</span></div>
  47. </td>
  48. EOF
  49. for upcard in $(seq 2 9) T A; do
  50. if [ "x$upcard" = "xT" ]; then
  51. upcard_n=10
  52. elif [ "x$upcard" = "xA" ]; then
  53. upcard_n=1
  54. else
  55. upcard_n=$(($upcard))
  56. fi
  57. n=10000 # start with n hands
  58. best="x" # x means don't know what to so, so play
  59. while [ "${best}" = "x" ]; do
  60. # tell the user which combination we are trying and how many we will play
  61. echo -n ${t}${hand}-${upcard} \($card1 $card2\) "n="${n}
  62. for play in s d h; do
  63. # start with options.conf as a template and add some custom stuff
  64. cp options.conf blackjack.conf
  65. cat << EOF >> blackjack.conf
  66. hands = ${n}
  67. player = internal
  68. arranged_cards = ${card1}, $((${upcard_n} + 13)), $((${card2} + 26))
  69. report = ${t}${hand}-${upcard}-${play}.yaml
  70. #log = ${t}${hand}-${upcard}-${play}.log
  71. EOF
  72. # read the current strategy
  73. while read w p2 p3 p4 p5 p6 p7 p8 p9 pT pA; do
  74. # w already has the "h" or the "s"
  75. strategy[${w},2]=$p2
  76. strategy[${w},3]=$p3
  77. strategy[${w},4]=$p4
  78. strategy[${w},5]=$p5
  79. strategy[${w},6]=$p6
  80. strategy[${w},7]=$p7
  81. strategy[${w},8]=$p8
  82. strategy[${w},9]=$p9
  83. strategy[${w},T]=$pT
  84. strategy[${w},A]=$pA
  85. done < ${type}.txt
  86. # override the read strategy with the explicit play: s, d or h
  87. strategy[${t}${hand},${upcard}]=${play}
  88. # save the new (temporary) strategy
  89. rm -f ${type}.txt
  90. for h in $(seq 20 -1 ${min[${type}]}); do
  91. echo -n "${t}${h} " >> ${type}.txt
  92. # extra space if h < 10
  93. if [ ${h} -lt 10 ]; then
  94. echo -n " " >> ${type}.txt
  95. fi
  96. for u in $(seq 2 9) T A; do
  97. echo -n "${strategy[${t}${h},${u}]} " >> ${type}.txt
  98. done
  99. echo >> ${type}.txt
  100. done
  101. # debug, comment for production
  102. if [ "${debug}" != "0" ]; then
  103. cp ${type}.txt ${t}${hand}-${upcard}-${play}.str
  104. fi
  105. # ensamble the full bs.txt with no pairing
  106. cat hard.txt soft.txt pair-no.txt > bs.txt
  107. # play!
  108. blackjack
  109. # evaluate the results
  110. ev[${t}${hand},${upcard},${play}]=$(grep mean ${t}${hand}-${upcard}-${play}.yaml | awk '{printf("%+g", $2)}')
  111. error[${t}${hand},${upcard},${play}]=$(grep error ${t}${hand}-${upcard}-${play}.yaml | awk '{printf("%.1g", $2)}')
  112. done
  113. # choose the best one
  114. ev_s=$(printf %g ${ev[${t}${hand},${upcard},s]})
  115. ev_d=$(printf %g ${ev[${t}${hand},${upcard},d]})
  116. ev_h=$(printf %g ${ev[${t}${hand},${upcard},h]})
  117. if [ $n -le 9999999 ]; then
  118. # if we still have room, take into account errors
  119. error_s=${error[${t}${hand},${upcard},s]}
  120. error_d=${error[${t}${hand},${upcard},d]}
  121. error_h=${error[${t}${hand},${upcard},h]}
  122. else
  123. # instead of running infinite hands, above a threshold asume errors are zero
  124. error_s=0
  125. error_d=0
  126. error_h=0
  127. fi
  128. echo -ne "\ts=${ev_s} (${error_s})"
  129. echo -ne "\td=${ev_d} (${error_d})"
  130. echo -ne "\th=${ev_h} (${error_h})"
  131. if (( $(echo "${ev_s}-${error_s} > ${ev_d}+${error_d}" | bc -l) )) &&
  132. (( $(echo "${ev_s}-${error_s} > ${ev_h}+${error_h}" | bc -l) )); then
  133. best="s"
  134. echo -e "\tstand"
  135. elif (( $(echo "${ev_d}-${error_d} > ${ev_s}+${error_s}" | bc -l) )) &&
  136. (( $(echo "${ev_d}-${error_d} > ${ev_h}+${error_h}" | bc -l) )); then
  137. best="d"
  138. echo -e "\tdouble"
  139. elif (( $(echo "${ev_h}-${error_h} > ${ev_s}+${error_s}" | bc -l) )) &&
  140. (( $(echo "${ev_h}-${error_h} > ${ev_d}+${error_d}" | bc -l) )); then
  141. best="h"
  142. echo -e "\thit"
  143. else
  144. best="x"
  145. n=$((${n} * 10))
  146. echo -e "\tuncertain"
  147. fi
  148. done
  149. strategy[${t}${hand},${upcard}]=${best}
  150. # echo "| ${t}${hand}-${upcard} | ${n} | ${ev_s} (${error_s}) | ${ev_h} (${error_h}) | ${ev_d} (${error_d}) |" >> table.md
  151. #
  152. # echo " <!-- ${upcard} -->" >> ${type}.html
  153. # echo " <td>" >> ${type}.html
  154. # echo ${ev_s} ${error_s} | awk -f cell.awk >> ${type}.html
  155. # echo ${ev_h} ${error_h} | awk -f cell.awk >> ${type}.html
  156. # echo ${ev_d} ${error_d} | awk -f cell.awk >> ${type}.html
  157. # echo " </td>" >> ${type}.html
  158. # save the strategy again with the best strategy
  159. rm -f ${type}.txt
  160. for h in $(seq 20 -1 ${min[${type}]}); do
  161. echo -n "${t}${h} " >> ${type}.txt
  162. # extra space if h < 10
  163. if [ ${h} -lt 10 ]; then
  164. echo -n " " >> ${type}.txt
  165. fi
  166. for u in $(seq 2 9) T A; do
  167. echo -n "${strategy[${t}${h},${u}]} " >> ${type}.txt
  168. done
  169. echo >> ${type}.txt
  170. done
  171. done
  172. echo "</tr>" >> ${type}.html
  173. done
  174. done
  175. exit
  176. cat << EOF >> table.md
  177. | Hand | \$n\$ | Yes | No |
  178. | ---- | ----- | ----- | ---- |
  179. EOF
  180. # --------------------------------------------------------------------
  181. # pairs
  182. type="pair"
  183. t="p"
  184. cp pair-no.txt pair.txt
  185. for hand in A T `seq 9 -1 2`; do
  186. if [ "${hand}" = "A" ]; then
  187. pair=1
  188. elif [ "${hand}" = "T" ]; then
  189. pair=10
  190. else
  191. pair=$((${hand}))
  192. fi
  193. cat << EOF >> ${type}.html
  194. <tr>
  195. <td>${t}${hand}</td>
  196. <td>
  197. <div class="text-right">y<span class="d-none d-lg-inline">es</span></div>
  198. <div class="text-right">n<span class="d-none d-lg-inline">o</span></div>
  199. </td>
  200. EOF
  201. for upcard in `seq 2 9` T A; do
  202. if [ "$upcard" = "T" ]; then
  203. upcard_n=10
  204. elif [ "$upcard" = "A" ]; then
  205. upcard_n=1
  206. else
  207. upcard_n=$(($upcard))
  208. fi
  209. n=1000 # start with n hands
  210. best="x" # x means don't know what to so, so play
  211. while [ "${best}" = "x" ]; do
  212. # tell the user which combination we are trying and how many we will play
  213. echo -n ${t}${hand}-${upcard} "n="${n}
  214. for play in y n; do
  215. # start with options.conf as a template and add some custom stuff
  216. cp options.conf blackjack.conf
  217. cat << EOF >> blackjack.conf
  218. hands = ${n}
  219. dealer2player = internal
  220. arranged_cards = ${pair} $((${upcard_n} + 13)) $((${pair} + 26))
  221. yaml_report = ${t}${hand}-${upcard}-${play}.yaml
  222. log = ${t}${hand}-${upcard}-${play}.log
  223. EOF
  224. # read the current strategy
  225. while read w p2 p3 p4 p5 p6 p7 p8 p9 pT pA; do
  226. # w already has the "p"
  227. strategy[${w},2]=$p2
  228. strategy[${w},3]=$p3
  229. strategy[${w},4]=$p4
  230. strategy[${w},5]=$p5
  231. strategy[${w},6]=$p6
  232. strategy[${w},7]=$p7
  233. strategy[${w},8]=$p8
  234. strategy[${w},9]=$p9
  235. strategy[${w},T]=$pT
  236. strategy[${w},A]=$pA
  237. done < ${type}.txt
  238. # override the read strategy with the explicit play: y or n
  239. strategy[${t}${hand},${upcard}]=${play}
  240. # save the new (temporary) strategy
  241. rm -f ${type}.txt
  242. for h in A T `seq 9 -1 2`; do
  243. echo -n "${t}${h} " >> ${type}.txt
  244. for u in `seq 2 9` T A; do
  245. echo -n "${strategy[${t}${h},${u}]} " >> ${type}.txt
  246. done
  247. echo >> ${type}.txt
  248. done
  249. if [ "${debug}" != "0" ]; then
  250. cp ${type}.txt ${t}${hand}-${upcard}-${play}.str
  251. fi
  252. # ensamble the full bs.txt
  253. cat hard.txt soft.txt pair.txt > bs.txt
  254. # play!
  255. blackjack > /dev/null
  256. # evaluate the results
  257. ev[${t}${hand},${upcard},${play}]=`grep return ${t}${hand}-${upcard}-${play}.yaml | awk '{printf("%+g", $2)}'`
  258. error[${t}${hand},${upcard},${play}]=`grep error ${t}${hand}-${upcard}-${play}.yaml | awk '{printf("%.1g", $2)}'`
  259. done
  260. # choose the best one
  261. ev_y=`printf %g ${ev[${t}${hand},${upcard},y]}`
  262. ev_n=`printf %g ${ev[${t}${hand},${upcard},n]}`
  263. if [ $n -le 999999 ]; then
  264. # if we still have room, take into account errors
  265. error_y=${error[${t}${hand},${upcard},y]}
  266. error_n=${error[${t}${hand},${upcard},n]}
  267. else
  268. # instead of running infinite hands, above a threshold asume errors are zero
  269. error_y=0
  270. error_n=0
  271. fi
  272. echo -ne "\ty=${ev_y} (${error_y})"
  273. echo -ne "\tn=${ev_n} (${error_n})"
  274. if (( $(echo "${ev_y}-${error_y} > ${ev_n}+${error_n}" | bc -l) )); then
  275. best="y"
  276. echo -e "\tyes"
  277. elif (( $(echo "${ev_n}-${error_n} > ${ev_y}+${error_y}" | bc -l) )); then
  278. best="n"
  279. echo -e "\tno"
  280. else
  281. best="x"
  282. n=$((${n} * 10))
  283. echo -e "\tuncertain"
  284. fi
  285. done
  286. echo "| ${t}${hand}-${upcard} | ${n} | ${ev_y} (${error_y}) | ${ev_n} (${error_n}) |" >> table.md
  287. echo " <!-- ${upcard} -->" >> ${type}.html
  288. echo " <td>" >> ${type}.html
  289. echo ${ev_y} ${error_y} | awk -f cell.awk >> ${type}.html
  290. echo ${ev_n} ${error_n} | awk -f cell.awk >> ${type}.html
  291. echo " </td>" >> ${type}.html
  292. strategy[${t}${hand},${upcard}]=${best}
  293. # save the strategy again with the best strategy
  294. rm -f ${type}.txt
  295. for h in A T `seq 9 -1 2`; do
  296. echo -n "${t}${h} " >> ${type}.txt
  297. for u in `seq 2 9` T A; do
  298. echo -n "${strategy[${t}${h},${u}]} " >> ${type}.txt
  299. done
  300. echo >> ${type}.txt
  301. done
  302. done
  303. done
  304. cat header.txt hard.txt header.txt soft.txt header.txt pair.txt > bs.txt
  305. if [ "${debug}" == "0" ]; then
  306. rm -f *.yaml
  307. rm -f *.str
  308. rm -f *.log
  309. fi