class Card(object): """ a playing card """ VALUES = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"] def __init__(self, value, suit): self.value = value self.suit = suit def __str__(self): s = "<" s += self.value + " of " + self.suit s += ">" return s class Hand(object): """ a hand of playing card objects """ def __init__(self): self.cards = [] def add(self, card): self.cards.append(card) def discard(self, card): if card in self.cards: self.cards.remove(card) def __str__(self): if self.cards: s = "" for card in self.cards: s += str(card) + " " else: s = "" return s def clear(self): self.cards = [] class Deck(Hand): """ a deck of playing cards """ def reset(self): self.clear() for suit in Card.SUITS: for value in Card.VALUES: self.add(Card(value, suit)) def shuffle(self): import random random.shuffle(self.cards) def deal(self, hands, cards_per_hand = 1): for rounds in range(cards_per_hand): for hand in hands: if self.cards: c = self.cards[0] self.discard(c) hand.add(c) else: print "No more cards!" print Card.VALUES print Card.VALUES[2] c1 = Card("Ace", "Clubs") # Card.__init__(c1, "Ace", "Clubs") print c1 c2 = Card("2", "Clubs") c3 = Card("8", "Diamonds") h1 = Hand() print h1 h1.add(c1) h1.add(c2) h1.add(c3) print h1 h1.discard(c2) print h1 h1.discard( Card("8", "Diamonds") ) print h1 deck = Deck() deck.reset() print deck deck.shuffle() print deck h1 = Hand() h2 = Hand() h3 = Hand() hands = [h1, h2, h3] deck.deal(hands, cards_per_hand = 2) print deck print h1 print h2 print h3