0

I am using the this hand evaluator found here for nodejs:

https://github.com/chenosaurus/poker-evaluator

It works great and it's really fast at evaluating hands. However, I want to use it to write a script to caluclate the % chance of winning a hand against an opponent.

For example, two players have two hands: [Ac, 8c] Vs [9d,9h] etc.

I've tried doing a loop through all the possible board cards like:

var cards = [
    "2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc","Ac",
    "2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd","Ad",
    "2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh","Ah",
    "2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks","As",
];

var hand = ["As", "Ks"];
var trials = 0;
for(i=0;i<cards.length;i++) {
    for(j=i+1;j<cards.length;j++) {
        for(k=j+1;k<cards.length;k++) {
            for(l=k+1;l<cards.length;l++) {
                for(m=l+1;m<cards.length;m++) {             
                    trials++;
                    var nh = hand.slice();
                    nh.push(cards[i]);
                    nh.push(cards[j]);
                    nh.push(cards[k]);
                    nh.push(cards[l]);
                    nh.push(cards[m]);

                    PokerEvaluator.evalHand(nh);
                }
            }
        }
    }
}

Obviously this isn't finished (it doesn't check the rank of the hand or compare it), but this is the only way I know to evaluate all possible board combinations for the two hands.

It's very slow, despite the algorithm being able to evaluate millions of hands per second. Is there a better way to check two hands against each other, instead of using for loops on all possible evaluations?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Patchesoft
  • 317
  • 6
  • 21
  • That part of your code is not the slow part, unless you are looping this code multiple times, in which case you probably have a bad code design. – Adam Buchanan Smith Oct 07 '15 at 22:40
  • What is the point of nesting 5 loops? Why not just eval both hands and compare the results? – Malk Oct 07 '15 at 22:44
  • @Malk because not all 7 cards are known. For example, [Ac, 8c] Vs [9d,9h], we now need the 5 board cards, so we have to loop through all possible values to get our 7 cards. – Patchesoft Oct 07 '15 at 22:47
  • Word, NP. If you figure out how to do it without brute force you win the internet. One thing, use a variable in your for loops instead of `cards.length`. It's a small array but one less calculation per iteration helps. – Malk Oct 07 '15 at 23:04
  • Also, it might help to pull dealt cards out of the array before looping. – Malk Oct 07 '15 at 23:07
  • Take a look at my onejoker library on Github. It has an iterator class for precisely this purpose...to step through every combination of N cards out of M, and its evaluator is almost as fast (in fact it's faster for five cards...but not for seven). – Lee Daniel Crocker Oct 08 '15 at 19:48

1 Answers1

2

The easiest way to do this is by monte carlo. Basically, you just randomly generate the rest of the hand a few thousand times, then count up the number of wins and divide by the total hands generated. The more hands you play, the more accurate your result.

Kody Puebla
  • 227
  • 2
  • 10