473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

PyJack

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.'
Jul 18 '05 #1
2 1888
ju*********@gmail.com (Chris Patton) writes:
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?
Jul 18 '05 #2
Paul Rubin <http://ph****@NOSPAM.invalid> wrote in message news:<7x************@ruckus.brouhaha.com>...
ju*********@gmail.com (Chris Patton) writes:
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: patrick | last post by:
hello everyone, is there a way to make python output audio in jack: http://jackaudio.org/ jack is the best audio solution for linux, mac and lately windows. i found 1 project called pyjack,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.