Python二十一点游戏:有关值字典的问题

时间:2018-06-26 08:41:21

标签: python python-3.x

我一直在关注Jose Portilla编写的关于udemy的python课程,对于我们的一个项目,我们必须创建一个基于文本的二十一点游戏。我的部分代码错误,如下所示。

我为卡创建了一个值字典,这样我就可以轻松地看到一张卡的价值(例如,如果我有俱乐部之王,我希望它等于10)。

rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}

然后,我有一个涉及玩家手牌的课程。我在这部分上遇到了麻烦,因此解决方案如下所示。

class Hand:

    def __init__(self):
        self.cards = []  # start with an empty list as we did in the Deck class
        self.value = 0   # start with zero value
        self.aces = 0    # add an attribute to keep track of aces

    def add_card(self,card):
        self.cards.append(card)
        self.value += rankValues[card.rank]      #Where I need help!#
        if card.rank == 'Ace':
            self.aces += 1  # add to self.aces

    def adjust_for_ace(self):
        while self.value > 21 and self.aces:
            self.value -= 10
            self.aces -= 1

我完全不理解这一行。帮助将不胜感激!

编辑-完整代码

import random
import time


suits = ['Clubs', 'Spades', 'Diamonds', 'Hearts']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}


class Card:
    def __init__(self, suit, rank):
        self.suit = suit
        self.rank = rank

    def __str__(self):
        return(f'{self.rank} of {self.suit}')


class Deck:
    def __init__(self):
        self.deck = []
        for suit in suits:
            for rank in ranks:
                self.deck.append(Card(rank, suit))

    def __str__(self):
        comp = ''
        for card in self.deck:
            comp += '\n' + card.__str__()
        return comp

    def shuffle(self):
        random.shuffle(self.deck)

    def deal(self):
        single_card = self.deck.pop()
        return single_card

class Chips:

    def __init__(self):
        self.chips = 100

    def win_bet(self, opponent_bet):
        self.chips += opponent_bet

    def lose_bet(self, my_bet):
        self.chips -= my_bet

    def __str__(self):
        return(str(self.chips))

class Hand:
    def __init__(self):
        self.cards = []
        self.points = 0
        self.aces = 0

    def add_card(self, card):
        self.cards.append(card)
        self.points += rankValues[card.rank]  #error

    def adjust_for_aces(self):
        if self.points > 21 and self.aces:
            self.aces -= 1
            self.points -= 10

    def __str__(self):
        strversion = ' '
        for card in self.cards:
            strversion.append(card)

1 个答案:

答案 0 :(得分:1)

编辑后,仍不清楚您不了解的内容。您有一本字典,其中包含在类卡中定义的卡的值:

class Card:
    def __init__(self, suit, rank):
        self.suit = suit
        self.rank = rank

rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}

示例:

c1 = Card("Clubs", "Queen")

# To find the value, you need to look at what a Queen is worth:
rankValues["Queen"] # Returns 10. The key if place between [].

# Same as:
rankValues[c1.rank] # Because the attribute rank of c1 is "Queen"

现在Hand

class Hand:
    # Init an empty hand
    def __init__(self):
        self.cards = []
        self.points = 0
        self.aces = 0

    # Add an object card to the hand
    def add_card(self, card):
        self.cards.append(card)
        self.points += rankValues[card.rank]

c1的示例:

my_hand = Hand()
my_hand.add_card(c1) # Enters the method add_card()

# What it does:
# self.cards.append(card) => my_hand.cards is a list() and will have the card c1 appended.
# self.points += rankValues[card.rank]
# => self.points is a value that will be incremented by the value of the card added.
# The value is given by the dictionnary rankValues, and is fetch by the rank of the card as the key.