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 15 // CREATE A TWENTY SIDED DIE AND ROLL IT 5 TIMES 16 createAndRollTwentySidedDieFiveTimes(); 17 18 // CREATE A STANDARD DIE AND ROLL IT 20 TIMES 19 createAndRollStandardDie(20); 20 21 // CREATE A STANDARD DIE AND ROLL IT 30 TIMES 22 createAndRollStandardDie(30); 23 24 // CREATE A NINE SIDED DIE AND ROLL IT 20 TIMES 25 createAndRollNineSidedDie(20); 26 27 // CREATE A NINE SIDED DIE AND ROLL IT 30 TIMES 28 createAndRollNineSidedDie(30); 29 30 // TEN TIMES, CREATE A STANDARD DIE AND ROLL IT UNTIL YOU GET A 1 31 // System.out.println("Ten times, roll a standard die for a 1"); 32 for (int i = 1; i <= 10; i++) { 33 createAndRollStandardDieFor1(); 34 } 35 36 // TEN TIMES, CREATE A TWELVE SIDED DIE AND ROLL IT UNTIL YOU GET A 1 37 // System.out.println("Ten times, roll a twelve sided die for a 1."); 38 for (int i = 1; i <= 10; i++){ 39 createAndRollTwelveSidedDieFor1(); 40 } 41 } 42 43 private static void createAndRollStandardDieFiveTimes() { 44 System.out.println("Roll a standard die 5 times... "); 45 Die die = new Die(); 46 die.roll(); System.out.print(die.top() + " "); 47 die.roll(); System.out.print(die.top() + " "); 48 die.roll(); System.out.print(die.top() + " "); 49 die.roll(); System.out.print(die.top() + " "); 50 die.roll(); System.out.print(die.top() + " "); 51 System.out.println(); 52 } 53 54 private static void createAndRollTwentySidedDieFiveTimes() { 55 System.out.println("Roll a Twenty sided die 5 times... "); 56 Die die20 = new Die(20); 57 die20.roll(); System.out.print(die20.top() + " "); 58 die20.roll(); System.out.print(die20.top() + " "); 59 die20.roll(); System.out.print(die20.top() + " "); 60 die20.roll(); System.out.print(die20.top() + " "); 61 die20.roll(); System.out.print(die20.top() + " "); 62 System.out.println(); 63 } 64 65 private static void createAndRollStandardDie(int nrOfTimes) { 66 System.out.println("Roll a standard die " + nrOfTimes + " times ..."); 67 Die lucky = new Die(); 68 for (int i = 1; i<=nrOfTimes; i = i + 1) { 69 lucky.roll(); 70 System.out.print(lucky.top() + " "); 71 } 72 System.out.println(); 73 } 74 75 private static void createAndRollNineSidedDie(int nrOfTimes) { 76 System.out.println("Roll a Nine sided die " + nrOfTimes + " times..."); 77 Die die9 = new Die(9); 78 for (int i = 1; i <= nrOfTimes; i = i + 1) { 79 die9.roll(); 80 System.out.print(die9.top() + " "); 81 } 82 System.out.println(); 83 } 84 85 private static void createAndRollStandardDieFor1() { 86 System.out.println("Roll a standard die for one, 10 times..."); 87 Die diefor1 = new Die(); 88 diefor1.roll(); 89 System.out.print(" " + diefor1.top()); 90 while (diefor1.top() != 1) { 91 diefor1.roll(); 92 System.out.print(" " + diefor1.top()); 93 } 94 System.out.println(); 95 96 } 97 98 private static void createAndRollTwelveSidedDieFor1() { 99 System.out.println("Roll a Twelve sided die for one, 10 times..."); 100 Die dietwelvefor1 = new Die(12); 101 dietwelvefor1.roll(); 102 System.out.print(" " + dietwelvefor1.top()); 103 while (dietwelvefor1.top() != 1) { 104 dietwelvefor1.roll(); 105 System.out.print(" " + dietwelvefor1.top()); 106 } 107 System.out.println(); 108 } 109 }