I'm trying to run a poker simulation and have the following data about a poker table
- how much each player contributed to the pot
- a "hand score" (after flop) for each player (ie, if
player[0].score == player[1].score
, they tied)
I'm stuck calculating how much each player should win without needing to create sidepots and assigning players to each of them.
For example,
player[0].contributed = 100
player[1].contributed = 80
player[2].contributed = 20
player[0].score = 10
player[1].score = 2
player[2].score = 10
total_pot = 200;
In this example, do I first need to return player[0]
20 back and remove it from the pot?
Then, since player[0]
and player[2]
have tied for first spot,
and player[1]
has lost, should the pot be divided as:
player[0].received = 170
player[1].received = 0
player[2].received = 30
And subsequently, if player[1]
had won, should the pot be divided as:
player[0].received = 20
player[1].received = 180
player[2].received = 0
First sort by score descending, so you'll end up with two groups: { 0, 2 }, { 1 }.
Then, sort each group by the order they have contributed ascending: { 2 (20), 0 (100) }, { 1 (80) }.
Now, divide the pot in that order:
First you'll take (max) 20 away from each players contributions to create the first pot. And divide it evenly to 2 and 0. The first pot will be (20 + 20 + 20 = 60. So both 0 and 2 will be given 30). After that, the first players winnings are done, and you are left with: { 0 (80) }, { 1 (60) }.
Now, you'll take (max) 80 away from each players contributions to create the next pot (80 + 60 = 140). And give it to 0 (no division needed as there are no longer more than one in the top group, so 0 will receive the whole 140). You'll be left with: { 1 (0) }.
No more contributions left, so you are done.
So, in total in your example, 0 would receive 170 and 2 would receive 30.
The following code has a very large number of assertions, but BE CAREFUL because I have not tested it carefully. It's not clear what to do with the odd chips; I give them to the players that appear later in the collection.