Pokerkit: A Comprehensive Python Library For Fine-Grained Multi-Variant Poker Game Simulations
Pokerkit: A Comprehensive Python Library For Fine-Grained Multi-Variant Poker Game Simulations
Abstract—PokerKit is an open-source Python library designed card stud, razz, and deuce-to-seven triple draw. Moreover,
arXiv:2308.07327v5 [cs.AI] 16 Oct 2023
to overcome the restrictions of existing poker game simulation PokerKit provides a programmatic interface that allows users
and hand evaluation tools, which typically support only a handful to define their own variants and customize game parameters
of poker variants and lack flexibility in game state control. In
contrast, PokerKit significantly expands this scope by supporting such as betting structures, high/low-split options, antes, blinds,
an extensive array of poker variants and it provides a flexible and straddles.
architecture for users to define their custom games. This paper This paper explores the development of PokerKit, delving
details the design and implementation of PokerKit, including its into its features and capabilities, its rigorous testing and
intuitive programmatic API, multi-variant game support, and a validation processes, and its existing and potential applica-
unified hand evaluation suite across different hand types. The
flexibility of PokerKit allows for applications in diverse areas, tions. Through this comprehensive examination, we aim to
such as poker AI development, tool creation, and online poker underscore PokerKit’s unique contribution to the field of
casino implementation. PokerKit’s reliability has been established computational poker.
through static type checking, extensive doctests, and unit tests,
achieving 99% code coverage. The introduction of PokerKit
represents a significant contribution to the field of computer II. R ELATED W ORK
poker, fostering future research and advanced AI development The functionalities of PokerKit primarily fall into two
for a wide variety of poker games. The source code is available categories: game simulations and hand evaluations. Game
at https://github.com/uoftcprg/pokerkit
simulations encompass creating an environment where poker
Index Terms—Board games, Card games, Games of chance, games can be played out programmatically, simulating real-
Game design, Multi-agent systems, Poker, Rule based systems, world scenarios with high fidelity. On the other hand, hand
Scripting, Strategy games
evaluations are concerned with determining the strength of
particular poker hands. In this section, we explore notable
I. I NTRODUCTION
contributions in these areas, discussing their limitations and
Fig. 2. An example game simulation. Note that the game was created as a
pre-defined variant.
4
h0 = OmahaHoldemHand.from_game(
"6c7c8c9c", # Hole cards
"8s9sTc", # Board cards
)
h1 = OmahaHoldemHand("6c7c8s9sTc")
TABLE III
S TATE PHASES AND THEIR CORRESPONDING OPERATIONS . def verify_operation(self, ...):
...
Phases Operations if is_inoperable:
Ante posting Ante posting raise ValueError("...")
Bet collection Bet collection
Blind or straddle posting Blind or straddle posting ...
Card burning
Hole dealing def can_operate(self, ...):
Dealing
Board Dealing
Standing pat or discarding try:
Folding self.verify_operation(...)
Checking or calling except ValueError:
Betting
Bring-in posting
Completion, betting, or raising to return False
Showdown Hole cards showing or mucking return True
Hand killing Hand killing
Chips pushing Chips pushing
def operate(self, ...):
Chips pulling Chips pulling
self.verify_operation(...)
...
TABLE IV
S TATE OPERATIONS AND THEIR AUTOMATABILITIES .
Fig. 5. The method triplets of an example operation.
Phases Automatable
Ante posting Yes
Bet collection Yes
- Deck: Most variants use a 52-card deck.
Blind or straddle posting Yes
Card burning Yes - Hand types: Most variants have one, but high/low-split
Hole dealing Yes games have two.
Board Dealing Yes - Streets: Each specifies whether to burn a card, deal
Standing pat or discarding No
Folding No
the board, deal the players, draw cards, the opener, the
Checking or calling No minimum bet, and the maximum number of bets or raises.
Bring-in posting No - Betting structure: Betting limits such as no-limit, pot-
Completion, betting, or raising to No limit, or fixed-limit.
Hole cards showing or mucking Yes
Hand killing Yes This flexibility in the definition gives PokerKit the ability
Chips pushing Yes to describe not only every variant specified in the 2023 World
Chips pulling Yes
Series of Poker Tournament Rules [10] but also esoteric
variants, ranging from Greek hold ’em, Kuhn poker, 6-card
9) Chips Pulling: Players may incorporate the chips they’ve Omaha, Rhode Island hold ’em, and countless more.
won back into their stack. In poker, at least one player is In addition to the parameters related to the variants, users
guaranteed to win the pot. Consequently, this phase is never can supply additional values, namely automations, antes (uni-
skipped. form antes or non-uniform antes such as big blind antes),
Each operation is coupled with two associated methods: blinds/straddles, bring-ins, and starting stacks.
a verification method and an action query. The verification
method validates if a move can be executed within the rules,
B. Hand Evaluation
considering the current game state and the variant in play.
It raises an error if any discrepancy is detected. Users can Hand evaluation is another vital aspect of PokerKit. The
directly invoke this or use a corresponding action query library generates a lookup table for each hand type. The hands
method (with optional arguments), which simply checks if the are generated in the order or reverse order of strength and
verification method triggers an error and returns a boolean assigned indices, which are used to compare hands. High-level
value indicating the validity of the action. The method that interfaces allow users to construct hands by passing in the
performs the operation initially runs the verification method, necessary cards and using standard comparison operators to
executing the operation only if no errors are raised. If the compare the hand strengths. It’s worth noting that “strength”
verification fails, the state remains unchanged. in poker hands does not necessarily mean “low” or “high”
PokerKit’s philosophy is that it should focus on maintaining hands [13]. Each hand type in PokerKit handles this distinction
the game state and enforcing rules. Error handling is left to internally, making it transparent to the end user.
the user, who may need to handle errors differently depending In the lookup process, cards are converted into unique
on the application. Assertions are used sparingly throughout integers that represent their ranks. Each rank corresponds to a
the library for sanity checks. On runtime, users may choose unique prime number and the converted integers are multiplied
to disable them for optimization purposes without impacting together. The suitedness of the cards is then checked. Using
the library’s functionalities. the product and the suitedness, the library looks for the
The game variant is defined through several attributes, matching hand entries which are then used to compare hands.
which are as follows: This perfect hashing approach was originally employed in the
6
PokerKit B. Doctests
Speed (# games/s) 847.31 Docstrings serve a dual purpose of providing not only
documentation and sample usage but also tests [17]. Most
Deuces hand evaluation library and is also used by Treys [5], classes and methods within the library are accompanied by
[6]. example usages in doctests that illustrate their functionality.
Furthermore, these doctests offer insight into potential sce-
V. P ERFORMANCE B ENCHMARKS narios where errors are raised, helping users understand the
The benchmarks in this section were conducted using a library’s expected behavior under various circumstances.
computer equipped with a 12th Gen Intel® Core™ i7-1255U
processor and 16 GB of RAM, utilizing Python version 3.11.5. C. Unit Tests
Note that the specific values reported here reflect the perfor- Unit tests are employed to test broader functionalities that
mance under this particular hardware and software setup and do not fall into normal usage patterns. For example, some
may vary on different systems. of the unit tests evaluate all possible hand for each lookup
and validates the hands by comparing them against the results
A. Game Simulation from other poker hand evaluation libraries. For efficiency, this
The general performance of the game simulation component is achieved by obtaining an MD5 checksum of the large text
of PokerKit has been evaluated by digitalizing all 83 televised string of ordered hands generated by the lookup tables in
hands from the final table of the 2023 World Series of PokerKit and other open-source libraries.
Poker (WSOP) $50,000 Poker Players Championship, shown One of the methods of validating PokerKit has been the
in Table VI. The selection of this particular tournament was recreation of various famous poker hands and games. This
motivated by its varying number of players (as the finalists includes all 83 televised hands from the final table of the
were eliminated), and its rotation through nine different diverse 2023 World Series of Poker (WSOP) $50,000 Poker Players
variants of poker [14]. Championship which features poker games of nine different
variants [14]. The library was tested for consistency, ensuring
B. Hand Evaluation that pot values after each street and resulting stacks after the
The hand evaluation facilities in PokerKit sacrifice slight hand is over accurately match the real outcomes.
speed to provide an intuitive high-level interface. As a result, Additional unit tests were implemented to rigorously scru-
PokerKit’s hand evaluation suite is slower than open-source tinize PokerKit’s behavior under unusual circumstances like
Python hand evaluation libraries such as Treys as shown in scenarios involving extremely low starting stacks that often
Table VII [6]. Note that the Deuces library, another popular fall below the blinds or antes.
alternative, is not benchmarked as it lacks Python 3 support Together, the doctests and unit tests cover 99% of the
and Treys is a fork of Deuces that supports Python 3 [5], [6]. PokerKit codebase, ensuring a comprehensive examination.
For the benchmarking process, we iterated through all In conclusion, through the diligent application of static type
possible Texas hold ’em hands in both PokerKit and Treys. checking, doctests, unit tests, and recreations of real-life hands,
The speeds are reported as the number of hands evaluated per PokerKit has undergone thorough testing and validation. This
second. rigorous approach guarantees that the library can dependably
handle a wide array of poker game scenarios, solidifying
VI. T ESTING AND VALIDATION its position as an invaluable resource for researchers and
Ensuring robustness and error-free operation has been a developers in the field.
top priority throughout PokerKit’s development. To validate
its functionality, a variety of rigorous testing methods were VII. U SE C ASES AND A PPLICATIONS
employed, spanning from extensive doctests to unit tests, and Thanks to its flexible architecture and fine-grained control,
recreating renowned poker hands and games. PokerKit is adaptable to a wide array of tasks. It proves
7