A Poker Hands There are many variants of the gambling cardga
A. 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 Jokers or special 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 should include at least one instance variable: a list of 52 Card objects. 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 .shuffle() from the random module) c. dealcards(n) : Return the \"next\" n Card objects from the deck in a list. Note that after the last card in the deck has been dealt, a new shuffled deck should be created automatically! If n is greater than the number of cards remaining in the deck, the new deck should not include the cards at the end that have just been dealt. 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(deck): get 5 new cards from the specified deck 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 and shuffle it. b. Instantiate a single Pokerhand object. c. Use a loop to \"deal\" 100,000 5-card Pokerhands and use a dictionary to count the frequency of occurrence of each of the 9 possible poker hand rankings d. 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
Solution
Hello,
Hope the below code helps ,
1. Class Card:
public class Card {
public final static int SPADES = 0;
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;
public final static int JOKER = 4;
public final static int ACE = 1;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;
private final int suit;
private final int value;
public Card() {
suit = JOKER;
value = 1;
}
public Card(int theValue, int theSuit) {
if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS &&
theSuit != CLUBS && theSuit != JOKER)
throw new IllegalArgumentException(\"Illegal playing card suit\");
if (theSuit != JOKER && (theValue < 1 || theValue > 13))
throw new IllegalArgumentException(\"Illegal playing card value\");
value = theValue;
suit = theSuit;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String getSuitAsString() {
switch ( suit ) {
case SPADES: return \"Spades\";
case HEARTS: return \"Hearts\";
case DIAMONDS: return \"Diamonds\";
case CLUBS: return \"Clubs\";
default: return \"Joker\";
}
}
public String getValueAsString() {
if (suit == JOKER)
return \"\" + value;
else {
switch ( value ) {
case 1: return \"Ace\";
case 2: return \"2\";
case 3: return \"3\";
case 4: return \"4\";
case 5: return \"5\";
case 6: return \"6\";
case 7: return \"7\";
case 8: return \"8\";
case 9: return \"9\";
case 10: return \"10\";
case 11: return \"Jack\";
case 12: return \"Queen\";
default: return \"King\";
}
}
}
public String toString() {
if (suit == JOKER) {
if (value == 1)
return \"Joker\";
else
return \"Joker #\" + value;
}
else
return getValueAsString() + \" of \" + getSuitAsString();
}
}
2. Class DeckofCards:
public class DeckOfCards
{
public static final int NCARDS = 52;
private Card[] deckOfCards;
private int currentCard;
public DeckOfCards( )
{
deckOfCards = new Card[ NCARDS ];
int i = 0;
for ( int suit = Card.SPADE; suit <= Card.DIAMOND; suit++ )
for ( int rank = 1; rank <= 13; rank++ )
deckOfCards[i++] = new Card(suit, rank);
currentCard = 0;
}
public void shuffle(int n)
{
int i, j, k;
for ( k = 0; k < n; k++ )
{
i = (int) ( NCARDS * Math.random() );
j = (int) ( NCARDS * Math.random() );
/*swap the randomly picked cards*/
Card tmp = deckOfCards[i];
deckOfCards[i] = deckOfCards[j];
deckOfCards[j] = tmp;;
}
currentCard = 0; // Reset current card to deal
}
public Card deal()
{
if ( currentCard < NCARDS )
{
return ( deckOfCards[ currentCard++ ] );
}
else
{
System.out.println(\"Out of cards error\");
return ( null ); // Error;
}
}
public String toString()
{
String s = \"\";
int k;
k = 0;
for ( int i = 0; i < 4; i++ )
{
for ( int j = 1; j <= 13; j++ )
s += (deckOfCards[k++] + \" \");
s += \"\ \";
}
return ( s );
}
}



