# Calculating the probability of bettering a 5 card poker hand by replacing one card with a dealt card

I am writing a Poker game followed by a Poker playing AI in Java as a college project. In my code, a hand is stored as a 5 element array ordered, firstly, by the "most important" cards and then by card value, as shown in the following examples

3 of a Kind: [4H, 4D, 4S, KH, 8C]

Full House: [7H, 7D, 7C, 9S, 9C]

Pair: [3H, 3C, AS, 4D, 2C]

These hands are also represented using a 24-bit binary string in the form of:

|- 4 bits -| |- 4 bits -| |- 4 bits -| |- 4 bits -| |- 4 bits -| |- 4 bits -|
Hand Type    Best Card        ...         ...          ...       Worst Card


where Hand Type corresponds to:

High Hand = 0

One Pair = 1

Two Pair = 2

...

Straight Flush = 8

Royal Flsuh = 9


and the 4-bits for each card is its value (0 - 12 as it is card value minus 2).

These binary strings then allow cards to be compared to each other - a higher value is a better hand.

Our next task is to create an algorithm that determines the probability of the hand improving if a given card in the hand is discarded and replaced with the next (random) card in the deck. As of now, we are under the assumption that there are no other players/hands, so there are 47 cards left in the deck and discarded cartds cannot be dealt again. We were given examples such as if we have the hand [9H, 6D, 5C, 3D, 2S] (a broken straight) then what is the probability of exchanging the 9 for a 4 and completing the straight?

We do not, yet, need to determine whether or not we should discard this card based on the probability - we are strictly just finding the probability that the hand will be improved.

Of course, this becomes quite complex when you need an algorithm that can calculate the probability of impoving any given hand into a better one (e.g. certain Straights could be changed to a Flush or a Straight Flush or a Royal Flush).

Since my hands have a binary representation (this was not a requirement for the project - it was a result of one of my posts), is there a more simple approach to solving this problem?

• Have you considered computing the 47 hands separately and then calculating the win chances from that? – Carl Patenaude Poulin Feb 27 '17 at 19:17
• I think in the real code for poker game, there is a large look up table. In this look up table, there are ${52 \choose 5}=2598960$ rows and for every row there are $47$ nodes that contain $47$ cases for that row. This is my idea and maybe not true. In fact, when I was working with Maple software and run maple to compute the small prime numbers, the Maple calculate so fast but if you ask maple to produce a large prime numbers, it takes more time that means, the Maple use the look up table for small prime numbers and does not calculate these numbers. – Amin235 Feb 27 '17 at 19:34
• @CarlPatenaudePoulin I actually never thought of that simple solution. So I simply create all 47 other possible hands, count how many are better than the current hand, then (count / 47) * 100 is the probability that the hand will be improved? – KOB Feb 27 '17 at 19:47

You are making this way too hard
Poker players count outs at the table

Td 7h 6h 5h 4h

Take out the Td

13 - 4 = 9 cards to make a flush
6 cards make a straight (cannot count the cards you counted in the flush)
12 cards make a pair
12 cards make a higher top card or kicker
39/52 chance to improve

When you take out the 7h you don't have a chance of making a straight or flush
12 cards make a pair
24 cards make a better kicker

If you have have top pair

Td Tc 6h 5h 4h

If you take out any T you have zero ways to improve

At the table we would not count a higher kicker as an out. In playing you only count an out if it is likely to be the best hand. And you don't count it as as an out if it also help your opponent (e.g. pair the board). So you can include board improving or not.