Skip to main content

A package for finding the best poker hand from 7 cards.

Project description

Poker65

Python library for working with poker hands.

Functionality includes:

  • creating card objects
  • creating hand objects
  • comparing poker hands
  • getting the best poker hand from a list of 7 cards

Installation

Install using pip:

pip install Poker65

Example Usage

Creating Cards

Cards store the suit and rank data as integers, but the Card class has helper functions to convert strings to the corresponding int.

Suits:

from Poker65.card import Card
# int : str : meaning
#   0 : 'S' : spade
#   1 : 'H' : heart
#   2 : 'C' : club
#   3 : 'D' : diamond
suit : int = Card.suit2int('H') # 1

Ranks:

from Poker65.card import Card
# int : str : meaning
#   0 : '2' : Two
#   1 : '3' : Three
#   2 : '4' : Four
#   3 : '5' : Five
#   4 : '6' : Six
#   5 : '7' : Seven
#   6 : '8' : Eight
#   7 : '9' : Nine
#   8 : 'T' : Ten
#   9 : 'J' : Jack
#  10 : 'Q' : Queen
#  11 : 'K' : King
#  12 : 'A' : Ace
rank : int = Card.rank2int('T') # 8

Cards can be created like so

from Poker65.card import Card

cardWithInt = Card(suit=0, rank=0)

# creating a full deck of 52 cards
deck = []
for suit in 'SHCD': # spade, heart, club, diamond
    for rank in '23456789TJQKA': # ace is highest
        # Card constructor uses suit2int and rank2int
        Card(suit=suit, rank=rank)

Note that cards are immutable, suit and rank cannot be modified after creating the Card.

Creating Hands

All hand types are subclasses of the abstract class Hand, and they all implement a get_cards() method that returns the list of the 5 cards that make up the hand, and the hands allow for comparison.

from Poker65.card import Card
from Poker65.hands import *

hand1 = RoyalFlush(suit='H')
print(hand1) # ♡A, ♡K, ♡Q, ♡J, ♡T

hand2_cards = [
    Card('H', '6'),
    Card('S', 'A'),
    Card('C', '3'),
    Card('H', '9'),
    Card('H', 'K'),
]
hand2 = HighCard(cards=hand2_cards)
print(hand2) # ♠A, ♡K, ♡9, ♡6, ♣3

# hands can be compared, better hands are larger
print(hand1 > hand2) # True
print(hand1 == hand1) # True
print(hand1 == hand2) # False
print(hand1 <= hand2) # False

Hands can be created using their constructors.

RoyalFlush needs the suit of the hand.

# ♢A, ♢K, ♢Q, ♢J, ♢T
print(RoyalFlush(suit='D'))

StraightFlush needs the suit and highest ranking card

# ♣J, ♣T, ♣9, ♣8, ♣7
print(StraightFlush(suit='C', high='J'))

FourOfAKind needs the rank of the quad and the kicker card.

# ♠5, ♡5, ♣5, ♢5, ♢2
print(FourOfAKind(quad_rank='5', kicker=Card('D', '2')))

FullHouse needs the rank of the triplet, the missing suit from the triplet, the rank of the pair, and the two suits of the pair

# ♠6, ♡6, ♢6, ♡K, ♣K
print(FullHouse(
    trip_rank='6',
    trip_missing_suit='C',
    pair_rank='K',
    pair_suits=('C', 'H')
))

Flush needs the suit of the flush and the ranks of the cards.

# ♢J, ♢9, ♢8, ♢4, ♢3
print(Flush(suit='D', ranks=['8', 'J', '4', '9', '3']))

Straight needs the highest card and the suits of the cards, in the order of descending rank.

# ♢T, ♠9, ♡8, ♢7, ♣6
print(Straight(high='T', suits=['D', 'S', 'H', 'D', 'C']))

ThreeOfAKind needs the triplet rank, the triplet's missing suit, and the two kickers.

# ♠Q, ♡Q, ♣Q, ♡9, ♠2
print(ThreeOfAKind(
    trip_rank='Q',
    trip_missing_suit='D',
    kicker1=Card('S', '2'),
    kicker2=Card('H', '9'),
))

TwoPair needs the ranks of the two pairs and the suits of the cards in the two pairs, as well as the kicker.

# ♠J, ♡J, ♠3, ♣3, ♡2
print(TwoPair(
    pair1_rank='3', pair1_suits=('C', 'S'),
    pair2_rank='J', pair2_suits=('H', 'S'),
    kicker=Card('H', '2')
))

OnePair needs the pair rank and suits along with three kickers.

# ♠T, ♡T, ♠8, ♡7, ♣4
print(OnePair(
    pair_rank='T', pair_suits=('S', 'H'),
    kickers=[Card('H', '7'), Card('S', '8'), Card('C', '4')]
))

HighCard needs the five cards.

# ♢K, ♢Q, ♠7, ♠4, ♡3
print(HighCard(cards=[
    Card('D', 'Q'),
    Card('S', '7'),
    Card('D', 'K'),
    Card('S', '4'),
    Card('H', '3')
]))

Using get_best_hand

get_best_hand() takes in a list of 7 cards, and returns the best 5 card hand from the 7 cards.

from Poker65.card import Card
from Poker65.hands import get_best_hand

# 4 of spades and 10 of clubs
hand = [Card(suit='S', rank='4'), Card(suit='C', rank='T')]

community_cards = [
    Card(suit='H', rank='9'), # 9 of hearts
    Card(suit='S', rank='2'), # 2 of spades
    Card(suit='C', rank='J'), # Jack of clubs
    Card(suit='H', rank='7'), # 7 of hearts
    Card(suit='D', rank='8'), # 8 of diamonds
]

# ♣J, ♣T, ♡9, ♢8, ♡7
print(get_best_hand(hand + community_cards))
# Straight(♣J, ♣T, ♡9, ♢8, ♡7) 
print(repr(get_best_hand(hand + community_cards)))

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

poker65-0.0.4.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

poker65-0.0.4-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file poker65-0.0.4.tar.gz.

File metadata

  • Download URL: poker65-0.0.4.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for poker65-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8b7ba6522b5ba9909ade81579c4cbb7855d3c1770e2cec537704f872157fc2d1
MD5 403f2b5de63ecd7ebf6c75fc1ee0aa88
BLAKE2b-256 cdbfe8d78013141099f8b99af9c5e7392f435622c2f84e4433c7b37aaad55a4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for poker65-0.0.4.tar.gz:

Publisher: python-publish.yml on FallingSky65/Poker65

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file poker65-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: poker65-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for poker65-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 44f66dcaa5ba26e9ec646b0def6270077adb4cc1fe22e68d00c2ea2b8a97700c
MD5 351ee6196577db7043938c4a8067df87
BLAKE2b-256 a415701a8450972b39d92bbf3201c820d5f1c2a963e19ce937bb67b15f81a4cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for poker65-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on FallingSky65/Poker65

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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