The Poker Program

With all the hand-ranking functions complete (see last week’s Lesson), the final version of my poker program restores the randomizer, draws and sorts a hand, then outputs the hand’s value. It’s quite disappointing.

No, the code isn’t disappointing. I think the code is nifty, but the output shows you how long the odds are on a typical 5-card hand of poker.

Here’s a sample run:

Your hand is 6♦ Q♣ Q♦ 10♥ 4♣ - One Pair

Yeah! A pair of queens.

You can see that the output is unsorted, through internally the code sorts the hand by card value, which is what the various evaluation functions expect. Here’s another sample run:

Your hand is 4♠ A♥ 10♣ 2♠ 9♣ - High card: 10♣

Whoops! I noticed that my high-card if test doesn’t count for the value of an Ace as 1, low. So I fixed up the relevant part of code:

/* account for ace as the high card */
if( hand[0].value==1 )
    wprintf(L" - High card: %s%lc\n",hand[0].face,hand[0].suit);
else
    wprintf(L" - High card: %s%lc\n",hand[4].face,hand[4].suit);

This test fixed the problem:

Your hand is A♣ 6♦ 8♥ K♣ 9♦ - High card: A♣

The output you see above is most typical. Single pairs may also appear, but mostly you get a lot of high-card hands. Getting a flush or full house is truly rare. I ran the program over and over and the best I saw was three-of-a-kind. This outcome is the disappointing part.

At this point, the code can be modified further. Don’t worry! I’m not going to extend this series one more Lesson. But given what I have now, the code could be modified to be more interactive, to allow for cards to be discarded and new cards drawn. You could add a routine to draw a second hand for “the computer” and pit player’s hand against the computer’s.

If I were to modify the game further, I’d code it in Ncurses and use a full terminal window display. That way the cards can be displayed graphically (well, in text mode) and the program can truly be interactive. I may complete this task one day when I’m bored, but I’m rarely bored.

Click here to view the full code on Github. Do with it what you will.

2 thoughts on “The Poker Program

Leave a Reply