Coding A Poker Odds Calculator

Hey guys,

If you’re an avid poker player, or even if you’re just starting out, I invite you to check out the poker odds calculator for Texas Hold ’em. With this tool, you can calculate your odds for winning any game. I found a very polished calculator that is available here.

Basic operation of a calculator is adding the cards to your hand and community. It then determines the odds your hand has of winning.

In this article, I’ll cover how you can code a simple poker odds calculator. First, let’s go over how the calculator stores the cards in your hand and the community.

Setup Card Data

A card is the basic data block we’ll be using. We’ll create a simple Card object, that will store the suit and value of each card. You can write it as:

Card = function(value, suit) {
  this.value = value;
  this.suit = suit;
};

A player’s hand is stored as an array of Card objects. The array has two elements, one for each card. You’d setup that up like this:

Note: If you’d like to see, in detail, the setup for card values, suits, Card objects, and hands, please see this link to an earlier article.

Setup Player and Community Data

The player’s hand and community cards are both defined as arrays, and will contain several Card objects:

var playerHand = [];
var communityCards = [];

Adding Player And Community Cards To Be Played

To add a card to the player’s hand, construct a new Card object, set its value and suit, then add it to the playerHand array. Say you want to add the 4♣. You’d write:

var card = new Card(CardValues.four, CardSuits.clubs);
var playerHand = allPlayerHands[0];
playerHand.push(card);

Adding a community card is similar. To add a Q♦:

var card = new Card(CardValues.queen, CardSuits.diamonds);
communityCards.push(card);

Evaluating Odds

Now that you have set up the players and cards, you can begin calculating odds.

First, create the scenario you want to evaluate. In the following example, we’ll use four community cards from the flop and turn. Let’s say your hand is 4♣ 9♣, and the community cards are 7♦, 5♣ , K♥, 8♣. You’d code this as:

playerHand.push(new Card(CardValues.four, CardSuits.clubs));
playerHand.push(new Card(CardValues.nine, CardSuits.clubs));
communityCards.push(new Card(CardValues.seven, CardSuits.diamonds));
communityCards.push(new Card(CardValues.five, CardSuits.clubs));
communityCards.push(new Card(CardValues.king, CardSuits.hearts));
communityCards.push(new Card(CardValues.eight, CardSuits.clubs));

Then, determine the best possible hand for the player using the cards in the player’s hand, and the community cards. You do this by finding how many “outs” you have. An out is an unknown card (in this case, the final river card) that, when added, will give you the best hand.

Next, determine which potential river cards would give you a strong or winning hand (a flush or straight): any 6 (four cards), or 2♣, 3♣, 7♣, 10♣, J♣, Q♣, K♣, and A♣. So, there are a total of 12 outs.

Note: Writing code to determine the outs themselves, or compare odds for multiple players both go beyond the scope of this article. I may cover them in future articles.

When matching and comparing poker hands, you may find it easier to use a “temporary hand” (a new array) when evaluating numerous combinations of the player’s cards with the community cards. For example, consider this:

var workingHand = [
  playerHand[1],
  communityCards[0],
  communityCards[1],
  communityCards[2],
  communityCards[3]
];

The above code gives you a working hand of 9♣, 7♦, 5♣, K♥, 8♣. You could then perform the various card matching actions on this array, using functions from previous articles, such as sorting ( CardMatchUtils.SortCardsByDescendingValue ), or determining if a hand is a straight ( CardMatchUtils.AreCardsStraight ).

To calculate the percentage you have of getting one of these out cards, you need to factor in the remaining number of cards in the deck, which is 46. A breakdown is:

const MAX_NUM_CARDS_IN_DECK = 52;
var numCardsInPlayerHand = playerHand.length; //2
var numCommunityCards = communityCards.length; //4
var numberOfCardsRemaining = MAX_NUM_CARDS_IN_DECK - numCardsInPlayerHand - numCommunityCards; //52 - 4 - 2 = 46

Of those 46 cards, 12 of them (the outs we found earlier) will give you a strong or winning hand. You can find the percentage of getting one of them with this:

var percentage = 1 - (numberOfCardsRemaining - numOuts) / numberOfCardsRemaining;

In our example, this is 1 – (46 – 12) / 46 = 0.26, or a 26% chance.

If you want to go further and represent this in odds notation (“x to y”), do this calculation:

var odds = 1 / percentage - 1;

Which yields, 1 / 0.26 – 1 = 2.8, or “2.8 : 1”. This means for about every 3 games, you might get a flush or straight.

That’s it for this article! I’ll talk to you next time. Thanks and take care 🙂

– C. out.