bj_rulz.py
'''\
Blackjack - Playing Rules.
Cards are integer values from 1 (Ace) to 10.
Rules:
0 - null
1 - stay
2 - hit
3 - double
4 - split pair
5 - bust
6 - push
7 - win (got 21)
8 - blackjack (player)
9 - blackjack (dealer)
Developer@Sonnack.com
March 2016
'''
from __future__ import print_function
from sys import stdout, stderr,argv
from os import path
from datetime import datetime, timedelta
from htmlpage import htmlpage
HtmlPath = r'C:\CJS\www\root\tmp'
RuleNames = ['','Stay','Hit','Double Down','Split Pair','Bust','Push','Win','Blackjack!','Dealer Wins', '', '']
HardHitStay = [
[0,0,0,0, 4,5,6,7,8, 9,0,1,2,3,4,5,6, 7,8,9,0],
[1,0,0,0, 2,2,2,2,2, 2,2,3,2,2,2,2,2, 1,1,1,1],
[2,0,0,0, 2,2,2,2,2, 3,3,3,2,1,1,1,1, 1,1,1,1],
[3,0,0,0, 2,2,2,2,2, 3,3,3,2,1,1,1,1, 1,1,1,1],
[4,0,0,0, 2,2,2,2,2, 3,3,3,1,1,1,1,1, 1,1,1,1],
[5,0,0,0, 2,2,2,2,2, 3,3,3,1,1,1,1,1, 1,1,1,1],
[6,0,0,0, 2,2,2,2,2, 3,3,3,1,1,1,1,1, 1,1,1,1],
[7,0,0,0, 2,2,2,2,2, 2,3,3,2,2,2,2,2, 1,1,1,1],
[8,0,0,0, 2,2,2,2,2, 2,3,3,2,2,2,2,2, 1,1,1,1],
[9,0,0,0, 2,2,2,2,2, 2,3,3,2,2,2,2,2, 1,1,1,1],
[0,0,0,0, 2,2,2,2,2, 2,2,3,2,2,2,2,2, 1,1,1,1],
]
SoftHitStay = [
[0,0, 2,3,4,5,6,7, 8,9,0],
[1,0, 2,2,2,2,2,1, 1,1,5],
[2,0, 2,2,2,2,3,1, 1,1,5],
[3,0, 2,2,2,2,3,3, 1,1,5],
[4,0, 3,3,3,3,3,3, 1,1,5],
[5,0, 3,3,3,3,3,3, 1,1,5],
[6,0, 3,3,3,3,3,3, 1,1,5],
[7,0, 2,2,2,2,2,1, 1,1,5],
[8,0, 2,2,2,2,2,1, 1,1,5],
[9,0, 2,2,2,2,2,2, 1,1,5],
[0,0, 2,2,2,2,2,2, 1,1,5],
]
SplitPair = [
[0, 1,2,3,4,5,6,7,8,9,0],
[2, 4,4,4,2,3,4,4,4,4,1],
[3, 4,4,4,2,3,4,4,4,4,1],
[4, 4,4,4,2,3,4,4,4,4,1],
[5, 4,4,4,4,3,4,4,4,4,1],
[6, 4,4,4,2,3,4,4,4,4,1],
[7, 4,4,4,2,3,4,4,4,1,1],
[8, 4,2,2,2,3,1,4,4,4,1],
[9, 4,2,2,2,3,1,1,4,4,1],
[0, 4,2,2,2,2,1,1,4,1,1],
[1, 4,2,2,2,2,1,1,4,1,1],
]
def RuleHard (fp, dealer, player):
if player.win():
return 7
if player.bust():
return 5
dc = dealer.cards[0]
n = player.counts[0]
r = HardHitStay[dc][n]
print('RuleHard: [%d] %d = %s' % (dc, n, RuleNames[r]), file=fp)
return r
def RuleSoft (fp, dealer, player):
if player.win():
return 7
if player.bust():
return 5
dc = dealer.cards[0]
n = player.counts[0]
if 11 < n:
return RuleHard(fp, dealer, player)
r = SoftHitStay[dc][n-1]
print('RuleSoft: [%d] %d = %s' % (dc, n, RuleNames[r]), file=fp)
return r
def RuleSplit (fp, dealer, player):
dc = dealer.cards[0]
pc = player.cards[0]
r = SplitPair[dc][pc]
if r == 4:
print('Split Pair: [%d] %d-%d' % (dealer.cards[0], player.cards[0],player.cards[1]), file=fp)
return True
return False
class PageWriter (object):
'''HTML Page Writer class.'''
cardlist = ['', '2','3','4','5','6','7','8','9','10','A']
symbols = [''
, '<span style="color:#009900;">—</span>'
, '<span style="color:#ff0000;">●</span>'
, '<span style="color:#cc0000;">⦿</span>'
, '<span style="color:#0000ff;">⌽</span>'
, '?', '?', '?', '?', '?', '?', '?', '?', '?'
]
tabtag = '<table style="font-size:small;border-spacing:4px;padding:0;border:1px solid black;margin:9pt;">\n'
tabcap = '<caption style="padding:2px 6pt 3px 6pt;">%s</caption>\n'
cellstyle = 'text-align:center;'
def __init__ (self, hhs=HardHitStay, shs=SoftHitStay, sp=SplitPair):
self.hhs = hhs
self.shs = shs
self.sp = sp
def create_html_page (self, filename):
fp = open(filename, 'w')
try:
H1 = 'Blackjack'
H2 = 'Python'
H3 = 'Blackjack Card Rules'
SS = ['/basic.css']
JS = []
pg = htmlpage(H3, h1=H1, h2=H2, csspages=SS, jspages=JS)
pg.writepage(fp, self)
print('wrote: %s' % filename)
except Exception as e:
raise
finally:
fp.close()
def writepage (self, fp):
fp.write('<table style="border-spacing:0;padding:0;border:0;margin:0;">\n')
fp.write('<tbody>\n')
fp.write('<tr><td>\n')
fp.write(self.tabtag)
fp.write(self.tabcap % '<strong>Hard Count</strong>')
fp.write('<thead><tr>')
for card in self.cardlist:
fp.write('<th>%s</th>' % card)
fp.write('</tr></thead>\n')
fp.write('<tbody>\n')
for hand in [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]:
fp.write('<tr>')
fp.write('<th>%d</th>' % hand)
for card in [2,3,4,5,6,7,8,9,10,1]:
cell = self.hhs[card][hand]
fp.write('<td style="%s">%s</td>' % (self.cellstyle, self.symbols[cell]))
fp.write('</tr>\n')
fp.write('</tbody></table>\n')
fp.write('</td><td>\n')
fp.write(self.tabtag)
fp.write(self.tabcap % '<strong>Soft Count</strong>')
fp.write('<thead><tr>')
for card in self.cardlist:
fp.write('<th>%s</th>' % card)
fp.write('</tr></thead>\n')
fp.write('<tbody>\n')
for hand in [2,3,4,5,6,7,8,9]:
fp.write('<tr>')
fp.write('<th>A+%d</th>' % hand)
for card in [2,3,4,5,6,7,8,9,10,1]:
cell = self.shs[card][hand]
fp.write('<td style="%s">%s</td>' % (self.cellstyle, self.symbols[cell]))
fp.write('</tr>\n')
fp.write('</tbody></table>\n')
fp.write('</td><td>\n')
fp.write(self.tabtag)
fp.write(self.tabcap % '<strong>Split Pair</strong>')
fp.write('<thead><tr>')
for card in self.cardlist:
fp.write('<th>%s</th>' % card)
fp.write('</tr></thead>\n')
fp.write('<tbody>\n')
for hand in [2,3,4,5,6,7,8,9,10,1]:
fp.write('<tr>')
fp.write('<th>%d+%d</th>' % (hand,hand))
for card in [2,3,4,5,6,7,8,9,10,1]:
cell = self.sp[card][hand]
fp.write('<td style="%s">%s</td>' % (self.cellstyle, self.symbols[cell]))
fp.write('</tr>\n')
fp.write('</tbody></table>\n')
fp.write('</td></tr>\n<tr><td colspan="3" style="font-size:x-small;margin:24pt 9pt;">')
s = 'Stay: %s, Hit: %s, Double: %s, Split Pair: %s'
t = (self.symbols[1], self.symbols[2], self.symbols[3], self.symbols[4])
fp.write(s % t)
fp.write('</td></tr>\n</tbody></table>\n')
def do_main (*args):
print('main/parameters: %d' % len(args))
filename = args[0] if 0 < len(args) else 'bj_rulz.html'
htmlname = path.join(HtmlPath, filename)
pgwr = PageWriter()
pgwr.create_html_page(htmlname)
return 'Done!'
def dispatch (cmd, *args):
print('command: %s' % cmd)
print('arguments: %d' % len(args))
if cmd == 'test': return do_main(*args)
if cmd == 'main': return do_main(*args)
return 'Nothing to do!'
if __name__ == '__main__':
print('autorun: %s' % argv[0])
cmd = argv[1] if 1 < len(argv) else ''
etc = argv[2:] if 2 < len(argv) else []
obj = dispatch(cmd, *etc)
print(obj)
'''eof'''