QuestionQuestion

Transcribed TextTranscribed Text

Advanced Data Structure On a teacher’s salary, it is necessary for your instructor to supplement his income by traveling to Las Vegas frequently to play Omaha, a variation of poker similar to Texas Hold-em. Omaha is a form of Poker where each player is dealt four cards, all players share five community cards and the players make the best five card hand they can. Players must use exactly two of their four cards in making their hand. Poker hands typically consist of five cards from a 52-card deck. The cards are identified by their rank and by their suit. Ranks are 2, 3, 4, 5, 6, 7, 8, 9, 10 (T), Jack (J), Queen (Q), King (K), Ace (A). Suits are Hearts (h), Diamonds (d), Clubs (c), Spades (s). In most forms of poker, hands are ranked in the order: 1 Pair: 5♥8♦8♣J♣K♠ Cards contain two cards of identical rank (and do not make a better hand). 2 Pair: 5♥8♦8♣J♣J♠ Cards contain pairs of two different ranks (and no better hand can be made). 3 of a kind: 2♥7♦7♣7♠Q♦ Cards contain three of the same rank (and nothing better). Straight: T♦J♣Q♠K♥A♠ All five cards can be arranged in sequential order (and nothing better). Flush: 3♠5♠9♠J♠K♠ All five cards are of the same suit Full House: K♠K♦A♠A♥A♦ One pair and one three of a kind simultaneously. Four of a kind: 3♠J♥J♦J♠J♣ You hold all the cards of one rank. Straight Flush: 2♠3♠4♠5♠6♠ Not only is it a straight... It’s a flush too!!! He hasn’t been doing well so he has decided to change the game to make it more interesting (If you’re going to play a losing game you might as well make it funny and confusing). In Crazy Omaha the rules are changed as follows: 1. Players are dealt five cards instead of four. This provides each player with a total of ten cards to make their hand from (including the usual five community cards). 2. Hands consist of six cards instead of five. Players may choose to use either two or three cards from their private cards to make their best six card hand. Of course, this makes for weird possibilities that aren’t possible in a 5-card hand such as two three of a kind (4♠4♦4♥J♠J♣J♥). So, to make the game funnier (and to confuse opponents so he wins more) the following hands and rankings have been established: Non-Rainbow: 2♣3♣4♣5♦8♦J♥ You don’t have at least one of each suit. Rainbow: 8♣4♥6♦Q♠K♠A♣ You have one of each suit. Swingers: K♠Q♠8♦3♥K♦Q♦ You have two sets of suited Kings and Queens. Monochromatic: 8♦4♦Q♥4♦9♥K♦ Your cards are either all black or all red. 3 pair: 4♠4♦8♣8♦9♥9♠ Cards consist of exactly three unique ranks. Monarchy: 4♠8♠T♥J♠Q♠K♠ You hold A Jack, Queen and King of the same suit and no other face cards. Even: 2♥4♠6♦8♣T♣T♥ All your cards are a 2,4,6,8 or 10. Odd: 3♠5♥7♦7♦9♣9♠ All of your cards are a 3,5,7 or 9. Flush: 3♠5♠6♠7♠J♠Q♠ All six of your cards are the same suit. Triplets: 3♠3♦3♥T♦T♣T♥ You have two different three of a kinds. Overfull house: 5♠5♦5♥5♣J♠J♣ Four of a kind and a pair. Homosapiens: J♠J♦J♣Q♠Q♦K♣ All your cards are face cards. Kingdom: 4♠8♠T♠J♠Q♠K♠ (Monarchy + flush) Same as a Monarchy but the remaining cards are of the same. Orgy: J♠J♦J♣J♥Q♠Q♥ All your cards are Jacks and Queens. Politics: J♠J♦Q♠Q♦K♠K♦ You hold two Monarchys. Dinner party: Q♠Q♦Q♥K♠K♦K♥ All your cards are suited kings and queens. All hand descriptions assume that you cannot simultaneously make a better hand than the one described. For Example: A♠A♦4♣7♥8♥8♣ does not count as 1 pair because it could also be interpreted as 2 pair (And a rainbow too!). In Hold’em poker games the cards “speak”; which is to say that, players are not responsible for identifying what they have; the dealer does that. Your task is to write a program that determines the ranked order of the hands and who beats who from a list of players. The input to your program is simply a list of command line arguments that identify the cards that have been dealt. Your program will be executed as illustrated in the following example: java Poker 2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js This example illustrates the input for five players playing a game. The first five cards (2♣3♠6♦7♠8♥) are the community cards and are shared by all players. The player’s individual cards are: Player 1: K♥J♦4♦J♣K♣ Player 2: Q♣T♣9♣J♥9♠ Player 3: 9♥9♦Q♠K♠J♠ The output your program should generate is simply: (This is example format only and not a correct result for the input example given, which is left as an exercise for the student.) 1: Player 3 2: Player 2 3: Player 1

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.*;

public class Poker {

int count = -5;
ArrayList<ArrayList<String>> players;

// Constructor
public Poker(int numPlayers) {
    players = new ArrayList<ArrayList<String>>();
    for (int i=0; i<numPlayers; i++) {
      players.add(new ArrayList<String>());
    }
}

// Method to add card
private void addCard(String card) {
    if (count < 0) {
      for (int i=0; i<players.size(); i++) {
       players.get(i).add(card);
      }
    } else {
      int i = count / 5;
      players.get(i).add(card);
    }
    count++;
}

// Method to return hand name
private String handName(int i) {
    i = scoreHand(i);
    if (i == 15) {
      return "Dinner Party";
    } else if (i == 14) {
      return "Politics";
    } else if (i == 13) {
      return "Orgy";
    } else if (i == 12) {
      return "Kingdom";
    } else if (i == 11) {
      return "Homosapiens";
    } else if (i == 10) {
      return "Overfull House";
    } else if (i == 9) {
      return "Triplets";
    } else if (i == 8) {
      return "Flush";
    } else if (i == 7) {
      return "Odd";
    } else if (i == 6) {
      return "Even";
    } else if (i == 5) {
      return "Monarchy";
    } else if (i == 4) {
      return "3 Pair";
    } else if (i == 3) {
      return "Monochromatic";
    } else if (i == 2) {
      return "Swingers";
    } else if (i == 1) {
      return "Rainbow";
    } else {
      return "Non-rainbow";
    }
}

// Method to score hand
private int scoreHand(int i) {
    List<String> c = players.get(i).subList(0,5);
    List<String> p = players.get(i).subList(5,10);
    if (dinnerParty(c,p)) {
       return 15;
    } else if (politics(c,p)) {
       return 14;
    } else if (orgy(c,p)) {
       return 13;
    } else if (kingdom(c,p)) {
       return 12;
    } else if (homosapiens(c,p)) {
       return 11;
    } else if (overfullHouse(c,p)) {
       return 10;
    } else if (triplets(c,p)) {
       return 9;
    } else if (flush(c,p)) {
       return 8;
    } else if (odd(c,p)) {
       return 7;
    } else if (even(c,p)) {
       return 6;
    } else if (monarchy(c,p)) {
       return 5;
    } else if (threePair(c,p)) {
       return 4;
    } else if (monochromatic(c,p)) {
       return 3;
    } else if (swingers(c,p)) {
       return 2;
    } else if (rainbow(c,p)) {
       return 1;
    } else if (nonrainbow(c,p)) {
       return 0;
    }
    return -1;
}

// Method to score all hands
public void scoreHands() {
      ArrayList<String> playersCopy = new ArrayList<String>();
      for (int i = 0; i < players.size(); i++) {
          playersCopy.add(String.valueOf(i));
      }
      int rank = 1;
      while(playersCopy.size() > 0) {
          int bestScore = -1;
          ArrayList<String> best = new ArrayList<String>();
          for (String s: playersCopy) {
             int i = Integer.parseInt(s);
             if (scoreHand(i) > bestScore) {
                  bestScore = scoreHand(i);
                  best = new ArrayList<String>();
                  best.add(s);
             } else if (scoreHand(i) == bestScore) {
                  best.add(s);
             }
          }
          if (best.size() > 1) {
             System.out.println("TIE");
          }
          for (String s: best) {
             int i = Integer.valueOf(s);
             System.out.println("Rank "+rank+": Player "+(i+1)+" -- Hand: "+handName(i));
             playersCopy.remove(s);
          }
          rank++;
      }
}

// Dinner Party: All your cards are suited kings and queens
private boolean dinnerParty(List<String> c, List<String> p) {
    int countC = 0;
    for (String s: c) {
      if ((s.charAt(0) == 'K') || (s.charAt(0) == 'Q')) {
       countC++;
      }
    }
    int countP = 0;
    for (String s: p) {
      if ((s.charAt(0) == 'K') || (s.charAt(0) == 'Q')) {
       countP++;
      }
      if (countP == 3) {
       break;
      }
    }
    int count = countC + countP;
    return (count >= 6 && countC >= 3);
}

// Politics: You hold two Monarchies
private boolean politics(List<String> c, List<String> p) {
    int[] countC = new int[4];
    for (String s: c) {
       if ((s.charAt(0) == 'K') || (s.charAt(0) == 'Q') || (s.charAt(0) == 'J')) {
            if (s.charAt(1) == 'h') {
                countC[0]++;
            } else if (s.charAt(1) == 'd') {
                countC[1]++;
            } else if (s.charAt(1) == 'c') {
                countC[2]++;
            } else if (s.charAt(1) == 's') {
                countC[3]++;
            }
       }
    }
    int[] countP = new int[4];
    for (String s: p) {
      if ((s.charAt(0) == 'K') || (s.charAt(0) == 'Q') || (s.charAt(0) == 'J')) {
       if (s.charAt(1) == 'h') {
          countP[0]++;
       } else if (s.charAt(1) == 'd') {
          countP[1]++;
       } else if (s.charAt(1) == 'c') {
          countP[2]++;
       } else if (s.charAt(1) == 's') {
          countP[3]++;
       }
      }
    }
    int count = 0;
    for (int i = 0; i < 4; i++) {
       if (countC[i] + countP[i] >= 3) {
            count++;
       }
    }
    return count >= 2;
}

// Orgy: All your cards are Jacks and Queens
private boolean orgy(List<String> c, List<String> p) {
    int countC = 0;
    for (String s: c) {
      if ((s.charAt(0) == 'Q') || (s.charAt(0) == 'J')) {
       countC++;
      }
    }
    int countP = 0;
    for (String s: p) {
      if ((s.charAt(0) == 'Q') || (s.charAt(0) == 'J')) {
       countP++;
      }
      if (countP == 3) {
       break;
      }
    }
    int count = countC + countP;
    return count >= 6;
}

// Kingdom: Monarchy + the remaining cards are of the same
private boolean kingdom(List<String> c, List<String> p) {
    int[] countC = new int[4];
    int[] countC2 = new int[4];
    for (String s: c) {
      if ((s.charAt(0) == 'K') || (s.charAt(0) == 'Q') || (s.charAt(0) == 'J')) {
       if (s.charAt(1) == 'h') {
          countC...

By purchasing this solution you'll be able to access the following files:
Solution.PNG and Solution.java.

50% discount

Hours
Minutes
Seconds
$40.00 $20.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats