0

我是一个快乐的业余爱好者,他试图创造一个“或多或少”的游戏。我在积分分配上是不对的。我的 if 语句无法正常工作。我没有收到错误消息,但一切都在“其他”中运行。条件永远不会满足,尽管 K、Q、J、Ace 是随机的……为什么?

class Card(object):

    totPoints = 0

    VALUE = {"A":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}

    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    SUIT = ["Python/projekt/bilder/hearts.png", "Python/projekt/bilder/spades.png", "Python/projekt/bilder/diamond.png", "Python/projekt/bilder/clubs.png"]

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

    def __str__(self):
        rep = self.rank + self.suit
        return rep

    def draw(self):

        bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))

        cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1, relief='solid', highlightthickness=2)
        cardGraph.photo=bg
        cardGraph.pack(side = "left", anchor=NW)

class Hand(object):
    def __init__(self):
        self.cards = []

    def __str__ (self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)

class Deck(Hand):

    def populate(self):
        for suit in Card.SUIT:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)
        DrawCard = self.cards[0]
        DrawCard.draw()

    def deal(self, hands, per_hand = 0):
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                         top_card = self.cards[0]
                         self.give(top_card, hand)         
                else:
                    print("Cant continue deck. Out of cards!!")

    def setValue(self):
            if self.cards[0] == "K":
                Card.totPoints += 10
                print Card.totPoints
            elif self.cards[0] == "Q":
                Card.totPoints += 10
            elif self.cards[0] == "J":
                Card.totPoints += 10
            elif self.cards[0] == "A":
                Card.totPoints += 10
            else:
                Card.totPoints += self.cards
                print Card.totPoints
4

2 回答 2

2

尝试对其进行重组,以便在找到所需的价值后返回;这使得它不太容易混淆 elif 的顺序。此外,对大量值进行“测试”更容易:

def setValue(self):
   first_card = self.cards[0]
   face_cards = ['K','Q','J','A']
   if first_card in face_cards: 
      card.totPoints += 10
      return
   card.totPoints += self.cards

顺便说一句:如果第一张卡是人脸卡,这将导致得分为 10,如果不是,则为所有卡的总分。那是你要的吗?

最后,您似乎正在从卡片组对象中更新卡片的值。也许你应该只在初始化中设置卡片的分数, 这样你就不必从外面做?

于 2013-09-01T17:25:33.647 回答
2

您的代码从不将卡片仅表示为字符串。相反,您正在使用类的实例Card()

def populate(self):
    for suit in Card.SUIT:
        for rank in Card.RANKS:
            self.add(Card(rank, suit))

您需要针对该.rank属性进行测试:

def setValue(self):
    if self.cards[0].rank == "K":
        Card.totPoints += 10
        print Card.totPoints
    elif self.cards[0].rank == "Q":
        Card.totPoints += 10
    elif self.cards[0].rank == "J":
        Card.totPoints.rank += 10
    elif self.cards[0].rank == "A":
        Card.totPoints += 10
    else:
        Card.totPoints += int(self.cards[0].rank)

请注意,.rank始终是一个字符串,因此当它是数字卡时,您需要将其转换为整数。在任何情况下,我都认为这是else:分支的目标。

您在该函数中的代码可以大大简化:

def setValue(self):
    rank = self.cards[0].rank
    Card.totPoints += 10 if rank in 'KQJA' else int(rank)

或者,您可以只使用Class.VALUE已有的映射:

def setValue(self):
    Card.totPoints += Card.VALUE[self.cards[0].rank]
于 2013-09-01T17:31:17.670 回答