By Jarred Simmer | Principal Software Engineer
October 22, 2018

Zynga Poker has a feature wherein we show to the player the “strength” of her poker hand. This helps players new to the game intuitively understand how likely they are to win or lose. Because Zynga Poker is a live, multiplayer game where a player can have as little as five seconds to act (or even less when considering a user’s network latency), this feature has been designed to operate incredibly quickly for immediate surfacing to the user such that her turn doesn’t end before she sees this information. However, to account for this time constraint we’ve had to use a somewhat-but-not-completely-accurate means of calculating the strength of a poker hand. I’ll be explaining:

1. How the feature currently works
2. Tradeoffs made in this design
3. The design for a future iteration of this feature
4. Why that design has yet to be released


How It Works

The way this feature currently works is relatively straightforward:

1. Using the player’s 2 hole cards and visible community cards, the best possible 5 card (or less, if the community cards are not yet visible) hand the user could make is found on our game server.
2. That hand is classified as one of the following

High Card
One Pair With A Kicker
Two Pair
Three-Of-A-Kind With A Kicker
Straight
Flush
Full House
Four-Of-A-Kind
Straight Flush

3. That hand will win in a showdown on the river a certain percentage of the time. For example, the full house “Three-of-a-kind twos and a pair of Jacks” has historically won, let’s say, seventy percent of the time when shown down on the river. Note that this excludes all the times where someone holding that hand won because all their opponents folded. This percentage is already stored in one of our databases. It is retrieved whenever a new card is dealt and sent down to our client.
4. This percentage is translated on our client into the visual filling of a meter displayed to a user. For example, a winning percentage of fifty percent would be translated into a meter filled halfway.

Zynga Poker’s Hand Strength Meter

Tradeoffs of This Design

The Good

Because the winning percentages have already been pre-calculated and stored in Part 3 of “How It Works” rather than calculated on-the-fly, the process of looking up the winning percentage for a hand is very fast. This allows us to show the same information to all of our users while allowing them a reasonable time with which to take their turn.

The Bad

The percentage calculated in Part 3 of “How It Works” is usually fairly accurate, but in some cases (particularly degenerate ones) this percentage can be far from accurate.

As one of the most extreme examples, let’s suppose we have

Our Hole Cards

and the current community cards are

Community Cards

For simplification, let’s assume that we need at least a straight or a flush to win the hand. In that case, any of the following cards would give us the winning hand:

Outs

Both the turn and the river would have to not be one of these cards for us to lose the hand, a likelihood of

100 * (34/47 * 34/46) = 53.47%

This in turn means that we have a 46.53% chance of winning the hand.

However, the percentage found in Part 3 from “How It Works” only takes into account our current hand, Jack high, not our potential for improving our hand. Since our hand has a high potential for improving, we’re missing key information in our estimate.

Why We Chose This Tradeoff

The degenerate case above raises the question, “Why did you do this approximation instead of actually calculating the exact percentage? How much time could that really take to calculate?” Let’s find out, for the most extreme case, just how long this takes.

For this specific example, we’ll assume there are eight opponents, all still in the hand, with all 5 community cards yet to come. To find our exact chance of winning, we need to:

1. Enumerate the total possible ways the yet unknown cards could be revealed.
2. For each of these possibilities, find
a. The best hand we could make
b. The best hand each of our opponents could make
c. Whether our hand is better than all of our opponents’ hands.
3. Take the number of possibilities that would have resulted in us having a winning hand, divide it by the total number of possibilities, and multiply it by 100 to give us our percentage chance of winning.

We have 8 opponents, each with two unrevealed cards, and 5 unrevealed community cards. This means that there are

(50 choose 2) * (48 choose 2) * (46 choose 2) * (44 choose 2) * (42 choose 2) * (40 choose 2) * (38 choose 2) * (36 choose 2) * (34 choose 5)
=
50!/(2 * 48!) * 48!/(2 * 46!) * 46!/(2 * 44!) * 44!/(2 * 42!) * 42!/(2 * 40!) * 40!/(2 * 38!) * 38!/(2 * 36!) * 36!/(2 * 34!) * 34!/(5! * 29!)
=
50!/2 * 1/2 * 1/2 * 1/2 * 1/2 * 1/2 * 1/2 * 1/2 * 1/(5! * 29!)
=
50!/(256 * (5! * 29!))
=
50!/(30720 * 29!)
=
(50*49*48…*30)/30720
=
111,973,393,664,173,216,721,145,600,000

possible outcomes.

All of these would need to be checked to see if you would have the winning hand. For the sake of argument, let’s say we can make that check (step 2 from above) for a given outcome in one millisecond. This means you would complete step (2) from above in 3,548,222,731,265,153,773 years. According to scientists who hypothesize an eventual collapse of the universe (The Big Crunch), you wouldn’t be done until ~ 35 quadrillion years after the universe collapsed on itself!

Knowing that it’s physically impossible to find an exact percentage chance that we will win a given hand in this situation, our current method of providing a “best guess” is a decent alternative despite the existence of degenerate cases.

Future Hand Strength Meter Design

In order to consistently present our users with a much closer approximation to their exact chance of winning a hand, we devised an adaptation of Monte Carlo simulation. Rather than search through all possible eventual outcomes of a hand for the ones in which you would win, we instead randomly generate a subset of those outcomes and see how many of that subset you would win.

There are key tradeoffs regarding the size of this sample subset. A smaller sample size will result in faster completion of the simulation such that a solution is presented to the user with more time with which they can act. However, that smaller sample size will lead to a potentially less accurate approximation. Conversely, a larger sample size will require more time to complete while providing a more accurate approximation.

One way to allow for the benefits of both these approaches while lessening their respective downfalls is for us to show to users the current value of the approximation as the algorithm progresses. That is, on your turn we can show you the current approximation that you will win the hand at showdown. That number will be constantly changing, becoming ever more precise until you either take an action or run out of time. This allows us to show the user as accurate an answer as possible while also allowing them as much time as possible with which to act.

Why Our New Hand Strength Meter Hasn’t Been Released

In a word: fairness. We aim to make our game equally fair to all players. A user with a 5-year-old phone would be at a distinct disadvantage to a user with a brand new, state of the art phone should this feature be released today. We support such a wide range of devices in our game that the number of simulations one device can perform in a given time can be drastically different than the number of simulations that another device can perform in that same time. More simulations means higher accuracy in this approximation, and higher accuracy leads to a competitive advantage.

There is hope, however. After a certain number of iterations, the discrepancy between the current approximation and the approximation after further iteration diminishes to the point of inconsequence. As our minimum supported devices continue to improve and allow for faster iterations, we get closer to being able to provide this amazing feature to all our players without introducing any unfairness.

Conclusion

Calculating the exact chance of winning a poker hand at the beginning of a hand of 9 players is currently unsolvable. In order to solve this problem, we are currently approximating that chance and presenting it to our users. This solution has its own downfall of inaccuracy, though. We’ve created a feature that goes a long way in solving this problem, but we are currently waiting for the right time to present it to our users while allowing for complete fairness. We have a U.S. Patent No. 9,940,783 (issued Apr. 10, 2018) regarding this design if you are interested in learning more about it.

Thank you for the read! If you found this post interesting or helpful, give it a share. If the ramifications of insurmountable combinatorics in our game is of interest to you, I encourage you to read our previous post regarding random number generation. If you have any input you’d like to give us, reach out to us at Poker-Engineering-Blog@zynga.com or shoot me an email at jarredwsimmerengineering@gmail.com.

References:

https://www.iflscience.com/physics/big-crunch-back-possible-end-universe/

https://en.wikipedia.org/wiki/Monte_Carlo_method

Zynga Engineering

Engineers connecting the world through games. https://www.zynga.com/careers

Zynga Poker Engineering

Written by

Based in San Francisco but operating worldwide, we continuously improve our technology in order to provide the best gaming experience for our users.

Zynga Engineering

Engineers connecting the world through games. https://www.zynga.com/careers

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade