我目前正在开展一个小项目。我试图在python中制作二十一点。我可以计算卡的总和,但这是一个非常漫长的过程。我必须手动输入每一件事。有什么方法可以缩短我的代码吗?任何建议将不胜感激。
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]
答案 0 :(得分:2)
您可以使用字典将各种卡片映射到值,然后使用它,再加上blackjack1
方法,您只需print "BlackJack!"
一次......
例如(demo):
import random
stringlist=['2','3','4','5','6','7','8','9','10',"J","Q","K","A"]
bmap = { 'A' : 11,
'K' : 10,
'Q' : 10,
"J" : 10,
'10': 10,
'9' : 9,
'8' : 8,
'7' : 7,
'6' : 6,
'5' : 5,
'4' : 4,
'3' : 3,
'2' :2
}
cards=[]
cards.append(stringlist[random.randrange(0,13)])
cards.append(stringlist[random.randrange(0,13)])
print "Got %s and %s. Sum: %s" % (cards[0],cards[1], bmap[cards[0]] + bmap[cards[1]])
if(bmap[cards[0]] + bmap[cards[1]] == 21):
print "Yay"
else:
print "..."
答案 1 :(得分:1)
你应该将牌映射到值,然后计算牌的总和,如果是21,然后打印出二十一点!&#39;:
import random
cards_values = {'J': 10, 'K': 10, 'Q': 10} # Face cards are 10
suits = ['C','H','D','S'] # Club, Heart, Diamond, Spade
cards = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
deck = ['{}{}'.format(i,k) for i in suits for k in cards]
random.shuffle(deck) # shuffle the deck
card1, card2 = deck[:2] # Get two random cards
# Each card is SuitNumber, so a two of clubs is C2
# We need to get the second value to figure out if
# we have a winning blackjack hand
# Here we are checking the second part, if its one of the face cards
# get its value, otherwise the value is its actual number
# We convert it to an integer, so we can sum it and get
# the value of the hand
value_card1 = cards_values.get(card1[1], card1[1])
value_card2 = cards_values.get(card2[1], card2[1])
hand_value = int(value_card1) + int(value_card2)
if hand_value == 21:
print 'BlackJack!'
else:
# We need to check if the difference is
# 1 or 11, and the person had an Ace, he can
# still win
if hand_value - 21 in (1,11) and card1[1] == 'A' or card2[1] == 'A':
print 'BlackJack!'
else:
print 'Oops, you lose. Your cards were {} {}'.format(card1, card2)
答案 2 :(得分:0)
这里有一个提示。 绝不是完整或考虑的生产代码。它甚至可能无法运行或编译。我使用额外的数组来映射&#34; stringlist&#34;中的值。他们的等价值。更正式的实现将使用更高级的数据结构(哈希表,对象)。 &#34; ace&#34;需要额外的工作。卡的双重价值(11或1)。
此外,您的&#34; stringlist&#34;数组实际上是字符串和整数的混合。考虑将此数组中的每个项目都设为字符串。
stringlist=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
valuelist= [2,3,4,5,6,7,8,9,10,10, 10, 10, 11]
def getCardValue(c):
for i in stringlist:
if (stringlist[i] == c):
return valuelist[i];
return 0
def isBlackJack(c1, c2):
return (21 == (getCardValue(c1)+getCardValue(c2))
def blackjack1(cards):
if (isBlackJack(cards[0], cards[1])):
print "BlackJack!"