Uncle Sean's Electronix Fun Page
Home
Contact
Links
Legal

Scoring

Now that the hand is dealt or drawn, it's time to score it.

There is surprisingly little you need to know about a poker hand in order to score it. Only five characteristics need to be found:

  • Flush Flag: 1 if all cards in the hand have the same suit, 0 otherwise.

  • Straight Flag: 1 if the cards form a straight, 0 otherwise.

  • Number of Pairs: Every pair is enumerated. For instance, three cards of the same rank form three pairs. If the hand contains the Five of Hearts, the Five of Diamonds, and the Five of Spades, the pairs are Five of Hearts + Five of Diamonds; Five of Hearts + Five of Spades; Five of Diamonds + Five of Spades. Similarly, four of a kind forms six pairs.

  • Lowest Rank: Rank of lowest-ranking card in the hand. Only matters for spotting Royal Flushes.

  • Pair Rank: Rank of last (or only) pair in the hand. Relevant only in spotting high pairs.

Once you know these, the following table shows how they determine the score. In it, values marked “X” mean that it doesn't matter what value that characteristic has. The osbervant reader, though, will have figured out that any hand that is a flush or straight cannot have any pairs in it, and vice versa.

Flush

Straight

# of Pairs

Low Rank

Pair Rank

Score

1

1

X

=10

X

Royal Flush

1

1

X

<10

X

Straight Flush

X

X

6

X

X

4 of a kind

X

X

4

X

X

Full House

1

0

X

X

X

Flush

0

1

X

X

X

Straight

X

X

3

X

X

3 of a kind

X

X

2

X

X

Two Pair

X

X

1

X

ace or >10

Jacks or Better

All others worthless

Coding Details

Here's pseudocode for finding these characteristics. The assembly code differs from it in minor ways but this is how it runs.

Flush Flag

  1. clear flush flag.
  2. Find and memorize the suit of the first card in the hand.
  3. For each of the other 4 cards:
    1. Find the suit.
    2. If it doesn't match the first card's suit, quit.
  4. If you've reached here, it's a flush. Set flush flag and quit.

Number of Pairs / Pair Rank

  1. set pair count (numpairs) to 0.
  2. For card i = 0..3:
    1. memorize card i's rank (temprank).
    2. For card j = i+1 to 4:
      1. Find card j's rank.
      2. If card j's rank = temprank, increment numpairs and set pairrank = temprank.
As a side effect Pair Rank is found in this process. As seen above it's used for spotting high pairs.

Straight Flag / Lowest Rank

  1. Clear straight flag.
  2. Make a temporary copy of the hand (temphand).
    You need to do this because it gets trashed for ace-high straight spotting, below.
  3. call the find high & low subroutine (below); this sets hicard to the rank of the highest-ranking card in the hand, & locard to the lowest rank.
  4. if hicard-locard = 4, it's a straight! set straight flag and return.
  5. there may still be an ace-high straight: if locard != 0 (Low Ace), there isn't. In that case, quit.
  6. There may be an ace-high straight. For each card in temphand:
    1. Find the rank.
    2. If rank = 0 (low ace), rewrite the card as rank 13 (high ace).
  7. Go to step 3.
    The exact same code that looked for an ace-low or non-ace straight works verbatim!

Finding High & Low card:

  1. find rank of first card in temphand.
  2. set hicard and locard both to first card's rank.
  3. For each of the other 4 cards in temphand:
    1. Find the rank.
    2. If rank < locard, set locard = rank.
    3. If rank > hicard, set hicard = rank.

A happy side-effect of this process is that locard is used in spotting royal flushes.

Find the score

Equipped with the characteristics, you can use the table above to figure out what kind of hand you've got. At this point, if it's the initial deal, report the score and await the player's discard instructions. If it's after the draw, calculate and deliver the FILTHY LUCRE if any is forthcoming.

The Scoring Test Program

The unit test for this step is a good illustration of how the PC can guide the PIC. Also, since the unit test on dealing didn't test the card-drawing code, it seemed appropriate to jam it in here.

using the PC-side C poker simulator, generate hands. Start at seed 0.

  1. Generate the 10-card hand for the seed.
  2. For h = each of the 32 possible held-card configurations (heldmasks):
    1. perform the draw to make a 5-card hand.
    2. score the 5-card hand, yielding the quantity x (from 0 to 9, where 0 = worthless and 9 = royal flush).
    3. if no seed has yet generated a 5-card hand scoring x using hold config h, record this seed as doing so.
  3. stop when every possible score from every possible heldmask has been yielded by every possible draw configuration.
    until then, increment the seed and try again.

This should yield, then, a set of 320 seeds: 10 different scores times 32 draw combinations.

On the PIC side, the test program tries out each of these 320 seeds, knowing the heldmask and the expected score. It deals the hand using the random seed, then draws according to the heldmask, then scores the hand. If the score returned by the scoring subroutine matches the expected score, all's well.

Pat Royal Flush!

Here's a screenshot from the score testing program. On the top, we see the seed that was used to deal the hand, followed by the heldmask (here, all cards held), followed by the expected score once the draw is applied. The second line shows the 10-card hand drawn. On the third line, there is the pat hand and its score, then finally on the last line there's the drawn hand and its score.

Almost Royal Flush

Here's a bit more typical situation as far as royal flushes are concerned – in the pat hand, all but one card is from the royal flush in diamonds. Faced with this, a wise player would hold the 4 Royal Flush cards and hope for the best. In this case, it works out!

Note that the held-mask reads backwards from the hand display; the non-held card is number 5 which is the first bit shown.

Hidden Royal Flush

And here's an interesting case indeed – a hand which, as dealt, is a respectable Two Pair – but if you toss all of them, the drawn five cards form a Royal Flush. Things like this happen, and there's no way to know! How perverse it is, our world.