card

card.card

class texasholdem.card.card.Card(arg)[source]

Bases: int

Usage Card("Kd")

We represent Card objects as native Python 32-bit integers. Most of the bits are used, and have a specific meaning. See below:

Card

xxxbbbbb

bbbbbbbb

cdhsrrrr

xxpppppp

  • p = prime number of rank (in binary) (deuce=2, trey=3, four=5, …, ace=41)

  • r = rank of card (in binary) (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 (deuce=1st bit, trey=2nd bit, …)

  • x = unused

Example

Card

xxxAKQJT

98765432

CDHSrrrr

xxPPPPPP

King of Diamonds

00001000

00000000

01001011

00100101

Five of Spades

00000000

00001000

00010011

00000111

Jack of Clubs

00000010

00000000

10001001

00011101

This representation allows for minimal memory overhead along with fast applications necessary for poker:

  • Make a unique prime product for each hand (by multiplying the prime bits)

  • Detect flushes (bitwise && for the suits)

  • Detect straights (shift and bitwise &&)

Parameters:

arg (str | int) – A string of the form “{rank}{suit}” e.g. “Kd” or “As” or a properly-formed Card-int as described above.

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 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
INT_SUIT_TO_CHAR_SUIT = 'xshxdxxxc'
PRETTY_SUITS = {1: '♠', 2: '♥', 4: '♦', 8: '♣'}
PRIMES = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41)
STR_RANKS = '23456789TJQKA'
property binary_string

For debugging purposes. Displays the binary number as a human readable string in groups of four digits.

property bitrank

The bitrank of the card. This returns 2^k where k is the rank of the card.

Example

134236965 (“Kd”) –> 2^11

Returns:

2^k where k is the rank of the card.

Return type:

int

classmethod from_int(card_int)[source]

Converts an already well-formed card integer as described above

Example

134236965 –> Card(“Kd”)

Parameters:

card_int (int) – An int representing a card.

Returns:

The 32-bit int representing the card as described above

Return type:

Card

classmethod from_string(string)[source]

Converts Card string to binary integer representation of card, inspired by:

http://www.suffecool.net/poker/evaluator.html

Example

“Kd” –> int(0b 00001000 00000000 01001011 00100101) = 134236965

Parameters:

string (str) – A string representing a card.

Returns:

The 32-bit int representing the card as described above

Return type:

Card

property pretty_string

A human-readable pretty string with ascii suits.

property prime

The prime associated with the card. This returns the kth prime starting at 2 where k is the rank of the card.

Example

134236965 (“Kd”) –> 37

property rank

The rank of the card as an int.

Example

134236965 (“Kd”) –> 11 268440327 (“As”) –> 12

Returns:

Number between 0-12, representing the rank of the card.

Return type:

int

property suit

The suit int of the card using the following table:

Suit

Number

Spades

1

Hearts

2

Diamonds

4

Clubs

8

Example

134236965 (“Kd”) –> 2

Returns:

1,2,4, or 8, representing the suit of the card from the above table.

Return type:

int

texasholdem.card.card.card_list_to_pretty_str(cards)[source]

Prints the given card in a human-readable pretty string with ascii suit.

Parameters:

cards (List[Card]) – A list of card ints in the proper form.

Returns:

A human-readable pretty string with ascii suits.

Return type:

string

texasholdem.card.card.card_strings_to_int(card_strs)[source]
Parameters:

card_strs (Iterable[str]) – An iterable of card strings.

Returns:

The cards in the corresponding int format.

Return type:

List[Card]

texasholdem.card.card.prime_product_from_hand(cards)[source]
Parameters:

cards (Iterable[Card]) – An Iterable of cards

Returns:

The product of all primes in the hand, corresponding to the rank of the

card (See Card.prime())

Return type:

int

texasholdem.card.card.prime_product_from_rankbits(rankbits)[source]

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.

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):

xxxbbbbb

bbbbbbbb

Parameters:

rankbits (int) – a single 32-bit (only 13-bits set) integer representing the ranks of 5 different ranked card (5 of 13 bits are set)

Returns:

The product of all primes in the hand, corresponding

to the rank of the card.

Return type:

int

card.deck

class texasholdem.card.deck.Deck[source]

Class representing a deck. The first time we create, we seed the static deck with the list of unique card integers. Each object instantiated simply makes a copy of this object and shuffles it.

copy(shuffle=True)[source]

Make a copy of the deck

Parameters:

shuffle (bool) – Shuffle the deck when copying, defaults to true

Returns:

A copy of the deck

Return type:

Deck

draw(num=1)[source]

Draw card(s) from the deck. These cards leave the deck and are not saved.

Parameters:

num (int) – How many cards to draw. Defaults to 1.

Returns:

A list of length n of cards (See Card).

Return type:

List[Card]

Raises:

ValueError – If the deck size is less than the given n.

shuffle()[source]

Shuffles the remaining cards in the deck.