PyJack

C

Chris Patton

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.'
 
P

Paul Rubin

import whrandom, os

Use random rather than whrandom. whrandom is obsolete and deprecated.
If you're imagining a situation where players will bet real money on
the hands dealt by this program, you have to get even more serious
about the sources of randomness.
def scorer(score,playerdeck):...
while d < len(playerdeck):
if score > 10 and 'A' in playerdeck:
typevalues['A'] = 1

This is pretty ugly, changing the contents of what's conceptually a
fixed table, depending on the player score. I think it's better to
just score the hand with A=1, then count the aces and adjust the score
accordingly.
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

This is a horrendous method of shuffling the cards. When there's only
one card left to deal, it will take several tries to find it. See
the random.shuffle function for a direct way to shuffle.
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


Move these to a function that deals one hand, and call it for
the player and the computer.
score = 0
compscore = 0
stay = 0
over = 0
compstay = 0
compover = 0

Maybe you want to think of using fewer variables, e.g. create class
instances for the two players. Right now you only handle a two-player
game (the human and the computer). What if you want to handle n players?
 
C

Chris Patton

Paul Rubin said:
import whrandom, os

Use random rather than whrandom. whrandom is obsolete and deprecated.
If you're imagining a situation where players will bet real money on
the hands dealt by this program, you have to get even more serious
about the sources of randomness.
def scorer(score,playerdeck):...
while d < len(playerdeck):
if score > 10 and 'A' in playerdeck:
typevalues['A'] = 1

This is pretty ugly, changing the contents of what's conceptually a
fixed table, depending on the player score. I think it's better to
just score the hand with A=1, then count the aces and adjust the score
accordingly.
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

This is a horrendous method of shuffling the cards. When there's only
one card left to deal, it will take several tries to find it. See
the random.shuffle function for a direct way to shuffle.
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


Move these to a function that deals one hand, and call it for
the player and the computer.
score = 0
compscore = 0
stay = 0
over = 0
compstay = 0
compover = 0

Maybe you want to think of using fewer variables, e.g. create class
instances for the two players. Right now you only handle a two-player
game (the human and the computer). What if you want to handle n players?

Thanks for your analization, Paul. I'm getting to work on the code.
IF you tried to run the code, you might have ran into a few bugs. If
you noticed, that was very immature code.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,525
Members
48,248
Latest member
LouveniaUc

Latest Threads

Top