Skip to content

Instantly share code, notes, and snippets.

@IliasDeros
Last active May 1, 2025 16:54
Show Gist options
  • Save IliasDeros/58967d2005c26ebe428ab83b13cfc864 to your computer and use it in GitHub Desktop.
Save IliasDeros/58967d2005c26ebe428ab83b13cfc864 to your computer and use it in GitHub Desktop.
Calculate Poker side pots using this NodeJS script written in javascript ES6.
#!/usr/bin/env node
let players = process.argv.slice(2)
if (players.length < 2) return console.log('There has to be at least two players to calculate side-pots.')
players = players
// give each player a name
.map((p, i) => ({name: `Player ${i + 1}`, bet: p}))
// sort from lowest to highest
.sort((a, b) => a.bet - b.bet)
// distribute side pots
const sidePots = []
players.forEach(({name, bet}, currentPlayerIndex) => {
// no side pot for the first player that goes all-in
if (currentPlayerIndex === 0) return
// the last player to go all-in doesn't create a side pot
for (let i = currentPlayerIndex - 1;i > 1;i--){
bet -= players[i].bet - players[i - 1].bet
}
// everyone bets in the main pot
bet -= players[0].bet
// add current player to all existing sidepots
sidePots.forEach(({players}) => players.push(name))
// the side pot is equal to the amount of players in the pot * the minimum bet
let sidePot = {pot: (players.length - currentPlayerIndex) * bet, players: [name]}
// do not push empty sidepot
sidePot.pot && sidePots.push(sidePot)
})
// if a player is alone in a side pot he can take back his money right away
let potOver
const lastSidePot = sidePots.slice(-1)[0]
if (lastSidePot.players && lastSidePot.players.length === 1) potOver = sidePots.pop()
// OUTPUT
potOver && console.log(`${potOver.players[0]} takes back ${potOver.pot}$`)
console.log(`Main pot: ${players.length * players[0].bet}$ - All players`)
sidePots.forEach(({pot, players}, i) => {
console.log(`Side pot ${i + 1}: ${pot}$ - ${players.sort()}`)
})
@pdesign
Copy link

pdesign commented May 1, 2025

what happens if someone has raised and first player that has bet folded... you can not calculate pot amount simply just by multiplying first bet with player count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment