|
Explanation:
|
|
|
|
|
|
How do we detect a flush:
Flush:
|
Hint:
|
Sort the cards in the Poker hand by the suit; if ( lowest suit == highest suit ) Hand contain a flush (only 1 suit of cards in thehand !); else Hand does not contain a flush; |
/* --------------------------------------------- isFlush(): true if h has a flush false otherwise --------------------------------------------- */ public static boolean isFlush( Card[] h ) { if ( h.length != 5 ) return(false); // Make sure we have 5 cards.... sortBySuit(h); // Sort the cards by the suit values return( h[0].suit() == h[4].suit() ); // All cards has same suit } |
|
/* --------------------------------------------- Sort hand by suit: smallest suit card first .... (Finding a flush is eaiser that way) --------------------------------------------- */ public static void sortBySuit( Card[] h ) { int i, j, min_j; /* --------------------------------------------------- The selection sort algorithm --------------------------------------------------- */ for ( i = 0 ; i < h.length ; i ++ ) { /* --------------------------------------------------- Find array element with min. value among h[i], h[i+1], ..., h[n-1] --------------------------------------------------- */ min_j = i; // Assume elem i (h[i]) is the minimum for ( j = i+1 ; j < h.length ; j++ ) { if ( h[j].suit() < h[min_j].suit() ) { min_j = j; // We found a smaller suit value, update min_j } } /* --------------------------------------------------- Swap a[i] and a[min_j] --------------------------------------------------- */ Card help = h[i]; h[i] = h[min_j]; h[min_j] = help; } } |
Straight:
|
Hint:
|
Sort the cards in the Poker hand by the rank; if ( highest rank card == ACE ) Check if other 4 cards are K, Q, J, 10 or 2, 3, 4, 5 else Check if 5 cards are continuous in rank |
public static boolean isStraight( Card[] h ) { int i, testRank; if ( h.length != 5 ) return(false); sortByRank(h); // Sort the poker hand by the rank of each card /* =========================== Check if hand has an Ace =========================== */ if ( h[4].rank() == 14 ) { /* ================================= Check straight using an Ace ================================= */ boolean a = h[0].rank() == 2 && h[1].rank() == 3 && h[2].rank() == 4 && h[3].rank() == 5 ; boolean b = h[0].rank() == 10 && h[1].rank() == 11 && h[2].rank() == 12 && h[3].rank() == 13 ; return ( a || b ); } else { /* =========================================== General case: check for increasing values =========================================== */ testRank = h[0].rank() + 1; for ( i = 1; i < 5; i++ ) { if ( h[i].rank() != testRank ) return(false); // Straight failed... testRank++; // Next card in hand } return(true); // Straight found ! } } |
|
/* --------------------------------------------- Sort hand by rank: smallest suit card first .... (Finding a flush is eaiser that way) --------------------------------------------- */ public static void sortByRank( Card[] h ) { int i, j, min_j; /* --------------------------------------------------- The selection sort algorithm --------------------------------------------------- */ for ( i = 0 ; i < h.length ; i ++ ) { /* --------------------------------------------------- Find array element with min. value among h[i], h[i+1], ..., h[n-1] --------------------------------------------------- */ min_j = i; // Assume elem i (h[i]) is the minimum for ( j = i+1 ; j < h.length ; j++ ) { if ( h[j].rank() < h[min_j].rank() ) { min_j = j; // We found a smaller rank value, update min_j } } /* --------------------------------------------------- Swap a[i] and a[min_j] --------------------------------------------------- */ Card help = h[i]; h[i] = h[min_j]; h[min_j] = help; } } |
Note:
|
|
Example:
isStraight( PokerHand ) && isFlush( PokerHand ) |
|
Four of a Kind:
|
Hint:
|
public static boolean is4s( Card[] h ) { boolean a1, a2; if ( h.length != 5 ) return(false); sortByRank(h); // Sort by rank first /* ------------------------------------------------------ Check for: 4 cards of the same rank + higher ranked unmatched card ------------------------------------------------------- */ a1 = h[0].rank() == h[1].rank() && h[1].rank() == h[2].rank() && h[2].rank() == h[3].rank() ; /* ------------------------------------------------------ Check for: lower ranked unmatched card + 4 cards of the same rank ------------------------------------------------------- */ a2 = h[1].rank() == h[2].rank() && h[2].rank() == h[3].rank() && h[3].rank() == h[4].rank() ; Return( a1 || a2 ); } |
Full House:
|
Hint:
|
public static boolean isFullHouse( Card[] h ) { boolean a1, a2; if ( h.length != 5 ) return(false); sortByRank(h); // Sort by rank first /* ------------------------------------------------------ Check for: x x x y y ------------------------------------------------------- */ a1 = h[0].rank() == h[1].rank() && h[1].rank() == h[2].rank() && h[3].rank() == h[4].rank(); /* ------------------------------------------------------ Check for: x x y y y ------------------------------------------------------- */ a2 = h[0].rank() == h[1].rank() && h[2].rank() == h[3].rank() && h[3].rank() == h[4].rank(); return( a1 || a2 ); } |
Three of a Kind:
|
Hint:
|
public static boolean is3s( Card[] h ) { boolean a1, a2, a3; if ( h.length != 5 ) return(false); sortByRank(h); // Sort by rank first /* ------------------------------------------------------ Check for: x x x a b ------------------------------------------------------- */ a1 = h[0].rank() == h[1].rank() && h[1].rank() == h[2].rank() && h[3].rank() != h[0].rank() && h[4].rank() != h[0].rank() && h[3].rank() != h[4].rank() ; /* ------------------------------------------------------ Check for: a x x x b ------------------------------------------------------- */ a2 = h[1].rank() == h[2].rank() && h[2].rank() == h[3].rank() && h[0].rank() != h[1].rank() && h[4].rank() != h[1].rank() && h[0].rank() != h[4].rank() ; /* ------------------------------------------------------ Check for: a b x x x ------------------------------------------------------- */ a3 = h[2].rank() == h[3].rank() && h[3].rank() == h[4].rank() && h[0].rank() != h[2].rank() && h[1].rank() != h[2].rank() && h[0].rank() != h[1].rank() ; return( a1 || a2 || a3 ); } |
|
We can use these methods in writing the 3 of a kind test method
public static boolean is3s( Card[] h ) { boolean a1, a2, a3; if ( h.length != 5 ) return(false); if ( is4s(h) || isFullHouse(h) ) return(false); // The hand is not 3 of a kind (but better) /* ---------------------------------------------------------- Now we know the hand is not 4 of a kind or a full house ! ---------------------------------------------------------- */ Can we make the test a little more simpler ??? } |
We can use this knowledge to simplify the full version of is3s()
|
The other 2 cases:
/* ------------------------------------------------------ Check for: a x x x b ------------------------------------------------------- */ a2 = h[1].rank() == h[2].rank() && h[2].rank() == h[3].rank() && h[0].rank() != h[1].rank() && h[4].rank() != h[1].rank() && h[0].rank() != h[4].rank() ; /* ------------------------------------------------------ Check for: a b x x x ------------------------------------------------------- */ a3 = h[2].rank() == h[3].rank() && h[3].rank() == h[4].rank() && h[0].rank() != h[2].rank() && h[1].rank() != h[2].rank() && h[0].rank() != h[1].rank() ; |
can be simplified into:
/* ------------------------------------------------------ Check for: a x x x b ------------------------------------------------------- */ a2 = h[1].rank() == h[2].rank() && h[2].rank() == h[3].rank() ; /* ------------------------------------------------------ Check for: a b x x x ------------------------------------------------------- */ a3 = h[2].rank() == h[3].rank() && h[3].rank() == h[4].rank() ; |
public static boolean is3s( Card[] h ) { boolean a1, a2, a3; if ( h.length != 5 ) return(false); if ( is4s(h) || isFullHouse(h) ) return(false); // The hand is not 3 of a kind (but better) sortByRank(h); /* ------------------------------------------------------ Check for: x x x a b ------------------------------------------------------- */ a1 = h[0].rank() == h[1].rank() && h[1].rank() == h[2].rank() ; /* ------------------------------------------------------ Check for: a x x x b ------------------------------------------------------- */ a2 = h[1].rank() == h[2].rank() && h[2].rank() == h[3].rank() ; /* ------------------------------------------------------ Check for: a b x x x ------------------------------------------------------- */ a3 = h[2].rank() == h[3].rank() && h[3].rank() == h[4].rank() ; return( a1 || a2 || a3 ); } |
Two pairs:
|
Hint:
|
public static boolean is22s( Card[] h ) { boolean a1, a2, a3; if ( h.length != 5 ) return(false); if ( is4s(h) || isFullHouse(h) || is3s(h) ) return(false); // The hand is not 2 pairs (but better) sortByRank(h); /* -------------------------------- Checking: a a b b x -------------------------------- */ a1 = h[0].rank() == h[1].rank() && h[2].rank() == h[3].rank() ; /* ------------------------------ Checking: a a x b b ------------------------------ */ a2 = h[0].rank() == h[1].rank() && h[3].rank() == h[4].rank() ; /* ------------------------------ Checking: x a a b b ------------------------------ */ a3 = h[1].rank() == h[2].rank() && h[3].rank() == h[4].rank() ; return( a1 || a2 || a3 ); } |
Note:
|
Two pairs:
|
Hint:
|
public static boolean is2s( Card[] h ) { boolean a1, a2, a3, a4; if ( h.length != 5 ) return(false); if ( is4s(h) || isFullHouse(h) || is3s(h) || is22s(h) ) return(false); // The hand is not one pair (but better) sortByRank(h); /* -------------------------------- Checking: a a x y z -------------------------------- */ a1 = h[0].rank() == h[1].rank() ; /* -------------------------------- Checking: x a a y z -------------------------------- */ a2 = h[1].rank() == h[2].rank() ; /* -------------------------------- Checking: x y a a z -------------------------------- */ a3 = h[2].rank() == h[3].rank() ; /* -------------------------------- Checking: x y z a a -------------------------------- */ a4 = h[3].rank() == h[4].rank() ; return( a1 || a2 || a3 || a4 ); } |
Note:
|
Note: First find the line:
/*********************************************************** Methods used to determine a certain Poker hand ***********************************************************/ |
The Poker checking method (discussed in this web page) are below this line
How to use the program:
|