Continuing the series of comparing poker hands of the same rank, this article will show you how to write code to compare two flushes to see which ranks higher.
In a previous article for poker hands, you saw how to check a hand to determine if a it’s a flush. That is, all five of its cards are the same suit, and their values are not in sequential order.
Comparing two flush hands is a bit easier than the others we’ve covered so far. You need to perform these three steps:
- Temporarily replace all wilds with aces.
- Sort cards in hand by descending value.
- Compare cards in both hands.
And we’ve performed these tasks before, so let’s get started!
Setup
Note: The first stages of the setup can be found on the introduction page to comparing poker hands, here.
There aren’t any other specific preparations we need before we begin.
Writing The Code For Comparing Two Flushes
When both hands are flushes, the GetWinningPlayerId
function will eventually call a the FindWinnerFlush
function that compares them to determine which one wins.
Let’s have a look at that function.
FindWinnerFlush
FindWinnerFlush = function(hand1, hand2) {
SET workingHand1 = CopyHand(hand1)
ReplaceWildsWithAces(workingHand1)
SET workingHand1 = CardMatchUtils.SortCardsByDescendingValue(workingHand1)
SET workingHand2 = CopyHand(hand2)
ReplaceWildsWithAces(workingHand2)
SET workingHand2 = CardMatchUtils.SortCardsByDescendingValue(workingHand2)
SET winningPlayerIndex = CardMatchUtils.CompareHands(workingHand1, workingHand2)
RETURN winningPlayerIndex
}
Let’s go over the three steps in more detail.
STEP 1: Temporarily Replace All Wilds With Aces
First, you want to replace any wild cards in the hand with aces. Although a Flush is a hand where all cards are the same suit, if we’re comparing two flushes, the cards’ values now play a factor. So wilds can be replaced with aces to give the highest possible hand value. Also remember, wilds can assume any suit as well as any value, so the condition of the flush is still met.
Say, you had this hand:
That one wild card would be replaced with an ace:
Note: Wilds, as covered in this series, and for the sake of keeping the pseudo code relatively simple when wilds are involved, have no restrictions on the actual card suits and values they can replace. As you can see in the substitution in the above image, it contains two ad cards. If you’d like more official rulings on wilds, please have a look at this Wiki.
Since the card values are being altered, we don’t want to modify the original hands (hand1
and hand2
from above). So, first, you need to make a copy of the hand. You’ll then do all your work and comparing on this “working hand”.
CopyHand
CopyHand = function(hand) {
SET copyHand = new Array()
FOR EACH card in hand
(create a copy of the card)
SET copyCard = new Card(card.value, card.suit)
(add the copied card to the copy hand)
copyHand.push(copyCard)
END
RETURN copyHand
}
Now that you have a copy of all cards in the hand, let’s have a look at the psuedo code that replaces wilds with aces.
ReplaceWildsWithAces
ReplaceWildsWithAces = function(hand) {
FOR EACH card in hand
IF (card.value EQUALS CardValues.wild)
card.value = CardValues.ace
END
END
}
STEP 2: Sort Cards In Hand By Descending Value
Next, after all the wilds have been replaced with aces, sort the cards in the working hand in order of descending value (highest to lowest). We turn to our hero, CardMatchUtils.SortCardsByDescendingValue
! We’ve used this guy many times now… where would we be without him? I don’t know about you, but I don’t wanna find out! 😆
Step 3: Compare Cards In Both Hands
The final step is comparing the cards in both hands.
Once again, we turn to a highly used function, CardMatchUtils.CompareHands
. This function will compare two hands, both are assumed to already be sorted by descending card value. It accepts two parameters, hand1
and hand2
. If hand1
has the higher hand, the function returns 0
. If hand2
is higher, 1
is returned. Otherwise, -1
is return if both hands are equal.
So compared to other hand-comparing topics, comparing two flush hands is relatively easy. We’ve already written most of the functions used comparing flushes.
Example
Let’s have one example, using the two hands featured at the top of this article. Which one wins? Let’s find out! âš”
First, let replace all wilds with aces. Hand 1 has one wild in it, so it now looks like:
Hand 2 has no wilds, so we’re done with step 1.
Next, we sort the cards in each hand by value in descending order. The hands now look like:
Finally, compare the cards, starting with the first card in each hand. Both hands have an ace, so move on to the second card.
Hand 1 has a jack, while hand 2 hand a queen. The queen outranks the jack, so hand 2 is the winning flush hand. 🙂
That’s it for comparing flushes! Below is a set of controls you can play with to creating and compare your own flush hands.
Compare Your Own Hands
Player 1
Player 2
Player 3
If you have any questions about anything in this article, please let me know at cartrell@gameplaycoder.com.
Thanks and take care,
– C. out.