poker

A poker hand as one number: the best hand is the highest.

What it is

A poker hand evaluator that turns a hand of 5 to 7 cards into one whole number. The better hand gets the higher number, and equal hands get the same one. So the winner of a showdown is just the highest score: no sorting, and no comparing one hand with another.

  • Every hand scores between 372,197 (7 5 4 3 2) and 6,783,750 (a royal flush). That fits in 23 bits, in exact integer arithmetic.
  • The score reads back: its digits are the hand's category and the cards that decide a tie.
  • Five-card draw and Texas hold'em take the same path, with no trying of the 21 ways to pick five cards from seven.
  • It is checked on every hand of 5, 6 and 7 cards. The 5-card check runs right here, below.
  • One small JavaScript file, with no dependencies.

How it works

  1. Lay the hand out as a grid: a row per suit and a column per rank.
  2. Count down each column. A count of 4, 3 or 2 is four, three or two of a kind.
  3. Count along each row. Five in one row is a flush.
  4. Look for five filled columns side by side. That is a straight, and the ace also counts below the 2.
  5. Read it all from the high end, so the five cards that play are the first ones met.
  6. Write the score as the hand's category, 0 for high card up to 8 for a straight flush, then the ranks that break ties, as digits in base 15. A rank is at most 14, so a digit never carries into the next one, and a better category always wins.

Each hand below shows its grid with the counts it reads, and its score in base 15: the first digit is the category, the rest are the deciding ranks, with 10 to 14 written a to e.

Try it

Hands

Every hand

There are 2,598,960 hands of 5 cards, and the rules sort them into 7,462 kinds. The check scores every hand, then holds the scores to a separate, textbook ranking: every hand of a kind must score the same, and the scores must rise kind by kind. It runs in your browser, on the same code as above.

Each 6- and 7-card hand must score the same as the best 5 cards in it. npm run every checks all 20,358,520 hands of 6 cards and all 133,784,560 of 7.

Where it comes from

The grid is the idea of pokerIn4Hours, which I wrote in 2011 for a four-hour developer test. Its code is kept as it was in original/, and the same checks run on it. The README says what they find, line by line.