Texas Hold 'em Win Probability
Time limit8sMemory limit256 MB
Given both hole cards and the flop, enumerate all 990 turn and river pairs and report the share where your best five-card hand beats the opponent's.
- Level
Medium5 of 10
- Topics
- Brute force, Implementation
- Solved
- No attempts yet
Problem
Texas hold 'em is a poker game played with a standard deck of 52 cards. The deck has 4 suits (spades, hearts, diamonds, clubs) and 13 ranks (A, K, Q, J, and 10 down to 2). There are no jokers.
With the betting set aside, one game runs like this.
Each player first receives two cards face down. These are the hole cards, and they stay hidden until the showdown. The dealer then deals three cards face up as community cards, shared by every player. Those three cards are the flop. One more community card follows, the turn, and then a last one, the river.
After the river the game reaches the showdown. Every player reveals the hole cards. Each player then chooses five of the seven available cards, two hole cards plus five community cards, and those five cards form a hand. The player with the strongest hand wins.
Ten kinds of hands exist. From the strongest to the weakest:
- Royal straight flush: A, K, Q, J, 10 of the same suit. This is a special case of a straight flush.
- Straight flush: five cards in sequence (for example 7, 6, 5, 4, 3) and all of the same suit.
- Four of a kind: four cards of the same rank.
- Full house: three cards of one rank plus a pair of another rank.
- Flush: five cards of the same suit that are not in sequence.
- Straight: five cards in sequence that are not all of the same suit.
- Three of a kind: exactly three cards of the same rank.
- Two pairs: two cards of one rank and two cards of another rank.
- One pair: exactly two cards of the same rank.
- High card: any other hand.
Inside a sequence, J, Q and K count as 11, 12 and 13. A can sit either right above K or right below 2, so A, K, Q, J, 10 and 5, 4, 3, 2, A are both sequences, while 3, 2, A, K, Q and anything like it is not.
When more than one player holds the same kind of hand, compare card ranks to break the tie. Compare the cards that form sets (pairs, triples, quads) first, then the remaining cards one by one from the highest rank down, until the tie breaks. In detail:
- Royal straight flush: the tie does not break.
- Straight flush: compare the highest card.
- Four of a kind: compare the four cards, then the remaining one.
- Full house: compare the three cards, then the pair.
- Flush: compare all five cards one by one.
- Straight: compare the highest card.
- Three of a kind: compare the three cards, then the remaining two.
- Two pairs: compare the higher pair, then the lower pair, then the last card.
- One pair: compare the pair, then the remaining three.
- High card: compare all five cards one by one.
The ranks run A, K, Q, J, 10, 9, ..., 2 from the highest to the lowest, except that an A placed below the 2 in a straight ranks lower than the 2. Some pairs of hands still tie after every comparison. Suits never take part in tie breaking.
Here are a few comparisons. They only explain the rules, and some of these combinations cannot occur in Texas hold 'em.
- J, J, J, 6, 3 against K, K, Q, Q, 8. The first hand wins because three of a kind beats two pairs.
- J, J, J, 6, 3 against K, Q, 8, 8, 8. Both hands are three of a kind, so the triples decide, J against 8. J is higher, the first hand wins, and the remaining cards 6, 3 and K, Q never come up.
- Q, J, 8, 6, 3 against Q, J, 8, 5, 3. Both are high card hands. The three highest cards Q, J, 8 are the same, so the fourth cards are compared, and 6 is higher than 5.
- 9, 9, Q, 7, 2 against 9, 9, J, 8, 5. Both are one pair with a pair of the same rank, so the remaining cards are compared from the highest down. Q is higher than J, so the first hand wins.
Now suppose you are playing a game of Texas hold 'em against one opponent. The hole cards and the flop are already dealt, and you know which cards the opponent holds. You do not know what the turn and the river will be.
Compute the probability that you win, assuming the turn and the river are chosen uniformly at random from the 45 cards nobody has seen. You and the opponent always form the strongest hand possible. A tie counts as a loss.
Input
The input consists of several datasets. Each dataset takes three lines.
YourCard1 YourCard2
OpponentCard1 OpponentCard2
CommunityCard1 CommunityCard2 CommunityCard3
The first line holds your two hole cards, the second line holds the opponent's two hole cards, and the third line holds the flop. Cards on one line are separated by spaces.
Each card is written with two characters. The first one is the suit: S (spades), H (hearts), D (diamonds) or C (clubs). The second one is the rank: A, K, Q, J, T (10), or a digit from 9 to 2. The seven cards of one dataset are all different.
A line holding a single # ends the input. Do not process that line.
Output
For each dataset print your winning probability on one line, rounded to exactly 10 digits after the decimal point. Print the leading 0 and every trailing zero, so a probability of 0 prints as 0.0000000000 and a probability of 1 prints as 1.0000000000.
The turn and the river are one of the ways to pick two of the 45 remaining cards, so the probability is always an integer over 990 and the rounded value is unique.