implement cactus kev's poker hand evaluator

This commit is contained in:
Flummi 2023-07-11 18:39:38 +02:00
parent 45033a64a3
commit b2980e01b5
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
5 changed files with 51 additions and 5 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules node_modules
config.json config.json
data/HandRanks.dat

BIN
data/generate_table Executable file

Binary file not shown.

3
package-lock.json generated
View File

@ -7,6 +7,9 @@
"": { "": {
"name": "schmirc", "name": "schmirc",
"version": "1.0.0", "version": "1.0.0",
"cpu": [
"x64"
],
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@xpressit/winning-poker-hand-rank": "^0.1.6", "@xpressit/winning-poker-hand-rank": "^0.1.6",

View File

@ -2,9 +2,13 @@
"name": "schmirc", "name": "schmirc",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "index.mjs",
"cpu": [
"x64"
],
"scripts": { "scripts": {
"start": "node ./src/index.mjs" "start": "node ./src/index.mjs",
"build": "cd ./data && ./generate_table"
}, },
"author": "Flummi", "author": "Flummi",
"license": "ISC", "license": "ISC",

View File

@ -1,6 +1,44 @@
import fs from 'node:fs';
export default new class handranker { export default new class handranker {
#ranks;
constructor() { constructor() {
return this; this.#ranks = fs.readFileSync('./data/HandRanks.dat');
this.handtypes = [
"InvalidHand", "HighCard", "Pair",
"TwoPairs", "ThreeOfAKind", "Straight",
"Flush", "FullHouse", "FourOfAKind",
"StraightFlush"
];
this.cards = [''];
for(const cv of [..."23456789TJQKA"])
for(const cs of [..."CDHS"])
this.cards.push(cv + cs);
};
evalHand(cards) {
if(!this.#ranks)
throw new Error("HandRanks.dat not loaded, run 'npm run build' first!");
if(![7,6,5,3].includes(cards.length))
throw new Error("Hand must be 3, 5, 6, or 7 cards");
cards = cards.map(c => this.cards.indexOf(c));
let p = 53;
for(let i = 0; i < cards.length; i++)
p = this.#ranks.readUInt32LE((p + cards[i]) * 4);
if(cards.length == 5 || cards.length == 6)
p = this.#ranks.readUInt32LE(p * 4);
return {
handType: p >> 12,
handRank: p & 0x00000fff,
value: p,
handName: this.handtypes[p >> 12]
};
}; };
}; };