在用python制作21点时,如何避免大量的手工操作?

2024-10-02 16:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在做一个小项目。我正在尝试用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]

Tags: andtoaddfalseyourtypeintcards
3条回答

你应该把卡片映射到数值,然后计算手的总数,如果是21,然后打印“21点!”公司名称:

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)

您可以使用字典将各种卡片映射到值,然后再使用它,另外在您的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 "..."

这里有个提示。这绝不是完整的或被认为是生产代码。它甚至可能无法运行或编译。我使用一个额外的数组将“stringlist”中的值映射到它们的等效值。更正式的实现将使用更高级的数据结构(哈希表、对象)。“王牌”卡的双值(11或1)需要额外的工作。在

另外,“stringlist”数组实际上是字符串和整数的混合。请考虑将此数组中的每个项设为字符串。在

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!"

相关问题 更多 >