PyJack

Chris Patton jumpthewall at gmail.com
Wed Aug 11 19:36:08 EDT 2004


I just finished my first program (PyJack)! If you'll noticed, there
are many ways inwich I can shorten the program (I really don't know
all the python syntax.) Please post suggestions.

code:



# PYJACKv1
# This is a 'blackjack' program written in python. There are two os
calls, that makes this program disqualified for 'operating system
independency', but oh well. These calls only screen. Change
accordingly
import whrandom, os
def scorer(score,playerdeck):
        b = 0
        while b == 0:
                try:
                        d = 0
                        while d < len(playerdeck):
                                if score > 10 and 'A' in playerdeck:
typevalues['A'] = 1
                                elif score <= 10 and 'A' in
playerdeck: typevalues['A'] = 11
                                score += typevalues[playerdeck[d]]
                                d = d + 1
                        b = 1
                except KeyError:
                        b = 0
                return score                        
a = 'y'
b = 0
while a == 'y':
        # shuffle deck
        print "loading..."
        types = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
        typevalues = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7
, '8':8,'9':9, '10':10, 'J':10, 'Q':10, 'K':10}
        left = [4,4,4,4,4,4,4,4,4,4,4,4,4]
        deck = []
        decksize = 0
        while decksize < 52:
                a = whrandom.randint(0,12)
                if left[a] > 0:
                        left[a] = left[a] - 1
                        deck.append(types[a])
                        decksize = decksize + 1
                elif left[a] == 0: pass
        os.system('cls')
        print
        # play?
        print "---*Black Jack*---"
        if b == 0: a = raw_input('\nWould you like to play? [y,n]')
        elif b > 0: a = raw_input('\nPlay again? [y,n]')
        while a not in ['y','n']:
                if b == 0: a = raw_input('\nWould you like to play?
[y,n]')
                elif b > 0: a = raw_input('\nPlay again? [y,n]')
        if a == 'y': pass
        elif a == 'n': break
        # deal
        c = 0
        humandeck = []
        while c <= 1:
                humandeck.append(deck[0])
                del deck[0]
                c = c + 1
        compdeck = []
        c = 0
        while c <= 1:
                compdeck.append(deck[0])
                del deck[0]
                c = c + 1
        score = 0
        compscore = 0
        stay = 0
        over = 0
        compstay = 0
        compover = 0
        # playing the game
        score += scorer(score,humandeck)
        while over == 0 and stay == 0:
                move = 0
                a = 0
                printdeck = ''
                epd = compdeck[0]+' |?|'
                while a < len(humandeck):
                        printdeck += humandeck[a]+' '        
                        a += 1
                os.system('clear')
                print '\n---*Black Jack*---'
                print '\n\n  Dealer:  '+epd
                print '\n  Player:  '+printdeck+'\n 
score='+str(score)
                # your turn
                if stay == 0 and over == 0:
                        move = raw_input('\n  <a>hit <b>stay \n\n 
<$>')
                        while move not in ['a','b','hit','stay']: move
= raw_input('\n  <not an option $>')
                elif stay > 0: pass
                if move in ['a','hit']:
                        humandeck.append(deck[0])
                        del deck[0]
                elif move in ['b','stay']: stay = stay + 1
                score = 0
                score += scorer(score,humandeck)
                if score > 21:
                        over = over + 1
                        printdeck = ' **OVER**'
        # Dealer's turn
        while 1:
                f = 0
                f = scorer(compscore,compdeck)
                if f > 21:
                        compover = 1
                        break
                if f > 15: break
                if f <= 15:
                        try:
                                compdeck += deck[0]
                                del deck[0]
                        except IndexError: pass

        epd = ''
        c = 0
        while c < len(compdeck):
                epd += compdeck[c]+' '
                c = c + 1
        compscore = scorer(compscore,compdeck)
        # Winner?
        os.system('clear')
        print '\n---*Black Jack*---'
        print '\n\n  Dealer:  '+epd+'\n  score='+str(compscore)
        print '  Player:  '+printdeck+'\n  score='+str(score)
        e = 0
        if score > compscore and over == 0 and compover == 0: winner =
'Player'
        elif score == compscore and len(humandeck) < len(compdeck):
winner = 'Player'
        elif compover > over: winner = 'Player'
        elif score < compscore and compover == 0 and over == 0: winner
= 'Dealer'
        elif score == compscore and len(humandeck) > len(compdeck):
winner = 'Dealer'
        elif compover < over: winner = 'Dealer'
        elif compover == over and score == compscore:
                print '\n\n  TIE'
                e = 1
        else:
                if over < compover: winner = 'Player'
                elif over > compover: winner = 'Dealer'
                else: winner = 'Dealer'
                
        if e == 0: print '\n\n  The '+winner+' wins this round.'
        raw_input()
        a = 'y'
        b = 1
print '\nGood day.'



More information about the Python-list mailing list