How to Code Poker Hands – Comparing Hands For A Flush

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.

Each card is diamonds, and they are not all 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:

  1. Temporarily replace all wilds with aces.
  2. Sort cards in hand by descending value.
  3. 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! ⚔

Hand 2
Hand 2

First, let replace all wilds with aces. Hand 1 has one wild in it, so it now looks like:

The wild assumes both value and suit, and becomes an ace of diamonds.

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:

Hand 1
Hand 2

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

Result:

Player 2

Result:

Player 3

Result:

Who Won:

If you have any questions about anything in this article, please let me know at cartrell@gameplaycoder.com.

Thanks and take care,

– C. out.

How to Code Poker Hands – Flush

Hey there,

In this article, we’re continuing the series of writing code that recognizes the various poker hands. Today, you’ll build the solution for a hand that matches a flush.

A flush is a hand where all five cards are of the same suit, but not in order of sequential rank.

If you’d like to see poker hands that have already been covered, please check the links below.

Setup

The code setup is the same as in the previous articles of this mini-series. A quick rundown of the setup is below, but if you’d like to skip ahead, check here. If you’d like to see the full details of the setup, check out this link.

Card Values
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
};
Card Suits
CardSuits = {
  hearts: 1,
  diamonds: 2
  clubs: 3,
  spades: 4
};
Card
Card = function(value, suit) {
  this.value = value;
  this.suit = suit;
};

And a hand is an array of Card objects:

var hand = [
  new Card([card value 1], [card suit 1]),
  new Card([card value 2], [card suit 2]),
  ...
  new Card([card value 5], [card suit 5])
];
Writing The Code To Identify A Flush

As mentioned before, a flush is a hand where all the cards have the same suit, but their ranks are not in sequential order. Now, programming the flush is one of the easier poker hands to match, because all you’re doing is checking that all the cards have the same suit, using the first card’s suit as the criteria.

But remember, we’re also using “wild cards” here. So far you’ve used them to assume any value (rank), but in the case of a flush, a wild card can also assume any suit.

You don’t necessarily need to sort the cards by descending rank, since the flush is not a value-based hand. However, doing so will programming the matching logic a little easier.

Like you did with the previous hand, the straight, you’ll be adding a function to the “utilities” file, CardMatchUtils.js. In an upcoming article, you’ll program the straight flush, which is a combination of the straight and the flush. So, you’ll add a new function, CardMatchUtils.AreCardsFlush, CardMatchUtils, which will be used by both hands.

There are two functions that CardMatchUtils will use:

  1. CardMatchUtils.SortCardsByDescendingValue – This function sorts the cards in the hand by descending value. This orders the cards from highest value to lowest.
  2. CardMatchUtils.AreCardsFlush – This function has not yet been written (but that’s what you’ll write in this article! 🙂 ) This function will be used in recognizing both, the flush, and the straight flush poker hands.

And without further ado, let’s get into some pseudocode for the flush! It looks like this:

Flush = FUNCTION(hand) {
  SORT the hand by descending card value and store in a variable called sortedHand

  IF AreCardsFlush(sortedHand)
    //we have a flush hand
    RETURN TRUE 

  //this hand is not a flush
  RETURN FALSE
}

This function looks very similar to the AreCardsStraight function from the last article. The bulk of the details is in the AreCardsFlush function. Let’s now have a look at its pseudocode:

AreCardsFlush
AreCardsFlush = FUNCTION(sortedHand) {
  FOR EACH card in the sortedHand {
    IF this card (the current loop card) is the first card being checked {
      set a variable called baseCardSuit to the suit of this card
      re-start FOR loop again with next card (in other words, "continue" if you're familiar with for-loops)
    }

    IF this card is a wild card {
      RETURN TRUE (since cards are already sorted, once we get to a wild card we're done; all preceding cards match the base card suit)
    }

    IF the suit of this card EQUALS the value of baseCardSuit {
      re-start FOR loop again with next card
    } ELSE {
      RETURN FALSE (suit of this card doesn't match the vase card suit - this hand is not a flush)
    }
  }

  RETURN TRUE (all cards have the same suit)
}

This code gets the suit of the first card in the sorted hand, and compares it to the suit of every other card. As long as they match, the loop will continue until it reaches the end, or encounters either a wild card (success), or a card whose suit doesn’t match that of the first card’s (failure).

Say you have a hand that looks like this (after sorting all cards in descending value order, with wild cards at the end):

The first card is the king of hearts, so the “base card suit” will be assigned to value CardSuits.diamonds.
The suit of the second card is spades, which does not match diamonds. So, this hand is not a flush.

Now say we had this hand:

The base card suit is the spade, from the jack of spades. The next two cards are also spades. So far so good. Then then fourth card is a wild. At this point, you can say that this hand is a flush. The cards are already sorted with wilds at the end, and because wilds can assume any suit (in this case, spades), so they will complete the flush. There is no need to continue checking the cards, because they will all be wilds. This is how sorting helps simplify recognizing a flush.

Not too much more to say about programming a flush, since it’s one of the easier hands to identify – even easier than a 🍐, in my opinion.

Build Your Own Hand

You can use the controls below to create your own hand to see if it is a flush or not. It will also recognize previous hands we’ve covered up to this point. Have fun playing around and making various hands!


Result:

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

Finally, if you’d like to hire me to program an actual card game for you, you can e-mail me, or you may use the contact form here.

That’s it for today. Thanks, and stay tuned because we more poker hands to program. The next one will be the full house!

– C. out.