/** * 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; computeRank(); } /** * This method computes the rank of the hand * Input: none * Output: none */ private void computeRank() { 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() { String result = new String(""); for(int i = 0; i < hand.length; i++) { result += "(" + (i+1) + ") " + hand[i]; if(i < 4) result += "\n"; } return result; } /** * This method returns a string representation * of the rank of the hand. * Input: none * Output: String representation of a rank */ public String rankToString() { String result = new String(""); result += "This hand is a "; switch(this.getRank()) { case 0: result += "Bust"; break; case 1: result += "Pair"; break; case 2: result += "Two Pair"; break; case 3: result += "Three of a Kind"; break; case 4: result += "Straight"; break; case 5: result += "Flush"; break; case 6: result += "Full House"; break; case 7: result += "Four of a Kind"; break; case 8: result += "Straight Flush"; break; case 9: result += "Royal Flush"; break; } result += "!"; return result; } /** * 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() /** * draw * This method takes in an index and a card reference, and * replaces the original card at index i with Card c * If the index is out of the range 0-4, it does nothing * Input: int i representing the index (range of 0 - 4) * Card c representing the new card * Output: none */ public void draw(int i, Card c) { hand[i] = c; computeRank(); } //////////////////////////////////////////////CONTAINS METHODS/////////////////////////////////////////////////// /** * containsPair() * Input: none * Output: boolean -- true if the hand contains a pair, false otherwise */ public boolean containsPair() { for(int i = 0; i < sortedHand.length - 1; i++) if(sortedHand[i].getRank() == sortedHand[i+1].getRank()) return true; return false; } /** * containsTwoPair() * Input: none * Output: boolean -- true if the hand contains two pair, false otherwise */ public boolean containsTwoPair() { int countPair = 0; for(int i = 0; i < sortedHand.length - 1; i++) if(sortedHand[i].getRank() == sortedHand[i+1].getRank()) { countPair++; i++; } return (countPair > 1); } /** * containsThreeOfAKind() * Input: none * Output: boolean -- true if the hand contains three of a kind, false otherwise */ public boolean containsThreeOfAKind() { for(int i = 0; i < sortedHand.length - 2; i++) if(sortedHand[i].getRank() == sortedHand[i+1].getRank() && sortedHand[i+1].getRank() == sortedHand[i+2].getRank()) return true; return false; } /** * containsStraight() * Input: none * Output: boolean -- true if the hand contains a straight, false otherwise */ public boolean containsStraight() { if(!containsPair() && (sortedHand[4].getRank() - sortedHand[0].getRank() == 4)) return true; else if(sortedHand[0].getRank() == 2 && sortedHand[1].getRank() == 3 && sortedHand[2].getRank() == 4 && sortedHand[3].getRank() == 5 && sortedHand[4].getRank() == 14) return true; return false; } /** * containsFlush() * Input: none * Output: boolean -- true if the hand contains a flush */ public boolean containsFlush() { for(int i = 0; i < hand.length - 1; i++) if(hand[i].getSuit() != hand[i+1].getSuit()) return false; return true; } /** * Input: none * Output: boolean -- true if the hand contains a full house */ public boolean containsFullHouse() { if(sortedHand[0].getRank() == sortedHand[2].getRank() && sortedHand[3].getRank() == sortedHand[4].getRank()) return true; if(sortedHand[0].getRank() == sortedHand[1].getRank() && sortedHand[2].getRank() == sortedHand[4].getRank()) return true; return false; } /** * Input: none * Output: boolean -- true if the hand contains Four of a Kind */ public boolean containsFourOfAKind() { if(sortedHand[0].getRank() == sortedHand[3].getRank() || sortedHand[1].getRank() == sortedHand[4].getRank()) return true; return false; } /** * Input: none * Output: boolean -- true if the hand contains a straight flush */ public boolean containsStraightFlush() { if(this.containsStraight() && this.containsFlush()) return true; return false; } /** * Input: none * Output: boolean -- true if the hand contains a royal flush */ public boolean containsRoyalFlush() { if(this.containsStraightFlush() && sortedHand[0].getRank() == 10) return true; return false; } }