1 /* 2 * Program to make use of the Die class. 3 */ 4 5 package chanceapps; 6 7 import chance.Die; 8 9 public class Roller { 10 11 public static void main(String[] args) { 12 //CREATE A STANDARD DIE AND ROLL IT 5 TIMES 13 createAndRollStandardDieFiveTimes(); 14 //CREATE A 20 SIDED DIE AND ROLL IT 5 TIMES 15 createAndRollTwentySidedDieFiveTimes(); 16 // CREATE A STANDARD DIE AND ROLL IT 20 TIMES 17 createAndRollStandardDie(20); 18 // CREATE A STANDARD DIE AND ROLL IT 30 TIMES 19 createAndRollStandardDie(30); 20 // CREATE A 9 SIDED DIE AND ROLL IT 20 TIMES 21 createAndRollNineSidedDie(20); 22 // CREATE A 9 SIDED DIE AND ROLL IT 20 TIMES 23 createAndRollNineSidedDie(30); 24 // 10 TIMES, CREATE A STANDARD DIE AND ROLL IT UNTIL YOU GET A 1 25 System.out.println("Ten times, roll a standard die for a 1."); 26 for (int i = 1; i <= 10; i ++) { 27 createAndRollStandardDieFor1(); 28 } 29 // 10 TIMES, CREATE A 12 sided DIE AND ROLL IT UNTIL YOU GET A 1 30 System.out.println("Ten times, roll a twelve sided die for a 1."); 31 for (int i = 1; i <= 10; i ++) { 32 createAndRollTwelveSidedDieFor1(); 33 } 34 } 35 36 private static void createAndRollStandardDieFiveTimes() { 37 System.out.println("Roll a standard die 5 times ..."); 38 Die die = new Die(); 39 die.roll(); System.out.print(die.top() + " "); 40 die.roll(); System.out.print(die.top() + " "); 41 die.roll(); System.out.print(die.top() + " "); 42 die.roll(); System.out.print(die.top() + " "); 43 die.roll(); System.out.print(die.top() + " "); 44 System.out.println(); 45 } 46 47 private static void createAndRollTwentySidedDieFiveTimes() { 48 System.out.println("Roll a 20 sided die 5 times ..."); 49 Die twentySide = new Die(20); 50 twentySide.roll(); System.out.print(twentySide.top() + " "); 51 twentySide.roll(); System.out.print(twentySide.top() + " "); 52 twentySide.roll(); System.out.print(twentySide.top() + " "); 53 twentySide.roll(); System.out.print(twentySide.top() + " "); 54 twentySide.roll(); System.out.print(twentySide.top() + " "); 55 System.out.println(); 56 } 57 58 private static void createAndRollStandardDie(int nrOfTimes) { 59 System.out.println("Roll a standard die " + nrOfTimes + " times ..."); 60 Die lucky = new Die(); 61 int i = 1; 62 while(i <= nrOfTimes) { 63 lucky.roll(); 64 System.out.print(lucky.top() + " "); 65 i = i + 1; 66 } 67 System.out.println(); 68 } 69 70 private static void createAndRollNineSidedDie(int nrOfTimes) { 71 System.out.println("Roll a 9 sided die " + nrOfTimes + " times ..."); 72 Die nineSide = new Die (9); 73 int i = 1; 74 while ( i <= nrOfTimes) { 75 nineSide.roll(); 76 System.out.print(nineSide.top() + " "); 77 i = i + 1; 78 } 79 System.out.println(); 80 81 } 82 83 private static void createAndRollStandardDieFor1() { 84 Die untilOne = new Die(); 85 untilOne.roll(); 86 System.out.print(untilOne.top() + " "); 87 while (!(untilOne.top() == 1)) { 88 untilOne.roll(); 89 System.out.print(untilOne.top() + " "); 90 } 91 System.out.println(); 92 } 93 94 private static void createAndRollTwelveSidedDieFor1() { 95 Die twelveOne = new Die(12); 96 twelveOne.roll(); 97 System.out.print(twelveOne.top() + " "); 98 while (! (twelveOne.top() == 1)) { 99 twelveOne.roll(); 100 System.out.print(twelveOne.top() + " "); 101 } 102 System.out.println(); 103 } 104 } 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142