One or Two Pairs (Poker VIII)

Matching two card values in a poker hand counts as a pair. It’s the lowest-ranking hand (above the non-ranked “high card”) and the most common. In my poker program, it’s also the last ranking hand tested.

Because the winning hands four-of-a-kind, three-of-a-kind, and full house have already been eliminated in last week’s Lesson, the single pair hand isn’t that complex of a test. What I discovered when coding my pairs() function, however, was that it also makes sense to check for two pairs after the first pair is detected.

The playing_card array p[] is sorted by card value. In this arrangement, only four possible positions exist for a pair:

p[0].value==p[1].value
p[1].value==p[2].value
p[2].value==p[3].value
p[3].value==p[4].value

If checked in the above order, only three possibilities exist for two pairs:

p[0].value==p[1].value && p[2].value==p[3].value
p[0].value==p[1].value && p[3].value==p[4].value
p[1].value==p[2].value &&p[3].value==p[4].value

These combinations can be tested in sequence, which is what I did in my pairs() function:

/* Check for pairs */
int pairs(struct playing_card p[])
{
    /* test the first two cards */
    if( p[0].value==p[1].value )
    {
        /* check for a second pair */
        if( p[2].value==p[3].value || p[3].value==p[4].value )
            return(2);
        else
            return(TRUE);
    }

    /* test the second two cards */
    if( p[1].value==p[2].value )
    {
        /* check for a second pair */
        if( p[3].value==p[4].value )
            return(2);
        else
            return(TRUE);
    }

    /* test for a pair at the 3rd position */
    if( p[2].value==p[3].value )
        return(TRUE);

    /* test for a pair at the final position */
    if( p[3].value==p[4].value )
        return(TRUE);

    return(FALSE);
}

The first if test compares values for the first two cards. If true, the second pair (at two positions) is also tested.

The second if test checks for a pair at the second position, as well as a second pair at the fourth position.

The third if text checks for a pair at the third position, and the final if test checks for a pair at the fourth position.

Like the threekind() function (covered in last week’s Lesson), this function returns values other than TRUE and FALSE: TRUE (1) for a pair, 2 for two pair, and FALSE (0) for no match.

The main() function is updated to call and evaluate the pairs() function and evaluate its return value. I also added the final test, which returns the high-card value:

wprintf(L" - High card %s%lc\n",hand[x][a].face,hand[x][a].suit);

Click here to view the full code on GitHub.

Here’s a sample run:

Hand 1: 5♦ 5♠ 6♦ 9♦ 9♥ - Two Pairs
Hand 2: 2♠ 4♠ 6♠ 10♠ K♠ - Flush
Hand 3: 6♠ 7♣ 7♥ 9♥ J♦ - One Pair
Hand 4: 4♥ 5♥ 6♥ 7♥ 8♥ - Straight Flush
Hand 5: A♠ A♥ A♦ A♣ K♥ - Four-of-a-kind
Hand 6: A♦ 4♣ 4♠ 4♥ 4♦ - Four-of-a-kind
Hand 7: 5♠ 5♣ 5♦ 9♣ 9♥ - Full House
Hand 8: A♠ 9♥ 9♦ 9♣ Q♥ - Three-of-a-kind
Hand 9: A♠ A♥ K♦ K♣ K♥ - Full House

In next week’s Lesson, I wrap up the program. The original randomizer and sorter are restored as the code simulates drawing a poker hand and reporting its value.

Leave a Reply