Posted By

bsauberman on 02/18/17


Tagged


Versions (?)

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.

  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package greedypig;
  7.  
  8. /**
  9.  *
  10.  * @author bensauberman
  11.  */
  12. public class GreedyPig {
  13.  
  14. /**
  15.   * @param args the command line arguments
  16.   */
  17. public static void main(String[] args) {
  18. // TODO code application logic here
  19. int roundScore = 0;
  20. int rolls = 0;
  21. int roundNumber = 0;
  22. boolean advance = true;
  23. int[][] scoresheet = new int[10000][2];
  24. double[][] averageTracker = new double[15][2];
  25.  
  26. for(int trialLimit = 10; trialLimit < 25; trialLimit++) {
  27. for(roundNumber = 0; roundNumber < scoresheet.length; roundNumber++) {
  28. advance = true;
  29. while (advance) {
  30. int roll = (int)(6.0 * Math.random()) + 1;
  31. rolls++;
  32. if(roll != 5) {
  33. roundScore += roll;
  34. }
  35. if(roll == 5) {
  36. roundScore = 0;
  37. scoresheet[roundNumber][0] = rolls;
  38. scoresheet[roundNumber][1] = roundScore;
  39. advance = false;
  40. }
  41. if(roundScore >= trialLimit) {
  42. scoresheet[roundNumber][0] = rolls;
  43. scoresheet[roundNumber][1] = roundScore;
  44. roundScore = 0;
  45. advance = false;
  46. }
  47. }
  48. rolls = 0;
  49. roundNumber++;
  50. }
  51.  
  52. double sum = 0;
  53. double avgSum = 0;
  54. for(int x = 0; x < scoresheet.length; x++) {
  55. sum += scoresheet[x][1];
  56. avgSum = sum / scoresheet.length;
  57. //System.out.print(trialLimit + " Round Number " + x + " " + scoresheet[x][0] + " "); //UNCOMMENT THESE OUT TO SEE EACH TRIAL
  58. //System.out.println(scoresheet[x][1]);
  59.  
  60. }
  61. averageTracker[trialLimit-10][0] = avgSum;
  62. averageTracker[trialLimit-10][1] = trialLimit;
  63. System.out.println("Sum for " + scoresheet.length + " trials with " + trialLimit + " as the number to stop at: " + sum);
  64. System.out.println("Average sum for " + scoresheet.length + " trials with " + trialLimit + " as the number to stop at: " + avgSum);
  65. System.out.println();
  66. System.out.println();
  67.  
  68. }
  69.  
  70. for(int x = 0; x < 15; x++) {
  71. for(int y = 0; y < 14; y++) {
  72. double temp0 = 0;
  73. double temp1 = 0;
  74.  
  75. if(averageTracker[y][0] < averageTracker[y+1][0]) {
  76. temp0 = averageTracker[y][0];
  77. temp1 = averageTracker[y][1];
  78.  
  79. averageTracker[y][0] = averageTracker[y+1][0];
  80. averageTracker[y][1] = averageTracker[y+1][1];
  81.  
  82. averageTracker[y+1][0] = temp0;
  83. averageTracker[y+1][1] = temp1;
  84.  
  85. }
  86. }
  87. }
  88.  
  89. for(int x = 0; x < 15; x++) {
  90. System.out.println("Trial Limit of " + averageTracker[x][1] + ": Average Score of " + averageTracker[x][0]);
  91. }
  92. }
  93. }

Report this snippet  

You need to login to post a comment.