2014-02-07 2 views
0

나는 꽤 좋은 파이썬 책을 읽고 있으며, 일부 카드 게임에 대한 시뮬레이션을 만드는 법을 가르쳐줍니다. 아래에서 볼 수 있듯이 갑판에있는 모든 카드 조합을 색인화하기 위해 "카드"클래스를 만들었습니다. 그런 다음 "카드"의 카드 조합에서 갑판을 만드는 "갑판"클래스를 만들었습니다. 내 질문은, 내가 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에 속성을 할당해야합니다.

둘째, 난 당신이 같은 일을하는지 추측하고있어 :

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 메서드는 기본적으로 파이썬의 개체가 ids가 동일한 경우에만 동등합니다. 예 :

A와 B 모두 Card(1,1)으로 생성 되더라도
>>> a = Card(1,1) 
>>> b = Card(1,1) 

>>> a == a 
True 
>>> id(a) == id(a) 
True 

>>> a == b 
False 
>>> id(a) == id(b) 
False 

들은 동일하지 않다. if card in self.cards:을 시도하면 파이썬이 묻습니다. self.cards에 정확한 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 .__ init__'의 끝에서 random.shuffle (self.cards)'를 호출 한 다음'removeCard' 함수를'self.cards.pop()'를 사용하여'drawCard' 함수에 묶기 만하면됩니다. 실제 카드 갑판에서 주어진 카드를 제거하는 것은 일반적으로 다른 방법으로 필요하지 않습니다. –

+0

제가이 일을 얼마나 오래하는지 말할 수 없습니다. 큰 충고! – srhoades28