evaluator

evaluator.evaluator

Evaluates hand strengths with optimizations in terms of speed and memory usage.

texasholdem.evaluator.evaluator.evaluate(cards, board)[source]

Evaluates the best five-card hand from the given cards and board. Returns the corresponding rank.

Parameters:
  • cards (List[int]) – A list of length two of card ints that a player holds.

  • board (List[int]) – A list of length 3, 4, or 5 of card ints.

Returns:

A number between 1 (highest) and 7462 (lowest) representing the relative

hand rank of the given card.

Return type:

int

texasholdem.evaluator.evaluator.get_five_card_rank_percentage(hand_rank)[source]

The percentage of how many of the 7462 hand strengths are worse than the given one.

Parameters:

hand_rank (int) – The rank of the hand given by evaluate()

Returns:

The percentile strength of the given hand_rank (i.e. what percent of hands is worse

than the given one).

Return type:

float

texasholdem.evaluator.evaluator.get_rank_class(hand_rank)[source]

Returns the class of hand given the hand hand_rank returned from evaluate from 9 rank classes.

Example

straight flush is class 1, high card is class 9, full house is class 3.

Returns:

A rank class int describing the general category of hand from 9 rank classes.

Example, straight flush is class 1, high card is class 9, full house is class 3.

Return type:

int

texasholdem.evaluator.evaluator.rank_to_string(hand_rank)[source]

Returns a string describing the hand of the hand_rank.

Example

166 -> “Four of a Kind”

Parameters:

hand_rank (int) – The rank of the hand given by evaluate()

Returns:

A human-readable string of the hand rank (i.e. Flush, Ace High).

Return type:

string

evaluator.lookup_table

The lookup table module keeps the books on all possible hand strengths. We construct the table once on import and reference it repeatedly.

Number of Distinct Hand Values:

Straight Flush   10
Four of a Kind   156      [(13 choose 2) * (2 choose 1)]
Full Houses      156      [(13 choose 2) * (2 choose 1)]
Flush            1277     [(13 choose 5) - 10 straight flushes]
Straight         10
Three of a Kind  858      [(13 choose 3) * (3 choose 1)]
Two Pair         858      [(13 choose 3) * (3 choose 2)]
One Pair         2860     [(13 choose 4) * (4 choose 1)]
High card      + 1277     [(13 choose 5) - 10 straights]
-------------------------
TOTAL            7462

Here we create a lookup table which maps:

  • 5 card hand’s unique prime product -> rank in range [1, 7462]

Example

  • Royal flush (best hand possible) -> 1

  • 7-5-4-3-2 unsuited (worst hand possible) -> 7462

texasholdem.evaluator.lookup_table.LOOKUP_TABLE = <texasholdem.evaluator.lookup_table.LookupTable object>

The lookup table that is created when imported

class texasholdem.evaluator.lookup_table.LookupTable[source]

Bases: object

flush_lookup

map from prime-product to rank for suited cards

Type:

Dict[int, int]

unsuited_lookup

map from prime-product to rank for unsuited cards

Type:

Dict[int, int]

MAX_FLUSH = 1599
MAX_FOUR_OF_A_KIND = 166
MAX_FULL_HOUSE = 322
MAX_HIGH_CARD = 7462
MAX_PAIR = 6185
MAX_STRAIGHT = 1609
MAX_STRAIGHT_FLUSH = 10
MAX_THREE_OF_A_KIND = 2467
MAX_TO_RANK_CLASS = {10: 1, 166: 2, 322: 3, 1599: 4, 1609: 5, 2467: 6, 3325: 7, 6185: 8, 7462: 9}
MAX_TWO_PAIR = 3325
RANK_CLASS_TO_STRING = {1: 'Straight Flush', 2: 'Four of a Kind', 3: 'Full House', 4: 'Flush', 5: 'Straight', 6: 'Three of a Kind', 7: 'Two Pair', 8: 'Pair', 9: 'High card'}