When you have two poker hands, and both are straight flushes, this article will show you the code that compares them to find which one wins.
A previous article for poker hands demonstrated how to compare a hand to determine if it was straight flush (the article also covered a royal flush as well). A straight flush is a hand where all the cards are in sequential order, and they all have the same suit. This hand is a straight flush:
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 Fours Of Kinds
When two hands that are both a straight flush, the GetWinningHand
function will call the FindWinnerStraightFlush
function that compares the two hands.
Note: As it turns out, comparing two straight flush hands is the same as comparing two straight hands! The only addition is that you have to make sure all the cards are the same suit.
The only code featured in this article are the functions specific to comparing straight flushes. To see the concepts and code for comparing two straights, see this article.
FindWinnerStraightFlush
FindWinnerStraightFlush = function(hand1, hand2) {
SET workingHand1 = CardMatchUtils.SortCardsByDescendingValue(hand1);
SET hand1Info to an empty object { }
CardMatchUtils.AreCardsStraight(workingHand1, hand1Info)
SET workingHand1 = WildsAssumeValues(workingHand1, hand1Info)
SET sortedHand2 = CardMatchUtils.SortCardsByDescendingValue(hand2);
SET hand2Info to an empty object { }
CardMatchUtils.AreCardsStraight(workingHand2, hand2Info)
SET workingHand2 = WildsAssumeValues(workingHand2, hand2Info)
SET winningPlayerIndex = CardMatchUtils.CompareHands(workingHand1, workingHand2)
RETURN winningPlayerIndex
}
WildsAssumeValues
WildsAssumeValues = function(hand, info) {
SET index to 0
WHILE index IS LESS THAN number of cards in the hand (for example, "hand.length")
SET card = hand[index] (get the nth card in hand)
IF the card is a wild (card.value EQUALS CardValues.wild)
SET the card value to the last value in the info.cardValues array
REMOVE the last card value from the info.cardValues array (info.cardValues.pop())
END
SET index = index + 1
END
SET sortedHand to CardMatchUtils.SortCardsByDescendingValue(hand)
RETURN sortedHand
}
Compare Your Own Hands
You can use the controls below to build your own hands and compare fours of kinds. Have fun playing around with it!
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.