Question: PYTHON PROGRAMMING card.py code import collections import itertools import random SUIT_LIST = (Hearts, Spades, Diamonds, Clubs) NUMERAL_LIST = (2, 3, 4, 5, 6, 7, 8,

PYTHON PROGRAMMING

card.py code

import collections import itertools import random

SUIT_LIST = ("Hearts", "Spades", "Diamonds", "Clubs") NUMERAL_LIST = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace")

class card: def __init__(self, numeral, suit): self.numeral = numeral self.suit = suit self.card = self.numeral, self.suit def __repr__(self): return self.numeral + "-" + self.suit

def __lt__(self, other): return self.rank < other.rank

def __eq__(self, other): return self.rank == other.rank

deck.py code

class deck(set): def __init__(self): for numeral, suit in itertools.product(NUMERAL_LIST, SUIT_LIST): self.add(card(numeral, suit)) def get_card(self): a_card = random.sample(self, 1)[0] self.remove(a_card) return a_card def get_hand(self, number_of_cards=5): if number_of_cards == 5: return poker_hand([self.get_card() for x in range(number_of_cards)]) else: raise NotImplementedError

for i in range(100000): print(deck().get_hand())

constants.py

# Poker hand types for the PokerHand class. UNCLASSIFIED = 0 NO_PAIR = 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 = 9

Deliverables: card.py, deck.py, test2a.py, constants.py, pokerhand.py, test2b.py, test3.py

(1) In the test2a.py file, write unit tests to test the _lt_ and _eq_ methods in card.py.

(2) Use a copy from constants.py which contains constants for the PokerHand class.

(3) Implement a PokerHand class in the pokerhand.py module that inherits from the base class Deck.

- Use these import statements at the top of the pokerhand.py module:

from card import Card from deck import Deck from constants import *

- Define an _init_ method with the header: def _init_(self,the_array):

that (i) initializes the _cards instance variable to [ ], (ii) appends each Card object in the_array to the _cards array, (iii) initializes the instance variable hand_type to UNCLASSIFIED.

(4)

Implement the instance method classify, which determines the hand type of the PokerHand object. This source code contains the complete source code to check for a hand of type FOUR_OF_A_KIND and hints for completing the other hand types:

def classify(self): self._cards.sort( ) if # ... code to classify hand of type STRAIGHT_FLUSH: elif self._cards[0] == self._cards[1] and \ self._cards[1] == self._cards[2] and \ self._cards[2] == self._cards[3]: self.hand_type = FOUR_OF_A_KIND elif self._cards[1] == self._cards[2] and \ self._cards[2] == self._cards[3] and \ self._cards[3] == self._cards[4]: self.hand_type = FOUR_OF_A_KIND elif # ... code to classify hand of type FULL_HOUSE: elif # ... code to classify hand of type FLUSH: elif # ... code to classify hand of type STRAIGHT: elif # ... code to classify hand of type THREE_OF_A_KIND: elif # ... code to classify hand of type TWO_PAIR: elif # ... code to classify hand of type ONE_PAIR: else: self.hand_type = NO_PAIR 

Note: self._cards.sort( ) works because you added the __lt__ method to the card class.

(5) Write unit tests to test PokerHand class

(6) In the test3.py file, create a new array like this:

counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

This counts array will keep track of how many poker hands of each type are obtained.

(7) In the test3.py file, repeat the following 2,000 times:

Create a new deck object.

Shuffle the deck object.

Deal out five poker hands. Use an array to hold the five PokerHandobjects.

Classify the five poker hands by calling the PokerHand#classify method for each hand.

Update the counts array like this:

for ph in poker_hands counts[ph.hand_type] += 1 end 

Compare your results to the expected values shown in the Poker Hands in Rank Order section. Here is some pseudocode that you can translate. Recall that if you want the range to be the values 0, 1, 2, 3, 4, you need to use range(0, 5). 0 is the starting point and 5 is the length of the range.

import constants module Create array named counts, initialized to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] repeat 2,000 times: Create a new Deck object. Call it deck. Shuffle the deck object. Create an an empty array like this: poker_hands = [ ] repeat 5 times: Create an empty array called arr. repeat 5 times: Deal a card from deck and append it to arr. end Create a new PokerHand object ph using the cards in arr and append ph to poker_hands. end end for each ph in the poker_hands array: invoke the classify method of ph. end for each ph in the poker_hands array: counts[ph.hand_type] += 1 end end Print out the number of occurances of each hand type. 

Here is an example of the beginning of the output printing the number of occurances of each hand type:

print("Number of Occurrences of each Hand Type") print("=======================================") print(f"Straight Flush: {counts[STRAIGHT_FLUSH]}") print(f"Four of a Kind: {counts[FOUR_OF_A_KIND]}") ...

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!