2010-06-03 53 views
1

我是Python新手,這是我第一次問一個stackoverflow問題,但是很長時間的讀者。我正在開發一個簡單的基於卡片的遊戲,但在管理我的Hand類的實例時遇到了麻煩。如果你看下面你可以看到手類是一個簡單的卡片容器(這只是int值),每個Player類都包含一個手類。但是,無論何時創建我的Player類的多個實例,它們都似乎操縱Hand類的單個實例。從我在C和Java中的經驗看來,我似乎讓我的Hand類變成靜態的。如果有人可以幫助解決這個問題,我將非常感激。在Python中管理實例

謝謝 薩德

澄清:這種情況的一個例子是

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

,這將導致: [15,21]
即使只有一個卡被向對

手工類:

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 

Player類

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

從我在C和Java的經驗看來,我不知怎麼做我的Hand類是靜態的。

其實,這基本上是你在做什麼。那麼,並不是真的讓這個類變成靜態的,而是讓這個變量靜態的。

當你寫的聲明是這樣的:

class Hand: 
    cards = [] 

變量(cards)與的類相關聯,而不是與實例。爲了類比Java,Python類中不屬於該類方法的每個語句基本上都在靜態初始化器中運行。你幾乎可以把它像這樣:

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

(僅僅是一個粗略的比喻,當然)

在Python中創建一個實例變量,你必須將其設置爲實例的屬性,要求您等待,直到您參考實例。在實踐中,這意味着你初始化它在構造函數中,像這樣:

class Hand: 
    def __init__(self): 
     self.cards = [] 
+0

非常感謝!我無法告訴你有多大的幫助。 – BeensTheGreat 2010-06-03 14:22:15

3

你的問題很簡單

如果分配名單Python類的身體,當你追加項目,他們將存儲在Class級別,而不是實例級別。

您可以通過添加行解決這個問題:

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

這是蟒蛇陷阱的一個非常著名的情況下,我建議你閱讀: http://zephyrfalcon.org/labs/python_pitfalls.html

+0

精彩文章,謝謝。 – BeensTheGreat 2010-06-03 14:22:44

0

至於其他的答案中提到的,你對類變量和實例變量感到困惑。我建議您查看Python類如何工作的基礎知識。這是我爲另一個問題寫的答案;讀這可能會幫助你。

How to define a class in Python