Determining winning amount in Poker without creating side-pots

后端 未结 2 1226
忘掉有多难
忘掉有多难 2021-02-14 19:14

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
2条回答
  •  爱一瞬间的悲伤
    2021-02-14 19:50

    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.

    import java.util.*;
    
    public class Player {
        int contributed, score, received;
    
        static void winnings(Collection players) {
            for (Player player : players) {
                assert player.contributed >= 0;
                player.received = 0;
            }
            int potCutoff = 0;
            while (true) {
                int playerCount = 0;
                int nextPotCutoff = Integer.MAX_VALUE;
                int scoreMax = Integer.MIN_VALUE;
                int winnerCount = 0;
                for (Player player : players) {
                    if (player.contributed <= potCutoff) {
                        continue;
                    }
                    playerCount++;
                    assert playerCount > 0;
                    nextPotCutoff = Math.min(nextPotCutoff, player.contributed);
                    if (player.score > scoreMax) {
                        scoreMax = player.score;
                        winnerCount = 1;
                    } else if (player.score == scoreMax) {
                        winnerCount++;
                        assert winnerCount > 0;
                    } else {
                        assert player.score < scoreMax;
                    }
                }
                if (playerCount == 0) {
                    break;
                }
                assert playerCount > 0;
                assert nextPotCutoff > potCutoff;
                assert potCutoff >= 0;
                assert Integer.MAX_VALUE / (nextPotCutoff - potCutoff) >= playerCount;
                int potTotal = playerCount * (nextPotCutoff - potCutoff);
                assert potTotal > 0;
                assert winnerCount > 0;
                assert winnerCount <= playerCount;
                for (Player player : players) {
                    if (player.contributed <= potCutoff) {
                        continue;
                    }
                    assert player.contributed >= nextPotCutoff;
                    if (player.score == scoreMax) {
                        assert winnerCount > 0;
                        int winnerShare = potTotal / winnerCount;
                        winnerCount--;
                        assert winnerShare > 0;
                        assert potTotal >= winnerShare;
                        potTotal -= winnerShare;
                        player.received += winnerShare;
                        assert player.received > 0;
                    } else {
                        assert player.score < scoreMax;
                    }
                }
                assert winnerCount == 0;
                assert potTotal == 0;
                potCutoff = nextPotCutoff;
            }
        }
    
        public static void main(String[] args) {
            Player p0 = new Player(), p1 = new Player(), p2 = new Player();
            p0.contributed = 100;
            p1.contributed = 80;
            p2.contributed = 20;
            p0.score = 10;
            p1.score = 2;
            p2.score = 10;
            Collection players = new ArrayList();
            players.add(p0);
            players.add(p1);
            players.add(p2);
            winnings(players);
            for (Player player : players) {
                System.out.println(player.received);
            }
        }
    }
    

提交回复
热议问题