(根据手牌的值,编号为2-10的卡片的值应分别为2-10。J,Q和K的值为10,A的值为1或11)。

我如何为卡片组分配这些值?此外,游戏需要进行3回合。我这样做的方式只有一轮。我如何使游戏进行三遍,同时跟踪玩家的赢/输?

有人可以解释一下如何做到这一点吗?

最佳答案

这是一个完整的实施方案

看一下。得分

import random

class Card:
    def __init__(self,rank,suite):
        self.rank = rank
        self.suite = suite
    def Rank(self):
        return "Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King".split()[self.rank]
    def Suite(self):
        return "Hearts Spades Clubs Diamonds".split()[self.suite]
    def __str__(self):
        #print "Get Self:",type(self)
        #print "Dir:",dir(self)
        #return "OF"
        return self.Rank()+" of "+ self.Suite()


class Hand:
    def __init__(self):
        self.cards = []
    def Score(self):
        aces_ct = 0
        score = 0
        for c in self.cards:
            if c.rank == 0:
                aces_ct += 1
                score += 11
            if 0 < c.rank < 9:
                score += c.rank+1
            else:
                score += 10
        while score > 21 and aces_ct > 0:
            score -= 10
            aces_ct -= 1
        return score
    def add(self,card):
        self.cards.append(card)
    def Show(self,show_only=None):
        if not show_only:
            for k in self.cards:
                print "%s"%k
        else:
            if isinstance(show_only,int):
                print "%s"%self.cards[show_only]
            elif isinstance(show_only,(list,tuple)):
                for idx in show_only:
                    print "%s"%self.cards[idx]


class deck:
    def __init__(self):
        self.cards = []
        for i in range(4):
            for j in range(13):
                self.cards.append(Card(j,i))
        random.shuffle(self.cards)
    def shuffle(self):
        random.shuffle(self.cards)
    def pop(self):
        return self.cards.pop()

if __name__ == "__main__":
    d = deck()
    player_hand = Hand()
    dealer_hand = Hand()
    player_hand.add(d.pop())
    dealer_hand.add(d.pop())
    player_hand.add(d.pop())
    dealer_hand.add(d.pop())
    print "Player Score :",player_hand.Score()
    player_hand.Show()
    print "\n\nDealer Score :",dealer_hand.Score()
    dealer_hand.Show()

关于python - 给卡片组赋值的麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11730723/

10-12 23:31