Hi
This is the first code that I have programed. I think there is something
wrong with it. It is not a loop and computer seems wont bust. In the game,
ace will be fixed as low (value=1). And can you write documentation comments
for the first 39lines.
kind regards
Neo Gao
_________________________________________________________________
您可以借助 Windows Live 整理、编辑和共享您的照片。
http://www.microsoft.com/china/windows/windowslive/products/photo-gallery-edit.aspx
from random import *
from math import *
#GLOBAL VARIABLES
cards = range(0,52)
def randRange(in_lower,in_upper):
""" generates a random number between in_lower and in_upper"""
temp_range = in_upper - in_lower
return int(round((temp_range)*random() + (in_lower)))
def popRandArray(in_list):
return in_list.pop(randRange(0,len(in_list)-1))
def realDealCard():
global cards
if len(cards)==0:
print "new deck"
cards = range(0,52)
return popRandArray(cards)
def cardAsString(in_card):
value =
["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
suit = ["hearts","diamonds","spades","clubs"]
return value[in_card%13]+ " of " + suit[in_card/13]
def cardScore(in_card):
score = in_card%13+1
if score > 10:
score = 10
return score
print "$Blackjack$"
#~ player is delt with two cards
player_card1 = realDealCard()
player_card2 = realDealCard()
#~ show player the two cards
print "your card1 is", cardAsString(player_card1)
print "your card2 is", cardAsString(player_card2)
#~ count score of player
player_score = cardScore(player_card1) + cardScore(player_card2)
#~ show playerthe score
print "your score is", player_score
#~ computer is delt with two cards
computer_card1 = realDealCard()
computer_card2 = realDealCard()
#~ show player one of the two cards
print "The card1 of computer is", cardAsString(computer_card1)
#~ count score of computer
computer_score = computer_card1+computer_card2
#~ ask players action
while True:
player_action = str(raw_input("twist (t) or stick (s)?"))
#~ if player chooses twist
if player_action == "t" :
#~ player is delt with one more card
player_card3 = realDealCard()
#~ show player the third card
print "your card3 is", cardAsString(player_card3)
#~ count current score of player
player_score += cardScore(player_card3)
#~ show player current score
print "your score is", player_score
#~ check bust
#~ if current score of player > 21
if player_score > 21 :
#~ bust
print "you bust"
#~ player lose
print "you lose and computer wins"
#~ elif current score of player == 21
elif player_score == 21 :
#~player has a blackjack
print "blackjack!"
#~win
print "you win and computer loses"
#~ elif player chooses stick
elif player_action == "s" :
#~ dealers turn
print "you choose stick"
print "It is computers turn"
#~ if first score of computer <= 18
if computer_score <=18 :
#~ computer chooses twist
print "computer twist"
#~computer is delt one more card
computer_card3 = realDealCard()
computer_score += cardScore(computer_card3)
#~ check bust
#~ if current score of computer > 21
if computer_score > 21 :
#~computer bust
print "computer score is",
computer_score
print "computer bust and You win"
elif computer_score == player_score :
print "computer score is",
computer_score
print "draw- No winner"
#~ elif first two score > 18
elif computer_score > 18 :
#~computer choose stick
print "computer stick"
print "computer score is", computer_score
#~compare score
if computer_score < player_score :
print "you win and computer loses"
elif computer_score > player_score :
print "you lose and computer wins"
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor