i already have a "def show hand" section, which shows winning hands. but i need it to do wht i have written below
here is a black jack code, i want it basicialyy show score for each game detailing the users total, the computers total and the winner
and When the user quits the program, show a list of all hands played, the user and computer totals and the number of hands played and who won how many hands i.e.:
4 hands played
You win by 3 hands to 1
Hand 1: user 19 computer 18
Hand 2: user 20 computer bust
Hand 3: user 16 computer bust
Hand 4: user 18 computer 18
below is the code :
import random as r
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]*4
computer = []
player = []
c = 'y'
#Clear works only if you run it from command line
def clear():
import os
if os.name == 'nt':
os.system('CLS') #Pass CLS to cmd
if os.name == 'posix':
os.system('clear') #Pass clear to terminal
[B]def showHand():
hand = 0
for i in player: hand += i #Tally up the total
print "The computer is showing a %d" % computer[0]
print "Your hand totals: %d (%s)" % (hand, player)[/B]
#Gives player and computer their cards
def setup():
for i in range(2):
dealcomputer = deck[r.randint(1, len(deck)-1)]
dealPlayer = deck[r.randint(1, len(deck)-1)]
computer.append(dealcomputer)
player.append(dealPlayer)
deck.pop(dealcomputer)
deck.pop(dealPlayer)
setup()
while c != 'q':
showHand()
c = raw_input("[D]ealt [S]tick [Q]uit: ").lower()
clear()
if c == 'd':
dealPlayer = deck[r.randint(1, len(deck)-1)]
player.append(dealPlayer)
deck.pop(dealPlayer)
hand = 0
for i in computer: hand += i
if not hand > 17: #computer strategy.
dealplayer = deck[r.randint(1, len(deck)-1)]
computer.append(dealplayer)
deck.pop(dealplayer)
hand = 0
for i in player: hand += i
if hand > 21:
print "BUST!"
player = [] #Clear player hand
computer = [] #Clear computer's hand
setup() #Run the setup again
hand = 0
for i in computer: hand +=i
if hand > 21:
print "computer Busts!"
player = []
computer = []
setup()
elif c == 's':
cHand = 0 #computer's hand total
pHand = 0 #Player's hand total
for i in computer: cHand += i
for i in player: pHand += i
if pHand > cHand:
print "you Win!" #If playerHand (pHand) is greater than computeHand (cHand) you win...
computer = []
player = []
setup()
else:
print "computer win!" #...otherwise you loose.
computer= []
player = []
setup()
else:
if c == 'q':
gb= raw_input("Press Enter (q to quit): ").lower()
if 'q' in exit:
break
# print "Wins, player = %d computer = %d" % (player, computer)
print "Thanks for playing blackjack with the computer!"