Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON! It has to include all 3 object classes: Card, Carddeck, and Pokerhand

ID: 3689528 • Letter: I

Question

IN PYTHON! It has to include all 3 object classes: Card, Carddeck, and Pokerhand. Can only import random! Poker Hands There are many variants of the gambling card-game "Poker". The classic 5-card draw variant involves ranking a hand consisting of 5 cards from a 52-card deck of cards and then awarding the accumulated "pot" of money to the person with the highest ranking hand. Write a Python program that will simulate "dealing" 100,000 5-card hands and then report the frequency of occurrence of "natural" Poker hands. A "natural hand" is one that is dealt "as-is" without replacing any cards. You should first consult the following wikipedia page for a full description of the ranking of 5-card poker hands: http://en.wikipedia.org/wiki/List_of_poker_hands Requirements: You must do the following:

1. Construct an object class named Card that will represent individual playing cards from a standard 52- card deck (no Jokersorspecial cards). Each Card object will have two instance variables to represent the value and suit of the playing card. The values you choose are up to you. Include the following methods in the Card class: a. getValue() : Return the value of the card b. getSuit() : Return the suit of the card c. __str__() : Return a string representing the card value and suit

2. Construct an object class named Carddeck that will simulate a deck of playing cards. The Carddeck class must include two instance variables: a list of 52 Card objects and an integer "index" value to keep track of the position in the deck from which to deal the next card.

3. The Carddeck class must include the following methods:

a. __repr__() : Return a string with the deck displayed as a list

b. shuffle() : Shuffle the deck (use random.shuffle()) and set the next card index to zero

c. dealcard() : Return the "next" Card object from the deck. Note that after the last card in the deck has been dealt, the deck should be reshuffled automatically!

4. Construct a third object class named Pokerhand that will represent a 5-card poker hand of "cards" dealt from a Carddeck object. The Pokerhand class must include a single instance variable: a list containing 5 Cards.

5. The Pokerhand class must include the following methods: a. newHand(): get 5 new cards from the Carddeck b. __repr__ : Return the hand-list as a string c. rank() : Return the rank of the poker hand as an integer value as follows:

0 : High card

1 : One pair

2 : Two pair

3 : Three of a kind

4 : Straight

5 : Flush

6 : Full house

7 : Four of a kind

8 : Straight flush

Note that this may be somewhat challenging to construct. It will be much simpler if you use the Python container classes set and dict to evaluate the number of different suits and values in the hand. For example, if there is exactly 1 suit, then the hand must be some sort of 'flush'...

6. Write a non-pure function named main that will take no arguments and do the following:

a. Instantiate a single Carddeck object

b. Use a loop to "deal" 100,000 5-card hands from the Carddeck object and use a dictionary to count the frequency of occurrence of each of the 9 possible poker hand rankings c. Display the resulting counts using the hand "names" (strings) in order of least frequent to most frequent (see example)

Example: Straight Flush : 8 Four of a kind : 33 Full house : 163 Flush : 206 Straight : 407 Three of a kind : 2244 Two pair : 4850 One pair : 42270 High card : 49819

Explanation / Answer

import random class Deck(object): """Represent a deck of 52 playing cards.""" def __init__(self, start_shuffled): """Initializes a deck with 52 cards.""" suits=["H","S","D","C"] ranks= list ( range(2, 11)) + ["J", "Q", "K", "A"] self.cards=[] for suit in suits: for rank in ranks: self.cards.append(Card(suit, rank)) if start_shuffled: random.shuffle(self.cards) def __str__(self): res = [] for card in self.cards: res.append(str(card)) return ' '.join(res) def has_card(self, card): return card in self.cards def draw_random_card(self): card=random.choice(self.cards) self.cards.remove(card) return card def draw_hand(self, num_cards): cards=[ ] for _ in range(num_cards): cards.append(self.draw_random_card()) return cards class Card(object) : def __init__(self, s , r): self.suit=s self.rank=r def __str__(self): rep = self.rank + self.suit return rep def __eq__(self, other_card): t1 = self.suit, self.rank t2 = other_card.suit, other_card.rank return eq(t1, t2) def main(): deck1=Deck(True) print("All the cards in the deck:") print(deck1.cards) print("Does the deck have the Queen of Hearts? True or False") print(deck1.has_card(Card("H", "Q"))) card=deck1.draw_random_card() print("A random card from the deck:") print(card) if deck1.has_card(card): print("Something bad happened...") print(card, "Shouldn't be in the deck anymore.") print("A hand of six random cards:") print (deck1.draw_hand(6)) if __name__=="__main__": main()