Intro
I’m building a poker game in C with two goals: a cli graphics system, and computer adversary. I thought those would be the main sticking points, but having just finished the poker logic, I realized this was a much larger undertaking than I had anticipated.
Successes
There are a few systems I am especially proud of.
Checking for straights
to check for straights I opted for a hash-map. The code first initializes the hash-map with null pointers. The hash function will be, the card is placed in the index corresponding with its value. This is fine since there are only 13 possibilities. Then the code checks for the first non-Null index. This is the start index. Finally we search the next 4 values after the start. If there are any null pointers there is no straight. Here’s the code:

Finding Pairs, Three of a kind, Four of a kind, Full house, and Two pairs
For this code I also used a hash-map to count the occurrences of each value. the hash function is similar except I initialize the hash-map with zeros then increment the index of a value each time it appears in a hand. Then we create a count of every value that is greater than two. This count tells us the case. A count of 2 would be a pair, 3 is three of a kind, 4 is four of a kind, and 5 is a full house. We need to add an additional pair counter to differentiate between 4 being four of a kind or a two pair. Here’s the code:

Failures
The code that I am least satisfied with is the function that sets the best hand. I struggled to find an efficient way to generate combinations. The challenge: selecting the best five-card hand from seven cards available(two in the pocket, five on the table) proved the be a big obstacle. I ended up choosing the only way I knew would work… A five level for loop. It’s really bad and as I work on the project I will keep it in the back of my head as a problem that needs reassessing. In spite of what my heart tells me, here’s the code:

The Future
So far I have implemented the basic logic behind poker. I can shuffle a deck of cards, deal cards to players, deal the flop, turn, and river, as well as decide a winner based on the players’ best hands. Here’s an example output:

The player got a great hand; a two pair! That’s easy the player wins against the computer’s pair of twos. Let’s try a more complex one:

Close! But the player’s pair of eights beats out the computer’s pair of fours.
There are more features to be added. I need to get betting involved. I want the player to have basically infinite money, but poker isn’t the same without something to bet on. To do this I need to make a probability function for the computer betting. I also would love to make a visual display using special characters in the command line. And of course I’m gonna revisit that five level for loop. Basically, I’m not done yet so stay tuned!
Conclusion
So far this project has been really thought provoking. I was really stumped by two pairs and deciding the highest pair. I had no idea that I could spend this much time figuring out the basic rules of poker. I decided that I would not look at the internet for this project, so coming up with solutions on my own has been really exciting. I highly recommend that you try limiting yourself in the same way on your projects. You can find all the code on my Github!
Leave a comment