Question: IN PYTHON Overview In this project, you will continue with modeling the game of poker. In the previous project, you wrote code to rank 5-card
IN PYTHON
Overview
In this project, you will continue with modeling the game of poker. In the previous project, you wrote code to rank 5-card poker hands. In this version, there are two main changes:
Your code will use Object-Oriented Programming
Instead of just 5-card poker hands, we will have community cards and hole cards from which to make the best possible 5-card hand.
Objectives
Learn how to create and use classes and objects.
Get more practice turning pseudo-code algorithms into code.
Get more practice modularizing and using helper methods.
Get more practice with refactoring.
Get more practice choosing good variable and function/method names.
Get more practice testing and debugging.
Part I: OOP
In an OOP program, we create some objects and then do things with those objects. In order to create objects, you first need classes that specify the properties and behavior of the objects you will create.
You should refactor your Project 2 code so that it uses classes and objects. You must have the following classes (you can have others as well, but you must have these):
Card (in module card), an instance of which models a single card. You should have:
A constructor
Getter methods for getting the rank and suit
Other methods???
Deck (in module deck), an instance of which models a deck of cards. You should have:
A constructor
A method to deal a single card (i.e. remove a card from the deck and return it)
A method to shuffle the deck
Other methods???
PokerHand (in module poker_hand), an instance of which models a 5-card hand of cards. You should have:
A constructor
A method to add a card to a hand
compare_to, which compares this hand to another PokerHand. Here's the required signature:
def compare_to(self, other): """ Determines whether how this hand compares to another hand, returns positive, negative, or zero depending on the comparison. :param self: The first hand to compare :param other: The second hand to compare :return: a negative number if self is worth LESS than other, zero if they are worth the SAME, and a positive number if self is worth MORE than other """
Other methods???
Refactor your main function so that it uses Card, Deck, and Hand objects to play the game from Project 2.
Part II: Further Refactoring
AFTER you have refactored your Project 2 to use OOP, it's time to add functionality. The new game should make a simple game that draws 5 community cards from a deck and then repeatedly:
Draws two new (2-card) hands from the deck (the hole cards),
Shows the community cards and the hands to the player, asking them which hand is worth more (or if they have the same value), taking into account the community cards,
If the player was correct, they get one point and can continue.
If the player is incorrect, the game is over and the total score should be indicated.
The game is also over if there are not enough cards left to play another round.
The hole cards and the community cards are combined when we compare two hands. The value of a hand is the value of the best 5-card hand that can be made from 2 hole cards and 5 community cards, combined (so, choose the best 5 card hand from 7 cards).
Here's some possible output (your program need not output exactly like this):
The community cards are: Q of S ; 5 of D ; 2 of S ; 6 of C ; 7 of D Which of the following hands is worth more? Hand A: 6 of S ; 8 of C or Hand B: 4 of C ; 8 of S Enter a or b (or SPACE to indicate they are of equal value)a got input: a CORRECT!!! The community cards are: Q of S ; 5 of D ; 2 of S ; 6 of C ; 7 of D Which of the following hands is worth more? Hand A: K of S ; 2 of H or Hand B: 6 of H ; J of S Enter a or b (or SPACE to indicate they are of equal value)b got input: b CORRECT!!! The community cards are: Q of S ; 5 of D ; 2 of S ; 6 of C ; 7 of D Which of the following hands is worth more? Hand A: 9 of C ; J of D or Hand B: 9 of H ; J of C Enter a or b (or SPACE to indicate they are of equal value) got input: CORRECT!!!
New classes/methods
To support this new functionality, you must have the following new classes:
CommunityCards (in module community_cards), an instance of which represents the 5 community cards. You should have:
A constructor
Methods to add and remove cards from the community cards
Other methods???
StudPokerHand (in module stud_poker_hand), an instance of which represents a 2-card poker hand. You should have
A constructor, which should take as a parameter a reference to the CommunityCards to use.
Methods to add and remove cards to/from the hand.
compare_to, which compares this hand to another StudPokerHand. Here's the required signature:
def compare_to(self, other): """ Determines whether how this hand compares to another hand, returns positive, negative, or zero depending on the comparison. :param self: The first hand to compare :param other: The second hand to compare :return: a negative number if self is worth LESS than other, zero if they are worth the SAME, and a positive number if self is worth MORE than other """
Other methods???
Refactor your main function so that it now uses Card, Deck, CommunityCard, and StudPokerHandobjects.
Recommendations
Here are some recommendations to make the project easier to manage:
I recommend making StudPokerHand have the same API (Application Programmer's Interface) as PokerHand. Make sure that all of the public methods are implemented.
When you refactor to use StudPokerHand, the part of the code that plays that actual game should be able to call StudPokerHand methods in ways that completely match the methods it used to call on PokerHands.
The actual implementation of the public methods for StudPokerHand will be simple if you first make a helper method in the new StudPokerHand class that determines the best possible 5-card PokerHand from the available cards. Here's my version of that helper method:
def __get_best_five_card_hand(self): hands = self.__get_all_five_card_hands() best_so_far = hands[0] for i in range(1, len(hands)): if hands[i].compare_to(best_so_far) > 0: best_so_far = hands[i] return best_so_far
Notice that it uses another helper method to get all possible PokerHands from the hole cards and the community cards. You'll need to write that still.
With this helper method that gets the best PokerHand, our compare_to method can quite simple. How?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
