/* PokerHand ClassName: PokerHand.java Purpose: Encompassing hand structure for poker Date: 10/11/2000 Written By: Jason Hertzog & Mike Dise Written For: Dr. R. Webster */ public class PokerHand { private Card[] hand = new Card[5]; private int card_count = 0; private boolean SortBySuit = true; public PokerHand(Card c1, Card c2, Card c3, Card c4, Card c5) { hand[0] = c1; hand[1] = c2; hand[2] = c3; hand[3] = c4; hand[4] = c5; card_count = 5; this.sortCards(); } public PokerHand(Card c1, Card c2, Card c3, Card c4, Card c5, String s) { hand[0] = c1; hand[1] = c2; hand[2] = c3; hand[3] = c4; hand[4] = c5; card_count = 5; changeSort(s); this.sortCards(); } public PokerHand() { } public void changeSort(String S) { if (S.toUpperCase() == "SUIT") { SortBySuit = true; } else { SortBySuit = false; } this.sortCards(); } public Card getCard(int i) { if (i<5 && i>-1) { return hand[i]; } else { return null; } } public boolean replaceCard(Card old, Card newer) { int i; for (i = 0; i < card_count; i++) { if (hand[i].isExactlyEqual(old)) { hand[i] = newer; break; } } if (i == card_count) { return false; } else { this.sortCards(); return true; } } public void deleteAll(Card old, Card newer) { for (int i = 0; i < card_count; i++) { hand[i] = null; } } public boolean addCard(Card add) { if (card_count == 5) { return false; } else { hand[card_count-1] = add; this.sortCards(); return true; } } public String toString() { String ret = ""; for (int i = 0; i < 5; i++) { ret = ret.concat(hand[i].toString()); } return ret; } public String toParsableString(String delimiter) { String ret = ""; for (int i = 0; i < 5; i++) { ret = ret.concat(hand[i].getSuit() + delimiter + hand[i].getRank()); if (i < 4) { ret = ret.concat(delimiter); } } return ret; } public String toString(String delimiter) { String ret = ""; for (int i = 0; i < 5; i++) { ret = ret.concat(hand[i].toString() + delimiter); } return ret; } private void sortCards() { if (!this.SortBySuit) { this.sortByRankCards(hand,0,card_count-1); } else { this.sortBySuitCards(hand,0,card_count-1); } } private void sortByRankCards (Card [] feld, int l, int r) { int i = l; int j = r; Card Pivot = feld[ (l+r) >> 1]; do { while(feld[i].isByRankLessThan(Pivot))i++; while (Pivot.isByRankLessThan(feld[j]))j--; if(i <= j){ Card temp = feld[j]; feld[j] = feld [i]; feld[i] = temp; i++; j--; } } while(i <= j); if( l < j) sortByRankCards(feld,l,j); if( i < r) sortByRankCards(feld,i,r); } private void sortBySuitCards (Card [] feld, int l, int r) { int i = l; int j = r; Card Pivot = feld[ (l+r) >> 1]; do { while(feld[i].isBySuitLessThan(Pivot))i++; while (Pivot.isBySuitLessThan(feld[j]))j--; if(i <= j){ Card temp = feld[j]; feld[j] = feld [i]; feld[i] = temp; i++; j--; } } while(i <= j); if( l < j) sortBySuitCards(feld,l,j); if( i < r) sortBySuitCards(feld,i,r); } public boolean isStraightFlush() { return (isFlush() & isStraight()); } public boolean isFourOfAKind() { boolean sort = SortBySuit; int lastNum = 0, currNum = 0; int ct = 0; SortBySuit = false; sortCards(); for (int i = 0; i < card_count; i++) { currNum = hand[i].getRank(); if (currNum == lastNum) { ct++; } else { lastNum = currNum; ct = 1; } if (ct == 4) break; } SortBySuit = sort; sortCards(); return (ct==4); } public boolean isFullHouse() { boolean sort = SortBySuit, triple = false, duple = false; SortBySuit = false; sortCards(); if (card_count == 5) { triple = (hand[0].getRank() == hand[1].getRank() && hand[1].getRank() == hand[2].getRank()) || (hand[2].getRank() == hand[3].getRank() && hand[3].getRank() == hand[4].getRank()); duple = (hand[0].getRank() == hand[1].getRank()) && (hand[3].getRank() == hand[4].getRank()); } SortBySuit = sort; sortCards(); return (triple & duple); } public boolean isFlush() { int ct = 0; char lastSuit = ' '; if (card_count == 5) { for (int i = 1; i < card_count; i++) { if (hand[i-1].getSuit() == hand[i].getSuit()) { ct++; } } } return (ct==4); } public boolean isStraight() { boolean sort = SortBySuit; SortBySuit = false; sortCards(); int ct = 0; if (card_count == 5) { for (int i = 1; i < card_count; i++) { if (hand[i-1].getRank() == hand[i].getRank()-1) { ct++; } } } SortBySuit = sort; sortCards(); return (ct==4); } public boolean isThreeOfAKind() { boolean sort = SortBySuit, triple = false; SortBySuit = false; sortCards(); if (card_count == 5) { triple = (hand[0].getRank() == hand[1].getRank() && hand[1].getRank() == hand[2].getRank()) || (hand[1].getRank() == hand[2].getRank() && hand[2].getRank() == hand[3].getRank()) || (hand[2].getRank() == hand[3].getRank() && hand[3].getRank() == hand[4].getRank()); } SortBySuit = sort; sortCards(); return (triple); } public boolean isTwoPair() { boolean sort = SortBySuit; int duples = 0; SortBySuit = false; sortCards(); if (card_count == 5) { if (hand[0].getRank() == hand[1].getRank()) duples++; if (hand[1].getRank() == hand[2].getRank()) duples++; if (hand[2].getRank() == hand[3].getRank()) duples++; if (hand[3].getRank() == hand[4].getRank()) duples++; } SortBySuit = sort; sortCards(); return (duples == 2); } public boolean isPair() { boolean sort = SortBySuit; int duples = 0; SortBySuit = false; sortCards(); if (card_count == 5) { for (int j = 0; j < 4; j++) if (hand[j].getRank() == hand[j+1].getRank()) duples++; } SortBySuit = sort; sortCards(); return (duples == 1); } public Card getHighCard() { boolean sort = SortBySuit; int duples = 0; SortBySuit = false; sortCards(); Card High = hand[4]; SortBySuit = sort; sortCards(); return High; } public boolean isBetterThan(PokerHand hand1, int compareType) { boolean retVal = false; switch (compareType) { case PokerWin.STRAIGHT_FLUSH: { retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); break; } case PokerWin.FULL_HOUSE: { boolean sort = SortBySuit; SortBySuit = false; sortCards(); hand1.changeSort(""); if (hand[2].getRank() != hand1.getCard(2).getRank()) { retVal = hand[2].getRank() > hand1.getCard(2).getRank(); } else { //not right need to dealer with dealer retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); } SortBySuit = sort; sortCards(); break; } case PokerWin.FLUSH: { retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); break; } case PokerWin.STRAIGHT: { retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); break; } case PokerWin.FOUR_OF_A_KIND: { boolean sort = SortBySuit; SortBySuit = false; sortCards(); hand1.changeSort(""); if (hand[2].getRank() != hand1.getCard(2).getRank()) { retVal = hand[2].getRank() > hand1.getCard(2).getRank(); } else { //not right need to dealer with dealer retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); } SortBySuit = sort; sortCards(); break; } case PokerWin.THREE_OF_A_KIND: { boolean sort = SortBySuit; SortBySuit = false; sortCards(); hand1.changeSort(""); if (hand[2].getRank() != hand1.getCard(2).getRank()) { retVal = hand[2].getRank() > hand1.getCard(2).getRank(); } else { //not right need to dealer with dealer retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); } SortBySuit = sort; sortCards(); break; } case PokerWin.TWO_PAIR: { boolean sort = SortBySuit; int duples1 = -1,duples2 = -1; SortBySuit = false; sortCards(); hand1.changeSort(""); if (card_count == 5) { for (int j = 0; j < 4; j++) { if (hand[j].getRank() == hand[j+1].getRank()) { if (hand[j].getRank() > duples1) duples1 = hand[j].getRank(); } if (hand1.getCard(j).getRank() == hand1.getCard(j+1).getRank()) { if (hand1.getCard(j).getRank() > duples2) duples2 = hand1.getCard(j).getRank(); } } } if (duples2 != duples1) { retVal = duples1 > duples2; } else { //not right need to dealer with dealer retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); } SortBySuit = sort; sortCards(); break; } case PokerWin.PAIR: { boolean sort = SortBySuit; int duples1 = 0,duples2 = 0; SortBySuit = false; sortCards(); hand1.changeSort(""); if (card_count == 5) { for (int j = 0; j < 4; j++) { if (hand[j].getRank() == hand[j+1].getRank()) { duples1 = hand[j].getRank(); } if (hand1.getCard(j).getRank() == hand1.getCard(j+1).getRank()) { duples2 = hand1.getCard(j).getRank(); } } } if (duples2 != duples1) { retVal = duples1 > duples2; } else { //not right need to dealer with dealer retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); } SortBySuit = sort; sortCards(); break; } case PokerWin.HIGH_CARD: { retVal = (this.getHighCard().isByRankGreaterThan(hand1.getHighCard())); break; } } return retVal; } }