Excalidraw

Pretty useful and intresting tool.

Omg, you can convert it into a markdown and then add tags, aliases, etc. This is so useful.

You can also reference other notes from the drawing!

Poker Hand Calculations

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠

Text Elements

Texas Hold’Em Calculations

From the research I have seen online, it seems that binary representation is the way to go.

{A,2,3,4,5,6,7,8,9,β€œ10”,J,Q,K,A} = 14 elements < 2^4 = 16

In the implementation, you will see we use 64-bit integers instead of 56-bit integers, since they are more commonly used, but the first 8-bits will always be 0s.

There are 52^7 possible card combinations, which is a lot… (actually, it’s 52 * 51 * 50 * … 45 which is approx 52^7)

52 < 2^6 = 64, so 52^7 < (2^6)^7 = 2^42

00100011

Suits

Rank

Suits

Brain Dump

β€œ0000000000000000000000000000000000000000000000000”

56-bit Integer

Some Sources of Inspiration:

Cactus Kev’s

Stack Overflow

AC

AD

AH

AS

2C

2D

2H

2S

3C

3D

3H

…

β€œAce of Clubs”

…

QD

QH

QS

3S

AC

AD

AH

AS

KC

KD

KH

KS

QC

Some interesting options:

  1. Use a 56-bit integer, where each bit represents a particular card (such as 2 of Diamonds). If you have 7 cards, you have 7 bits that will be turned on in this 64-bit integer.
  2. Represent each card as an 8-bit integer, and have a long row of them?

1 - Royal Flush

This is the best hand in poker. We must have the 5 highest cards (10, J, Q, K and A), all with the same suits. To determine if our hand has a royal flush, we must ignore the first 9 ranks, so we shift our representation by 9 ranks * 4 bits/rank = 36 bits.

royal_flush = (h >> 36) & (h >> 40) & (h >> 44) & (h >> 48) & (h >> 52)

β†’ Returns a 4-bit integer, ex: β€œ0010”. If it is non-zero, then you have a royal flush. The bit will represent the suit of the royal flush.

Case for tie: If both players have a royal flush, they automatically tie the pot.

β€œ3”s

β€œ2”s

β€œA”s

*Duplicate Aces to facilitate calculation of straights and comparing certain hand strengths

2 - Straight Flush

Straight flush means 5 cards of the same suit are next to each other.

hh = (h) & (h >> 4) & (h >> 8) & (h >> 12) & (h >> 16).

β†’ Returns a 40-bit integer. If hh is non-zero, then you have a straight flush.

Case for tie: If both players have a straight flush, then the winner is the one with the left-most β€œ1” bit. Both players tie if their left-most β€œ1” bit is in the same position.

3 - Four of a Kind

Example: Four of a Kind for 3s

0000…00010000

β†’ Returns a 49-bit integer. If hh is non-zero, then you have a four of a kind.

Case for tie: If both players have a four of a kind, the player with the left-most 1-bit wins. If tie, check for kicker.

14 β€œ1”s in hex

This is a bitmask, which we apply to make sure the groupings of β€œ1”s are only every 4th positions.

4 - Full House

A Full House can be checked by checking for 3 of a kind and pair.

Case for tie: Check the next highest card. Only split pot if you’ve reached the 5th card and they still tie.

10 - High Card

5 - Flush

For a flush, we check that there are at least 5 cards which have the same suit. We have 4 possible suits:

Clubs Flush β†’ β€œ0001”. Apply a mask of β€œ0x111…1” Diamonds Flush β†’ β€œ0010”. Apply a mask of β€œ0x222…2” Hearts Flush β†’ β€œ0100”. Apply a mask of β€œ0x444…4” Spades Flush β†’ β€œ1000”. Apply a mask of β€œ0x888…8”

For each of the masks, after applying the mask, we count the number of 1 bits. Ignore the last 4 bits because we don’t want duplicate Aces.

for mask in masks: hh = (h) & mask hh = hh >> 4 if countones(hh) >= 5: return hh

Case for tie: If both players have a flush, check for the ranks of the cards. *TODO

β†’ returns a 52-bit integer. If hh has more than 5 1-bits, it is a flush.

β†’ Return the original 56-bit hand representation

6 - Straight

A straight is anywhere we have a sequence of 5 consecutive ranks of cards. Since we don’t care about suits, we will first β€œactivate” (make to a 1) all other cards where we have a card with the same suit.

hh1 = (h) & (0x111…1) hh1 = (hh1) | (hh1 << 1) | (hh1 << 2) | (hh1 << 3)

Case for tie: If both players have a straight, the winner is the one with the highest straight card value (i.e. the left most β€œ1” bit).

0001

hh2 = (h) & (0x222…2) hh2 = (hh2) | (hh2 >> 1) | (hh2 << 1) | (hh2 << 2)

hh4 = (h) & (0x444…4) hh4 = (hh4) | (hh4 << 1) | (hh4 >> 1) | (hh4 >> 2)

hh8 = (h) & (0x888…8) hh8 = (hh8) | (hh8 >> 1) | (hh8 >> 2) | (hh8 >> 3)

hh = hh1 | hh2 | hh4 | hh8

hh = (hh) & (hh >> 4) & (hh >> 8) & (hh >> 12) & (hh >> 16)

β†’ Returns a 40-bit integer. If hh is non-zero, then you have a straight.

7 - Three of a Kind

For three of a kind, we have different permutations in each group that we need to check. We then use an OR for all of these possibilities. The results finds us all three of a kind.

Case for tie: If both players have a three of a kind, find the highest three. If still tie, looking for kickers.

β†’ Returns a 53-bit integer. If hh is non-zero, then you have a three of a kind. The β€œ1” will represent the value of the rank. Ex: 0000 0001 0000 0000 … 0000 means a King three of a kind

0010

0100

1000

We have = 4 possible combinations,

0111 or 1110 or 1011 or 1101

0111 1110 1011 1101

hh = (h) & (h >> 1) & (h >> 2) & (0x111…1)

hh = (h >> 1) & (h >> 2) & (h >> 3) & (0x111…1)

hh = (h) & (h >> 1) & (h >> 3) & (0x111…1)

hh = (h) & (h >> 2) & (h >> 3) & (0x111…1)

hh = ((h) & (h >> 1) & (h >> 2) & (0x111…1)) | ((h >> 1) & (h >> 2) & (h >> 3) & (0x111…1)) | ((h) & (h >> 1) & (h >> 3) & (0x111…1)) | ((h) & (h >> 2) & (h >> 3) & (0x111…1))

Combining all 4 possible combinations with OR, we have

Apply the distributivity rule to put out the (0x111…1) mask,

hh = (((h) & (h >> 1) & (h >> 2)) | ((h >> 1) & (h >> 2) & (h >> 3)) | ((h) & (h >> 1) & (h >> 3)) | ((h) & (h >> 2) & (h >> 3))) & (0x111…1))

(AC) + (BC) = (A+B)C

Check the hand for the left most 1-bit.

8 - Two Pair

Two pair is literally as the one pair algorithm, and very similar to the three of a kind / 4 of a kind algorithm. We are looking for 2 places where there are bits overlapped.

Case for tie: If both players have two pairs, check the values of the pairs. If tie, check kicker

β†’ Returns a 49-bit integer. Two pair when there are two places with 1 bits

We have = 6 possible combinations,

0011 or 0101 or 1001 or 1100 or 1010 or 0110

0011 0101

0110

hh = (h) & (h >> 1) & (0x111…1)

hh = (h) & (h >> 2) & (0x111…1)

…

hh = (h >> 1) & (h >> 2) & (0x111…1)

Combining all 6 possible combinations with OR, and applying distributivity, we have

hh = (((h) & (h >> 1)) | ((h) & (h >> 2)) | … | … | … | ((h >> 1) & (h >> 2))) & (0x111…1))

hh = hh >> 4

9 - One Pair

See Two pair for the algorithm.

Case for tie: If both players have pairs, check the value of the pair. If tie, check kicker.

β†’ Returns a 49-bit integer. One pair exists if hh has a non-zero value

Four of a Kind happens when you find a group of four β€œ1” bits next to each other, which are all of the same rank.

hh = (hh) & (hh >> 1) & (hh >> 2) & (hh >> 3) & (0x111…1)

0000…11110000

0000…01111000

0000…00111100

0000…00011110

0000…00010000

Example: Four of a Kind for 3s

hh = (h) >> 4 # Ignore first 4 aces

h = 0000…111100000000

β€œ3”s

h = 0000…11110000

(h = h >> 4)

0001…00010001

(BIT_MASK)

(h)

(h >> 1)

(h >> 2)

(h >> 3)

&

&

&

&

=

Example: Three of a Kind for 3s

h = 0000…101100000000

β€œ3”s

hh = 0000…00010000

…

0000…11110000

0000…01111000

0000…00111100

0000…00011110

h = 0000…111100000000

β€œ3”s

h = 0000…11110000

(h = h >> 4)

0001…00010001

(BIT_MASK)

(h)

(h >> 1)

(h >> 2)

(h >> 3)

&

&

&

&

=

h = 0000…10110000

(h = h >> 4)

Embedded files

2c319581d58ec2c35124bb5b7a6f964c479d8966: 76b6e6d4f3dfefde076a203bc36116bae16ab169: 0077554832a116b9fd535514364127f8576e8885: Pasted Image 20220519223328_964.png

Link to original