import java.util.concurrent.ThreadLocalRandom;

/**
 * Calculates the probability of winning the game of Craps. 
 *
 * @author Drue Coles
 */
public class CrapsProbabilityCalculator {

    public static void main(String[] args) {

        int games = 10_000_000; // number of games to simulate
        int wins = 0;
        for (int i = 0; i < games; i++) {
            if (playerWins()) {
                wins++;
            }
        }

        double winPercent = (double) wins / games * 100;
        String dice = "Game of Craps \u2680 \u2681 \u2682 \u2683 \u2684 \u2685";
        String s1 = String.format("Win rate: %,d/%,d", wins, games);
        String s2 = String.format("Winning percentage: %.3f%%", winPercent);
          
        String output = dice + "\n" + s1 + "\n" + s2;
        System.out.println(output);
    }

    /**
     * Simulates the game of Craps.
     *
     * @return true if player wins, false otherwise
     */
    public static boolean playerWins() {
        final int firstSum = rollDice();

        if (firstSum == 7 || firstSum == 11) {
            return true;
        }
        if (firstSum == 2 || firstSum == 3 || firstSum == 12) {
            return false;
        }

        int newSum = rollDice();
        while (newSum != firstSum && newSum != 7) {
            newSum = rollDice();
        }
        return newSum == firstSum;
    }

    /**
     * Simulates rolling a pair of dice.
     *
     * @return the sum of numbers rolled
     */
    public static int rollDice() {
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        int die1 = rand.nextInt(1, 7);
        int die2 = rand.nextInt(1, 7);
        return die1 + die2;
    }
}