How to Code Poker Hands – Comparing Hands

Now that the poker hands have been covered, there is another set of functionality to cover. When two poker hands are compared, which one wins?

This series presented the hands in order of lowest rank to highest rank. So they are ranked like this:

  1. Pair (Jacks or higher)
  2. Two pair
  3. Three of a kind
  4. Straight
  5. Flush
  6. Full house
  7. Four of a kind
  8. Straight flush
  9. Royal flush

In earlier articles, you may have noticed in the “Build Your Own Hand” section that you could have a four of a kind, which is also a three of a kind, or even a pair. But the four of a kind was the result. This is because a four of a kind ranks higher than a three of a kind, which ranks higher than and a pair.

Or you may have formed a full house, which is also a pair, a three of a kind, and even a two pair. The full house beats the three of a kind, which beats the two pair, which beats the pair.

This is happening because even if a hand contains multiple matches, the highest ranking one will always be picked. The next set of articles of the poker hand-matching series cover writing game code to determine the highest ranking poker hand between two hands.

If I may digress for a moment, the new “Build Your Own Hand” will look something like this, so you can compare two hands to each other:

You could do something like this, by adding all the previous psuedocode function for matching poker hands, and running them on the player’s hand. You’d run them in order of highest to lowest rank, because you want to find the first poker hand that matches the player’s hand.

Have a look at this code:

GetPokerHand
GetPokerHand = function(hand) {
  IF RoyalFlush(hand)
    RETURN PokerHandIds.RoyalFlush
  ELSE IF StraightFlush(hand)
    RETURN PokerHandIds.StraightFlush
  ELSE IF FourOfAKind(hand)
    RETURN PokerHandIds.FourOfAKind
  ELSE IF FullHouse(hand)
    RETURN PokerHandIds.FullHouse
  ELSE IF Flush(hand)
    RETURN PokerHandIds.Flush
  ELSE IF Straight(hand)
    RETURN PokerHandIds.Straight
  ELSE IF ThreeOfAKind(hand)
    RETURN PokerHandIds.ThreeOfAKind
  ELSE IF TwoPair(hand)
    RETURN PokerHandIds.TwoPair
  ELSE IF JacksOrBetterPair(hand)
    RETURN PokerHandIds.JacksOrBetterPair
  END

  RETURN PokerHandIds.HighCard
}

The above code will return a value that tells you what type of poker hand the players hand is. But, look at these PokerHandIds.HandIsRoyalFlush, PokerHandIds.HandIsStraightFlush, and such pieces. What are those?

There needs to be a way to uniquely identify each poker hand, hence these “poker hand IDs”. You’ll assign each poker hand a unique ID. This brings us to the first part of the setup.

Setup
Poker Hand IDs

Let’s create a new data object, PokerHandIds, and similar to CardValues and CardSuits, we’ll assign it some properties.

PokerHandIds = {
  HighCard: 0,
  JacksOrBetterPair: 1,
  TwoPair: 2,
  ThreeOfAKind: 3,
  Straight: 4,
  Flush: 5,
  FullHouse: 6,
  FourOfAKind: 7,
  StraightFlush: 8,
  RoyalFlush: 9
};

Note: The HighCard means a hand that is not any poker hand. It’s of the lowest value, and the GetPokerHand function defaults to returning HighCard if the specified hand doesn’t match any of the poker hands.

Writing The Code To Compare Poker Hands

So you might be able to see where this is going. To compare two poker hands to find the higher-ranking one, you get the poker hand ID of both hands, then compare those IDs.

Say you have two players and their hands are stored in hand1 and hand2. The code for matching them might look something like this:

GetWinningHand
GetWinningHand = function(hand1, hand2) {
  hand1PokerId = GetPokerHand(hand1);
  hand2PokerId = GetPokerHand(hand2);

  IF (hand1PokerId > hand2PokerId)
    RETURN PlayerId1 ("player 1 wins")
  ELSE IF (hand2PokerId > hand1PokerId)
    RETURN PlayerId2 ("player 2 wins")
  END

  RETURN GetWinningPlayerId(hand1, hand2, hand1PokerId)
}

Note: The PlayerId1 and PlayerId2 are just used to uniquely identify which player we are referring to.

If one ID is bigger than the other, it’s pretty easy to determine which player wins. But what if both hands are the same rank, for instance, both are flushes, or both are two pairs? It’s not necessarily a “draw”, because you’ll need to do some additional checking on each hand to determine which flush, for example wins, or which two pair wins. And this type of comparison depends on the poker hand.

This new GetWinningPlayerId function accepts three parameters: both players’ hands, and the poker hand ID. (It doesn’t matter if you use either hand1PokerId or hand2PokerId, because they’re both the same.) And GetWinningPlayerId looks like:

GetWinningPlayerId
GetWinningPlayerId = function(hand1, hand2, pokerHandId) {
  IF pokerHandId EQUALS PokerHandIds.HighCard
    RETURN FindWinnerHighCard(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.JacksOrBetterPair
    RETURN FindWinnerJacksOrBetterPair(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.TwoPair
    RETURN FindWinnerTwoPair(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.ThreeOfAKind
    RETURN FindWinnerThreeOfAKind(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.Straight
    RETURN FindWinnerStraight(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.Flush
    RETURN FindWinnerFlush(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.FullHouse
    RETURN FindWinnerFullHouse(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.FourOfAKind
    RETURN FindWinnerFourOfAKind(hand1, hand2)
  ELSE IF pokerHandId EQUALS PokerHandIds.StraightFlush
    RETURN FindWinnerStraightFlush(hand1, hand2)
  END IF

  RETURN FindWinnerRoyalFlush(hand1, hand2)
}

Depending on which player’s hand won, GetWinningPlayerId would return either PlayerId1 or PlayerId2.

It is also possible that both player hands, could the same rank, in cards, as well as poker hand. In that case, the match up really is a draw. So GetWinningPlayerId would return neither PlayerId1 nor PlayerId2, but Draw.

And these FindWinner... functions is where all the real fun of finding which poker had wins is! 😉 And like matching poker hands, you’ll be learning those in the next set of articles. So stay tuned for more!

That’s it for now. Talk you next time, and take care,

– C. out.

2 Replies to “How to Code Poker Hands – Comparing Hands”

  1. With this information we now have the means to cheat in poker games. 😈

    1. Ha! You’ll still have to hijack the server-side code. 🔨

Comments are closed.