/** * PokerHand.java * This is a class that represents a five-card poker hand. It consists of an array of 5 card objects. * It also contains methods that determine the "rank" of the hand. */ public class PokerHand { private Card[] hand; private Card[] sortedHand; //this holds a sorted version of the hand, which makes it easier to determine the rank private int rank; /** * Constructor that takes in 5 Card objects to create a hand * Input: 5 Card objects */ public PokerHand(Card c1, Card c2, Card c3, Card c4, Card c5) { hand = new Card[5]; //create an array of 5 cards hand[0] = c1; hand[1] = c2; hand[2] = c3; hand[3] = c4; hand[4] = c5; createSortedHand(); //create the sorted version of the hand rank = 0; if(this.containsPair()) rank = 1; if(this.containsTwoPair()) rank = 2; if(this.containsThreeOfAKind()) rank = 3; if(this.containsStraight()) rank = 4; if(this.containsFlush()) rank = 5; if(this.containsFullHouse()) rank = 6; if(this.containsFourOfAKind()) rank = 7; if(this.containsStraightFlush()) rank = 8; if(this.containsRoyalFlush()) rank = 9; } /** * getHand() * Input: none * Output: A reference to the hand array * A method that returns a reference to the hand array */ public Card[] getHand() { return hand; } /** * getRank() * Input: none * Output: an integer representing the rank of the hand * A method that returns the rank of a hand */ public int getRank() { return rank; } /** * toString() * Input: none * Output: A string representation of this hand */ public String toString() { return "" + rank; } /** * createSortedHand() * Input: none * Output: none * This private method creates the sortedHand array, that is useful * in determining the rank of the hand */ private void createSortedHand() { sortedHand = new Card[5]; //create an array of 5 cards //copy the hand array into the sorted hand array for(int i = 0; i < hand.length; i++) sortedHand[i] = hand[i]; //sort the sortedhand array using selection sort int minIndex; Card temp; for(int i = 0; i < sortedHand.length; i++) { minIndex = i; for(int j = i+1; j < sortedHand.length; j++) { if(sortedHand[minIndex].isHigher(sortedHand[j])) { minIndex = j; } } temp = sortedHand[minIndex]; sortedHand[minIndex] = sortedHand[i]; sortedHand[i] = temp; } }//end createSortedHand() /** * containsPair() * Input: none * Output: boolean -- true if the hand contains a pair, false otherwise */ public boolean containsPair() { return false; } /** * containsTwoPair() * Input: none * Output: boolean -- true if the hand contains two pair, false otherwise */ public boolean containsTwoPair() { return false; } /** * containsThreeOfAKind() * Input: none * Output: boolean -- true if the hand contains three of a kind, false otherwise */ public boolean containsThreeOfAKind() { return false; } /** * containsStraight() * Input: none * Output: boolean -- true if the hand contains a straight, false otherwise */ public boolean containsStraight() { return false; } /** * containsFlush() * Input: none * Output: boolean -- true if the hand contains a flush */ public boolean containsFlush() { return false; } /** * Input: none * Output: boolean -- true if the hand contains a full house */ public boolean containsFullHouse() { return false; } /** * Input: none * Output: boolean -- true if the hand contains Four of a Kind */ public boolean containsFourOfAKind() { return false; } /** * Input: none * Output: boolean -- true if the hand contains a straight flush */ public boolean containsStraightFlush() { return false; } /** * Input: none * Output: boolean -- true if the hand contains a royal flush */ public boolean containsRoyalFlush() { return false; } }