百家乐游戏模拟

时间:2016-05-23 05:31:33

标签: java simulation probability combinatorics

我写了一些代码来列举百家乐游戏中所有可能的牌。问题是结果与网上两个不同的地方的结果不同,这两个地方也是一致的。

以下是在线找到的答案:

银行家获胜0.458597  球员获胜0.446247
领带0.095156

wizard of odds
reviewpokerrooms.com/casino-games/baccarat/odds-calculator.html

可以找到enumBaccarat()函数的日志here

以下是enumBaccarat()函数的结果:

SCORE p:48074,b:44955 t:10382 问题p:0.44770758239239270625523092341969085952235129960809599898180311840,b:0.45959536930024790930245539965226079881547710295342223860381081920 t:0.09539546930852567198933768141898732254664847334297019797573259680

以下是来自random()函数的10亿手牌的结果:

领带:94258105,银行家:458337837,球员:447404058
领带:0.094258105,
银行家:0.458337837,
球员:0.447404058

以下是重要的课程:

import java.util.Vector;
import java.math.BigDecimal;
import java.math.RoundingMode;

import org.apache.commons.math3.util.CombinatoricsUtils;

public class BaccaratGame 
{

    private Vector<Integer> player = new Vector<Integer>(0);
    private Vector<Integer> banker = new Vector<Integer>(0);
    private int playerTotal;
    private int bankerTotal;
    private BaccaratShoe shoe;
    private int playerWin = 0;
    private int bankerWin = 0;
    private int tie = 0;

    private BigDecimal playerProb = new BigDecimal(0);
    private BigDecimal bankerProb = new BigDecimal(0);
    private BigDecimal tieProb = new BigDecimal(0);

    public BaccaratGame(BaccaratShoe shoe)
    {
        this.shoe = shoe;
        this.bankerWin=0;
        this.playerWin=0;
        this.tie=0;
    }
    //Player chooses bets
    //action happens

    //two cards delt
    private int getBankerTotal()
    {
        bankerTotal = 0;
        for(int i = 0; i < banker.size(); i++)
        {
            bankerTotal += banker.get(i);
        }
        bankerTotal = bankerTotal % 10;
        return bankerTotal;
    }

    private int getPlayerTotal()
    {
        playerTotal = 0;
        for(int i = 0; i < player.size(); i++)
        {
            playerTotal += player.get(i);
        }
        playerTotal = playerTotal % 10;
        return playerTotal;
    }
    public void reseed()
    {
        //shoe.replaceCards(player);
        //shoe.replaceCards(banker);
        player = new Vector<Integer>(0);
        banker = new Vector<Integer>(0);
        getPlayerTotal();
        getBankerTotal();       
    }
    public int oneRandomHand()
    {
        BaccaratShoe tmp = shoe;
        player.add(tmp.dealRandomCard().getValue());
        player.add(tmp.dealRandomCard().getValue());

        banker.add(tmp.dealRandomCard().getValue());
        banker.add(tmp.dealRandomCard().getValue());


        getBankerTotal();
        getPlayerTotal();

        if(playerTotal < 8 && bankerTotal < 8)
        {
            if(playerTotal < 6)
            {
                //player draws third card
                BaccaratCard third = tmp.dealRandomCard();
                player.add(third.getValue());
                getPlayerTotal();
                //weird banker rules
                if(third.getValue() < 4)
                {
                    if(bankerTotal < 5)
                    {
                        //banker draws third card
                        banker.add(tmp.dealRandomCard().getValue());
                        getBankerTotal();

                    }
                }
                else if(third.getValue() < 6)
                {
                    if(bankerTotal < 6)
                    {
                        //banker draws third card
                        banker.add(tmp.dealRandomCard().getValue());
                        getBankerTotal();

                    }
                }
                else if(third.getValue() < 8)
                {
                    if(bankerTotal < 7)
                    {
                        //banker draws third card
                        banker.add(tmp.dealRandomCard().getValue());
                        getBankerTotal();

                    }
                }
                else if(third.getValue() == 8)
                {
                    if(bankerTotal < 3)
                    {
                        //banker draws third card
                        banker.add(tmp.dealRandomCard().getValue());
                        getBankerTotal();

                    }
                }
                else
                {
                    //A9T
                    if(bankerTotal < 4)
                    {
                        //banker draws third card
                        banker.add(tmp.dealRandomCard().getValue());
                        getBankerTotal();       
                    }
                }       
            }
            else if(bankerTotal < 6)
            {
                //banker draws third card
                banker.add(tmp.dealRandomCard().getValue());
                getBankerTotal();
            }
        }
        return getWinner();

    }

    public void recordWinner(Vector<Integer> originalRanks)
    {
        int winner = getWinner();
        BigDecimal prob = getProb(originalRanks, player, banker);

        if(winner==-1)
        {
            this.bankerProb = this.bankerProb.add(prob);
            this.bankerWin++;
        }
        else if(winner==1)
        {
            this.playerProb = this.playerProb.add(prob);
            this.playerWin++;
        }
        else if(winner==0)
        {
            this.tieProb = this.tieProb.add(prob);
            this.tie++;
        }
        String playerHand = "p: ";
        for(int i=0; i < player.size(); i++)
        {
            playerHand += player.get(i).toString() + " ";
        }
        String bankerHand = "b: ";
        for(int i=0; i < banker.size(); i++)
        {
            bankerHand += banker.get(i).toString() + " ";
        }
        System.out.println("HAND " + playerHand + bankerHand);
        System.out.println("SCORE p:" + String.valueOf(this.playerWin) + ", b:" + String.valueOf(this.bankerWin) + " t:" + String.valueOf(this.tie));
        System.out.println("Prob p:" + this.playerProb.toString() + ", b:" + this.bankerProb.toString() + " t:" + this.tieProb.toString());
        System.out.println("current prob=" + prob.toString());

    }
    public int getWinner()
    {
        getBankerTotal();
        getPlayerTotal();
        if(bankerTotal == playerTotal)
        {
            return 0; // tie
        }
        else if(bankerTotal > playerTotal)
        {
            return -1; // banker wins
        } 
        else
        {
            return 1; // player wins
        }

    }
    public boolean dragon7Winner()
    {
        if(banker.size() != 3)
        {
            return false;
        }
        if(getBankerTotal() != 7)
        {
            return false;
        }
        if(getWinner() != -1)
        {
            return false;
        }
        return true;    
    }
    public boolean panda8Winner()
    {
        if(player.size() != 3)
        {
            return false;
        }
        if(getPlayerTotal() != 8)
        {
            return false;
        }
        if(getWinner() != 1)
        {
            return false;
        }
        return true;    
    }
    public BigDecimal getProb(Vector<Integer> ranks, Vector<Integer> player, Vector<Integer> banker)
    {
        //count of the ranks before the hands are dealt

        //check for pairs

        //get a total of ranks
        int totalCards = 0;
        for(int i=0; i < ranks.size(); i++)
        {
            totalCards += ranks.get(i);
        }
        String rankStr = "";
        for (int i = 0; i < 10; i++)
        {
            rankStr+= i + "[" + ranks.get(i) + "] ";
        }
        System.out.println(rankStr);
        int card1Count = ranks.get(player.get(0));
        int card2Count = ranks.get(player.get(1));

        BigDecimal current = getHandPercentge(card1Count, card2Count, totalCards, 2); 
        //System.out.println("c1:" + current.toString());
        if(player.get(0) == player.get(1))
        {
            current = getHandPercentge(card1Count, card2Count - 1, totalCards, 2).divide(new BigDecimal(2));
        }
        else
        {
            current = getHandPercentge(card1Count, card2Count, totalCards, 2);
        }
        totalCards = totalCards - 2;getClass();

        if(player.size() > 2)
        {
            int card3Count = ranks.get(player.get(2));
            if(player.get(2) == player.get(1))
            {
                card3Count = card3Count - 1;
            }
            if(player.get(2) == player.get(0))
            {
                card3Count = card3Count - 1;
            }   
            current = current.multiply(new BigDecimal(card3Count).divide(new BigDecimal(totalCards), 16, RoundingMode.HALF_UP));
        }
        totalCards = totalCards - 1;

        card1Count = ranks.get(banker.get(0));
        card2Count = ranks.get(banker.get(1));

        for(int i = 0; i < player.size(); i++)
        {
            if(player.get(i) == banker.get(0))
            {
                card1Count = card1Count - 1;
            }
            if(player.get(i) == banker.get(1))
            {
                card2Count = card2Count - 1;
            }
        }
        if(banker.get(0) == banker.get(1))
        {
            current = current.multiply(getHandPercentge(card1Count, card2Count - 1, totalCards, 2).divide(new BigDecimal(2)));
        }
        else
        {
            current = current.multiply(getHandPercentge(card1Count, card2Count, totalCards, 2));
        }

        totalCards = totalCards - 2;
        if(banker.size() > 2)
        {
            int card3Count = ranks.get(banker.get(2));
            if(banker.get(2) == player.get(1))
            {
                card3Count = card3Count - 1;
            }
            if(banker.get(2) == player.get(0))
            {
                card3Count = card3Count - 1;
            }
            if(player.size() > 2)
            {
                if(banker.get(2) == player.get(2))
                {
                    card3Count = card3Count - 1;
                }   
            }
            if(banker.get(2) == banker.get(1))
            {
                card3Count = card3Count - 1;
            }
            if(banker.get(2) == banker.get(0))
            {
                card3Count = card3Count - 1;
            }
            //System.out.println("card3Count=" + card3Count);
            current = current.multiply(new BigDecimal(card3Count).divide(new BigDecimal(totalCards), 16, RoundingMode.HALF_UP));

        }
        //System.out.println("last:" + current.toString());
        return current;
    }
    public static BigDecimal getHandPercentge(int card1Count, int card2Count, int totalCards, int numberToDraw)
    {
        return new BigDecimal(card1Count).multiply(new BigDecimal(card2Count).divide(new BigDecimal(CombinatoricsUtils.binomialCoefficient(totalCards, numberToDraw)), 16 ,RoundingMode.HALF_UP));
    }
    public void enumBaccarat()
    {
        Vector<Integer> ranks = new Vector<Integer>(10);
        for (int i = 0; i < 10; i++)
        {
            ranks.add(i, 0);
        }
        for (int i = 0; i < shoe.getSize(); i++)
        {
            int index = shoe.getCard(i).getValue();
            int current = ranks.get(index);
            ranks.set(index, current + 1);
        }
        Vector<Integer> originalRanks = new Vector<Integer>(0);
        originalRanks.addAll(ranks);
        String rankStr = "";
        for (int i = 0; i < 10; i++)
        {
            rankStr+= i + "[" + ranks.get(i) + "] ";
        }
        System.out.println(rankStr);
        for (int one = 0; one < 10; one++)
        {
            Vector<Integer> tmpRanks = ranks;
            int current = tmpRanks.get(one);
            if(current <= 0)
            {
                continue;
            }
            player.add(0, one);
            tmpRanks.set(one, current - 1);
            //deal first 4 cards
            //check if more should be dealt
            for (int two = 0; two < 10; two++)
            {

                current = tmpRanks.get(two);
                if(current <= 0)
                {
                    continue;//skip cards we've run out of
                }
                banker.add(0, two);
                tmpRanks.set(two, current - 1);
                for (int three = 0; three <= one; three++)
                {

                    current = tmpRanks.get(three);
                    if(current <= 0)
                    {
                        continue;
                    }
                    player.add(1, three);
                    tmpRanks.set(three, current - 1);
                    for (int four = 0; four <= two; four++)
                    {


                        current = tmpRanks.get(four);
                        if(current <= 0)
                        {
                            continue;
                        }
                        banker.add(1, four);
                        tmpRanks.set(four, current - 1);

                        if(getPlayerTotal() < 8 && getBankerTotal() < 8)
                        {
                            if(getPlayerTotal() < 6)
                            {
                                //player draws 3rd card
                                for(int five = 0; five < 10; five++)
                                {   
                                    current = tmpRanks.get(five);
                                    if(current <= 0)
                                    {
                                        continue;
                                    }
                                    player.add(2, five);
                                    tmpRanks.set(five, current - 1);
                                    int tmpBT = getBankerTotal();
                                    if(tmpBT == 7)
                                    {
                                        recordWinner(originalRanks);

                                        rankStr = "";
                                        for (int i = 0; i < 10; i++)
                                        {
                                            rankStr+= i + "[" + ranks.get(i) + "] ";
                                        }
                                        System.out.println(rankStr);
                                    }
                                    else if(tmpBT < 3)
                                    {
                                        for(int six = 0; six < 10; six++)
                                        {
                                            current = tmpRanks.get(six);
                                            if(current <= 0)
                                            {
                                                continue;
                                            }
                                            banker.add(2, six);
                                            recordWinner(originalRanks);

                                            rankStr = "";
                                            for (int i = 0; i < 10; i++)
                                            {
                                                rankStr+= i + "[" + ranks.get(i) + "] ";
                                            }
                                            System.out.println(rankStr);
                                            banker.remove(2);
                                        }
                                    }
                                    else if(tmpBT == 6)
                                    {
                                        if(five==6 || five==7)  
                                        {
                                            for(int six = 0; six < 10; six++)
                                            {
                                                current = tmpRanks.get(six);
                                                if(current <= 0)
                                                {
                                                    continue;
                                                }
                                                banker.add(2, six);
                                                recordWinner(originalRanks);

                                                rankStr = "";
                                                for (int i = 0; i < 10; i++)
                                                {
                                                    rankStr+= i + "[" + ranks.get(i) + "] ";
                                                }
                                                System.out.println(rankStr);
                                                banker.remove(2);
                                            }
                                        }
                                        else
                                        {
                                            recordWinner(originalRanks);

                                            rankStr = "";
                                            for (int i = 0; i < 10; i++)
                                            {
                                                rankStr+= i + "[" + ranks.get(i) + "] ";
                                            }
                                            System.out.println(rankStr);
                                        }
                                    }
                                    else if(tmpBT == 5)
                                    {
                                        if(five > 3 && five < 8)    
                                        {
                                            for(int six = 0; six < 10; six++)
                                            {
                                                current = tmpRanks.get(six);
                                                if(current <= 0)
                                                {
                                                    continue;
                                                }
                                                banker.add(2, six);
                                                recordWinner(originalRanks);

                                                rankStr = "";
                                                for (int i = 0; i < 10; i++)
                                                {
                                                    rankStr+= i + "[" + ranks.get(i) + "] ";
                                                }
                                                System.out.println(rankStr);
                                                banker.remove(2);
                                            }
                                        }
                                        else
                                        {
                                            recordWinner(originalRanks);

                                            rankStr = "";
                                            for (int i = 0; i < 10; i++)
                                            {
                                                rankStr+= i + "[" + ranks.get(i) + "] ";
                                            }
                                            System.out.println(rankStr);
                                        }
                                    }
                                    else if(tmpBT == 4)
                                    {
                                        if(five > 1 && five < 8)    
                                        {
                                            for(int six = 0; six < 10; six++)
                                            {
                                                current = tmpRanks.get(six);
                                                if(current <= 0)
                                                {
                                                    continue;
                                                }
                                                banker.add(2, six);
                                                recordWinner(originalRanks);

                                                rankStr = "";
                                                for (int i = 0; i < 10; i++)
                                                {
                                                    rankStr+= i + "[" + ranks.get(i) + "] ";
                                                }
                                                System.out.println(rankStr);
                                                banker.remove(2);
                                            }
                                        }
                                        else
                                        {
                                            recordWinner(originalRanks);

                                            rankStr = "";
                                            for (int i = 0; i < 10; i++)
                                            {
                                                rankStr+= i + "[" + ranks.get(i) + "] ";
                                            }
                                            System.out.println(rankStr);
                                        }
                                    }
                                    else if(tmpBT == 3)
                                    {
                                        if(five != 8)   
                                        {
                                            for(int six = 0; six < 10; six++)
                                            {
                                                current = tmpRanks.get(six);
                                                if(current <= 0)
                                                {
                                                    continue;
                                                }
                                                banker.add(2, six);
                                                recordWinner(originalRanks);

                                                rankStr = "";
                                                for (int i = 0; i < 10; i++)
                                                {
                                                    rankStr+= i + "[" + ranks.get(i) + "] ";
                                                }
                                                System.out.println(rankStr);
                                                banker.remove(2);
                                            }
                                        }
                                        else
                                        {
                                            recordWinner(originalRanks);

                                            rankStr = "";
                                            for (int i = 0; i < 10; i++)
                                            {
                                                rankStr+= i + "[" + ranks.get(i) + "] ";
                                            }
                                            System.out.println(rankStr);
                                        }
                                    }
                                    player.remove(2);
                                    tmpRanks.set(five, tmpRanks.get(five) + 1);
                                }

                            }
                            else if(getBankerTotal() < 6)
                            {
                                for(int five = 0; five < 10; five++)
                                {
                                    current = tmpRanks.get(five);
                                    if(current <= 0)
                                    {
                                        continue;
                                    }
                                    banker.add(2, five);
                                    recordWinner(originalRanks);

                                    rankStr = "";
                                    for (int i = 0; i < 10; i++)
                                    {
                                        rankStr+= i + "[" + ranks.get(i) + "] ";
                                    }
                                    System.out.println(rankStr);
                                    banker.remove(2);
                                }
                            }
                            else
                            {
                                recordWinner(originalRanks);

                                rankStr = "";
                                for (int i = 0; i < 10; i++)
                                {
                                    rankStr+= i + "[" + ranks.get(i) + "] ";
                                }
                                System.out.println(rankStr);
                            }
                        }
                        else
                        {
                            recordWinner(originalRanks);

                            rankStr = "";
                            for (int i = 0; i < 10; i++)
                            {
                                rankStr+= i + "[" + ranks.get(i) + "] ";
                            }
                            System.out.println(rankStr);
                        }

                        banker.remove(1);
                        tmpRanks.set(four, tmpRanks.get(four) + 1);
                    }
                    player.remove(1);
                    tmpRanks.set(three, tmpRanks.get(three) + 1);
                }
                banker.remove(0);
                tmpRanks.set(two, tmpRanks.get(two) + 1);
            }   
            player.remove(0);
            tmpRanks.set(one, tmpRanks.get(one) + 1);
        }
    }

1 个答案:

答案 0 :(得分:0)

让我们考虑另一个例子,一卷公平的模具。从理论上讲,概率是:

  1. 1/6
  2. 1/6
  3. 1/6
  4. 1/6
  5. 1/6
  6. 1/6
  7. 但是如果你进行了模拟,你最有可能会得到与1/6略有不同的概率。

    因此,如果您的百家乐模拟结果与理论结果略有不同,请不要惊讶。

    然而,在这个特定的例子中,似乎你的代码行为不同,导致你想要比较的赔率向导,例如,在third.getValue() < 4的情况下。确保您的模拟与您尝试将其与所有细节(包括规则和套牌数量)进行比较的内容相匹配。一旦从牌组发出了一些牌,下一张牌具有任何特定价值的概率与已经处理过不同价值牌时的概率略有不同。