Pages

Monday, April 7, 2014

Baccarat Game

Recently I was asked to create a simple code that generate a Baccarat statistics.

you can find more about the rules and how to play here: http://en.wikipedia.org/wiki/Baccarat_(card_game)

BACCARAT class
/**  * Game class, which represents a Baccarat game. It will have two main methods, one for  * playing the game and another to retrieve the statistics. A third method to print  * results will be also available.  * @author Wilson Soethe Cursino  * @since 1.0  */
public class Baccarat {
    private Shoedeck shoedeck;
    private final static int DEFAULT_DECKS_TO_USE = 8;
    private final static int MINIMUM_CARDS_IN_DECK = 6;
    public Baccarat() {
        shoedeck = new Shoedeck(DEFAULT_DECKS_TO_USE);
        shoedeck.shuffle();
    }
    public BaccaratStats statistics() {
        BaccaratStats baccaratStats = new BaccaratStats();
        int shoeSize = shoedeck.getShoe().size();
        if (shoeSize < 6)
            return baccaratStats;
        int[] cards = shoedeck.getQtyInShoeByValue();
        double playerWinCombs = 0;
        double bankerWinCombs = 0;
        double tieComb = 0;
        for (int playerCardOneCount = 0; playerCardOneCount < 10; playerCardOneCount++) {
            double playerCardOneWinComb = cards[playerCardOneCount]--;
            shoeSize--;
            for (int bankerCardOneCount = 0; bankerCardOneCount < 10; bankerCardOneCount++) {
                double bankerCardOneWinComb = playerCardOneWinComb * cards[bankerCardOneCount]--;
                shoeSize--;
                for (int playerCardTwoCount = 0; playerCardTwoCount < 10; playerCardTwoCount++) {
                    double playerCardTwoWinComb = bankerCardOneWinComb * cards[playerCardTwoCount]--;
                    shoeSize--;
                    for (int bankerCardTwoCount = 0; bankerCardTwoCount < 10; bankerCardTwoCount++) {
                        double bankerCardTwoWinComb = playerCardTwoWinComb * cards[bankerCardTwoCount]--;
                        shoeSize--;
                        int playerTotalPoints = (playerCardOneCount + playerCardTwoCount) % 10;
                        int bankerTotalPoints = (bankerCardOneCount + bankerCardTwoCount) % 10;
                        if (((playerTotalPoints >= 8) || (bankerTotalPoints >= 8))
                                || ((playerTotalPoints >= 6) && (bankerTotalPoints >= 6))) {
                            if (bankerTotalPoints > playerTotalPoints) {
                                bankerWinCombs += bankerCardTwoWinComb * shoeSize * (shoeSize - 1);
                            } else if (playerTotalPoints > bankerTotalPoints) {
                                playerWinCombs += bankerCardTwoWinComb * shoeSize * (shoeSize - 1);
                            } else {
                                tieComb += bankerCardTwoWinComb * shoeSize * (shoeSize - 1);
                            }
                        } else if (playerTotalPoints < 6) {
                            for (int playerCardThreeCount = 0; playerCardThreeCount < 10; playerCardThreeCount++) {
                                double playerCardThreeWinComb = bankerCardTwoWinComb * cards[playerCardThreeCount]--;
                                shoeSize--;
                                int playerTotalPointsWithThirdCard = (playerCardOneCount + playerCardTwoCount + playerCardThreeCount) % 10;
                                boolean bankerGetsExtraCard = true;
                                if (bankerTotalPoints == 7) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 6) && ((playerCardThreeCount < 6) || (playerCardThreeCount > 7))) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 5) && ((playerCardThreeCount < 4) || (playerCardThreeCount > 7))) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 4) && ((playerCardThreeCount < 2) || (playerCardThreeCount > 7))) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 3) && (playerCardThreeCount == 8)) {
                                    bankerGetsExtraCard = false;
                                }
                                if (bankerGetsExtraCard) {
                                    for (int bankerCardThreeCount = 0; bankerCardThreeCount < 10; bankerCardThreeCount++) {
                                        double bankerCardThreeWinComb = playerCardThreeWinComb * cards[bankerCardThreeCount]--;
                                        int bankerTotalWithThirdCard = (bankerCardOneCount + bankerCardTwoCount + bankerCardThreeCount) % 10;
                                        if (bankerTotalWithThirdCard > playerTotalPointsWithThirdCard) {
                                            bankerWinCombs += bankerCardThreeWinComb;
                                        } else if (playerTotalPointsWithThirdCard > bankerTotalWithThirdCard) {
                                            playerWinCombs += bankerCardThreeWinComb;
                                        } else {
                                            tieComb += bankerCardThreeWinComb;
                                        }
                                        cards[bankerCardThreeCount]++;
                                    }
                                } else {
                                    if (bankerTotalPoints > playerTotalPointsWithThirdCard) {
                                        bankerWinCombs += playerCardThreeWinComb * shoeSize;
                                    } else if (playerTotalPointsWithThirdCard > bankerTotalPoints) {
                                        playerWinCombs += playerCardThreeWinComb * shoeSize;
                                    } else {
                                        tieComb += playerCardThreeWinComb * shoeSize;
                                    }
                                }
                                cards[playerCardThreeCount]++;
                                shoeSize++;
                            }
                        } else if (bankerTotalPoints < 6) {
                            for (int bankerCardThreeCount = 0; bankerCardThreeCount < 10; bankerCardThreeCount++) {
                                double bankerCardThreeWinComb = bankerCardTwoWinComb * cards[bankerCardThreeCount]--;
                                shoeSize--;
                                int bankerTotalPointsWithThirdCard = (bankerCardOneCount + bankerCardTwoCount + bankerCardThreeCount) % 10;
                                if (bankerTotalPointsWithThirdCard > playerTotalPoints) {
                                    bankerWinCombs += bankerCardThreeWinComb * shoeSize;
                                } else if (playerTotalPoints > bankerTotalPointsWithThirdCard) {
                                    playerWinCombs += bankerCardThreeWinComb * shoeSize;
                                } else {
                                    tieComb += bankerCardThreeWinComb * shoeSize;
                                }
                                cards[bankerCardThreeCount]++;
                                shoeSize++;
                            }
                        }
                        cards[bankerCardTwoCount]++;
                        shoeSize++;
                    }
                    cards[playerCardTwoCount]++;
                    shoeSize++;
                }
                cards[bankerCardOneCount]++;
                shoeSize++;
            }
            cards[playerCardOneCount]++;
            shoeSize++;
        }
        double totalComb = bankerWinCombs + playerWinCombs + tieComb;
        baccaratStats.setTotalCombinations(totalComb);
        double bankerWinnerProbability = bankerWinCombs / totalComb;
        double playerWinnnerProbability = playerWinCombs / totalComb;
        double tieProbability = tieComb / totalComb;
        double bankerOdds = bankerWinCombs / (playerWinCombs + tieComb);
        double playerOdds = playerWinCombs / (bankerWinCombs + tieComb);
        double tieOdds = tieComb / (playerWinCombs + bankerWinCombs);
        baccaratStats.setBankerWinnerProbability(bankerWinnerProbability);
        baccaratStats.setPlayerWinnnerProbability(playerWinnnerProbability);
        baccaratStats.setTieProbability(tieProbability);
        baccaratStats.setBankerOdds(bankerOdds);
        baccaratStats.setPlayerOdds(playerOdds);
        baccaratStats.setTieOdds(tieOdds);
        return baccaratStats;
    }
    /**      * This method will play once and will remove the cards from the shoedeck.      * @return simple game result (TIE, BANKER or PLAYER)      */
    public BaccaratResult playOnce() {
        BaccaratResult result = new BaccaratResult();
        if (shoedeck.getShoe().size() < MINIMUM_CARDS_IN_DECK) {
            result.setGameResult(GameResult.FINAL_GAME);
            return result;
        }
        Hand playerHand = new Hand();
        Hand bankerHand = new Hand();
        playerHand.addCard(shoedeck.popCard());
        bankerHand.addCard(shoedeck.popCard());
        playerHand.addCard(shoedeck.popCard());
        bankerHand.addCard(shoedeck.popCard());
        int playerThirdCardValue = -1;
        if (!(bankerHand.points() >= 8 || playerHand.points() >= 8) && playerHand.points() <= 5) {
            Card thirdCard = shoedeck.popCard();
            playerHand.addCard(thirdCard);
            playerThirdCardValue = thirdCard.getValue().getValue();
        }
        boolean bankerGetsExtraCard = true;
        if (playerThirdCardValue == -1) {
            if (bankerHand.points() >= 8 || playerHand.points() >= 8) {
                bankerGetsExtraCard = false;
            }
            if (bankerHand.points() == 7) {
                bankerGetsExtraCard = false;
            }
            if (playerHand.points() >= 6) {
                if (bankerHand.points() == 6) {
                    bankerGetsExtraCard = false;
                } else {
                    bankerGetsExtraCard = true;
                }
            }
        } else {
            switch (bankerHand.points()) {
            case 0:
            case 1:
            case 2:
                bankerGetsExtraCard = true;
            case 3:
                if (playerThirdCardValue != 8)
                    bankerGetsExtraCard = true;
                break;
            case 4:
                if (playerThirdCardValue >= 2 && playerThirdCardValue <= 7)
                    bankerGetsExtraCard = true;
                break;
            case 5:
                if (playerThirdCardValue >= 4 && playerThirdCardValue <= 7)
                    bankerGetsExtraCard = true;
                break;
            case 6:
                if (playerThirdCardValue == 6 || playerThirdCardValue == 7)
                    bankerGetsExtraCard = true;
                break;
            }
            bankerGetsExtraCard = false;
        }
        if (bankerGetsExtraCard) {
            bankerHand.addCard(shoedeck.popCard());
        }
        GameResult gameResult;
        if (bankerHand.points() - playerHand.points() == 0) {
            gameResult = GameResult.TIE;
        } else if (bankerHand.points() - playerHand.points() > 0) {
            gameResult = GameResult.BANKER;
        } else {
            gameResult = GameResult.PLAYER;
        }
        result.setGameResult(gameResult);
        result.setBankerHand(bankerHand);
        result.setPlayerHand(playerHand);
        return result;
    }
}

No comments :