/** The Hand class simply represents what a poker hand is: 5 Cards * In this file we write the functions that see if we have a 4-of-a-kind, * full house, etc. Here we have written just 1 constructor, an initial * value constructor. The other kinds of constructors do not seem to be * useful for the purpose of a poker program, but it would not be a bad * idea to include them at some point if we wish to generalize this class * to handle a multi-player game, or a different card game. */ public class Hand { /** The attributes are simply 5 Card objects. */ private Card c1; private Card c2; private Card c3; private Card c4; private Card c5; /** Initial value constructor -- just initialize the 5 cards. */ public Hand(Card a, Card b, Card c, Card d, Card e) { c1 = a; c2 = b; c3 = c; c4 = d; c5 = e; } /** operations to determine what kind of hand we have * We have a 4 of a kind if the first 4 cards are of the same value, * or the last 4 cards have the same value. Because we assume that * the values of the cards are descending, the code is more concise: * we do not need to check for every consecutive pair of cards. If * c1 and c4 have the same value, c2 and c3 must therefore be the same. */ public boolean isFourOfKind() { return (c1.isSameValue(c4) || c2.isSameValue(c5)); } /** There are 2 kinds of full house. The top 3 cards could be the same, * or the bottom 3 could be the same. */ public boolean isFullHouse() { return (c1.isSameValue(c3) && c4.isSameValue(c5) || c1.isSameValue(c2) && c3.isSameValue(c5)); } /** 3 cases for 3 of a kind -- basically we ask ourselves which 3 of * the 5 cards could be the same, either the first 3, middle 3 or last 3. */ public boolean isThreeOfKind() { return (c1.isSameValue(c3) || c2.isSameValue(c4) || c3.isSameValue(c5)); } /** In a 2-pair, the card that is not in a pair is either the first, * third, or last. */ public boolean isTwoPair() { return (c2.isSameValue(c3) && c4.isSameValue(c5) || c1.isSameValue(c2) && c4.isSameValue(c5) || c1.isSameValue(c2) && c3.isSameValue(c4)); } /** A 1-pair has 4 possible positions where it could be in the hand. */ public boolean isOnePair() { return (c1.isSameValue(c2) || c2.isSameValue(c3) || c3.isSameValue(c4) || c4.isSameValue(c5)); } /** Flush means all cards are of the same suit. Note that in this * case we must check for each pair of cards because there is no * ordering of the suits (only for values). */ public boolean isFlush() { return (c1.isSameSuit(c2) && c2.isSameSuit(c3) && c3.isSameSuit(c4) && c4.isSameSuit(c5)); } /** Straight means the values are in consecutive order. Again, it is * necessary to check each pair of consecutive cards. For example, it * would not be enough to check that the value of the high card is 4 * higher than the low card because you could have 88774. */ public boolean isStraight() { return (c1.isOneHigherThan(c2) && c2.isOneHigherThan(c3) && c3.isOneHigherThan(c4) && c4.isOneHigherThan(c5)); } /** Now that we have implemented functions that check for straight * and flush, we have an elegant solution to find a straight-flush. * Both functions must return true. Note how we call the functions * using "this". */ public boolean isStraightFlush() { return (this.isStraight() && this.isFlush()); } /** Royal flush is really just a straight-flush with an ace as the * high card: another name for "ace high straight-flush". */ public boolean isRoyalFlush() { return (this.isStraightFlush() && c1.isAce()); } }