【问题标题】:Card Game Simulation list.remove纸牌游戏模拟 list.remove
【发布时间】:2014-02-07 19:00:37
【问题描述】:

我正在关注一本非常好的 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

【问题讨论】:

  • “但无法让最后一个类函数 removeCard 工作。” 您能否提供完整、可运行的代码来演示如何创建卡片组、移除卡片以及显示结果?另外,您能否再次检查您的代码在帖子中的格式是否正确?当我尝试运行它时,我收到了一个IndentationError
  • 这本书叫什么名字?

标签: class python-3.x simulation


【解决方案1】:

首先,您卡片的__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 的卡片?对于我的第一个代码 sn-p,它会回复 False

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

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.

【讨论】:

  • 您的回答正确而全面!做得好。 @srhoades28:考虑在Deck.__init__ 的末尾使用import random; random.shuffle(self.cards) 改组你的Deck,然后使用self.cards.pop() 简单地将removeCard 函数绑定到drawCard 函数中。通常没有必要从现实世界的一副牌中移除给定的牌。
  • 无法告诉你我在这方面工作了多长时间。很好的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多