2014-02-07 138 views
0

我跟着一本很不错的python书,它教你如何创建一些纸牌游戏的模拟。正如你在下面看到的,我已经创建了一个“卡片”类,用于索引卡片中所有可能的卡片组合。然后我创建了一个“Deck”类,它使卡片组合中的卡片成为“卡片”。我的问题是,我试图模拟某人从decl中取出一张卡片,但无法获得最后一个班级的removeCard功能,而且我不太清楚为什么。有人可以帮助我了解我的问题以及如何纠正它?谢谢。纸牌游戏模拟list.remove

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 
+0

_“但不能获得最后类功能removeCard工作。” _你能否提供演示如何创建甲板,取出存储卡,并显示结果完整的,可运行的代码?另外,你可以仔细检查你的代码在帖子中是否被正确格式化了?当我尝试运行它时,出现'IndentationError'。 – Kevin

+0

这本书的名字是什么? – Bazman

回答

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询问自身卡是否包含一张具有这个确切ID的卡?在我的第一个代码片段中,它回复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. 
+0

您的答案正确而全面!做得好。 @ srhoades28:考虑用'import random'来替换你的Deck; random.shuffle(self.cards)'放在'Deck .__ init__'的末尾,然后用'self.cards.pop()'将'removeCard'函数简单地绑定到'drawCard'函数中。通常不需要从真实世界的牌组中移除给定的牌。 –

+0

不能告诉你我在这工作了多久。伟大的建议! – srhoades28