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.

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

  1. One of the easiest hands PROVIDED you’re using wild cards. Seems a little less than easy otherwise.

    Also why no toilet pun?

    1. All the hands are much easier with wild cards, since a wild can assume any suite or value. But coding wild cards creates a few additional challenges.

      There’s no toilet pun, because then I’d have to explain the difference between a regular toilet, a straight toilet (and possible a crooked toilet [and if I introduce a crooked toilet, there’d have to be a crooked flush]), and finally, a royal toilet.

      And don’t even get me started on the fact that, if I talked about crooked hands, I’d also have to talk about corrupt hands.

      – C. out.

Comments are closed.