Last week’s Lesson brought the Poker program up to speed with regards to drawing a hand of 5 cards. The playing_card structure provides a convenient reference for each card, and the hand is sorted. Now comes the fun part: Determining the hand’s value.
Poker hands have ranks depending on the odds of obtaining a specific card arrangement. From low to high, the ranks are:
High card. No pattern is apparent; the hand is “garbage.” Yet one card is higher than the others. For two such hands, the one with the highest card(s) wins. Aces are considered high.
Pair. Two cards of the same face value match, such as two sixes or two tens. If two players have the same two pairs, the next highest card chooses the winner.
Two pair. Two sets of pairs.
Three-of-a-kind. The hand features three cards of the same face value.
Straight. All five cards have sequential face values, which is called a “run” in other card games. For a straight, the Ace can be high or low, though a straight with a high Ace is the highest straight.
Flush. All five cards are of the same suit.
Full-house. This hand combines a pair with three-of-a-kind.
Four-of-a-kind. Four cards are all of the same value.
Straight flush. The highest ranking hand, all give cards are of the same suit and in sequence. The highest-ranking straight flush: 10, J, Q, K, A, is called a royal flush, though this hand holds no special ranking in traditional poker. (It does in video poker.)
For a program to evaluate a poker hand, the cards are best arranged in sequential order, low to high. Last week’s code already performed this sort. The next step is to evaluate each hand to determine whether it’s a winner. This process can be done in a specific sequence to logically catch valid winning combinations.
The procedure I’ve outlined for my Poker program is in this order:
- Ace-high straight
- Straight flush
- Four of a kind
- Three of a kind
- Full house
- Two pair
This order isn’t the same as for the hand rankings shown earlier. The reason is processing.
For example, if you test for a pair, you must also test for two pair as well as three-of-a-kind, a full house, and four-of-a-kind. Therefore, I ordered the procedure to reduce the amount of testing required for each hand. I determined that the winning hand with the least amount of conflicts with other hands is an ace-high straight.
After testing the ace-high straight (because the Ace’s face value in the code is 1), the standard straight is tested. If a straight is detected, another test is made to determine whether the draw is also a flush. If so, a straight flush is determined.
Next, the code tests for a flush. At this point, the straight flush has been eliminated and any other patterns in the hand would be lower-ranking.
After the flush, comes a four-of-a kind test.
The three-of-a-kind test is followed by a test for a full house.
The final test is for a pair, which is immediately followed by a test for two pair.
In this order, each hand is pulled from the code in a logical sequence. Tests need not be repeated. Functions for the various conditions return TRUE or FALSE, which the code can sift through to determine the ranking of each hand. This process begins with next week’s Lesson.