BlackJack游戏跑分数计数器

时间:2017-06-28 00:14:58

标签: python oop blackjack

我的任务是为我正在参加的Python课程构建二十一点游戏。目标是尽可能多地使用OOP。我知道我的代码有点混乱(提前抱歉)。我根据发给经销商和玩家的两张牌的价值创建得分计数器时遇到了麻烦(更别提Ace的问题是1或11;我以后会越过那座桥)。基本上,根据玩家绘制的两张牌和经销商的抽奖,我希望得分计数器2将卡的值加在一起,并将值存储在 playerscore dealercore 。任何帮助表示赞赏。

import random

class Card(object):
    def __init__(self,suit,value):
        self.suit = suit
        self.value = value
    def show(self):
        if self.value==1:
            print("{} of {}".format('A', self.suit))
        elif self.value==11:
            print("{} of {}".format('J', self.suit))
        elif self.value==12:
            print("{} of {}".format('Q', self.suit))
        elif self.value==13:
            print("{} of {}".format('K', self.suit))
        else:
            print("{} of {}".format(self.value,self.suit))

class Deck(object):
    def __init__(self):
        self.cards = []
        self.build()
    def build(self):
        for suit in ("Spades","Clubs","Diamonds","Hearts"):
            for value in range(1,14):
                self.cards.append(Card(suit,value))
    def show(self):
        for card in self.cards:
            card.show()
    def shuffle(self):
        random.shuffle(self.cards)
    def drawCard(self):
        return self.cards.pop()

class Player(object):
    def __init__(self):
        self.hand = []
    def draw(self,deck):
        for num in range(1,3):
            self.hand.append(deck.drawCard())
    def showHand(self):
        for card in self.hand:
            card.show()

class Dealer(object):
    def __init__(self):
        self.hand = []
    def draw(self,deck):
        for num in range(1,3):
            self.hand.append(deck.drawCard())

playerscore = 0
dealerscore = 0

deck = Deck()
deck.shuffle()
player = Player()
player.draw(deck)
player.showHand()
dealer = Dealer()
dealer.draw(deck)

playerscore += ?????
dealerscore += ?????

1 个答案:

答案 0 :(得分:0)

如果您提前考虑一下,计算二十一点手牌值是完全确定的,而且相当简单。像这样:

def hand_total(hand):
    total = 0
    ace_found = False
    soft = False

    for card in hand:
        if card.value >= 10:
            total += 10
        else:
            total += card.value

        if card.value == 1:
            ace_found = True;

    if total < 12 and ace_found:
        total += 10
        soft = True

    return total, soft

返回手的值,以及表示硬/软的标志(遵循二十一点规则是必要的)。请注意,您永远不必计算A,并且永远不必做出任何选择。如果有任何ace,并且总数很小,则添加10.就是这样。

相关问题