/* Poker ClassName: Poker.java Purpose: Determine winner of a poker game from several PokerHands Date: 10/11/2000 Written By: Jason Hertzog & Mike Dise Written For: Dr. R. Webster */ import java.util.*; public class Poker { private int hand_count = 0; private int dealer = 0; private PokerHand[] hands = new PokerHand[6]; private int winReason = -1; public int determineWinner (PokerHand hand1, PokerHand hand2, int Dealer) { hand_count = 2; hands[0] = hand1; hands[1] = hand2; dealer = Dealer; return getWinner(); } public int determineWinner (Vector h, int Dealer) { hand_count = h.size(); for (int i = 0; i < h.size(); i++) hands[i] = (PokerHand)h.elementAt(i); dealer = Dealer; return getWinner(); } public int determineWinner (PokerHand hand1, PokerHand hand2, PokerHand hand3, int Dealer) { hand_count = 3; hands[0] = hand1; hands[1] = hand2; hands[2] = hand3; dealer = Dealer; return getWinner(); } public int determineWinner (PokerHand hand1, PokerHand hand2, PokerHand hand3, PokerHand hand4, int Dealer) { hand_count = 4; hands[0] = hand1; hands[1] = hand2; hands[2] = hand3; hands[3] = hand4; dealer = Dealer; return getWinner(); } public int determineWinner (PokerHand hand1, PokerHand hand2, PokerHand hand3, PokerHand hand4, PokerHand hand5, int Dealer) { hand_count = 5; hands[0] = hand1; hands[1] = hand2; hands[2] = hand3; hands[3] = hand4; hands[4] = hand5; dealer = Dealer; return getWinner(); } public int determineWinner (PokerHand hand1, PokerHand hand2, PokerHand hand3, PokerHand hand4, PokerHand hand5, PokerHand hand6, int Dealer) { hand_count = 6; hands[0] = hand1; hands[1] = hand2; hands[2] = hand3; hands[3] = hand4; hands[4] = hand5; hands[5] = hand6; dealer = Dealer; return getWinner(); } public int getReasonWin () { return winReason; } private int getWinner() { int potWinners_count = 0; PokerHand[] potentialWinners = new PokerHand[5]; int[] pWinId = new int[5]; PokerHand winner = null; int win = -1; int i; for (i = 0; i < hand_count; i++) if (hands[i].isStraightFlush()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.STRAIGHT_FLUSH; } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isFourOfAKind()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.FOUR_OF_A_KIND; } } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isFullHouse()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.FULL_HOUSE; } } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isFlush()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.FLUSH; } } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isStraight()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.STRAIGHT; } } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isThreeOfAKind()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.THREE_OF_A_KIND; } } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isTwoPair()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.TWO_PAIR; } } if (potWinners_count <= 0) { for (i = 0; i < hand_count; i++) if (hands[i].isPair()) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; winReason = PokerWin.PAIR; } } if (potWinners_count <= 0) { winReason = PokerWin.HIGH_CARD; for (i = 0; i < hand_count; i++) { potentialWinners[potWinners_count++] = hands[i]; pWinId[potWinners_count-1] = i; } } if (potWinners_count > 1) { winner = potentialWinners[0]; win = pWinId[0]; for (i = 1; i < potWinners_count; i++) if (potentialWinners[i].isBetterThan(winner,winReason)) { winner = potentialWinners[i]; win = pWinId[i]; } } else { winner = potentialWinners[0]; win = pWinId[0]; } return win; } }