【问题标题】:Card Game Deck Printing纸牌游戏卡组印刷
【发布时间】:2014-02-07 01:04:36
【问题描述】:

我正在关注一本非常好的 Python 书籍,它教你如何为一些纸牌游戏创建模拟。正如您在下面看到的,我创建了一个“卡片”类,用于索引一副牌中所有可能的卡片组合。然后我创建了一个类“Deck”,它通过“Card”中的卡片组合来制作卡片组。我的问题是这个,我正在尝试打印这副牌,每次它都会给我“黑桃王”。如果我改变循环中的范围来创建套牌,它会给我一些别的东西。我的范围有问题,或者类函数有问题吗?如果有的话,我希望代码对你们中的一些人来说很有趣。任何帮助,将不胜感激。谢谢。

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', 'One', '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,14):
            self.cards.append(Card(suit,rank))
def printDeck(self):
    for card in self.cards:
        print(card)
def __repr__(self):
    s = ""
    for i in range(len(self.cards)):
        s = s + " " + str(self.cards[i]) + "\n"
        return s
deck = Deck()
print(deck)

【问题讨论】:

    标签: class python-3.x simulation


    【解决方案1】:

    这是您如何初始化 Card 类的问题

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

    self 参数是初始化类所必需的。

    此外,您在 Deck 类中使用的 __repr__ 的实现存在问题,我认为它没有达到您想要的效果。当您调用print(deck) 时,它会为deck 查找__str__ 方法,然后如果找不到它,它将查找__repr__。您的代码中的问题是返回值只会让您过早地退出循环。

    def __repr__(self):
        s = ""
        for i in range(len(self.cards)):
            s = s + " " + str(self.cards[i]) + "\n"
            return s #oops leaves your loop on the first iteration
    

    改为这样做:

    def __repr__(self):
        s = ""
        for i in range(len(self.cards)):
            s = s + " " + str(self.cards[i]) + "\n"
    
        return s #now it gets the whole string in s before returning
    

    但更好的是删除此range(len()),因为它完全没有必要且难以阅读:

    def __repr__(self):
        s = ""
        for card in self.cards:
            s = s + " " + str(card) + "\n"
    
        return s
    

    此外,您在真正应该使用__str__ 的地方使用__repr____repr__ 意味着能够使用输出完全重新创建类。基本上__repr__ 是可以实例化和重新创建类的有效python 代码。如果你想要一个像你在这里所做的那样的类的非正式字符串表示,那么使用 __str__ 是 pythonic 约定。

    【讨论】:

    • 我对编程有点陌生,你能解释一下你的意思吗?您是指在初始化 Card 类时,还是在 init 卡片组函数中的代码开头?感谢您的澄清!
    • @srhoades28,这里到底有什么不合理的地方?
    • 所以我一直在为我的代码添加更多功能,但遇到了另一个问题。我正在尝试创建代码以从卡组中移除一张卡,但尽管卡在卡组中,但程序返回无。这是因为它没有在列表中找到深度平等吗?这是附加的删除功能:'def removeCard(self, card): if card in self.cards: self.cards.remove(card) return True else: pass'
    • @srhoades28,不看你写的代码真的很难知道发生了什么。
    【解决方案2】:

    我很确定答案就在下面一行:

    self.cards.append(Card(suit,rank))

    黑桃K是最后一张加入牌组的牌并非巧合,我认为这是对象传递引用的问题。我会尽可能用正确的答案更新它,但这可能会让你更好地了解出了什么问题

    编辑:这应该可以工作

    class Card:
        def __init__(self):
            Card.suit = 0
            Card.rank = 2
        #Two class attributes that are helpful in determining suit/rank
        ranklist = ['narf', 'Ace', 'One', '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,14):
                    x = Card()
                    x.suit = suit
                    x.rank = rank
                    self.cards.append(x)
        def printDeck(self):
            for card in self.cards:
                print(card)
        def __repr__(self):
            s = ""
            for i in range(len(self.cards)):
                s = s + " " + str(self.cards[i]) + "\n"
            return s
    deck = Deck()
    deck.printDeck()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多