1

我是 Python 新手,这是我第一次提出 stackOverflow 问题,但我是长期读者。我正在开发一个简单的基于卡片的游戏,但在管理我的手类的实例时遇到了麻烦。如果你看下面,你会看到手类是一个简单的卡片容器(只是 int 值),每个玩家类都包含一个手类。但是,每当我创建 Player 类的多个实例时,它们似乎都在操纵 Hand 类的单个实例。根据我在 C 和 Java 方面的经验,我似乎以某种方式使我的 Hand 类成为静态的。如果有人可以帮助解决这个问题,我将不胜感激。

谢谢你,泰德

澄清:这种情况的一个例子是

p = player.Player()
p1 = player.Player()
p.recieveCard(15)
p1.recieveCard(21)
p.viewHand()

这将导致: [15,21]
即使只有一张卡被添加到 p

手级:

class Hand:

    index = 0
    cards = [] #Collections of cards

    #Constructor
    def __init__(self):
        self.index
        self.cards

    def addCard(self, card):
        """Adds a card to current hand"""
        self.cards.append(card)
        return card

    def discardCard(self, card):
        """Discards a card from current hand"""
        self.cards.remove(card)
        return card

    def viewCards(self):
        """Returns a collection of cards"""
        return self.cards

    def fold(self):
        """Folds the current hand"""
        temp = self.cards
        self.cards = []
        return temp

玩家等级

import hand
class Player:

 name = ""
 position = 0
 chips = 0
 dealer = 0
        pHand = []

 def __init__ (self, nm, pos, buyIn, deal):
            self.name = nm
            self.position = pos
            self.chips = buyIn
            self.dealer = deal

            self.pHand = hand.Hand()
            return

        def recieveCard(self, card):
            """Recieve card from the dealer"""
            self.pHand.addCard(card)
            return card

        def discardCard(self, card):
            """Throw away a card"""
            self.pHand.discardCard(card)
            return card

        def viewHand(self):
            """View the players hand"""
            return self.pHand.viewCards()

        def getChips(self):
            """Get the number of chips the player currently holds"""
            return self.chips

        def setChips(self, chip):
            """Sets the number of chips the player holds"""
            self.chips = chip
            return

        def makeDealer(self):
            """Makes this player the dealer"""
            self.dealer = 1
            return

        def notDealer(self):
            """Makes this player not the dealer"""
            self.dealer = 0
            return

        def isDealer(self):
            """Returns flag wether this player is the dealer"""
            return self.dealer

        def getPosition(self):
            """Returns position of the player"""
            return self.position

        def getName(self):
            """Returns name of the player"""
            return self.name
4

3 回答 3

4

根据我在 C 和 Java 方面的经验,我似乎以某种方式使我的 Hand 类成为静态的。

实际上,这基本上就是您正在做的事情。好吧,并不是真正使类成为静态的,而是使变量成为静态的。

当您编写这样的声明时:

class Hand:
    cards = []

该变量 ( cards) 与相关联,而不是与实例相关联。为了类比 Java,Python 类中不属于该类方法的每个语句基本上都在静态初始化程序中运行。你几乎可以这样想:

class Hand {
    static {
        cards = new object[];
    }
}

(当然只是一个粗略的类比)

要在 Python 中创建实例变量,您必须将其设置为实例的属性,这需要您等到获得对实例的引用。实际上,这意味着您在构造函数中对其进行初始化,如下所示:

class Hand:
    def __init__(self):
        self.cards = []
于 2010-06-03T02:41:37.653 回答
3

你的问题很简单

如果您将列表分配给 python 类的主体,当您将项目附加到它时,它们将存储在类级别,而不是实例级别。

您可以通过添加以下行来解决此问题:

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

这是一个众所周知的 python 陷阱案例,我建议您阅读:http: //zephyrfalcon.org/labs/python_pitfalls.html

于 2010-06-03T02:41:58.113 回答
0

正如其他答案所述,您对类变量与实例变量感到困惑。我建议你回顾一下 Python 类是如何工作的基础知识。这是我为另一个问题写的答案;阅读本文可能会对您有所帮助。

如何在 Python 中定义一个类

于 2010-06-03T03:51:24.950 回答