Lecture Notes 01 Feb 2017 * We completed the skeleton of the Poker class * Modify the dealing of hands so that the cards are dealt round robin * Modify the sorting of Cards routines so that Cards are grouped in Suits and then within a suit sorted by rank import random class Card (object): RANKS = (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) SUITS = ('C', 'D', 'H', 'S') def __init__ (self, rank = 12, suit = 'S'): if (rank in Card.RANKS): self.rank = rank else: self.rank = 12 if (suit in Card.SUITS): self.suit = suit else: self.suit = 'S' def __str__ (self): if self.rank == 14: rank = 'A' elif self.rank == 13: rank = 'K' elif self.rank == 12: rank = 'Q' elif self.rank == 11: rank = 'J' else: rank = self.rank return str (rank) + self.suit def __eq__ (self, other): return (self.rank == other.rank) def __ne__ (self, other): return (self.rank != other.rank) def __lt__ (self, other): return (self.rank < other.rank) def __le__ (self, other): return (self.rank <= other.rank) def __gt__ (self, other): return (self.rank > other.rank) def __ge__ (self, other): return (self.rank >= other.rank) class Deck (object): def __init__ (self): self.deck = [] for suit in Card.SUITS: for rank in Card.RANKS: card = Card (rank, suit) self.deck.append (card) def shuffle (self): random.shuffle (self.deck) def deal (self): if (len(self.deck) == 0): return None else: return self.deck.pop(0) class Poker (object): def __init__ (self, num_players): self.deck = Deck() self.deck.shuffle () self.hands = [] numCards_in_Hand = 5 # deal the hand to each player for i in range (num_players): hand = [] for j in range (numCards_in_Hand): hand.append (self.deck.deal()) self.hands.append (hand) def play (self): for i in range (len (self.hands)): sortedHand = sorted (self.hands[i], reverse = True) hand = '' for card in sortedHand: hand = hand + str(card) + ' ' print ('Hand ' + str (i + 1) + ': ' + hand) def main(): num_players = int (input ('Enter number of players: ')) while (num_players < 2 or num_players > 6): num_players = int (input ('Enter number of players: ')) game = Poker (num_players) game.play() main()