const { Card, StandardDeck } = require("fh-cards"); // Require Deck of Cards
const { Hand } = require('pokersolver'); // Solves a Poker Hand
// Function to add to hands together (2 cards + 5 cards)
// Transforms them to string representations (for poker solver)
function joinCards(hand1, hand2) {
return hand1.concat(hand2).map((card) => {
return card.toString()
});
}
Here we imported the modules that we needed.
const deck = new StandardDeck(); // The Deck that we will be using, we can reshuffle and reset
const ITERATIONS = 100
const PLAYERS = 2;
let combinations = {}; // Initalize to empty hash set
// this will have the structure of { '4s2c': { wins: Number, total: Number} }
for(let i = 0; i < ITERATIONS; i++ ) {
deck.restart(); // Reset the Deck
deck.shuffle(); // Shuffle the Deck
const playerHands = []
let playerOneHand = [] // string representation of player 1's 2 hole cards
const communityCards = deck.draws(5);
for (let j = 0; j < PLAYERS; j++) {
const playerHand = joinCards(deck.draws(2), communityCards); // string representation
const playerHandSolved = Hand.solve(playerHand); // Object representation (scored)
playerHands.push(playerHandSolved)
if (j === 0) {
playerOneHand = [playerHand[0],playerHand[1]];
}
// console.log(playerHand);
}
// console.log(playerHandsHash)
const winningHand = Hand.winners(playerHands)
const wonGame = winningHand[0] === playerHands[0]; // Did first player win?
// update first players wins or losses
updateHash(playerOneHand, wonGame);
// console.log(playerHands);
// console.log(winningHand, wonGame)
}
function updateHash(playerOneHand, wonGame) {
playerOneHand.sort() // removes permuatations
const card = playerOneHand[0] + playerOneHand[1]; // Add the strings together to make a hash
// we have a hash record
if (combinations[card]) {
combinations[card].wins += wonGame ? 1: 0;
combinations[card].total += 1;
} else {
// create hash recrod
combinations[card] = {
wins: wonGame ? 1: 0,
total: 1
}
}
}
console.log(combinations)