# Evaluating poker hands programmatically

Given a hand, I am trying to determine the value of that hand, and implement this in a programming language for fun. Would the following work? I am not a poker player, so I don't know for sure.

Input is 7 cards --> check if there is a straight --> if there is a straight, then check if there is also a flush, in which case the hand's value must be straight flush --> if there is a straight, but not a flush, then the hand's value most be a straight --> if there wasn't a straight at all, then check if it is quads. If true, then the hand's value is quads --> if not true, check if its a full house --> if not true, check if its a flush --> if not true, check set --> if not true, check two pair --> if not true, check pair --> if not true, it's just a kicker.

• No. It won't. Learn poker first, then you can think about some simulations about it. Jan 11, 2017 at 21:09
• Over in the programming stacks there are good questions and answers about shuffling cards, and other things concerning poker programming..
– Jon
Jan 13, 2017 at 6:10

## 4 Answers

The general method you describe (that is, check-for-hand-x, then check-for-hand-y, ...) is what poker programmers would call a "naive" or "basic" evaluator. Not that there's anything wrong with it, it's just simple, straightforward, and probably slow.

To write one of these, I suggest the following:

1. Start with 5 cards. Once you have that written, you can evaluate 7-card hands by evaluating the 20 5-card combinations of each set of 7, then picking the best of those 20.

2. Start your evaluator by sorting the hand by rank, A...2. It's helpful for this if you choose to represent cards in a way that makes this the natural order--that depends on the language.

3. Then, check for hands in reverse strength order: i.e., check for straight flushes, then quads, the full houses, etc., down to one pair.

After you've done this, you might want to look at more sophisticated (and faster) approaches. For example, "Cactus Kev"'s prime-based algorithm, Pokersource's bitmap-based code, or my own OneJoker library, which uses a optimized directed acyclic graph to get what I believe is the fastest 5-card evaluator anywhere (The "2+2" evaluator uses the same approach and is faster for 7 cards, but takes a lot more memory).

This is a much more difficult programming exercise than one might think, but a very challenging puzzle.

• "Not that there's anything wrong with it" there's everything wrong with it ;) Nov 26, 2019 at 10:38
• I meant there's nothing wrong with a basic naive evaluation algorithm in principle. I wasn't speaking specifically to the OP's. Nov 26, 2019 at 19:06

Even if it did "work" (as Paparazzi points out, it wouldn't, because you can hold both a straight and a flush in 7 cards without also holding a straight flush), it's not going to be the most efficient way by a long stretch.

There's a bunch online about this and some contributors here like Paparazzi have created their own evaluation code.

Check out this page for one example of a histogram based evaluation method for example.

For one it can be a straight and a flush and not be a straight flush.

This is a 5 card evaluator here. 7 is like twice as complex. This is bit much for a hobby project.

As you describe would be very slow.

Do all the rank match counts once. Then flush, then straight, and then if straight and flush check for a straight flush.

Even with 7 you cannot have quads or boat and have either a straight of flush. Since they are cheap to run you do that first.

Check out this algorithm https://github.com/HenryRLee/PokerHandEvaluator/blob/master/Documentation/Algorithm.md. You don't have to iterate all 5-card hands from a 7-card poker. Moreover, you don't need to check each ranking category, from straight-flush to high-card, one by one. Just a few CPU cycles and a hash table, you can evaluate up to 9-card poker hands.

• Your link is broken, and you are supposed to indicate that the link is to your site, otherwise it may get marked as spam.
– Herb
Nov 24, 2019 at 16:22
• @HerbWolfe the link is fixed. Thank you. It's a github site, so I suppose it won't be recognized as spam. Nov 25, 2019 at 22:00