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

In Python,you are going to simulate dealing a Poker hand. A hand in Poker comes

ID: 3822173 • Letter: I

Question

In Python,you are going to simulate dealing a Poker hand. A hand in Poker comes from 5 cards out of a deck of cards. Each of those cards must be unique. That is a Poker hand can not have 2 identical cards in it.

We are once again going to simulate dealing with a deck of cards. Don't panic if you didn't get the card example from last week just right. I will give you the basic outline to set it up here. Plus, this version is going to be a little bit different. Even if you know very little about cards or if you struggled with the last exercise, we will simplify it quite a bit for this assignment.

NOTE: You might be tempted to use your card program from last week. You are welcome to do so, but I think you will find it much easier to simply create a new card program here since, as you will see, this one is much simpler. However, if you are pleased and confident with your previous card program, you are welcome to use it. But unless you are very confident, my suggestion is just to do the much simpler form I suggest here:

For this assignment, each card will be represented by two values:

A random number between 1 and 4.

A second random number between 1 and 13.

So each card will be something like: (1, 9) or (3, 11 ) or (4, 7) or (2, 1).

Each card must be stored as a tuple. So if your first random number is a 2, and your second random number is a 12, you would create a tuple with the values: (2, 12).

If you do NOT play cards, you may ignore the following: For people who play cards, you can think of the first random number as representing the suit (e.g.1 = Hearts etc) and the second number representing the value (e.g. 1 = Ace, 10=10, 11=Jack, etc).

Now for a Poker hand, we require 5 cards from the deck. However, the 5 cards must all be unique. Therefore, our "hand" will be represented by a Python set.Each time you create a tuple, place it into the set. Once your set contains 5 tuples, you now have a valid Poker hand.

Each time your program runs, you should create a set called 'hand'. This set should be filled with 5 cards. Output the set (i.e. the 5 cards) that was generated.

Once you have outputted the set (i.e. the Poker hand), ask the user if they want to repeat the program (i.e. if they want to be dealt another hand). Keep looping until they say: "No". Your program must work with either NO or nO or no or NO. Ie: It must not worry about case. In the "real world" of course, we'd also check to see if they wrote "Yes" and so on, but you don't have to do that here unless you want to.

Your output must look something like the following:

Sample run:

Card #1: Suit: 1 Card: 2
Card #2: Suit: 1 Card: 3
Card #3: Suit: 4 Card: 9
Card #4: Suit: 4 Card: 6
Card #5: Suit: 2 Card: 3

Would you like to play again? Sure

Card #1: Suit: 3 Card: 13
Card #2: Suit: 2 Card: 13
Card #3: Suit: 1 Card: 6
Card #4: Suit: 1 Card: 9
Card #5: Suit: 1 Card: 10

Would you like to play again? Okay

Card #1: Suit: 1 Card: 2
Card #2: Suit: 3 Card: 13
Card #3: Suit: 3 Card: 1
Card #4: Suit: 4 Card: 13
Card #5: Suit: 1 Card: 6

Would you like to play again? nO

>>>

So your output should display:

The word "Card #" followed by the number (from 1 to 5), and a colon

The word "Suit:" followed by the first random number

The word "Card:" followed by the second random number

You must use a format string to do this. Please review section 4.2 of your book (or any online resource) for format strings. However, your book gives a very good explanation in just a couple of pages, so I'd suggest you use that as your resource.

This program should exist inside a function called: pokerHand(). Don't forget to include a docstring.

Explanation / Answer

A)

private void sort() {
Arrays.sort(hand, Collections.reverseOrder());
if (isFourOfAKind()) {
sortFourOfAKind();
} else if (isFullHouse()) {
sortFullHouse();
} else if (isThreeOfAKind()) {
sortThreeOfAKind();
} else if (isTwoPair()) {
sortTwoPair();
} else if (isOnePair()){
sortOnePair();
}
}
private void sortFourOfAKind() {
if (hand[0].getGameValue() != hand[HAND_SIZE - 4].getGameValue()) {
swapCardsByIndex(0, HAND_SIZE - 1);   
}   
}
private void sortFullHouse() {
if (hand[0].getGameValue() != hand[HAND_SIZE - 3].getGameValue()) {
swapCardsByIndex(0, HAND_SIZE - 2);
swapCardsByIndex(HAND_SIZE - 4, HAND_SIZE - 1);   
}
}
private void sortThreeOfAKind() {
if (hand[0].getGameValue() != hand[HAND_SIZE - 3].getGameValue() && hand[HAND_SIZE - 1].getGameValue() != hand[HAND_SIZE - 3].getGameValue()) {
swapCardsByIndex(0, HAND_SIZE - 2);   
} else if (hand[0].getGameValue() != hand[HAND_SIZE - 3].getGameValue() && hand[HAND_SIZE - 4].getGameValue() != hand[HAND_SIZE - 3].getGameValue()) {
Arrays.sort(hand);   
swapCardsByIndex(HAND_SIZE - 1, HAND_SIZE - 2);   
}   
}
private void sortTwoPair() {
if (hand[0].getGameValue() != hand[HAND_SIZE - 4].getGameValue()) {   
for (int i = 0; i < HAND_SIZE - 1; i++) {
swapCardsByIndex(i, i + 1);   
}
} else if (hand[0].getGameValue() == hand[HAND_SIZE - 4].getGameValue() && hand[HAND_SIZE - 2].getGameValue() == hand[HAND_SIZE - 1].getGameValue()) {
swapCardsByIndex(HAND_SIZE - 3, HAND_SIZE - 1);
}   
}
private void sortOnePair() {
if (hand[HAND_SIZE - 4].getGameValue() == hand[HAND_SIZE - 3].getGameValue()) {
swapCardsByIndex(0, HAND_SIZE - 3);
} else if (hand[HAND_SIZE - 3].getGameValue() == hand[HAND_SIZE - 2].getGameValue()) {   
swapCardsByIndex(0, HAND_SIZE - 3);
swapCardsByIndex(HAND_SIZE - 4, HAND_SIZE - 2);
} else if (hand[HAND_SIZE - 2].getGameValue() == hand[HAND_SIZE - 1].getGameValue()) {
Arrays.sort(hand);
swapCardsByIndex(HAND_SIZE - 3, HAND_SIZE - 1);   
}
}
private void swapCardsByIndex(int index1, int index2) {
PlayingCard temp = hand[index1];
hand[index1] = hand[index2];
hand[index2] = temp;
}
public int getGameValue() {
String handValue = addPaddingToBinaryString(Integer.toBinaryString(getHandValue()));

for (int i = 0; i < HAND_SIZE; i++) {
handValue += addPaddingToBinaryString(Integer.toBinaryString(getCardValue(hand[i])));
}

return Integer.parseInt(handValue, 2);
}
private String addPaddingToBinaryString(String binary) {
switch (binary.length()) {
case 1: return "000" + binary;
case 2: return "00" + binary;
case 3: return "0" + binary;
default: return binary;
}
}
private int getHandValue() {
if (isRoyalFlush()) { Return ROYAL_FLUSH_VALUE; }   
if (isStraightFlush()) { Return STRAIGHT_FLUSH_VALUE; }
if (isFourOfAKind()) { Return FOUR_OF_A_KIND_VALUE; }
if (isFullHouse()) { Return FULL_HOUSE_VALUE; }
if (isThreeOfAKind()) { Return THREE_OF_A_KIND_VALUE; }
if (isTwoPair()) { Return TWO_PAIR_VALUE; }
if (isOnePair()) { Return ONE_PAIR_VALUE; }
return 0;
}
private int getCardValue(PlayingCard card) {
return card.getGameValue() - 2;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote