Hey guys,
In the next several article of this multi-part series, we’ll go over how to code the various poker hands, like Straight, Flush, and Full House. This is the first article of the series, and we’ll be writing code that matches the hand for a Pair of cards that are least a jack or higher.
When developing card games with poker hands in them, writing the code that acknowledges these hands can be a challenge. At least, writing something that can be completed in one lifetime.
You could write out every single card combination for every single hand, but that’d take forever.
So, we’re going to write something that’s a bit more robust robust when it comes to recognizing the card patterns that form the various poker hands.
For some clarity, a poker hand we’ll be using consists of five cards, and cards of certain suits and values and combinations for a poker hand. The hands we’ll be matching are:
- Pair (jacks or higher only)
- Two pair
- Three of a kind
- Straight
- Flush
- Full house
- Four of a kind
- Straight flush (a non-ace-high straight flush)
- Royal flush (an ace-high straight flush)
The list also orders the hands from lowest to highest.
We’ll also be using wild cards. A wild card can assume any suit and/or value of a card to help create a poker hand. For example, in this hand:
We have a pair of queens, because the wild card could be substituted for another queen. Having wild cards does create a challenge, but the results are amazing. (:
Setup
Before starting, we need to define a few data structures. We’ll need to define the suits, values, a “card” which will hold the suit and value, and a “hand” which is an array of cards. All the algorithms will be mostly pseudocode based on JavaScript.
Card values
We can store the card values in an object like this in ascending rank like this:
CardValues = {
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
jack: 11,
queen: 12,
king: 13,
ace: 14,
wild: 100
};
Note: The values assigned to the card values are arbitrary, as long as the are unique, and the values are in ascending order, corresponding to the rank of the card. For example, ace ranks higher than king, which ranks higher than queen, and so on. Also, we’re including the value of the wild card here, because it’ll be useful later on.
Card suits
CardSuits = {
hearts: 1,
diamonds: 2,
clubs: 3,
spades: 4
};
Note: The values assigned to the card suits are arbitrary, as long as the are unique (order doesn’t matter). Also, we only need the wild value specified in CardValues
, and not CardSuits
. A wild card really doesn’t have a suit or value, but putting it in CardValues
makes things easier, We don’t have to check two different places for the value of the card (meaning within CardValue
, and some other place).
Card
An individual card will hold two pieces of data: the value and the suit. We can define it in a constructor function like this:
Card = function(value, suit) {
this.value = value;
this.suit = suit;
};
Hand
We also have a “hand”. It won’t be a custom data structure like the previous three, because it’s really just an array of Card
objects. A hand might look like this:
var hand = [
new Card(CardValues.five, CardSuits.diamonds),
new Card(CardValues.eight, CardSuits.hearts),
new Card(CardValues.king, CardSuits.spades),
new Card(CardValues.ace, CardSuits.hearts),
new Card(CardValues.three, CardSuits.clubs)
];
Wild cards can be defined like this:
var wildCard = new Card(CardValues.wild);
No suit is specified, and wildCard.suit
will be undefined
.
Note: We could get into defining a “deck”, which would also be a container of cards, and “deal” those to the player, by removing the “top” card from the deck, and adding it to the player’s hand. However, for the purposes of this how-to, we won’t get into that. Each scenario will assume that the player’s hand already has five cards.
Note: We’re also working off the assumption that there is only one wild card in the deck. So the total number of cards in the deck is 53.
Common Functionality
Because we’ll be using some of the same functionality to match several of the poker hands. So, we’ll have a “utilities” JavaScript file, and let’s called it CardMatchUtils.js
. We’ll put these functions here, but for now, our CardMatchUtils
file is empty.
Onto Matching Them Poker Hands!
Those are all the data structures we’ll need for this guide. Now, let’s get into matching those poker hands! 🃏
Pair (jacks or higher only)
A pair is any two cards with the same value, and the other three cards are of arbitrary different values. Consider this hand:
there is a pair of fives. But our purposes, while this is a pair, it would not qualify because it’s not jacks or higher.
So we want something like this:
Now we have a pair of kings which does satisfy that requirement. If a wild card was involved, we could have something like this and still have a pair of kings:
You may have noticed that the wild card can create a pair with any of the other four cards. However, we’re only interested in jacks or higher, so the king is the only card that qualifies. Our code will handle this situation as well.
Each matching function will accept one parameter, the hand (which is our array of cards).
While performing matches, it is very helpful if we first sort the cards by descending value, meaning highest value to lowest. This will help with all of our matching functions. So first, let’s add a SortCardsByDescendingValue
to our CardMatchUtils
file:
SortCardsByDescendingValue
CardMatchUtils.SortCardsByDescendingValue = function(hand) {
set sortedHand = create a copy of the hand array
sort the sortedHand array by descending card value
return sortedHand
};
When you’re sorting hands of cards, be sure that the wild cards are always sorted after the other cards. So, if you had a hand of:
the sorted result would be:
Another utility function that will useful through this matching series is the ability to count how many cards in the hand have a specific value, including the wild card value. We can make a function that looks something like this:
CountCardsByValue
CardMatchUtils.CountCardsByValue = function(hand, cardValue) {
set numCardsFound = 0
for each card in the hand {
if card.value equals cardValue then add one to numCardsFound
}
return numCardsFound
}
We send this function our hand and the card value we’re looking for, and it’ll return to us the number of cards found in the hand that have our value.
Now, we have enough to write the code for function that matches a jacks or better pair:
JacksOrBetterPair = function(hand) {
sort the hand by descending card value and store in a variable called sortedHand
count the number of wild cards in the sorted hand, and store in a variable called numWilds
set a variable called numberOfMatches to 0
for each non-wild card in the sorted hand {
if the current card in the loop is the first card in the hand being checked in this for loop then {
if the value of the current card card is lower than a jack then
return false (there are no cards of at least a jack in this hand, so therefore we don't need to bother checking any further)
otherwise {
set the current card as the base card (the values of other cards will be compared with this one to see if they match)
set numberOfMatches to 1
restart the loop with the next card as the new current card in this loop
}
}
if the value of the base card equals the value of the current card then {
add one onto numberOfMatches
if numberOfMatches equals 2 then
return true (this hand has a pair of jacks or higher!)
}
otherwise
restart the loop with the next card as the new current card in this loop
note: the value of base card does not match the value of the current card's value in the loop, so check if there are enough wild cards to qualify
if numWilds + numberOfMatches >= 2 then
return true (this hand has a pair of jacks or higher)
note: no match has been established, so set a new base card value if it's at least a jack
if the value of the current card is less than a jack then
restart the loop with the next card as the new current card in this loop
set the current card as the new base card
set numberOfMatches to 1
}
note: if we've reached here, then the loop went through all the non-wild cards and didn't find a pair of jacks or higher
return false
}
That’s something just for looking for a pair of jacks or higher, huh? (:
Finally, if you want to try this out, you’d create your hand
like we did earlier. Next you’d call your JacksOrBetterPair
function, passing in your hand. TheJacksOrBetterPair
will return true
if the specified hand contains a pair of jacks or higher, andfalse
if it doesn’t.
Also, as we add more functionality to our CardMatchUtils
file, the card matching will become somewhat easier, even for the higher ranking poker hands.
That’s it for now! The next article will focus on matching a Two Pair hand!
I’ve been coding heavily in card games lately. I recently released a free Android version of my Blackjack game, Thunderjack!. I’m also working on converting a poker game that I made for a client from Flash to HTML5 using Phaser, which I wrote about here.
Please feel free to ask me any questions you may have about this by replying in the comments below or contacting me here.
And if you’re looking for someone to code your next card game, contact me at cartrell@gameplaycoder.com, and let’s talk.
Thanks, and I’ll talk to you next time. Take care,
– C. out.
So I had a bit of an idea how the coding of different hands would go, but I was pretty wrong and what I thought it would be would be far too primitive compared to this. But it is pretty interesting to know how you make those different combinations work.