Skip to main content

A texasholdem python package

Project description

texasholdem

A python package for Texas Hold 'Em Poker.

Current Release Version v0.4.2

v1.0.0 Roadmap

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 Guide

Playing a game from the command line is as simple as the following:

from texasholdem import TexasHoldEm
from texasholdem.gui.text_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)

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)
game.take_action(ActionType.RAISE, value=10)

assert game.chips_to_call(game.current_player) == 10 - game.big_blind

Card Module

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.card 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 ♦ ]"

The game.get_hand(player_id=...) method of the TexasHoldEm class will return a list of type list[Card].

Evaluator Module

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.card 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"

History Module

Export and import the history of hands:

from texasholdem.game.game import TexasHoldEm
from texasholdem.gui.text_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 "#".

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

texasholdem-0.4.2.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

texasholdem-0.4.2-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file texasholdem-0.4.2.tar.gz.

File metadata

  • Download URL: texasholdem-0.4.2.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for texasholdem-0.4.2.tar.gz
Algorithm Hash digest
SHA256 696cbd97b3cb4ec01df6aa300f225aa4f41d310ecbf49db68b60db7665ef4d4e
MD5 6eca508abd9c18dbd620105f63698726
BLAKE2b-256 cd31f33155d74553f3c00abc9b5130231286b6e235e9a20f200047fd06ceb3c9

See more details on using hashes here.

Provenance

File details

Details for the file texasholdem-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: texasholdem-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for texasholdem-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0e8771592bee300f98939e6f1002e830fa09d50772e5c8761a748bc9ae553022
MD5 7c901b8502098fa436a0d3b3c547a37d
BLAKE2b-256 6402e32b06aa9daefc9a4e11697259255e6448a1749b4291d747b693eddb2873

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page