Straight Draw (Poker V)

The first test for a hand of five cards is the straight, specifically an Ace-high straight followed by a standard straight. To perform this test, I’ve concocted a special version of the program, one that has a set of pre-drawn poker hands.

The reason for presetting the poker hands is that random draws would take too long to test the code. It’s possible, of course, but it’s far more efficient to manually create an array of poker hands with positives and negatives and run test functions on those.

You can view the full source code for this week’s Lesson on Github: click here.

From last week’s Lesson, I’ve updated the main() function to contain an array of 6 (set by the defined constant `HANDS`) preset playing_card structures:

```struct playing_card hand[HANDS][HAND_SIZE] = {
{ { DIAMONDS, "5", 5 }, { SPADES, "5", 5 }, { DIAMONDS, "6", 6 }, { DIAMONDS, "9", 9 }, { HEARTS, "9", 9 } },
{ { SPADES, "4", 4 }, { HEARTS, "6", 6 }, { CLUBS, "6", 6 }, { HEARTS, "7", 7 }, { DIAMONDS, "10", 10 } },
{ { SPADES, "6", 6 }, { CLUBS, "7", 7 }, { HEARTS, "7", 7 }, { HEARTS, "9", 9 }, { DIAMONDS, "J", 11 } },
{ { HEARTS, "4", 4 }, { CLUBS, "5", 5 }, { DIAMONDS, "6", 6}, { CLUBS, "7", 7 }, { SPADES, "8", 8 } },
{ { HEARTS, "A", 1 }, { HEARTS, "3", 3 }, { DIAMONDS, "7", 7 }, { CLUBS, "K", 13 }, { HEARTS, "K", 13 } },
{ { DIAMONDS, "A", 1 }, { CLUBS, "10", 10 }, { CLUBS, "J", 11 }, { HEARTS, "Q", 12 }, { CLUBS, "K", 13 } }
};```

The cards in the `hand[]` array are pre-sorted, so the code need not sort them. Instead, the main() function outputs each hand, then calls the straight() function:

`r = straight(hand[x]);`

The function returns `TRUE` or `FALSE` (defined in the code) based on its evaluation. Here is the straight() function:

```/* Determine a straight draw */
/* This function assumes the HAND_SIZE is fixed at 5 */
int straight(struct playing_card p[])
{
/* straight, ace high */
if( p[0].value==1 && p[1].value==10 && p[2].value==11 && p[3].value==12 && p[4].value==13 )
return(TRUE);

/* other straight */
if( p[0].value != p[1].value-1 )
return(FALSE);
if( p[1].value != p[2].value-1 )
return(FALSE);
if( p[2].value != p[3].value-1 )
return(FALSE);
if( p[3].value != p[4].value-1 )
return(FALSE);

return(TRUE);
}```

The first if test is a complex logical construction that checks for the condition: Ace, 10, Jack, Queen, King — an Ace-high straight. Remember, in my playing_card structure, the Ace is the lowest valued card, 1.

The second clutch of if tests determine the sequential value for each card. Remember: They’re sorted. So if the first card, `p[0]`, is one less than the second card, `p[1]`, the testing continues. Each successive card is tested until the fourth, `p[3]`, is compared with the fifth, `p[4]`. When all the tests pass, the hand is a straight.

Two straights are preset in the `hand[]` array to test the function’s accuracy. Here’s a sample run:

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

Hand 6 is a straight, but it looks awkward because the Ace is shown low. A correction could be made later in the code, or the code could present the hands as randomly drawn and internally sort itself. This change is made later in the program’s development.

In next week’s Lesson, the code is updated to check for a flush and straight flush.