纸牌游戏模拟list.remove

时间:2014-02-07 19:00:38

标签: class python-3.x simulation

我正在关注一本非常好的python书,它教你如何为某些纸牌游戏创建模拟。正如您在下面所见,我创建了一个类“卡”,用于索引卡中所有可能的卡组合。然后我创建了一个“Deck”类,它从“Card”中的卡组合制作了套牌。我的问题是,我正在尝试模拟某人从decl中取出一张卡片,但无法使最后一个类函数removeCard工作,我不太清楚为什么。有人可以帮助我理解我的问题以及如何纠正它吗?谢谢。

class Card:
def __init__(self, suit = 0, rank = 2):
    Card.suit = suit
    Card.rank = rank
#Two class attributes that are helpful in determining suit/rank
ranklist = ['narf', 'Ace', 'Two', 'Three', 'Four', 'Five', \
'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
suitlist = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
def __repr__(self):
    return (self.ranklist[self.rank] + " of " + self.suitlist[self.suit])


class Deck:
def __init__(self):
    self.cards = []
    for suit in range(4):
        for rank in range(1,13):
            x = Card()
            x.rank = rank
            x.suit = suit 
            self.cards.append(x)
def printDeck(self):
    for card in self.cards:
        print(card)

def removeCard(self, card):
        if card in self.cards:
            self.cards.remove(card)
            return True
        else:
            return False

1 个答案:

答案 0 :(得分:2)

首先,您的卡片__init__功能中有拼写错误。您应该将属性分配给self,而不是Card

其次, 我猜你正在做类似的事情:

class Card:
    def __init__(self, suit = 0, rank = 2):
        self.suit = suit
        self.rank = rank
    #Two class attributes that are helpful in determining suit/rank
    ranklist = ['narf', 'Ace', 'Two', 'Three', 'Four', 'Five', \
    'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
    suitlist = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    def __repr__(self):
        return (self.ranklist[self.rank] + " of " + self.suitlist[self.suit])


class Deck:
    def __init__(self):
        self.cards = []
        for suit in range(4):
            for rank in range(1,13):
                x = Card()
                x.rank = rank
                x.suit = suit 
                self.cards.append(x)
    def printDeck(self):
        for card in self.cards:
            print(card)

    def removeCard(self, card):
            if card in self.cards:
                self.cards.remove(card)
                return True
            else:
                return False

d = Deck()
print "The deck has {} cards.".format(len(d.cards))
card = Card(1,1)
print "Removing the {}.".format(card)
d.removeCard(card)
print "The deck has {} cards.".format(len(d.cards))

您希望输出为:

The deck has 48 cards.
Removing the Ace of Diamonds.
The deck has 47 cards.

但实际上你得到了:

The deck has 48 cards.
Removing the Ace of Diamonds.
The deck has 48 cards.

你的removeCard方法失败了,因为默认情况下,python中的对象只有在它们的id相等时才相等。例如:

>>> a = Card(1,1)
>>> b = Card(1,1)

>>> a == a
True
>>> id(a) == id(a)
True

>>> a == b
False
>>> id(a) == id(b)
False

尽管a和b都是用Card(1,1)创建的,但它们并不相同。当您尝试if card in self.cards:时,Python会问,self.cards是否包含具有此确切ID的卡?对于我的第一个代码段,它会回复False

您可以在__eq__课程中指定自己的Card来覆盖此默认行为。

class Card:
    def __init__(self, suit = 0, rank = 2):
        self.suit = suit
        self.rank = rank
    #Two class attributes that are helpful in determining suit/rank
    ranklist = ['narf', 'Ace', 'Two', 'Three', 'Four', 'Five', \
    'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
    suitlist = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    def __eq__(self, other):
        if not isinstance(other, Card): return False
        return self.suit == other.suit and self.rank == other.rank
    def __repr__(self):
        return (self.ranklist[self.rank] + " of " + self.suitlist[self.suit])

现在inremove的行为正常,并根据需要删除您的卡片。

The deck has 48 cards.
Removing the Ace of Diamonds.
The deck has 47 cards.