Permalink
Browse files

clean up eval code & add a speedtest

  • Loading branch information...
1 parent 9ccf79d commit 61d6821fa9a2b1a52279b708dfc95981ba9d0888 @chenosaurus committed Sep 17, 2012
Showing with 75 additions and 11 deletions.
  1. +11 −11 lib/PokerEvaluator.js
  2. +64 −0 test/speedtest.js
View
@@ -74,7 +74,6 @@ var PokerEvaluator = function(handRankFile) {
};
PokerEvaluator.prototype.init = function(handRankFile) {
- console.log('trying to load ', handRankFile);
this.ranks = fs.readFileSync(handRankFile);
};
@@ -98,16 +97,13 @@ PokerEvaluator.prototype.evalHand = function(cards) {
};
PokerEvaluator.prototype.eval = function(cards) {
- var p = this.ranks.slice((53 + cards[0]) * 4, (53 + cards[0]) * 4 + 4).readUInt32LE(0);
- p = this.ranks.slice((p + cards[1]) * 4, (p + cards[1]) * 4 + 4).readUInt32LE(0);
- p = this.ranks.slice((p + cards[2]) * 4, (p + cards[2]) * 4 + 4).readUInt32LE(0);
- p = this.ranks.slice((p + cards[3]) * 4, (p + cards[3]) * 4 + 4).readUInt32LE(0);
- p = this.ranks.slice((p + cards[4]) * 4, (p + cards[4]) * 4 + 4).readUInt32LE(0);
- p = this.ranks.slice((p + cards[5]) * 4, (p + cards[5]) * 4 + 4).readUInt32LE(0);
- var res = this.ranks.slice((p + cards[6]) * 4, (p + cards[6]) * 4 + 4).readUInt32LE(0);
-
- var handType = res >> 12;
- var handRank = res & 0x00000fff;
+ var p = this.evalCard(53 + cards[0]);
+ p = this.evalCard(p + cards[1]);
+ p = this.evalCard(p + cards[2]);
+ p = this.evalCard(p + cards[3]);
+ p = this.evalCard(p + cards[4]);
+ p = this.evalCard(p + cards[5]);
+ var res = this.evalCard(p + cards[6]);
return {
handType: res >> 12,
@@ -116,4 +112,8 @@ PokerEvaluator.prototype.eval = function(cards) {
}
};
+PokerEvaluator.prototype.evalCard = function(card) {
+ return this.ranks.readUInt32LE(card * 4);
+};
+
module.exports = PokerEvaluator;
View
@@ -0,0 +1,64 @@
+var PokerEvaluator = require("../index");
+var evaluator = new PokerEvaluator("../HandRanks.dat");
+
+function enumerateAllHands() {
+ var u0, u1, u2, u3, u4, u5;
+ var c0, c1, c2, c3, c4, c5, c6;
+ var handTypeSum = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
+ var count = 0;
+
+ console.log("Enumerating and evaluating all 133,784,560 possible 7-card poker hands...\n");
+
+ var startTime = Date.now();
+
+ for (c0 = 1; c0 < 47; c0++) {
+ u0 = evaluator.evalCard(53+c0);
+ for (c1 = c0+1; c1 < 48; c1++) {
+ u1 = evaluator.evalCard(u0+c1);
+ for (c2 = c1+1; c2 < 49; c2++) {
+ u2 = evaluator.evalCard(u1+c2);
+ for (c3 = c2+1; c3 < 50; c3++) {
+ u3 = evaluator.evalCard(u2+c3);
+ for (c4 = c3+1; c4 < 51; c4++) {
+ u4 = evaluator.evalCard(u3+c4);
+ for (c5 = c4+1; c5 < 52; c5++) {
+ u5 = evaluator.evalCard(u4+c5);
+ for (c6 = c5+1; c6 < 53; c6++) {
+
+ handTypeSum[evaluator.evalCard(u5+c6) >> 12]++;
+ count++;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ var endTime = Date.now();
+
+ console.log("BAD: %d\n", handTypeSum[0]);
+ console.log("High Card: %d\n", handTypeSum[1]);
+ console.log("One Pair: %d\n", handTypeSum[2]);
+ console.log("Two Pair: %d\n", handTypeSum[3]);
+ console.log("Trips: %d\n", handTypeSum[4]);
+ console.log("Straight: %d\n", handTypeSum[5]);
+ console.log("Flush: %d\n", handTypeSum[6]);
+ console.log("Full House: %d\n", handTypeSum[7]);
+ console.log("Quads: %d\n", handTypeSum[8]);
+ console.log("Straight Flush: %d\n", handTypeSum[9]);
+
+ // Perform sanity checks.. make sure numbers are where they should be
+ var testCount = 0;
+ for (var index = 0; index < 10; index++)
+ testCount += handTypeSum[index];
+ if (testCount != count || count != 133784560 || handTypeSum[0] != 0)
+ {
+ console.log("\nERROR!");
+ return;
+ }
+
+ console.log("\nEnumerated " + count + " hands in " + (endTime - startTime) + " milliseconds!\n");
+}
+
+enumerateAllHands();

0 comments on commit 61d6821

Please sign in to comment.