poker_ai.poker.evaluation package¶
Submodules¶
poker_ai.poker.evaluation.eval_card module¶
-
class
poker_ai.poker.evaluation.eval_card.
EvaluationCard
¶ Bases:
object
Static class that handles cards. We represent cards as 32-bit integers, so there is no object instantiation - they are just ints. Most of the bits are used, and have a specific meaning. See below:
EvaluationCard:
bitrank suit rank prime
xxxbbbbb
bbbbbbbb
cdhsrrrr
xxpppppp
p = prime number of rank (deuce=2,trey=3,four=5,…,ace=41)
r = rank of card (deuce=0,trey=1,four=2,five=3,…,ace=12)
cdhs = suit of card (bit turned on based on suit of card)
b = bit turned on depending on rank of card
x = unused
This representation will allow us to do very important things like: - Make a unique prime prodcut for each hand - Detect flushes - Detect straights
and is also quite performant.
-
CHAR_RANK_TO_INT_RANK
= {'2': 0, '3': 1, '4': 2, '5': 3, '6': 4, '7': 5, '8': 6, '9': 7, 'A': 12, 'J': 9, 'K': 11, 'Q': 10, 'T': 8}¶
-
CHAR_SUIT_TO_INT_SUIT
= {'c': 8, 'd': 4, 'h': 2, 's': 1}¶
-
INT_RANKS
= range(0, 13)¶
-
INT_SUIT_TO_CHAR_SUIT
= 'xshxdxxxc'¶
-
PRETTY_REDS
= [2, 4]¶
-
PRETTY_SUITS
= {1: '♠', 2: '♥', 4: '♦', 8: '♣'}¶
-
PRIMES
= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]¶
-
STR_RANKS
= '23456789TJQKA'¶
-
static
get_bitrank_int
(card_int)¶
-
static
get_prime
(card_int)¶
-
static
get_rank_int
(card_int)¶
-
static
get_suit_int
(card_int)¶
-
static
hand_to_binary
(card_strs)¶ Expects a list of cards as strings and returns a list of integers of same length corresponding to those strings.
-
static
int_to_binary
(card_int)¶ For debugging purposes. Displays the binary number as a human readable string in groups of four digits.
-
static
int_to_pretty_str
(card_int)¶ Prints a single card
-
static
int_to_str
(card_int)¶
-
static
new
(string)¶ Converts EvaluationCard string to binary integer representation of card, inspired by:
-
static
prime_product_from_hand
(card_ints)¶ Expects a list of cards in integer form.
-
static
prime_product_from_rankbits
(rankbits)¶ Returns the prime product using the bitrank (b) bits of the hand. Each 1 in the sequence is converted to the correct prime and multiplied in.
- Params:
- rankbits = a single 32-bit (only 13-bits set) integer representing
the ranks of 5 _different_ ranked cards (5 of 13 bits are set)
Primarily used for evaulating flushes and straights, two occasions where we know the ranks are ALL different.
Assumes that the input is in form (set bits):
rankbits
xxxbbbbb
bbbbbbbb
-
static
print_pretty_card
(card_int)¶ Expects a single integer as input
-
static
print_pretty_cards
(card_ints)¶ Expects a list of cards in integer form.
poker_ai.poker.evaluation.evaluator module¶
-
class
poker_ai.poker.evaluation.evaluator.
Evaluator
¶ Bases:
object
Evaluates hand strengths using a variant of Cactus Kev’s algorithm: http://suffe.cool/poker/evaluator.html
I make considerable optimizations in terms of speed and memory usage, in fact the lookup table generation can be done in under a second and consequent evaluations are very fast. Won’t beat C, but very fast as all calculations are done with bit arithmetic and table lookups.
-
_five
(cards)¶ Performs an evalution given cards in integer form, mapping them to a rank in the range [1, 7462], with lower ranks being more powerful.
Variant of Cactus Kev’s 5 card evaluator, though I saved a lot of memory space using a hash table and condensing some of the calculations.
-
_seven
(cards)¶ Performs five_card_eval() on all (7 choose 5) = 21 subsets of 5 cards in the set of 7 to determine the best ranking, and returns this ranking.
-
_six
(cards)¶ Performs five_card_eval() on all (6 choose 5) = 6 subsets of 5 cards in the set of 6 to determine the best ranking, and returns this ranking.
-
class_to_string
(class_int)¶ Converts the integer class hand score into a human-readable string.
-
evaluate
(cards, board)¶ This is the function that the user calls to get a hand rank.
Supports empty board, etc very flexible. No input validation because that’s cycles!
-
get_five_card_rank_percentage
(hand_rank)¶ Scales the hand rank score to the [0.0, 1.0] range.
-
get_rank_class
(hr)¶ Returns the class of hand from the hand hand_rank from evaluate.
-
hand_summary
(board, hands)¶ Gives a sumamry of the hand with ranks as time proceeds.
Requires that the board is in chronological order for the analysis to make sense.
-
poker_ai.poker.evaluation.lookup module¶
-
class
poker_ai.poker.evaluation.lookup.
LookupTable
¶ Bases:
object
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]
Examples: * Royal flush (best hand possible) => 1 * 7-5-4-3-2 unsuited (worst hand possible) => 7462
-
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'}¶
-
_fill_in_lookup_table
(rank_init, rankbits_list, lookup_table)¶ Iterate over rankbits and fill in lookup_table
-
flushes
()¶ Straight flushes and flushes.
Lookup is done on 13 bit integer (2^13 > 7462): xxxbbbbb bbbbbbbb => integer hand index
-
get_lexographically_next_bit_sequence
(bits)¶ Bit hack from here: http://www-graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation
Generator even does this in poker order rank so no need to sort when done! Perfect.
-
multiples
()¶ Pair, Two Pair, Three of a Kind, Full House, and 4 of a Kind.
-
straight_and_highcards
(straights, highcards)¶ Unique five card sets. Straights and highcards.
Reuses bit sequences from flush calculations.
-
write_table_to_disk
(table, filepath)¶ Writes lookup table to disk