How to Code Poker Hands – Comparing Hands For A High Card

Welcome!

The last article introduced you to writing code to compare two poker hands. You learned that when comparing poker hands of different ranks, the one with the higher rank wins (a full house beats a three of a kind). But what happens when you have poker hands that are the same rank? 🤔

In this article you’ll learn how to compare the lowest ranking poker hand, a high card.

A high card hand doesn’t fall into any of the other categories, such as a pair, two pair, three of a kind, straight, etc. When determining the type of hand, if it doesn’t match any of the poker hands, it’s automatically a high card hand.

When you compare high card hands, the one with the highest ranking card wins. If they have the same highest ranking card, then you compare the next highest-ranking card and so on.

Before comparing hands, sorting the cards in order of descending rank first makes the comparisons much easier. But instead of putting wild cards at the end, they’ll go at the beginning. We’ll cover this in more details later in this article.

Let’s have a look at these two hands:

As you can see, the first four cards both hands are of the same value. We get to the final card, and the second hand’s three beats the first hand’s two. So, the second high card hand beats the first one.

Setup

Note: The first stages of the setup can be found on the introduction page to comparing poker hands here.

In many of the hand comparisons, wild cards can be treated as the highest card value, because they can assume any value. So, it will assume the highest card value possible while still satisfying the poker hand (this would’ve already been determined when matching the poker hand). You may recall when matching poker hands, we sorted the cards in descending order, with the wild cards as the lowest value, ordering them at the end of the sorted hand. However, when comparing poker hands, the wild cards will be the highest value, ordering them at the beginning of the sorted hand.

From there, you’ll need a new, albeit simpler sort function. And because this sort will be used by multiple poker hand comparisons, you can put it in your CardMatchUtils class as a function:

SortCardsByDescendingValueHighWilds
CardMatchUtils.SortCardsByDescendingValueHighWilds = function(hand) {
  var sortedHand = hand.concat();
  sortedHand.sort(CardMatchUtils.OnSortCardsByDescendingValueHighWilds);
}
OnSortCardsByDescendingValueHighWilds
function CardMatchUtils.OnSortCardsByDescendingValueHighWilds(card1, card2) {
  if (card1.value < card2.value) {
    return(1);
  } else if (card1.value > card2.value) {
    return(-1);
  }
  
  return(0);
}

In the main function, SortCardsByDescendingValueHighWilds, a copy of the hand parameter, which is an array of Card objects.

The sort function is called on the array, along with a callback function, OnSortCardsByDescendingValueHighWilds, that specifies how the cards should be ordered. When this function returns a number greater than zero, card2 should be placed before card1 (card2 has a higher value). If the function returns a number less than zero, card1 should come first. And if the function returns zero, then both cards have equal value, and their sort order against each other doesn’t matter.

Writing The Code For Comparing High Card Hands

Since this article focuses on the high card hand only, it assumes that from the GetWinningHand function, the poker hands are the same rank, which happens to be the high card.

The GetWinningHand function will eventually call FindWinnerHighCard to compare two high card hands. FindWinnerHighCard is the main topic of discussion for this article. It accepts two parameters, the hand for both players, and it’ll return the ID of the player who has the winning high card hand. If both hands are equal, it’ll return 0 (which we’ll use to mean a draw). So let’s dive in with some pseudocode. 🙂

FindWinnerHighCard
FindWinnerHighCard = function(hand1, hand2) {
  sortedHand1 = CardMatchUtils.SortCardsByDescendingValueHighWilds(hand1);
  sortedHand2 = CardMatchUtils.SortCardsByDescendingValueHighWilds(hand2);
  
  FOR (index = 0; index < sortedHand1.length; index++) {
    card1 = sortedHand1[index];
    card2 = sortedHand2[index];
    
    IF (card1.value > card2.value)
      RETURN PlayerId1;
    ELSE IF (card1.value < card2.value)
      RETURN PlayerId2;
    END
  }
  
  RETURN 0;
}

This function first sorts the cards in descending card value, with wild cards ordered first. Next, it compares the first card of each hand to determine the winner. If both cards are the same, it compares the second card, and so on. It does this until it finds a winning card, or all cards in both hands have been compared. If the latter is the case, then both cards are of equal rank, and the function returns 0.

That’s pretty much it for comparing two hands for the highest card. ✔

Compare Your Own Hands

You can use the controls below to build your own hands and compare high cards. Have some fun playing around with it, and if you find an issue, please let me know!


Player 1

Result:

Player 2

Result:

Player 3

Result:

Who Won:

If you have any questions about recognizing a royal flush, or the above interaction, please let me know at cartrell@gameplaycoder.com.

That’s it for now! Thanks, and the next article in the series will cover comparing hands that have a pair of jacks or higher.

– C. out.

One Reply to “How to Code Poker Hands – Comparing Hands For A High Card”

  1. Probably the easiest hand to assemble but if you don’t know your game the most difficult to read since it’s literally anything that doesn’t fit into the other hands. Hopefully you’re playing with some experienced players to offset your experience if you’re new!

Comments are closed.