import random # this class represents a standard playing card class Card (object): RANKS = (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) SUITS = ('C', 'D', 'H', 'S') # initialize a card object with given rank (int) and suit (string) 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 takes no arguments and returns a string 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 # following functions take a card object as argument and return a boolean 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 representing a deck of cards class Deck (object): # initializes a deck object # self.deck is a list of cards in standard sorted order def __init__ (self): self.deck = [] for suit in Card.SUITS: for rank in Card.RANKS: card = Card (rank, suit) self.deck.append (card) # takes no arguments and does not return any value # randomly shuffles the cards in the deck def shuffle (self): random.shuffle (self.deck) # takes no arguments # returns a card object at the front of self.deck # self.deck has one less card after the deal function is called def deal (self): if (len(self.deck) == 0): return None else: return self.deck.pop(0) # class representing a game of poker class Poker (object): # takes two arguments num_players (number of players) and # num_cards (number of cards in a hand) # each player is represented by a list of num_cards card objects # self.all_hands is a list of lists of num_cards card objects def __init__ (self, num_players, num_cards): self.deck = Deck() self.deck.shuffle() self.all_hands = [] self.numCards_in_Hand = num_cards for i in range (num_players): hand = [] for j in range (self.numCards_in_Hand): hand.append (self.deck.deal()) self.all_hands.append (hand) # simulates the play of the game # sorts each hand and assigns points to each hand # returns a list of players in decreasing order of points # example: if there are three players and the points are 20, 16, 18 # return [1, 3, 2] def play (self): # sort the hands of each player and print for i in range (len(self.all_hands)): sorted_hand = sorted (self.all_hands[i], reverse = True) self.all_hands[i] = sorted_hand hand = '' for card in sorted_hand: hand = hand + str (card) + ' ' print ('Player ' + str (i + 1) + " : " + hand) # determine the each type of hand and print points_hand = [] # create list to store points for each hand # determine winner and print # determine if a hand is a royal flush # takes as argument a list of 5 card objects and returns a boolean def is_royal (self, hand): same_suit = True for i in range (len(hand) - 1): same_suit = same_suit and (hand[i].suit == hand[i + 1].suit) if (not same_suit): return False rank_order = True for i in range (len(hand)): rank_order = rank_order and (hand[i].rank == 14 - i) return (same_suit and rank_order) ''' def is_straight_flush (self, hand): ... def is_four_kind (self, hand): ... def is_full_house (self, hand): ... def is_flush (self, hand): ... def is_straight (self, hand): ... def is_three_kind (self, hand): ... def is_two_pair (self, hand): ... ''' # determine if a hand is one pair # takes as argument a list of 5 card objects and returns a boolean def is_one_pair (self, hand): for i in range (len(hand) - 1): if (hand[i].rank == hand[i + 1].rank): return True return False ''' def is_high_card (self, hand): ... ''' def main(): # prompt user to enter the number of players num_players = int (input ('Enter number of players: ')) while ((num_players < 2) or (num_players > 6)): num_players = int (input ('Enter number of players: ')) # create the Poker object game = Poker (num_players) # play the game (poker) game.play() # do not remove this line above main() if __name__ == '__main__': main()