poker-pattern-matching

### Description The most difficult part of making a poker game is the pattern-matching algorithm. Out of the combination of the total 7 cards (5 from flop, 2 from hand), how do you extract the highest scoring pattern?

Usage no npm install needed!

<script type="module">
  import pokerPatternMatching from 'https://cdn.skypack.dev/poker-pattern-matching';
</script>

README

Poker Matching Algoritm

Description

The most difficult part of making a poker game is the pattern-matching algorithm. Out of the combination of the total 7 cards (5 from flop, 2 from hand), how do you extract the highest scoring pattern?

This repository does two things:

  1. Finds the highest scoring pattern obtained from 7 cards.
  2. Compares two hands to account for same pattern and ties.

Poker Hands

These are the patterns accounted for:

  1. Royal Flush
  2. Straight Flush
  3. Straight Flush (Low Ace)
  4. Four of a Kind
  5. Full House
  6. Flush
  7. Straight
  8. Straight (Low Ace)
  9. Three of A Kind
  10. Two Pair
  11. One Pair
  12. High Card

mapBestPokerHand.js

Map best poker hand is the main function that is used to get the best 5 hand from the 7 hand. I explain the logic as below (it is sligtly convulated):

If you have competing pairs, straight, three, four-of-a-kind, fullhouse:

  1. Find the highest number in the pair.
  2. If the highest number in the pair is the same, find the highest number outside of the pattern.

If you have competing full houses:

  1. Find the highest three (threeHigh)
  2. If the highest three is the same, find the highest pair (twoHigh).

If you have competing two pairs (NOT DONE YET):

  1. Find the highest number in the pattern. It does not matter which pair you are looking at (max.inArray).
  2. If the highest number is the same, look at the lowest pair and find the highest number in the lowest pair.

If you have no pattern:

  1. Find the highest number in the pattern.
  2. If the highest number is the same, then "tie"

Below is the format output of the best poker hand:

let hand=[
  { suit: 'D', number: 9 },
  { suit: 'C', number: 9 },
  { suit: 'H', number: 11 },
  { suit: 'S', number: 11 },
  { suit: 'D', number: 3 },
  { suit: 'H', number: 11 },
  { suit: 'D', number: 8 }
];
{
  hand: [
    { suit: 'D', number: 9 },
    { suit: 'C', number: 9 },
    { suit: 'H', number: 11 },
    { suit: 'S', number: 11 },
    { suit: 'H', number: 11 }
  ],
  suits: [ 'D', 'C', 'H', 'S', 'H' ],
  numbers: [ 9, 9, 11, 11, 11 ],
  suitsReady: [ 8, 2, 4, 1, 4 ],
  pattern: 'FullHouse',
  posList: { '9': [ 0, 1 ], '11': [ 2, 3, 4 ] },
  pos: [ 0, 1, 2, 3, 4 ],
  array: { inArray: [ 9, 9, 11, 11, 11 ], outArray: [] },
  arrayRefine: {
    '9': { inArray: [Array], outArray: [Array] },
    '11': { inArray: [Array], outArray: [Array] }
  },
  max: { inArray: 11, outArray: -Infinity },
  twoHigh: 9,
  threeHigh: 11
}

Installation and Running

node main.js
node test.js

Will be made into npm repository soon.

References

Senzee Poker Algorithm

Cactus Kev 7-hand Algo

Bit Manipulation Algorithm

Js Fiddle for Bit Manipulation Algorithm

Forum

Stack Exchange Question

Bitwise Operators

Find all k-combinations