Algorithms – Understanding Poker Split and Side Pots

algorithms

I am writing a poker application and trying to figure out how to deal with split pots and side pots

There are four rounds of betting and each round of betting can have multiple orbits

Can only bet the chips in front of you
So if a player is all in a side pot is created

Hands can tie – in which case the pot is split
If it does not spit evenly the 'last aggressor (raise)' gets the odd chip(s)

If you are raised you must call or fold
If you fold you lose your bets even if the hand wins
You cannot just cap you betting and create a side pot
You only create a side pot if you are all in

PlayerA all in 40 Win
PlayerB 80 second
PlayerC 80 last

Player A would take 120 (win 80 as 40 was his money in)
Player B would take 80

But you don't know the win order until the end
And when PlayerA went in you don't know how many people are going to call

What could be good algorithm for this?

This code is C# but really just looking for an approach.

My initial thought is to just record all the betting for each player
street, orbit, bet

Where that gets messy is that in a single orbit you could have multiple players all in for different amounts (bet)

And a different amount is not just all in
Player could be raised and then fold
In that case you know they lose but you still don't know who they lose to

There are rarely more than 3 orbits in a round
You could assume there would never be more than 10 as with min raise players would run out of chip

Best Answer

Here is a working code to create and distribute winnings from side-pots.

class Player:
    def __init__(self, l, i, hs):
        self.live = l #False
        self.invested = i #0
        self.hand_strength = hs #100
        self.result = 0

def distribute(pot, players):
    for p in players:
        p.result= -p.invested # invested money is lost originally

    # while there are still players with money
    # we build a side-pot matching the lowest stack and distribute money to winners
    while len(players)>1 :
        min_stack = min([p.invested for p in players])
        pot += min_stack * len(players)
        for p in players:
            p.invested -= min_stack
        max_hand = max([p.hand_strength for p in players if p.live])
        winners = [p for p in players if p.hand_strength == max_hand if p.live]
        for p in winners:
            p.result += pot / len(winners)
        players = [p for p in players if p.invested > 0]
        pot = 0
    if len(players) == 1:
        p = players[0]
        # return uncalled bet
        p.result += p.invested

This example shows a case where there are 5 players and the starting pot is 100. The main pot is split between player who are allin for 20 and 80. One player has folded after investing 50 and his best hand is wasted. One player has overbet all others and his bet is returned to him while he loses only the amount matched by the other players (80)

players = [Player(True, 20, 100),  Player(False, 50, 200),  Player(True, 80, 90),  Player(True, 80, 100), Player(True, 1000, 0)]
distribute(100, players)
for p in players:
    print(p.result)
#80.0
#-50
#-80
#230.0
#-80    
Related Topic