""" cards.py Jim Mahoney | Nov 6 class exercise """ suit = {'s': 'spades', 'h': 'hearts', 'c': 'clubs', 'd': 'diamonds'} suits = ['c', 'd', 'h', 's'] rank = {1: 'ace', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'jack', 12: 'queen', 13: 'king'} ranks = range(1,14) class Card(object): """ A playing card. >>> print Card(3, 's') three of spades >>> Card(1, 's') > Card(5, 'd') True """ def __init__(self, rank, suit): self.rank = rank self.suit = suit def poker_rank(self): """ Return 2...14 for two...ace """ if self.rank == 1: return 14 else: return self.rank def __str__(self): return "{} of {}".format(rank[self.rank], suit[self.suit]) def __cmp__(self, other): return cmp(self.poker_rank(), other.poker_rank()) class Deck(object): def __init__(self): self.cards = [] for s in suits: for r in ranks: self.cards.append(Card(r,s)) self.shuffle() def shuffle(self): pass def deal_cards(self, how_many=5): """ Return a list of Cards""" return [Card(3, 's'), Card(7,'h')] # skeleton class Hand(object): def __init__(self, deck=False, n_cards=5): if deck: self.cards = deck.deal_cards(n_cards) def order(self): # sort the cards pass def __str__(self): return "a hand of cards" class PokerHand(Hand): def hand_type def __cmp__(self, other): return 1 def main(): print "Let's play poker!" the_deck = Deck() hand1 = PokerHand(deck=the_deck) print "The first hand is " + str(hand1) hand2 = PokerHand(deck=the_deck) print "The second hand is " + str(hand2) if hand1 > hand2: print "First player wins!!" elif hand2 > hand1: print "Second player wins!!" else: print "It's a draw!!!" print "And the house wins!!!!" if __name__=='__main__': import doctest doctest.testmod() main()