### Prepare to play Poker --- Assigning values to Poker hands

• Computing the value of a Poker hand

• The value of a Poker hand depends on the type of Poker hand

• We can give the following general method that determines the value of a Poker hand

 /* -------------------------------------------------------- valueHand(): return value of a hand -------------------------------------------------------- */ public static int valueHand( Card[] h ) { if ( isFlush(h) && isStraight(h) ) return valueStraightFlush(h); else if ( is4s(h) ) return valueFourOfAKind(h); else if ( isFullHouse(h) ) return valueFullHouse(h); else if ( isFlush(h) ) return valueFlush(h); else if ( isStraight(h) ) return valueStraight(h); else if ( is3s(h) ) return valueSet(h); else if ( is22s(h) ) return valueTwoPairs(h); else if ( is2s(h) ) return valueOnePair(h); else return valueHighCard(h) // Lowest Poker hand; }

• Assigning values to Poker hands: another encoding scheme

• In order to compare Poker hands, we want to assign values to Poker hands such that:

 Value( a better Poker hand ) > Value( a worse Poker hand )

• Inter and intra Poker hand comparisons

• 2 kinds of comparisions:

 Inter Poker hands: comparison between different kinds of Poker hands Intra Poker hands: comparison between same kind of Poker hands

• Inter Poker hand comparisons:

 Royal Flush > Straight Flush > 4 of a Kind > Full House > Flush > Straight > 3 of a Kind > 2 pairs > 1 pair > High Card

• Intra Poker hand comparisons:

 Royal Flush: all equal value Straight Flush: highest rank card wins 4 of a Kind: hihest rank quads wins Full House: highest rank set (3) cards wins Flush: highest rank card wins Straight: highest rank card wins 3 of a Kind: highest rank set (3) cards wins 2 pairs: highest pair wins if tie, lowest pair wins if also tie, highest unmatched card wins 1 pair: highest pair wins if tie, highest unmatched card wins if also tie, second highest unmatched card wins if also tie, lowest unmatched card wins High Card: highest rank card wins if tie, second highest rank card wins and so on...

• Value encoding method that take cares of both inter and intra Poker hand comparision

• Encoding method for the value of a Poker hand:

• Within the same type (kind) of Poker hand:

 A higher ranked hand is assigned a higher value We make sure the assigned value does not exceed the prescribe range

Example:

Explanation:

• Suppose you have 3 flush hands:

 Spade Flush with cards 2, 3, 4, 5, 7 Heart Flush with cards 2, 3, 4, 6, 7 Club Flush with cards 2, 3, 5, 6, 7

• The different flush hand must receive different values in order to tell them apart !!!

• We define the value of a Poker hand as follows:

 Value(Poker hand) = InterValue(Poker hand) + IntraValue(Poker hand)

where:

 InterValue(Poker hand) = the Inter Poker hand value InterValue(Poker hand) = the Intra Poker hand value

• Inter Poker hand encoding

• The inter Poker hand encoding is used to separate the different kinds of Poker hand

• We can use the following function to do this job:

 InterValue( Straight Flush ) = 8000000 InterValue( Four of a Kind ) = 7000000 InterValue( Full House ) = 6000000 InterValue( Flush ) = 5000000 InterValue( Straight ) = 4000000 InterValue( 3 of a Kind ) = 3000000 InterValue( Two pairs ) = 2000000 InterValue( One pair ) = 1000000 InterValue( High Card ) = 0

• Note:

• We must make sure that:

 IntraValue( Poker Hand ) < 1000000

Otherwise, we cannot satisfy the value encoding method given in:

• Intra Poker hand encoding

• We must design an intra Poker hand encoding method that reflects the reality (otherwise it will not be useful):

 Royal Flush: all equal value Straight Flush: highest rank card wins 4 of a Kind: hihest rank quads wins Full House: highest rank set (3) cards wins Flush: highest rank card wins Straight: highest rank card wins 3 of a Kind: highest rank set (3) cards wins 2 pairs: highest pair wins if tie, lowest pair wins if also tie, highest unmatched card wins 1 pair: highest pair wins if tie, highest unmatched card wins if also tie, second highest unmatched card wins if also tie, lowest unmatched card wins High Card: highest rank card wins if tie, second highest rank card wins and so on...

• We will examine each type of Poker hand separately....

• Value of a High Card Poker hand

• High Card Hand:

• Example of a wrong valuation method:

 value of High Card hand = sum of the ranks of each card

This will not assign the appropriate value

Here is why:

• Consider the following 2 High Card Poker hands:

 Sum of the ranks of all cards -------------------------------- High Card Poker hand 1: 2 3 4 5 K 2+3+4+5+13 = 27 High Card Poker hand 2: 6 7 8 9 Q 6+7+8+9+12 = 42

• Poker hand 1 is better than

But the assigned value of Poker hand 1 is less than the assigned value of Poker hand 2.

• Conclusion:

 We must weight the higher ranked cards more

• Value assigned to a High Card Poker hand:

 /* ----------------------------------------------------- valueHighCard(): return value of a high card hand value = 14^4*highestCard + 14^3*2ndHighestCard + 14^2*3rdHighestCard + 14^1*4thHighestCard + LowestCard ----------------------------------------------------- */ public static int valueHighCard( Card[] h ) { int val; sortByRank(h); // Sorth cards by their ranks first val = h[0].rank() + 14* h[1].rank() + 14*14* h[2].rank() + 14*14*14* h[3].rank() + 14*14*14*14* h[4].rank(); return val; }

Example:

 value of hand ------------------------------------ High Card Poker hand 1: 2 3 4 5 K 2+3*14+4*142+5*143+13*144 = 513956 High Card Poker hand 2: 6 7 8 9 Q 6+7*14+8*142+9*143+12*144 = 487360

By giving the highest card K the weight 144, we can make sure that the assigned value is greater than any hand with a high card that is smaller than K.

• Value of a One Pair Poker hand

• One Pair hand:

• Assign value to a One Pair hand:

• The highest pair wins

Conclusion:

 We must value the rank of the pair higher than the unmatched cards.

• When the pairs are equal, the hand is ranked by the unmatched cards Conclusion:

 We must weight the ranks of the unmatched cards (just like in the High card hand )

• Value assigned to a One Pair Poker hand:

 /* ----------------------------------------------------- valueOnePair(): return value of a One-Pair hand value = ONE_PAIR (the inter Poker hand) + 14^3*PairCard + 14^2*HighestCard + 14*MiddleCard + LowestCard ----------------------------------------------------- */ public static int valueOnePair( Card[] h ) { int val = 0; sortByRank(h); // Sort by rank first /* --------------------------------------------- Compute: intra "One Pair" hand value --------------------------------------------- */ if ( h[0].rank() == h[1].rank() ) // a a x y z val = 14*14*14*h[0].rank() + + h[2].rank() + 14*h[3].rank() + 14*14*h[4].rank(); else if ( h[1].rank() == h[2].rank() ) // x a a y z val = 14*14*14*h[1].rank() + + h[0].rank() + 14*h[3].rank() + 14*14*h[4].rank(); else if ( h[2].rank() == h[3].rank() ) // x y a a z val = 14*14*14*h[2].rank() + + h[0].rank() + 14*h[1].rank() + 14*14*h[4].rank(); else // x y z a a val = 14*14*14*h[3].rank() + + h[0].rank() + 14*h[1].rank() + 14*14*h[2].rank(); /* -------------------------------- Add the inter Poker hand value -------------------------------- */ return ONE_PAIR + val; }

• Value of a Two Pairs Poker hand

• Two Pairs hand:

• Assign value to a Two Pairs hand:

• The highest pair wins

Conclusion:

 We must weight the rank of the pair cards

• When the pairs are equal, the hand is ranked by the unmatched cards

Conclusion:

 We must weight the ranks of the unmatched cards (just like in the High card hand )

• Value assigned to a Two Pairs Poker hand:

 /* ----------------------------------------------------- valueTwoPairs(): return value of a Two-Pairs hand value = TWO_PAIRS + 14*14*HighPairCard + 14*LowPairCard + UnmatchedCard ----------------------------------------------------- */ public static int valueTwoPairs( Card[] h ) { int val = 0; sortByRank(h); /* --------------------------------------------- Compute: intra "One Pair" hand value --------------------------------------------- */ if ( h[0].rank() == h[1].rank() && h[2].rank() == h[3].rank() ) // a a b b x val = 14*14*h[2].rank() + 14*h[0].rank() + h[4].rank(); else if ( h[0].rank() == h[1].rank() && h[3].rank() == h[4].rank() ) // a a x b b val = 14*14*h[3].rank() + 14*h[0].rank() + h[2].rank(); else // x a a b b val = 14*14*h[3].rank() + 14*h[1].rank() + h[0].rank(); /* --------------------------------------------- Add the Inter Poker hand value --------------------------------------------- */ return TWO_PAIRS + val; }

• Value of a 3 of a Kind Poker hand

• 3 of a Kind hand:

• Assign value to a 3 of a Kind hand:

• The highest set wins

Conclusion:

 We must weight the rank of the set cards

• Important property:

 Since there are only 4 cards of the same rank, there is only one 3 of a kind hand of a given rank Example: There is only one "3 of a Kind" hand with 3 Aces, one "3 of a Kind" hand with 3 Kings, and so on.

• Value assigned to a 3 of a Kind Poker hand:

 /* --------------------------------------------------------------- valueSet(): return value of a Set hand value = SET + SetCardRank Trick: After sorting by rank, the middle card h[2] is always a card that is part of the set hand There is ONLY ONE hand with a set of a given rank. --------------------------------------------------------------- */ public static int valueSet( Card[] h ) { sortByRank(h); /* ----------------------------------- Possible "3 of a kind" hands: a a a x y x a a a y x y a a a ^ | h[2] ----------------------------------- */ return SET + h[2].rank(); }

• Value of a Full House Poker hand

• Full House:

• Assign value to a Full House hand:

• The highest set in a Full House hand wins

Conclusion:

 We must weight the rank of the set cards in a Full House hand

Note:

 The pair in a Full House is not important to rank a Full House hand

• Important property:

 Since there are only 4 cards of the same rank, there is only one Full House hand of a given set rank Example: There is only one Full House hand with 3 Aces, one Full House hand with 3 Kings, and so on.

• Value assigned to a Full House Poker hand:

 /* ----------------------------------------------------------- valueFullHouse(): return value of a Full House hand value = FULL_HOUSE + SetCardRank ----------------------------------------------------------- */ public static int valueFullHouse( Card[] h ) { sortByRank(h); /* ----------------------------------- Possible "Full House" hands: a a a x x x x a a a ^ | h[2] ----------------------------------- */ return FULL_HOUSE + h[2].rank(); }

• Value of a 4 of a Kind Poker hand

• 4 of a Kind hand:

• Assign value to a 4 of a Kind hand:

• The highest quads in a 4 of a Kind wins

Conclusion:

 We must weight the rank of the quad cards

• Important property:

 Since there are only 4 cards of the same rank, there is only one Full House hand of a given set rank Example: There is only one "4 of a Kind" hand with 4 Aces, one "4 of a Kind" hand with 4 Kings, and so on.

• Value assigned to a 4 of a Kind Poker hand:

 /* -------------------------------------------------------------- valueFourOfAKind(): return value of a 4 of a kind hand value = FOUR_OF_A_KIND + 4sCardRank Trick: card h[2] is always a card that is part of the 4-of-a-kind hand There is ONLY ONE hand with a quads of a given rank. --------------------------------------------------------------- */ public static int valueFourOfAKind( Card[] h ) { sortByRank(h); /* ----------------------------------- Possible "4 of a Kind" hands: a a a a x x a a a a ^ | h[2] ----------------------------------- */ return FOUR_OF_A_KIND + h[2].rank(); }

• Value of a Flush Poker hand

• Flush hand:

• Assign value to a Flush hand:

• Comparing 2 Flush hands is exactly the same as comparing 2 High Card hands !!!

Example:

 High Card hand ranking method ------------------------------ Spade Flush hand with: 2 3 4 5 7 2+3*14+4*142+5*143+7*144 = 283460 Club Flush nand with: 2 3 4 6 7 2+3*14+4*142+6*143+7*144 = 286204

The High Card hand value assignment method can rank the Flush hands correctly.

• Value assigned to a Flush Poker hand:

 /* ----------------------------------------------------- valueFlush(): return value of a Flush hand value = FLUSH + valueHighCard() ----------------------------------------------------- */ public static int valueFlush( Card[] h ) { return FLUSH + valueHighCard(h); }

Explanation:

• The return value (= the value of a Flush hand) consists of 2 parts:

 FLUSH + valueHighCard(h)

• The value FLUSH will put the Flush hand in the inter Poker hand ranking range.

• The value valueHighCard(h) will rank a particular Flush hand among other Flush hands in the intra Poker hand ranking range.

• Value of a Straight Poker hand

• Straight hand:

• Assign value to a Straight hand:

• We can also use that High Card hand valuation method to compare 2 Straight hands:

Example:

 High Card hand ranking method ------------------------------ Straight hand with: 2 3 4 5 6 2+3*14+4*142+5*143+6*144 = 245044 Straight nand with: 3 4 5 6 7 3+4*14+5*142+6*143+7*144 = 286415

The High Card hand value assignment method can rank the Straight hands correctly.

• Value assigned to a Straight Poker hand:

 /* ----------------------------------------------------- valueStraight(): return value of a Straight hand value = STRAIGHT + valueHighCard() ----------------------------------------------------- */ public static int valueStraight( Card[] h ) { return STRAIGHT + valueHighCard(h); }

• Value of a Straight Flush and a Royal Flush Poker hand

• Straight Flush and Royal Flush hands:

A Royal Flush is just the highest Straight Flush....

• Assign value to a Straight Flush hand:

• We can also use that High Card hand valuation method to compare 2 Straight Flush hands:

Example:

 High Card hand ranking method ------------------------------ Straight hand with: 2 3 4 5 6 2+3*14+4*142+5*143+6*144 = 245044 Straight nand with: 3 4 5 6 7 3+4*14+5*142+6*143+7*144 = 286415

The High Card hand value assignment method can rank the Straight Flush hands correctly.

• Value assigned to a Straight Flush Poker hand:

 /* ----------------------------------------------------- valueStraight(): return value of a Straight hand value = STRAIGHT + valueHighCard() ----------------------------------------------------- */ public static int valueStraightFlush( Card[] h ) { return STRAIGHT_FLUSH + valueHighCard(h); }

• The Poker Class

• Here is the Poker class that contains the methods discussed above:

 Your browser does not support the &amp;amp;lt;iframe&amp;amp;gt; tag.

• The Java Program File: (see the above code)

How to use the program:

 The Poker.java methods are library methods like sin(), cos(), etc. They help you write Poker game programs with very little effort