我现在正在做一个小项目。我正在尝试用python制作21点。我能算出牌的总数,但这是一个非常漫长的过程。我必须手动输入每件事。有什么方法可以让我的代码更短吗?如有任何建议,我们将不胜感激。在
import random
stringlist=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
cards=[]
cardssum=0
def deal():
cards.append(stringlist[random.randrange(0,13)])
cards.append(stringlist[random.randrange(0,13)])
print "First hand :"+str(cards)
blackjack="false"
def blackjack1(cards):
if cards[0]=="A" and cards[1]=="K":
print "BlackJack!"
blackjack="true"
elif cards[0]=="A" and cards[1]=="Q":
print "BlackJack!"
blackjack="true"
elif cards[0]=="A" and cards[1]=="J":
print "BlackJack!"
blackjack="true"
elif cards[0]=="A" and cards[1]==10:
print "BlackJack!"
blackjack="true"
elif cards[0]=="K" and cards[1]=="A":
print "BlackJack!"
blackjack="true"
elif cards[0]=="Q" and cards[1]=="A":
print "BlackJack!"
blackjack="true"
elif cards[0]=="J" and cards[1]=="A":
print "BlackJack!"
blackjack="true"
elif cards[0]==10 and cards[1]=="A":
print "BlackJack!"
blackjack="true"
cardsum=0
def givesum(cardsum):
if type(cards[0])==int and type(cards[1])==int:
print "Your Cards add up to "+str(cards[0]+cards[1])
cardsum+=cards[0]+cards[1]
blackjack="false"
elif cards[0]=="A" and type(cards[1])==int:
print "Your Cards add up to "+str(11+cards[1])
cardsum+=11+cards[1]
blackjack="false"
elif type(cards[0])==int and cards[1]=="A":
print "Your Cards add up to "+str(11+cards[0])
cardsum+=11+cards[0]
blackjack="false"
elif cards[0]=="K" and type(cards[1])==int:
print "Your Cards add up to "+str(10+cards[1])
cardsum+=10+cards[1]
blackjack="false"
elif type(cards[0])==int and cards[1]=="K":
print "Your Cards add up to "+str(10+cards[0])
cardsum+=10+cards[0]
blackjack="false"
elif cards[0]=="Q" and type(cards[1])==int:
print "Your Cards add up to "+str(10+cards[1])
cardsum+=10+cards[1]
blackjack="false"
elif type(cards[0])==int and cards[1]=="Q":
print "Your Cards add up to "+str(10+cards[0])
cardsum+=10+cards[0]
blackjack="false"
elif cards[0]=="J" and type(cards[1])==int:
print "Your Cards add up to "+str(10+cards[1])
cardsum+=10+cards[1]
blackjack="false"
elif type(cards[0])==int and cards[1]=="J":
print "Your Cards add up to "+str(10+cards[0])
cardsum+=10+cards[0]
blackjack="false"
elif cards[0]=="K" and cards[1]=="K":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="K" and cards[1]=="Q":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="Q" and cards[1]=="K":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="K" and cards[1]=="J":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="J" and cards[1]=="K":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="Q" and cards[1]=="Q":
print "Your Cards add up 20"
cardsum+=20
blackjack="false"
elif cards[0]=="Q" and cards[1]=="J":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="J" and cards[1]=="Q":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="J" and cards[1]=="J":
print "Your Cards add up to 20"
cardsum+=20
blackjack="false"
elif cards[0]=="A" and cards[1]=="A":
print "Close Call. Your Cards add up to 12"
cardsum+=12
blackjack="false"
deal()
blackjack1(cards)
givesum(cardsum)
var=raw_input("Would you like another card? Enter HIT or STAND").upper()
def deal2():
if var=="HIT" and cardsum<21 and blackjack=="false":
cards.append(stringlist[random.randrange(0,13)])
print cards
elif var=="STAND":
print "CHECK FOR DEALER'S CARDS"
deal2()
def givesum2(cardsum):
if cards[3]=="K" or cards[3]=="Q" or cards[3]=="J" or cards[3]
你应该把卡片映射到数值,然后计算手的总数,如果是21,然后打印“21点!”公司名称:
您可以使用字典将各种卡片映射到值,然后再使用它,另外在您的
blackjack1
方法中,您可以print "BlackJack!"
仅一次。。。在例如(demo):
这里有个提示。这绝不是完整的或被认为是生产代码。它甚至可能无法运行或编译。我使用一个额外的数组将“stringlist”中的值映射到它们的等效值。更正式的实现将使用更高级的数据结构(哈希表、对象)。“王牌”卡的双值(11或1)需要额外的工作。在
另外,“stringlist”数组实际上是字符串和整数的混合。请考虑将此数组中的每个项设为字符串。在
相关问题 更多 >
编程相关推荐