Flush, Straight, and Straight Flush Tests (Poker VI)

After pulling out any hands arranged in a straight pattern, covered in last week’s Lesson, the next logical test is for a flush. The flush draw is when all cards are of the same suit.

Here is the flush() function I wrote to determine when cards in the hand represented by the playing_card structure p are arranged as a flush draw:

/* Determine a flush draw */
int flush(struct playing_card p[])
{
    wchar_t s;
    int x;

    /* obtain the first card's suit */
    s = p[0].suit;
    /* compare with all the other cards */
    for( x=1; x<HAND_SIZE; x++ )
    {
        if( s != p[x].suit )
            return(FALSE);
    }

    return(TRUE);
}

After the first card’s suit is obtained (p[0].suit), its value is compared with the remaining four cards in the hand. If any other card doesn’t match, FALSE is returned. If the code survives the for loop, TRUE is returned.

To test the hand drawn, the main() function offers a series of if tests. The first is for the straight flush test:

if( straight(hand[x]) && flush(hand[x]) )
{
	wprintf(L" - Straight Flush\n");
	x++;
	continue;
}

If the hand returns TRUE for both the straight() and flush() functions, the result is a straight flush. Text is output, the looping variable x is incremented, and the continue statement skips the remaining tests, looping another time.

The next test is for a straight:

if( straight(hand[x]) )
{
	wprintf(L" - Straight\n");
	x++;
	continue;
}

Then comes the flush test:

if( flush(hand[x]) )
{
	wprintf(L" - Flush\n");
	x++;
	continue;
}

Click here to view the full source code on GitHub.

I modified the playing_card hand[][] array to feature a flush and straight flush in the samples. Here’s the program’s output:

Hand 1: 5♦ 5♠ 6♦ 9♦ 9♥
Hand 2: 2♠ 4♠ 6♠ 10♠ K♠ - Flush
Hand 3: 6♠ 7♣ 7♥ 9♥ J♦
Hand 4: 4♥ 5♥ 6♥ 7♥ 8♥ - Straight Flush
Hand 5: A♥ 3♥ 7♦ K♣ K♥
Hand 6: A♦ 10♣ J♣ Q♥ K♣ - Straight

In next week’s Lesson, the code is updated further to perform tests for four-of-a-kind, three-of-a-kind, and a full house. These tests are ordered to process the hands in a logical manner without having to write repetitive code. The final tests, presented later this month, is for one and two pairs.

Leave a Reply