# # This module has classes for a set of playing cards and a deck of cards # # the Card class # methods: # __init__: generate a card with given suit rank # __str__: print the given card # cmp: compare 2 cards, return 1, 0, -1 as appropriate # order is S, H, D, C (suits) and A, K, Q, J, 10, ... (ranks) # rcmp: compare the rank of 2 cards, return 1, 0, -1 as appropriate # scmp: compare the suit of 2 cards, return 1, 0, -1 as appropriate # class Card: """Playing Cards""" # first, the suits and ranks, in order # note the order is important as it is used to compare suits = [ "C", "D", "H", "S" ] ranks = [ "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", ] # create a card; default is AS def __init__(self, suit="S", rank="A"): self.suit = suit self.rank = rank # generate a string representing the card def __str__(self): return self.suit + self.rank # length of string representing card # this is useful for lining up cards in output # as all but the 10's are 2 chars and the 10's are 3 chars def len(self): return(len(self.__str__())) # compare suits def scmp(self, c): # get the number of the suits # for each card; the sign of the # difference is the result ss = Card.suits.index(self.suit) cs = Card.suits.index(c.suit) return ss - cs # compare ranks def rcmp(self, c): # get the number of the ranks # for each card; the sign of the # difference is the result sr = Card.ranks.index(self.rank) cr = Card.ranks.index(c.rank) return sr - cr # compare, taking rank and suit into account def cmp(self, c): # test suits sv = self.scmp(c) if rv != 0: return rv # test ranks; if you get here, whatever # this returns is it! return self.rcmp(c) # # the Deck class # represent it as a list of cards # methods: # __init__: initialize deck with 52 cards in order (low to high) # __str__: generate a string of cards with blanks between them # shuffle: shuffle the deck # requires: # class Cards (above) # import random (for the shuffling) # class Deck: # create a deck of cards def __init__(self): self.cards = [ ] for s in Card.suits: for r in Card.ranks: self.cards.append(Card(s, r)) # print the cards in the deck separated by blanks # note we have a leading blank so we strip it def __str__(self): s = '' for c in self.cards: s = s + ' ' + str(c) return s.strip() # shuffle the deck using the random module def shuffle(self): import random rng = random.Random() rng.shuffle(self.cards) # # the testing and debugging routines # omit this if it's being imported # if __name__ == "__main__": d = Deck() print("Here's the deck of cards:") print(d) print("Now we shuffle them:") d.shuffle() print(d)