A texasholdem python package
Project description
texasholdem
A python package for Texas Hold 'Em Poker.
Latest Stable Release Version: v0.4.5
Changelog
Documentation
Latest Experimental Release Version v0.5-alpha.3
Changelog
Documentation
Roadmap
v1.0.0
Contributing
To be added as a contributor, please email me at evyn.machi@gmail.com with your GitHub username and mention one of the open issues / a new issue you would like to tackle first. For more information about contributing, please see the wiki.
Install
pip install texasholdem
Quickstart
Playing a game from the command line is as simple as the following:
from texasholdem import TexasHoldEm
from texasholdem.gui import TextGUI
game = TexasHoldEm(buyin=500,
big_blind=5,
small_blind=2,
max_players=6)
gui = TextGUI()
gui.set_player_ids(list(range(6))) # see all cards
while game.is_game_running():
game.start_hand()
while game.is_hand_running():
gui.print_state(game)
action, val = gui.accept_input()
while not game.validate_move(game.current_player, action, val):
print(f"{action} {val} is not valid for player {game.current_player}")
action, val = gui.accept_input()
gui.print_action(game.current_player, action, val)
game.take_action(action, val)
Overview
Game Information
Get game information and take actions through intuitive attributes:
from texasholdem import TexasHoldEm, HandPhase, ActionType
game = TexasHoldEm(buyin=500,
big_blind=5,
small_blind=2,
max_players=9)
game.start_hand()
assert game.hand_phase == HandPhase.PREFLOP
assert HandPhase.PREFLOP.next_phase() == HandPhase.FLOP
assert game.chips_to_call(game.current_player) == game.big_blind
game.take_action(ActionType.CALL)
player_id = game.current_player
game.take_action(ActionType.RAISE, value=10)
assert game.player_bet_amount(player_id) == 10
assert game.chips_at_stake(player_id) == 20 # total amount in all pots the player is in
assert game.chips_to_call(game.current_player) == 10 - game.big_blind
Cards
The card module represents cards as 32-bit integers for simple and fast hand
evaluations. For more information about the representation, see the Card
module.
from texasholdem import Card
card = Card("Kd") # King of Diamonds
assert isinstance(card, int) # True
assert card.rank == 11 # 2nd highest rank (0-12)
assert card.pretty_string == "[ K ♦ ]"
Agents
The package also comes with basic agents including call_agent
and random_agent
from texasholdem import TexasHoldEm
from texasholdem.agents import random_agent, call_agent
game = TexasHoldEm(buyin=500, big_blind=5, small_blind=2)
game.start_hand()
while game.is_hand_running():
if game.current_player % 2 == 0:
game.take_action(*random_agent(game))
else:
game.take_action(*call_agent(game))
The game.get_hand(player_id=...)
method of the TexasHoldEm
class
will return a list of type list[Card]
.
Game History
Export and import the history of hands:
from texasholdem import TexasHoldEm
from texasholdem.gui import TextGUI
game = TexasHoldEm(buyin=500, big_blind=5, small_blind=2)
game.start_hand()
while game.is_hand_running():
game.take_action(*some_strategy(game))
# export to file
game.export_history("./pgns/my_game.pgn")
# import and replay
gui = TextGUI()
for state in TexasHoldEm.import_history("./pgns/my_game.pgn"):
gui.print_state(state)
PGN files also support single line and end of line comments starting with "#".
Poker Evaluator
The evaluator module returns the rank of the best 5-card hand from a list of 5 to 7 cards.
The rank is a number from 1 (strongest) to 7462 (weakest). This determines the winner in the TexasHoldEm
module:
from texasholdem import Card
from texasholdem.evaluator import evaluate, rank_to_string
assert evaluate(cards=[Card("Kd"), Card("5d")],
board=[Card("Qd"),
Card("6d"),
Card("5s"),
Card("2d"),
Card("5h")]) == 927
assert rank_to_string(927) == "Flush, King High"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file texasholdem-0.5a3.tar.gz
.
File metadata
- Download URL: texasholdem-0.5a3.tar.gz
- Upload date:
- Size: 27.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53b044594068fafd20499395130d2be5259a65a87c990d3d385f8ff0d8bdc372 |
|
MD5 | dea1043e447c7f5e04de13310e593ef9 |
|
BLAKE2b-256 | c6674276aefacef33013fd07b6f1382d43a253f1c5ba3df7b972820689048ff9 |
Provenance
File details
Details for the file texasholdem-0.5a3-py3-none-any.whl
.
File metadata
- Download URL: texasholdem-0.5a3-py3-none-any.whl
- Upload date:
- Size: 33.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9a820c78ba8e09c9877133ad2ac7bc7afa1659a04e9ae37da6836c4a2c19b38 |
|
MD5 | 1067745311cd6bfdace404402a185a91 |
|
BLAKE2b-256 | 1f66f6fb0124913933ae4b2991ec7368fd5dbd71917fa496bde3d1def3f21702 |