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 stdoutstderr,argv
from os import path
from datetime import datetimetimedelta
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,04,5,6,7,89,0,1,2,3,4,5,67,8,9,0],
    [1,0,0,02,2,2,2,22,2,3,2,2,2,2,21,1,1,1],
    [2,0,0,02,2,2,2,23,3,3,2,1,1,1,11,1,1,1],
    [3,0,0,02,2,2,2,23,3,3,2,1,1,1,11,1,1,1],
    [4,0,0,02,2,2,2,23,3,3,1,1,1,1,11,1,1,1],
    [5,0,0,02,2,2,2,23,3,3,1,1,1,1,11,1,1,1],
    [6,0,0,02,2,2,2,23,3,3,1,1,1,1,11,1,1,1],
    [7,0,0,02,2,2,2,22,3,3,2,2,2,2,21,1,1,1],
    [8,0,0,02,2,2,2,22,3,3,2,2,2,2,21,1,1,1],
    [9,0,0,02,2,2,2,22,3,3,2,2,2,2,21,1,1,1],
    [0,0,0,02,2,2,2,22,2,3,2,2,2,2,21,1,1,1],
]

##------------------------------------------------------------------------------------------------##
SoftHitStay = [
    [0,02,3,4,5,6,78,9,0],
    [1,02,2,2,2,2,11,1,5],
    [2,02,2,2,2,3,11,1,5],
    [3,02,2,2,2,3,31,1,5],
    [4,03,3,3,3,3,31,1,5],
    [5,03,3,3,3,3,31,1,5],
    [6,03,3,3,3,3,31,1,5],
    [7,02,2,2,2,2,11,1,5],
    [8,02,2,2,2,2,11,1,5],
    [9,02,2,2,2,2,21,1,5],
    [0,02,2,2,2,2,21,1,5],
]

##------------------------------------------------------------------------------------------------##
SplitPair = [
    [01,2,3,4,5,6,7,8,9,0],
    [24,4,4,2,3,4,4,4,4,1],
    [34,4,4,2,3,4,4,4,4,1],
    [44,4,4,2,3,4,4,4,4,1],
    [54,4,4,4,3,4,4,4,4,1],
    [64,4,4,2,3,4,4,4,4,1],
    [74,4,4,2,3,4,4,4,1,1],
    [84,2,2,2,3,1,4,4,4,1],
    [94,2,2,2,3,1,1,4,4,1],
    [04,2,2,2,2,1,1,4,1,1],
    [14,2,2,2,2,1,1,4,1,1],
]


##================================================================================================##
def RuleHard (fpdealerplayer):
    # No player card is an ace (1)
    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' % (dcnRuleNames[r]), file=fp)
    return r

##================================================================================================##
def RuleSoft (fpdealerplayer):
    # At least one player card is an ace (1)
    if player.win():
        return 7
    if player.bust():
        return 5
    dc = dealer.cards[0]
    n = player.counts[0]
    if 11 < n:
        return RuleHard(fpdealerplayer)
    r = SoftHitStay[dc][n-1]
    print('RuleSoft: [%d] %d = %s' % (dcnRuleNames[r]), file=fp)
    return r

##================================================================================================##
def RuleSplit (fpdealerplayer):
    # (player_card[0] == player_card[1])
    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;">&mdash;</span>'
    , '<span style="color:#ff0000;">&#x25cf;</span>'
    , '<span style="color:#cc0000;">&#x29bf;</span>'
    , '<span style="color:#0000ff;">&#x233d;</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__ (selfhhs=HardHitStayshs=SoftHitStaysp=SplitPair):
        self.hhs = hhs
        self.shs = shs
        self.sp  = sp

    def create_html_page (selffilename):
        fp = open(filename'w')
        try:
            H1 = 'Blackjack'
            H2 = 'Python'
            H3 = 'Blackjack Card Rules'
            SS = ['/basic.css']
            JS = []
            pg = htmlpage(H3h1=H1h2=H2csspages=SSjspages=JS)
            pg.writepage(fpself)
            #...
            print('wrote: %s' % filename)
        except Exception as e:
            raise
        finally:
            fp.close()

    def writepage (selffp):
        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>')
        # Header...
        fp.write('<thead><tr>')
        for card in self.cardlist:
            fp.write('<th>%s</th>' % card)
        fp.write('</tr></thead>\n')
        # Data...
        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.cellstyleself.symbols[cell]))
            fp.write('</tr>\n')
        # end...
        fp.write('</tbody></table>\n')

        fp.write('</td><td>\n')

        fp.write(self.tabtag)
        fp.write(self.tabcap % '<strong>Soft Count</strong>')
        # Header...
        fp.write('<thead><tr>')
        for card in self.cardlist:
            fp.write('<th>%s</th>' % card)
        fp.write('</tr></thead>\n')
        # Data...
        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.cellstyleself.symbols[cell]))
            fp.write('</tr>\n')
        # end...
        fp.write('</tbody></table>\n')

        fp.write('</td><td>\n')

        fp.write(self.tabtag)
        fp.write(self.tabcap % '<strong>Split Pair</strong>')
        # Header...
        fp.write('<thead><tr>')
        for card in self.cardlist:
            fp.write('<th>%s</th>' % card)
        fp.write('</tr></thead>\n')
        # Data...
        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.cellstyleself.symbols[cell]))
            fp.write('</tr>\n')
        # end...
        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[0if 0 < len(argselse 'bj_rulz.html'
    htmlname = path.join(HtmlPathfilename)
    # Print...
    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[1if 1 < len(argvelse ''
    etc = argv[2:if 2 < len(argvelse []
    obj = dispatch(cmd, *etc)
    print(obj)
####################################################################################################
'''eof'''