# card.py # Lillian Lee (LJL2) and Steve Marschner (srm2) # Mar 11, 2013 """Module providing a type for playing cards Implementation adapted from chapter 18 of the course text, _Think Python_, by Allen B. Downey. """ class Card(object): """Represents a standard playing card. Class variables: SUIT_NAMES [list of strings]: list of names of cards. NUM_SUITS [int]: length of SUIT_NAMES RANK_NAMES [list of strings except first item]: list of names for the ranks of cards, like 'Jack' or '9'. The first item is None. Instance variables: suit [int]: the suit of this particular card. The name of this suit is given by SUIT_NAMES[suit]. Always in 0..NUM_SUITS-1 rank [int]: the rank of this particular card. The name of this rank is given by RANK_NAMES[rank]. Always in 1..NUM_RANKS Hence, if we execute c = Card(0, 12), SUIT_NAMES[c.suit] is 'Clubs' and RANK_NAMES[c.rank] is '12' and this card is the Queen of Clubs. """ SUIT_NAMES = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] NUM_SUITS = len(SUIT_NAMES) # starts at None so that we can treat RANK_NAMES as a translation table: # RANK_NAME[1] is 'Ace', RANK_NAME[13] is 'King', etc. RANK_NAMES = [None, 'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] NUM_RANKS = 13 def __init__(self, suit, rank): """Initializer: A new card whose suit encoding is and rank encoding is . Example: if we execute c = Card(0, 12), then this card is the Queen of Clubs, since SUIT_NAMES[c.suit] is 'Clubs' and RANK_NAMES[c.rank] is 12. Preconditions: s in 0..NUM_SUITS-1 (inclusive) and r in 1..NUM_RANKS (inclusive)""" self.suit = suit self.rank = rank def __str__(self): """Readable string representation of this card. Example: '2 of Hearts'""" return Card.RANK_NAMES[self.rank] + ' of ' + Card.SUIT_NAMES[self.suit] def __eq__(self, other): """Equality determined by equality of attributes""" return (isinstance(other,Card) and (self.suit, self.rank) == (other.suit, other.rank)) def __ne__(self, other): """Equality determined by equality of attributes""" return not self.__eq__(other) def full_deck(): """Returns: list of the standard 52 cards""" output = [] # list of cards so far to be returned for suit in range(Card.NUM_SUITS): for rank in range(1,Card.NUM_RANKS+1): # skip the None value output.append(Card(suit,rank)) return output def print_cards(clist): """Print cards in list clist. Precondition: clist is a list of Cards, possibly empty.""" for c in clist: print str(c)