poker
Class HandEvaluator

java.lang.Object
  extended by poker.HandEvaluator

public class HandEvaluator
extends java.lang.Object

Class for identifying / comparing / ranking Hands.

Source Code: HandEvaluator.java

JNI Poker Evaluation Library Code

Version:
2.2
Author:
Aaron Davidson, Darse Billings, Denis Papp

Field Summary
static int FIVEKIND
           
static int FLUSH
           
static int FOURKIND
           
static int FULLHOUSE
           
static int HIGH
           
static int NUM_HANDS
           
static int PAIR
           
static int STRAIGHT
           
static int STRAIGHTFLUSH
           
static int THREEKIND
           
static int TWOPAIR
           
 
Constructor Summary
HandEvaluator()
          Construct a new Hand Evaluator.
 
Method Summary
static void CGetRanks(int[] board, int[] ranks)
           
static double CHandRank(int c1, int c2, int[] board)
          Calculates the probability of having the best hand against one opponent.
static int compareHands(Hand h1, Hand h2)
          Compares two 7 card hands against each other.
static int compareHands(int rank1, Hand h2)
          Compares two 5-7 card hands against each other.
static int CRankHandFast(int[] cards)
          Get a numerical ranking of this hand.
static int CRankHandFast7(int[] cards)
          Get a numerical ranking of this hand.
static int Find_Hand(int[] hand, int[] best)
           
static Hand getBest5CardHand(Hand h)
          Get the best 5 card poker hand from a 7 card hand
static int getCardThatPlays(int rank1, int rank2)
          Return the Card that plays between the two given hands ranks.
static int getHandClass(Hand h)
          Get the class of hand (PAIR, STRAIGHT)
 int getNumBetter()
          Get the number of hands better than the last hand ranked.
static int getNumCardsPlayed(int rank1, int rank2)
          Return the number of Cards that play between the two given hands ranks.
 int getNumTied()
          Get the number of hands tied with the last hand ranked.
 int getNumWorse()
          Get the number of hands worse than the last hand ranked.
static NChoose2IntTable getRanks(Hand board)
          Given a board, cache all possible two card combinations of hand ranks, so that lightenting fast hand comparisons may be done later.
static NChoose2IntTable getRanksNative(Hand board)
           
 double handRank(Card c1, Card c2, Hand h)
          Calculates the probability of having the best hand against one opponent.
 double handRank(Card c1, Card c2, Hand h, int np)
          Calculates the probability of having the best hand against several opponents.
static double handRank(Card c1, Card c2, NChoose2IntTable rankCache, Deck dk)
          Calculate the strength of the given hand.
static double handRankNative(Card c1, Card c2, Hand h)
           
 boolean isNative()
          Returns true if the native evaluation library is loaded.
static boolean isTheNuts(Card c1, Card c2, Hand board, NChoose2IntTable rankCache)
          Determine if the hand is the nuts (no hands beat it)
static java.lang.String name_hand(int rank, int num_cards)
          Return a string naming the hand with the number of kickers that play
static java.lang.String name_hand1(int rank, int card)
          Return a string representing the name of the hand with the proper kicker information
static java.lang.String nameHand(Card c1, Card c2, Hand b)
          Given a hand, return a string naming the hand ('Ace High Flush', etc..)
static java.lang.String nameHand(Hand h)
          Given a hand, return a string naming the hand ('Ace High Flush', etc..)
static double[] potential(Card c1, Card c2, Hand bd, com.biotools.poker.model.WeightTable wt)
           
static double ppot1(Card c1, Card c2, Hand bd, com.biotools.poker.model.WeightTable wt)
           
static double ppot1(int c1, int c2, int[] bd, double[] weights, double[] result)
           
static int rankHand_Java(Hand h)
          Get a numerical ranking of this hand.
static int rankHand(Card c1, Card c2, Hand h)
          Get a numerical ranking of this hand.
static int rankHand(Hand h)
          Get a numerical ranking of this hand.
static int rankHand7(Hand h)
          Get a numerical ranking of this hand.
static void test()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HIGH

public static final int HIGH
See Also:
Constant Field Values

PAIR

public static final int PAIR
See Also:
Constant Field Values

TWOPAIR

public static final int TWOPAIR
See Also:
Constant Field Values

THREEKIND

public static final int THREEKIND
See Also:
Constant Field Values

STRAIGHT

public static final int STRAIGHT
See Also:
Constant Field Values

FLUSH

public static final int FLUSH
See Also:
Constant Field Values

FULLHOUSE

public static final int FULLHOUSE
See Also:
Constant Field Values

FOURKIND

public static final int FOURKIND
See Also:
Constant Field Values

STRAIGHTFLUSH

public static final int STRAIGHTFLUSH
See Also:
Constant Field Values

FIVEKIND

public static final int FIVEKIND
See Also:
Constant Field Values

NUM_HANDS

public static final int NUM_HANDS
See Also:
Constant Field Values
Constructor Detail

HandEvaluator

public HandEvaluator()
Construct a new Hand Evaluator. If the libeval.so library is present, the native methods will be used, otherwise the slower java methods will be used instead.

Method Detail

isNative

public boolean isNative()
Returns true if the native evaluation library is loaded.


rankHand

public static final int rankHand(Hand h)
Get a numerical ranking of this hand. Uses a Native Method. (Make sure the feval library is installed!) Uses GNU Poker Lib: eval.h

Parameters:
h - a 5-7 card hand
Returns:
a unique number representing the hand strength of the best 5-card poker hand in the given 7 cards. The higher the number, the better the hand is.

rankHand

public static int rankHand(Card c1,
                           Card c2,
                           Hand h)
Get a numerical ranking of this hand. Uses a Native Method. (Make sure the feval library is installed!) Uses GNU Poker Lib: eval.h

Parameters:
c1 - first hole card
c2 - second hole card
h - a 3-5 card hand
Returns:
a unique number representing the hand strength of the best 5-card poker hand in the given cards and board. The higher the number, the better the hand is.

nameHand

public static java.lang.String nameHand(Card c1,
                                        Card c2,
                                        Hand b)
Given a hand, return a string naming the hand ('Ace High Flush', etc..)


nameHand

public static java.lang.String nameHand(Hand h)
Given a hand, return a string naming the hand ('Ace High Flush', etc..)


compareHands

public static int compareHands(Hand h1,
                               Hand h2)
Compares two 7 card hands against each other.

Parameters:
h1 - The first hand
h2 - The second hand
Returns:
1 = first hand is best, 2 = second hand is best, 0 = tie

compareHands

public static int compareHands(int rank1,
                               Hand h2)
Compares two 5-7 card hands against each other.

Parameters:
rank1 - The rank of the first hand
h2 - The second hand
Returns:
1 = first hand is best, 2 = second hand is best, 0 = tie

CRankHandFast

public static int CRankHandFast(int[] cards)
Get a numerical ranking of this hand. Native Method. (Make sure the feval library is installed!) Uses GNU Poker Lib: eval.h

Parameters:
cards - an array of up to 7 card integers
Returns:
a unique number representing the hand strength of the best poker hand in the given cards. The higher the number, the better the hand is.

CRankHandFast7

public static int CRankHandFast7(int[] cards)
Get a numerical ranking of this hand. Native Method. (Make sure the feval library is installed!) Uses GNU Poker Lib: eval7.h

Parameters:
cards - an array of 7 card integers
Returns:
a unique number representing the hand strength of the best 5-card poker hand in the given 7 cards. The higher the number, the better the hand is.

CHandRank

public static double CHandRank(int c1,
                               int c2,
                               int[] board)
Calculates the probability of having the best hand against one opponent.

Parameters:
c1 - hole card 1
c2 - hole card 2
board - the board
Returns:
probability of having the best hand.

handRankNative

public static double handRankNative(Card c1,
                                    Card c2,
                                    Hand h)

rankHand7

public static final int rankHand7(Hand h)
Get a numerical ranking of this hand. Uses a Native Method. (Make sure the feval library is installed!) Uses GNU Poker Lib: eval7.h

Parameters:
h - a 7 card hand
Returns:
a unique number representing the hand strength of the best 5-card poker hand in the given 7 cards. The higher the number, the better the hand is.

CGetRanks

public static void CGetRanks(int[] board,
                             int[] ranks)

getRanksNative

public static NChoose2IntTable getRanksNative(Hand board)

getRanks

public static NChoose2IntTable getRanks(Hand board)
Given a board, cache all possible two card combinations of hand ranks, so that lightenting fast hand comparisons may be done later.


handRank

public double handRank(Card c1,
                       Card c2,
                       Hand h,
                       int np)
Calculates the probability of having the best hand against several opponents.

Parameters:
c1 - hole card 1
c2 - hole card 2
h - the board
np - the number of active opponents in the hand
Returns:
probability of having the best hand.

handRank

public double handRank(Card c1,
                       Card c2,
                       Hand h)
Calculates the probability of having the best hand against one opponent.

Parameters:
c1 - hole card 1
c2 - hole card 2
h - the board
Returns:
probability of having the best hand.

handRank

public static double handRank(Card c1,
                              Card c2,
                              NChoose2IntTable rankCache,
                              Deck dk)
Calculate the strength of the given hand. Use the rank cache to speed the calculations up.

Parameters:
c1 - the first hole card
c2 - the second hole card
rankCache - the ranks for all hands against a board where rankCache[i][j] = the rank of Card(i) and Card(j) the array stores the same values both in [i][j] and [j][i] for faster access times. (this cache can be obtained from HandEvaluator.getRanks(Hand))
dk - the deck with all known cards removed
Returns:
the hand strength

getNumWorse

public int getNumWorse()
Get the number of hands worse than the last hand ranked.


getNumBetter

public int getNumBetter()
Get the number of hands better than the last hand ranked.


getNumTied

public int getNumTied()
Get the number of hands tied with the last hand ranked.


isTheNuts

public static final boolean isTheNuts(Card c1,
                                      Card c2,
                                      Hand board,
                                      NChoose2IntTable rankCache)
Determine if the hand is the nuts (no hands beat it)

Returns:
true if no other hands beat this hand.

getBest5CardHand

public static Hand getBest5CardHand(Hand h)
Get the best 5 card poker hand from a 7 card hand

Parameters:
h - Any 7 card poker hand
Returns:
A Hand containing the highest ranked 5 card hand possible from the input.

Find_Hand

public static int Find_Hand(int[] hand,
                            int[] best)

rankHand_Java

public static final int rankHand_Java(Hand h)
Get a numerical ranking of this hand. Uses java based code, so may be slower than using the native methods, but is more compatible this way. Based on Denis Papp's Loki Hand ID code (id.cpp) Given a 1-9 card hand, will return a unique rank such that any two hands will be ranked with the better hand having a higher rank.

Parameters:
h - a 1-9 card hand
Returns:
a unique number representing the hand strength of the best 5-card poker hand in the given 7 cards. The higher the number, the better the hand is.

getHandClass

public static int getHandClass(Hand h)
Get the class of hand (PAIR, STRAIGHT)


ppot1

public static double ppot1(int c1,
                           int c2,
                           int[] bd,
                           double[] weights,
                           double[] result)

ppot1

public static double ppot1(Card c1,
                           Card c2,
                           Hand bd,
                           com.biotools.poker.model.WeightTable wt)

potential

public static double[] potential(Card c1,
                                 Card c2,
                                 Hand bd,
                                 com.biotools.poker.model.WeightTable wt)

test

public static void test()

name_hand

public static java.lang.String name_hand(int rank,
                                         int num_cards)
Return a string naming the hand with the number of kickers that play

Parameters:
rank - calculated by rankHand_java()
num_kickers - is the number of kickers (cards) that play between the two hands
Returns:
a String representing our full hand with the give number of cards displayed

getNumCardsPlayed

public static int getNumCardsPlayed(int rank1,
                                    int rank2)
Return the number of Cards that play between the two given hands ranks. eg. ([A A K J T] vs [A A K T 7] will return 4, becuause exactly 4 cards play in the winning hand [A A K J].

Parameters:
rank1 - is the first players hand rank calculated by rankHand_java()
rank2 - is the second players hand rank calculated by rankHand_java()
Returns:
0 if the two hands are of different types (eg pair vs straight)

getCardThatPlays

public static int getCardThatPlays(int rank1,
                                   int rank2)
Return the Card that plays between the two given hands ranks. eg. ([A A K J T] vs [A A K T 7] will return 3, becuause the card that breaks the tie is the Jack and it is in position 3 in the array. The array is built from the unique rank number and is always ordered by poker hand strength.

Parameters:
rank1 - is the first players hand rank calculated by rankHand_java()
rank2 - is the second players hand rank calculated by rankHand_java()
Returns:
-1 if the two hands are of different types (eg pair vs straight), or that hands are identical and its a split pot.

name_hand1

public static java.lang.String name_hand1(int rank,
                                          int card)
Return a string representing the name of the hand with the proper kicker information

Parameters:
rank - calculated by rankHand_java()
card - is the array position of the card used to break the tie.
Returns:
a String representing our winning hand with our hole card kicker.