implement cactus kev's poker hand evaluator
This commit is contained in:
@ -1,6 +1,44 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
export default new class handranker {
|
||||
#ranks;
|
||||
|
||||
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]
|
||||
};
|
||||
};
|
||||
|
||||
};
|
||||
|
Reference in New Issue
Block a user