## Posted By

bsauberman on 02/18/17

# Greedy Pig

/ Published in: Java

After running this simulation of 10,0000 rounds for 15 trials each with a different maximum limit (first being 10, then 11 up to 24), I did not get the clear conclusion I sought for. Every time I run it, a maximum limit of 16-21 appears to be the best option but each comes up around the same amount of times.

Therefore, it is pretty safe to conclude that choosing your limit to sit down after you get at least any number between 16-21 is the most effective method to win Greedy Pig.

Note: The most opportune method usually gets an average of around 3.37 - 3.40 points per round and 3rd place gets around 3.34 - 3.36.

`/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package greedypig; /** * * @author bensauberman */public class GreedyPig {     /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here        int roundScore = 0;        int rolls = 0;           int roundNumber = 0;        boolean advance = true;        int[][] scoresheet = new int[10000][2];        double[][] averageTracker = new double[15][2];         for(int trialLimit = 10; trialLimit < 25; trialLimit++) {            for(roundNumber = 0; roundNumber < scoresheet.length; roundNumber++) {                 advance = true;                while (advance) {                    int roll = (int)(6.0 * Math.random()) + 1;                    rolls++;                    if(roll != 5) {                        roundScore += roll;                    }                    if(roll == 5) {                        roundScore = 0;                        scoresheet[roundNumber][0] = rolls;                        scoresheet[roundNumber][1] = roundScore;                        advance = false;                    }                    if(roundScore >= trialLimit) {                        scoresheet[roundNumber][0] = rolls;                        scoresheet[roundNumber][1] = roundScore;                        roundScore = 0;                        advance = false;                    }                }                    rolls = 0;                roundNumber++;            }             double sum = 0;            double avgSum = 0;            for(int x = 0; x < scoresheet.length; x++) {                 sum += scoresheet[x][1];                avgSum = sum / scoresheet.length;                //System.out.print(trialLimit + " Round Number " + x + "    " + scoresheet[x][0] + "   "); //UNCOMMENT THESE OUT TO SEE EACH TRIAL                 //System.out.println(scoresheet[x][1]);             }            averageTracker[trialLimit-10][0] = avgSum;            averageTracker[trialLimit-10][1] = trialLimit;                        System.out.println("Sum for " + scoresheet.length + " trials with " + trialLimit + " as the number to stop at: " + sum);            System.out.println("Average sum for " + scoresheet.length + " trials with " + trialLimit + " as the number to stop at: " + avgSum);                    System.out.println();            System.out.println();         }         for(int x = 0; x < 15; x++) {            for(int y = 0; y < 14; y++) {                double temp0 = 0;                double temp1 = 0;                 if(averageTracker[y][0] < averageTracker[y+1][0]) {                    temp0 =  averageTracker[y][0];                    temp1 =  averageTracker[y][1];                     averageTracker[y][0] = averageTracker[y+1][0];                    averageTracker[y][1] = averageTracker[y+1][1];                     averageTracker[y+1][0] = temp0;                    averageTracker[y+1][1] = temp1;                 }                    }        }         for(int x = 0; x < 15; x++) {            System.out.println("Trial Limit of " + averageTracker[x][1] + ": Average Score of " + averageTracker[x][0]);        }    }}`