1 # Cards Module
    2 # Basic classes for a game with playing cards
    3 
    4 class Card(object):
    5     """ A playing card. """
    6     RANKS = ["A", "2", "3", "4", "5", "6", "7",
    7              "8", "9", "10", "J", "Q", "K"]
    8     SUITS = ["c", "d", "h", "s"]
    9 
   10     def __init__(self, rank, suit, face_up = True):
   11         self.rank = rank
   12         self.suit = suit
   13         self.is_face_up = face_up
   14 
   15     def __str__(self):
   16         if self.is_face_up:
   17             rep = self.rank + self.suit
   18         else:
   19             rep = "XX"
   20         return rep
   21 
   22     def flip(self):
   23         self.is_face_up = not self.is_face_up
   24       
   25 class Hand(object):
   26     """ A hand of playing cards. """
   27     def __init__(self):
   28         self.cards = []
   29 
   30     def __str__(self):
   31         if self.cards:
   32            rep = ""
   33            for card in self.cards:
   34                rep += str(card) + "\t"
   35         else:
   36             rep = "<empty>"
   37         return rep
   38 
   39     def clear(self):
   40         self.cards = []
   41 
   42     def add(self, card):
   43         self.cards.append(card)
   44 
   45     def give(self, card, other_hand):
   46         self.cards.remove(card)
   47         other_hand.add(card)
   48 
   49 class Deck(Hand):
   50     """ A deck of playing cards. """
   51     def populate(self):
   52         for suit in Card.SUITS:
   53             for rank in Card.RANKS: 
   54                 self.add(Card(rank, suit))
   55 
   56     def shuffle(self):
   57         import random
   58         random.shuffle(self.cards)
   59 
   60     def deal(self, hands, per_hand = 1):
   61         for rounds in range(per_hand):
   62             for hand in hands:
   63                 if self.cards:
   64                     top_card = self.cards[0]
   65                     self.give(top_card, hand)
   66                 else:
   67                     print("Can't continue deal. Out of cards!")
   68 
   69 
   70 
   71 if __name__ == "__main__":
   72     print("This is a module with classes for playing cards.")
   73     input("\n\nPress the enter key to exit.")
   74