Lecture Notes on 9 Feb 2022 import random class Card (object): RANKS = (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) SUITS = ('C', 'D', 'H', 'S') # constructor 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' # string representation of a Card object 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 = str (self.rank) return rank + self.suit # equality tests 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): # constructor def __init__ (self, num_decks = 1): self.deck = [] for i in range (num_decks): for suit in Card.SUITS: for rank in Card.RANKS: card = Card (rank, suit) self.deck.append (card) # shuffle the deck def shuffle (self): random.shuffle (self.deck) # deal a card def deal (self): if (len(self.deck) == 0): return None else: return self.deck.pop(0) class Poker (object): # constructor def __init__ (self, num_players = 2, num_cards = 5): self.deck = Deck() self.deck.shuffle() self.players_hands = [] self.numCards_in_Hand = num_cards # deal all the hands for i in range (num_players): hand = [] for j in range (self.numCards_in_Hand): hand.append (self.deck.deal()) self.players_hands.append (hand) # simulate the play of the game def play (self): # sort the hands of each player and print the hand for i in range (len(self.players_hands)): sorted_hand = sorted (self.players_hands[i], reverse = True) self.players_hands[i] = sorted_hand hand_str = '' for card in sorted_hand: hand_str = hand_str + str(card) + ' ' print ('Player ' + str(i + 1) + ': ' + hand_str) # determine the type of hand # determine the winner def main(): # prompt the user to input the number of players num_players = int (input ('Enter the number of players: ')) while ((num_players < 2) or (num_players > 6)): num_players = int (input ('Enter the number of players: ')) # create the Poker object game = Poker (num_players) # play the game game.play() main()