function dealRiver(deck) {
deck.draw(); // Burn card
return deck.draw(); // River
}
//1. Check: If there is no bet in front of them, a player can pass their action to the next player without betting any chips.
//2. Bet: Players can choose to wager chips by placing them in the middle of the table in front of them.
//3. Raise: If a player bets before them, a player can raise that bet by betting more than the previous player.
//4. Call: A player matches the same amount of chips as a previous bettor to stay in the hand.
//5. Fold: Players can forfeit their hand and cards by discarding them face-down on top of each other.
//6. All-in: If a player does not have enough chips to match the current bet but still wants to play, they can go all-in and bet all their remaining chips.
var stats={}
for(var i=0;i<10000;i++)
{
var deck1 = new Deck();
var numPlayers = 6;
var playerHands = dealCards(deck1, numPlayers);
//console.log(playerHands);
//betting
randomFold(playerHands)
var flop = dealFlop(deck1);
//console.log(flop);
//betting
randomFold(playerHands)
var turn = dealTurn(deck1);
//console.log(turn);
//betting
randomFold(playerHands)
var river = dealRiver(deck1);
//console.log(river);
//betting
randomFold(playerHands)
var winners = determineWinners(playerHands, flop.concat(turn, river));
if(winners.length==0){console.log(JSON.stringify({playerHands:playerHands,flop_turn_river:flop.concat(turn, river)}));throw new Error();}
winners.forEach((winner) => {
var playerId = "PLAYER"+winner;
if (stats[playerId]==null)stats[playerId]=0;
stats[playerId]++;
if (stats.TOTALWINS==null)stats.TOTALWINS=0;
stats.TOTALWINS++;
});
///////////////////////////////////////////////////////////////////////////////////////////////////
var stats={}
for(var i=0;i<10000;i++)
{
var deck1 = new Deck();
var numPlayers = 2;
var playerHands = dealCards(deck1, numPlayers);
//console.log(playerHands);
//betting
randomFold(playerHands)
var flop = dealFlop(deck1);
//console.log(flop);
//betting
randomFold(playerHands)
var turn = dealTurn(deck1);
//console.log(turn);
//betting
randomFold(playerHands)
var river = dealRiver(deck1);
//console.log(river);
//betting
randomFold(playerHands)
var winners = determineWinners(playerHands, flop.concat(turn, river));
//console.log(winners);
winners.forEach((winner) => {
var playerId = "PLAYER"+winner;
if (stats[playerId]==null)stats[playerId]=0;
stats[playerId]++;
if (stats.TOTALWINS==null)stats.TOTALWINS=0;
stats.TOTALWINS++;
});
// Output player rows
for (let i = 0; i < players.length; i++) {
html += "";
html += <td>${players[i]}</td>;
html += <td>${stats[players[i]]}</td>;
html += "";
}
// Output other values column
//html += "Other Values:";
for (let i = 0; i < otherKeys.length; i++) {
html += "";
html += <td>${otherKeys[i]}</td>;
html += <td>${stats[otherKeys[i]]}</td>;
html+=""
}
html+= ""
return(html)
}
//REVIEW CODE TO FIND OUT WHY FOLLOWING GIVES 0 WINNERS:{"playerHands":[[{"suit":"♣","rank":"10"},{"suit":"♠","rank":"4"}],[{"suit":"♣","rank":"A"},{"suit":"♠","rank":"8"}],[{"suit":"♦","rank":"5"},{"suit":"♥","rank":"K"}],[{"suit":"♣","rank":"7"},{"suit":"♠","rank":"J"}],[{"suit":"♦","rank":"7"},{"suit":"♣","rank":"4"}],[{"suit":"♥","rank":"A"},{"suit":"♦","rank":"8"}]],"flop_turn_river":[{"suit":"♣","rank":"2"},{"suit":"♣","rank":"J"},{"suit":"♣","rank":"Q"},{"suit":"♣","rank":"5"},{"suit":"♥","rank":"3"}]}
After reviewing the code, I discovered that the function determineWinners() is incorrectly calculating winners for hands that have the same rank but different tiebreakers. This happens because the function compareTiebreakers() compares two tiebreakers by checking if they have the same elements at the same positions, but doesn't take into account the fact that one tiebreaker could actually beat the other even if they differ (Example: [K, 10] should beat [Q, A], but using the compareTiebreakers() function, it would return false because K and Q are different, and 10 and A are different as well).
A possible solution is to modify the compareTiebreakers() function to fix this error, and adjust the logic inside determineWinners() accordingly.
First, fix the compareTiebreakers() function to use the correct logic for comparing tiebreakers:
function compareTiebreakers(a1, a2) {
const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
for (let i = 0; i < a1.length; i++) {
const index1 = ranks.indexOf(a1[i]);
const index2 = ranks.indexOf(a2[i]);
if (index1 > index2) {
return 1;
} else if (index1 < index2) {
return -1;
}
}
return 0;
}
This function will now return 1 if a1 has a higher tiebreaker, -1 if a2 has a higher tiebreaker, and 0 if both tiebreakers are equal.
Next, adjust the logic inside determineWinners() to use the new compareTiebreakers() function:
function determineWinners(playerHands, sharedCards) {
const activePlayers = playerHands.filter(hand => hand.length > 0);
const playerEvaluations = activePlayers.map((hand) => evaluateHand(hand.concat(sharedCards)));
let maxRank = null;
const winners = [];
let maxTiebreaker = null;
for (const { rank, tiebreaker } of playerEvaluations) {
if (maxRank === null || rank > maxRank) {
maxRank = rank;
maxTiebreaker = tiebreaker;
} else if (rank === maxRank) {
const comparison = compareTiebreakers(tiebreaker, maxTiebreaker);
if (comparison === 1) {
maxTiebreaker = tiebreaker;
}
}
}
for (let i = 0; i < activePlayers.length; i++) {
const { rank, tiebreaker } = playerEvaluations[i];
if (rank !== maxRank) {
continue;
}
if (compareTiebreakers(tiebreaker, maxTiebreaker) === 0) {
winners.push(playerHands.indexOf(activePlayers[i]));
}
}
return winners;
}
After this modifications, the determineWinners() function will now correctly calculate the winners of a round, even with cases like the one mentioned that was giving 0 winners.