pokersolver

repository·master·Indexed 19 days ago

https://github.com/goldfire/pokersolver

A JavaScript poker hand solver and comparison tool compatible with Node.js and browser environments. It supports evaluating hands of 3 to 7 cards, calculating scores, and identifying hand names across various rule sets including standard, jacksbetter, joker, deuceswild, threecard, fourcard, and Pai Gow Poker. Features include the Hand class for solving and comparing hands, the Game class for rule configuration, and the PaiGowPokerHelper for managing high and low hand splits according to the House Way.

Tokens
3.5K
Snippets
13
Records
16
Agent score
16%

What's inside pokersolver

  1. Use pokersolver in Node.js or the Browser

    master

    Server Usage (Node.js)

    Import the Hand class using require:

    var Hand = require('pokersolver').Hand;

    Browser Usage

    Include the script via a <script> tag, then access the Hand object globally:

    <script src="/path/to/pokersolver.js"></script>
    <script>
      var hand = Hand.solve(['...']);
      ...
    </script>
  2. Handle Pai Gow Poker hands with PaiGowPokerHelper

    master

    The PaiGowPokerHelper class is designed to manage the complexities of Pai Gow Poker, specifically splitting a 7-card hand into a high hand (5 cards) and a low hand (2 cards) according to the 'House Way' (MGM Grand standard).

    Key Methods:

    • PaiGowPokerHelper.solve(fullHand): Takes an array of cards and automatically splits them into high and low hands using the House Way.
    • PaiGowPokerHelper.winners(player, banker): Compares two PaiGowPokerHelper instances. Returns 1 for Player win, -1 for Banker win, and 0 for a Push. Note that in Pai Gow, the Banker wins all ties.
    • PaiGowPokerHelper.setHands(hiHand, loHand): Manually sets the high and low hands if you have already split them.

    Mental Model:

    In Pai Gow, a player's hand is valid only if the high hand is stronger than the low hand. The qualifiesValid() method can be used to check this condition.

    // Automatically split a 7-card hand using House Way
    const helper = PaiGowPokerHelper.solve(['Ad', 'As', 'Ah', 'Ac', 'Kd', 'Ks', 'Kh']);
    
    console.log(helper.hiHand.name); // High hand ranking
    console.log(helper.loHand.name); // Low hand ranking
    
    // Compare against a banker
    const result = PaiGowPokerHelper.winners(playerHelper, bankerHelper);
  3. Solve a poker hand with Hand.solve()

    master

    Use Hand.solve(cards, game, canDisqualify) to evaluate a hand of 3 to 7 cards.

    Parameters:

    • cards (Array): An array of card strings. Note that 10 must be represented as T (e.g., 'Th' for Ten of hearts).
    • game (String, optional): The rule set to use. Defaults to 'standard'.
    • canDisqualify (Boolean, optional): Whether the hand is subject to qualification rules. Defaults to false.

    Example:

    var hand = Hand.solve(['Ad', 'As', 'Jc', 'Th', '2d', 'Qs', 'Qd']);
    console.log(hand.name); // Two Pair
    console.log(hand.descr); // Two Pair, A's & Q's
  4. Compare hands with Hand.winners()

    master

    Use Hand.winners(hands) to compare an array of solved hands and determine the winner(s). It can return multiple hands in the event of a tie.

    Parameters:

    • hands (Array): An array of hand objects previously returned by Hand.solve.

    Example:

    var hand1 = Hand.solve(['Ad', 'As', 'Jc', 'Th', '2d', '3c', 'Kd']);
    var hand2 = Hand.solve(['Ad', 'As', 'Jc', 'Th', '2d', 'Qs', 'Qd']);
    var winner = Hand.winners([hand1, hand2]); // hand2
  5. Use PaiGowPokerHelper for Pai Gow Poker

    master

    The PaiGowPokerHelper class is designed to handle the specific rules of Pai Gow Poker, including splitting hands into high and low components.

    Methods

    • solve(cards): Solves the provided cards and automatically sets the hands according to the 'House Way'.
    • setHands(hiHand, loHand): Manually sets the five-card high hand and two-card low hand.
    • winners(player, banker): Compares a player's hand and a banker's hand. Returns 1 for Player win, -1 for Banker win, or 0 for a Push.

    Properties

    • baseHand (Hand): The result of Hand.solve on all input cards.
    • hiHand (Hand): The five-card high hand.
    • loHand (Hand): The two-card low hand.

    Example:

    // Using solve to automatically determine hands via House Way
    var playerHand = PaiGowPokerHelper.solve(['Ad', '2d', '3d', '4d', 'Qc', 'Ks', '7h']);
    var bankerHand = PaiGowPokerHelper.solve(['As', '2s', '3s', '4s', 'Qh', 'Kh', '7d']);
    var result = PaiGowPokerHelper.winners(playerHand, bankerHand);
  6. Available poker game rule sets

    master

    When calling Hand.solve(cards, game), you can specify the following game rule sets:

    GameDescription
    standardTexas Hold'em, Seven Card Stud, Five Card Draw, etc.
    jacksbetterJacks or Better Video Poker. Use canDisqualify: true to check for a Pair of Jacks or better.
    jokerJoker Video Poker. Jokers are 'Or'. Qualification: Kings or better.
    deuceswildDeuces Wild Video Poker. Deuces are wild. Hands lower than Three of a Kind are High Card.
    threecardThree Card Poker. Qualification: Dealer must have Queen High or better.
    fourcardFour Card Poker. No qualifying hand.
    fourcardbonusFour Card Poker Aces Up Bonus. Qualification: Pair of Aces or better.
    paigowpokerhiPai Gow Poker High Hand. A2345 is the second highest straight. Joker ('Or') completes straights/flushes or counts as an Ace.
    paigowpokerloPai Gow Poker Low Hand. Joker ('Or') counts as an Ace.
    paigowpokerfullHELPER GAME: Used by PaiGowPokerHelper to create a hand to be split.
    paigowpokeraltHELPER GAME: Used by PaiGowPokerHelper for straight/flush alternatives.
    paigowpokersf6HELPER GAME: Used by PaiGowPokerHelper to check for 6-card straights/flushes.
    paigowpokersf7HELPER GAME: Used by PaiGowPokerHelper to check for 7-card straights/flushes.
  7. Access properties of a solved hand

    master

    When a hand is solved via Hand.solve(), the returned object contains the following properties:

    • cardPool (Array): All cards passed into the hand.
    • cards (Array): The specific cards involved in the identified hand type (maximum of 5).
    • descr (String): A detailed description (e.g., 'Two Pair, A's & Q's').
    • name (String): The name of the hand type (e.g., 'Two Pair').
    • rank (Number): The ranking of the hand type (0 is lowest; varies by game).

    You can also call .toString() on the solved hand to get a formatted string of the cards involved in the identified hand type.

  8. Compare two hands with Hand.compare()

    master

    The compare(a) method compares the current hand instance with another Hand instance a.

    Returns:

    • -1: If the current hand is higher rank than a (the current hand wins).
    • 1: If the current hand is lower rank than a (the current hand loses).
    • 0: If the hands are equal.

    If ranks are equal, it performs a tie-break by comparing the individual card ranks in descending order.

    const handA = Hand.solve(['Ad', 'Kd', 'Qd', 'Jd', '2d'], 'standard');
    const handB = Hand.solve(['2c', '3c', '4c', '5c', '6c'], 'standard');
    
    const result = handA.compare(handB);
    if (result === -1) {
      console.log("Hand A wins");
    }
  9. Find the winning hands with Hand.winners()

    master

    Use Hand.winners(hands) to determine which hands among a group are the winners. This method handles:

    1. Qualification: It filters out hands that do not meet the game's qualification requirements (if qualifiesHigh() is false).
    2. Highest Rank: It identifies the highest rank present among the remaining hands.
    3. Tie-breaking: It filters the highest-ranked hands to find those that do not lose to any other hand in the set (handling ties).

    Parameters:

    • hands (Array<Hand>): An array of Hand instances to compare.

    Returns:

    • An array of Hand instances that are the winners.
    const hand1 = Hand.solve(['Ad', 'Kd', 'Qd', 'Jd', '2d'], 'standard');
    const hand2 = Hand.solve(['As', 'Ks', 'Qs', 'Js', '2s'], 'standard');
    const hand3 = Hand.solve(['2c', '3c', '4c', '5c', '6c'], 'standard');
    
    const winners = Hand.winners([hand1, hand2, hand3]);
    // winners will contain both hand1 and hand2 if they tied for the highest rank
  10. Configure poker rules with the Game class

    master

    The Game class defines the ruleset for evaluation. You can instantiate it with a predefined rule string to set parameters like cardsInHand, handValues, wildValue, and noKickers.

    Supported rule identifiers:

    • 'standard': 5 cards, standard hand rankings.
    • 'jacksbetter': 5 cards, no kickers, specific lowest qualified hands.
    • 'joker': 5 cards, uses 'O' as a wild value.
    • 'deuceswild': 5 cards, uses '2' as a wild value.
    • 'threecard': 3 cards.
    • 'fourcard': 4 cards.
    • 'paigowpokerfull': 7 cards, used for Pai Gow Poker.
    • 'paigowpokerhi': 5 cards, used for the high hand in Pai Gow.
    • 'paigowpokerlo': 2 cards, used for the low hand in Pai Gow.
    const game = new Game('deuceswild');
    console.log(game.cardsInHand); // 5
    console.log(game.wildValue);   // '2'
  11. Determine winners with Hand.winners()

    master

    Use Hand.winners(hands) to compare multiple Hand objects and return an array of the winning hands. This is useful for determining the outcome of a showdown between players or between a player and a dealer.

    const winners = Hand.winners([playerHand, dealerHand]);
    if (winners.length === 1 && winners[0] === playerHand) {
      console.log('Player wins!');
    }