Drawing A Hand of Cards (Poker III)

Improving upon last week’s Lesson, this update to the playing card simulator offers one minor change: Multiple cards are pulled from the draw() function.

As each card is drawn, it’s stored in a hand[] array. Here’s the update to the main() function, which adds a for loop to last week’s code:

int main()
{
    int deck[CARDS];
    struct playing_card hand[HAND_SIZE];
    int x;

    /* seed the randomizer */
    srand((unsigned)time(NULL));
    /* activate wide characters */
    setlocale(LC_ALL,"en_US.UTF-8");

    /* initialize the deck */
        /* 0 = card not drawn
           1 = card drawn */
    for( x=0; x<CARDS; x++)
        deck[x] = 0;

    /* draw a hand of HAND_SIZE cards */
    printf("Your hand:");
    for( x=0; x<HAND_SIZE; x++)
    {
        hand[x] = draw(deck);
        wprintf(L" %s%lc",
                hand[x].face,
                hand[x].suit
              );
    }
    putchar('\n');

    return(0);
}

Click here to view the full code on Github.

The for loop fills array hand[] with five cards from the draw() function. The rest of the code remains unchanged. Here’s a sample run:

Your hand: 2♥ 10♦ K♣ 3♠ 2♠

I’m pleased with the results. To play a card game, however, the code must do what humans do when dealt a hand: sort the cards.

To organize the output, I added a bubble sort to an update of the main() function:

int main()
{
    int deck[CARDS];
    struct playing_card hand[HAND_SIZE],temp;
    int x,a,b;

    /* seed the randomizer */
    srand((unsigned)time(NULL));
    /* activate wide characters */
    setlocale(LC_ALL,"en_US.UTF-8");

    /* initialize the deck */
        /* 0 = card not drawn
           1 = card drawn */
    for( x=0; x<CARDS; x++)
        deck[x] = 0;

    /* draw a hand of HAND_SIZE cards */
    for( x=0; x<HAND_SIZE; x++)
        hand[x] = draw(deck);
    /* sort the hand by value */
    for( a=0; a<HAND_SIZE-1; a++)
    {
        for( b=a+1; b<HAND_SIZE; b++ )
        {
            if( hand[a].value > hand[b].value )
            {
                temp = hand[a];
                hand[a] = hand[b];
                hand[b] = temp;
            }
        }
    }

    printf("Your hand:");
    for( x=0; x<HAND_SIZE; x++ )
    {
        wprintf(L" %s%lc",
                hand[x].face,
                hand[x].suit
              );
    }
    putchar('\n');

    return(0);
}

(Github)

The bubble sort uses nested for loops and variables a and b to order the structures in hand[] by card value. This ordering makes it easier for a future update of the code to find things such as pairs or a straight.

Here’s sample output:

Your hand: 3♣ 3♥ 4♣ J♥ J♦

This output lets you quickly see two pairs, threes and jacks.

I could add a second sort to organize the hand by suit as well. Still, with only four suits and 13 different card values, sorting by value makes the most sense.

The next step in this program’s evolution is to run tests on the hand based on the game played. I’m choosing Poker, so the tests will determine the various hand rankings:

High card
Pair
Two pair
Three-of-a-kind
Straight
Flush
Full-house
Four-of-a-kind
Straight flush

Dealing with these rankings in a logical manner is discussed in next week’s Lesson.

Leave a Reply