[Tutor] inheritance problem

Roelof Wobben rwobben at hotmail.com
Thu Sep 30 20:38:19 CEST 2010


hello, 

Im following this page : http://openbookproject.net/thinkcs/python/english2e/ch17.html

So i have this programm now : 
class Card:
    suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
    ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "Jack", "Queen", "King"]
    def __init__(self, suit=0, rank=0):
        self.suit = suit
        self.rank = rank
    def __str__(self):
        return (self.ranks[self.rank] + " of " + self.suits[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 deal(self, hands, num_cards=999):
        num_hands = len(hands)
        for i in range(num_cards):
            if self.is_empty(): break   # break if out of cards
            card = self.pop()           # take the top card
            hand = hands[i % num_hands] # whose turn is next?
            hand.add(card)              # add the card to the hand
    
    def shuffle(self):
        import random
        num_cards = len(self.cards)
        for i in range(num_cards):
            j = random.randrange(i, num_cards)
            self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
            
    def remove(self, card):
        if card in self.cards:
            self.cards.remove(card)
            return True
        else:
            return False
    def is_empty(self):
        return (len(self.cards) == 0)
class Hand(Deck):
    def __init__(self, name=""):
       self.cards = []
       self.name = name
    def add(self,card):
        self.cards.append(card)
    def deal(self, hands, num_cards=999):
        num_hands = len(hands)
        for i in range(num_cards):
            if self.is_empty(): break   # break if out of cards
            card = self.pop()           # take the top card
            hand = hands[i % num_hands] # whose turn is next?
            hand.add(card)              # add the card to the hand
class CardGame:
    def __init__(self):
        self.deck = Deck()
        self.deck.shuffle()
        
class OldMaidHand(Hand):
    def remove_matches(self):
        count = 0
        original_cards = self.cards[:]
        for card in original_cards:
            match = Card(3 - card.suit, card.rank)
            if match in self.cards:
                self.cards.remove(card)
                self.cards.remove(match)
                print "Hand %s: %s matches %s" % (self.name, card, match)
                count = count + 1
        return count
class OldMaidGame(CardGame):
    def play(self, names):
        # remove Queen of Clubs
        self.deck.remove(Card(0,12))
        # make a hand for each player
        self.hands = []
        for name in names:
            self.hands.append(OldMaidHand(name))
        # deal the cards
        self.deck.deal(self.hands)
        print "---------- Cards have been dealt"
        self.printHands()
        # remove initial matches
        matches = self.removeAllMatches()
        print "---------- Matches discarded, play begins"
        self.printHands()
        # play until all 50 cards are matched
        turn = 0
        numHands = len(self.hands)
        while matches < 25:
            matches = matches + self.playOneTurn(turn)
            turn = (turn + 1) % numHands
        print "---------- Game is Over"
        self.printHands()
    def remove_all_matches(self):
        count = 0
        for hand in self.hands:
            count = count + hand.remove_matches()
        return count
    def find_neighbor(self, i):
        numHands = len(self.hands)
        for next in range(1,numHands):
            neighbor = (i + next) % numHands
            if not self.hands[neighbor].is_empty():
                return neighbor
            
game = CardGame()
hand = OldMaidHand("frank")
deck = Deck()
game.deck.deal([hand], 13)
OldMaidHand.print_hands() 
 
But now Im getting this error message:
 
Traceback (most recent call last):
  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 126, in <module>
    game.deck.deal([hand], 13)
  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 24, in deal
    card = self.pop()           # take the top card
AttributeError: Deck instance has no attribute 'pop'

 
What went wrong here.
 
Roelof
         
             
 
 

 
     

     
    
 
    

    
 
 
     



  		 	   		  


More information about the Tutor mailing list