/* * ScoreHand.java */ package boondock.holdem.Cards; //import boondock.holdem.cards.*; /** * The class used to score a player's hand * * @author Jonathan O'Keefe * @author Scott Semonian * @author Matt Brinza * @author Hamid R. Tahsildoost * * @author The Boondock Saints * @author No Limit Texas Holdem */ public class ScoreHand { private double score; private int highSet, lowSet; private boolean straight = false; private boolean flush = false; private boolean hasAce = false; private boolean lowAce = false; private Card cards[] = new Card[5]; private Card highCards[] = new Card[5]; public ScoreHand() { score = -1; } /** * Determines the score of a hand of Cards. * * @param newCards A 5-Card array of Cards to score. */ public ScoreHand(Card[] newCards) { for(int i = 0; i < 5; i++) { cards[i] = newCards[i]; } //check for ace for(int i = 0; i < 5; i++) if(cards[i].getNumRank() == 0 || cards[i].getNumRank() == 13) { cards[i].setNumRank(13); hasAce = true; score = 0; } //check PAIR for (int i = 0; i < 4; i++) { for (int j = i+1 ; j < 5 ; j++) { if (cards[i].getNumRank() == cards[j].getNumRank()) { score = 1; lowSet = 0; highSet = cards[i].getNumRank(); } } } //check TWO PAIR int pairs = 0; for (int i = 0 ; i < 4 ; i++) { for (int j = i+1 ; j < 5 ; j++) { if (cards[i].getNumRank() == cards[j].getNumRank()) { pairs++; if (cards[i].getNumRank() != highSet) if(cards[i].getNumRank() < highSet) lowSet = cards[i].getNumRank(); else { lowSet = highSet; highSet = cards[i].getNumRank(); } break; } } } if (pairs > 1) score = 2; //check THREE OF A KIND for (int i = 0 ; i < 3 ; i++) { for (int j = i+1 ; j < 4 ; j++) { for (int k = j+1; k < 5; k++) { if (cards[i].getNumRank() == cards[j].getNumRank() && cards[j].getNumRank() == cards[k].getNumRank()) { score = 3; lowSet = 0; highSet = cards[i].getNumRank(); } } } } //check FLUSH int streak = 0; for(int i = 0; i < 4; i++) { if( (cards[i].getSuit()).equals(cards[i+1].getSuit()) ) { streak++; if(streak == 4) { score = 5; flush = true; } } else break; } //check FULL HOUSE int otherCard = -1; int diffCards = 0; for(int i = 1; i < 5; i++) { if((cards[0].getNumRank() != cards[i].getNumRank()) && (cards[i].getNumRank() != otherCard)) { otherCard = cards[i].getNumRank(); diffCards++; } } if(diffCards == 1) score = 6; //check FOUR OF A KIND for (int i = 0 ; i < 2 ; i++) { for (int j = i+1 ; j < 3 ; j++) { for (int k = j+1; k < 4; k++) { for (int n = k+1; n < 5; n++) { if (cards[i].getNumRank() == cards[j].getNumRank() && cards[k].getNumRank() == cards[n].getNumRank() && cards[i].getNumRank() == cards[n].getNumRank()) { lowSet = 0; highSet = cards[i].getNumRank(); score = 7; } } } } } //check STRAIGHT (at end due to possible additional Ace from arrange method) arrange(); int maxCards = 5; if(hasAce) maxCards = 6; streak = 0; for (int i = 0 ; i < maxCards-1 ; i++) { if (cards[i].getNumRank() == cards[i+1].getNumRank() + 1) streak++; else streak = 0; if (streak == 4) { if (i == 4) lowAce = true; score = 4; straight = true; break; } } //check STRAIGHT FLUSH and ROYAL FLUSH if(straight && flush) score = 8; Card temp[] = this.getHand(); for(int i = 0; i < 5; i++) { score += (temp[i].getNumRank())/100.0; } } private void arrange() { int max; Card temp; for(int i = 0; i < 5; i++) { max = i; for(int j = i+1; j < 5; j++) { if(cards[j].getNumRank() > cards[max].getNumRank()) max = j; } temp = cards[i]; cards[i] = cards[max]; cards[max] = temp; } //positions a duplicate Ace at beginning of ordered hand if player has an ace if(hasAce) { Card tempCards[] = cards; cards = new Card[6]; cards[5] = new Card(0, tempCards[0].getNumSuit()); for(int i = 0; i < 5; i++) { cards[i] = tempCards[i]; } } } /** * Returns the value of the current hand. * * @return The score of this hand. */ public double getScore() { return score; } /** * Returns the current hand. * * @return This hand. */ public Card[] getHand() { Card temp[] = new Card[5]; int start; if(lowAce) start = 1; else start = 0; for(int i = start; i < 5+start; i++) temp[i-start] = cards[i]; return temp; } /** * Returns the high ranked set of cards. * This is used in pair, two pair, and full house situations. * * @return The rank of the high set of cards (if applicable). */ public int getHighSet() { return highSet; } /** * Returns the low ranked set of cards. * This is used in two pair and full house situations. * * @return The rank of the high set of cards (if applicable). */ public int getLowSet() { return lowSet; } }