1 /* 2 * Program to make use of the Die class 3 */ 4 package chanceapps; 5 6 import chance.Die; 7 8 public class Roller { 9 10 public static void main(String[] args) { 11 //CREATE A STANDARD DIE AND ROLL IT 5 TIMES 12 createAndRollStandardDieFiveTimes(); 13 //CREATE A TWENTY SIDED DIE AND ROLL IT 5 TIMES 14 createAndRollTwentySidedDieFiveTimes(); 15 //CREATE A STANDARD DIE AND ROLL IT 20 TIMES 16 createAndRollStandardDie(20); 17 //CREATE A STANDARD DIE AND ROLL IT 30 TIMES 18 createAndRollStandardDie(30); 19 //CREATE A NINE SIDED DIE AND ROLL IT 20 TIMES 20 createAndRollNineSidedDie(20); 21 //CREATE A NINE SIDED DIE AND ROLL IT 30 TIMES 22 createAndRollNineSidedDie(30); 23 //TEN TIMES, CREATE A STANDARD DIE AND ROLL IT UNTIL YOU GET A 1 24 System.out.println("Ten times, roll a standard die for a 1."); 25 for (int i = 1; i <= 10; i++) { 26 createAndRollStandardDieFor1(); 27 } 28 //TEN TIMES, CREATE A TWELVE SIDED DIE AND ROLL IT UNTIL YOU GET A 1 29 System.out.println("Ten times, roll a twelve sided die for a 1."); 30 for (int i = 1; i <= 10; i++) { 31 createAndRollTwelveSidedDieFor1(); 32 } 33 } 34 35 private static void createAndRollStandardDieFiveTimes() { 36 System.out.println("Roll a standard die 5 times ..."); 37 Die die = new Die(); 38 die.roll(); System.out.println(die.top() + " "); 39 die.roll(); System.out.println(die.top() + " "); 40 die.roll(); System.out.println(die.top() + " "); 41 die.roll(); System.out.println(die.top() + " "); 42 die.roll(); System.out.println(die.top() + " "); 43 System.out.println(); 44 } 45 private static void createAndRollTwentySidedDieFiveTimes() { 46 System.out.println("Roll a Twenty sided die 5 times ..."); 47 Die die = new Die(20); 48 die.roll(); System.out.println(die.top() + " "); 49 die.roll(); System.out.println(die.top() + " "); 50 die.roll(); System.out.println(die.top() + " "); 51 die.roll(); System.out.println(die.top() + " "); 52 die.roll(); System.out.println(die.top() + " " ); 53 System.out.println(); 54 } 55 56 private static void createAndRollStandardDie(int nrOfTimes) { 57 System.out.println("Roll a standard die " + nrOfTimes + " times ..."); 58 Die lucky = new Die(); 59 int i = 1; 60 while (i <= nrOfTimes) { 61 lucky.roll(); 62 System.out.print(lucky.top() + " "); 63 i = i + 1; 64 } 65 System.out.println(); 66 67 } 68 69 private static void createAndRollNineSidedDie(int nrOfTimes) { 70 System.out.println("Roll a nine sided die " + nrOfTimes + " times ..."); 71 Die lucky = new Die(9); 72 int i = 1; 73 while (i <= nrOfTimes) { 74 lucky.roll(); 75 System.out.print(lucky.top() + " "); 76 i = i + 1; 77 } 78 System.out.println(); 79 } 80 81 private static void createAndRollStandardDieFor1() { 82 System.out.println("Roll a standard die until you get a 1"); 83 Die lucky = new Die(); 84 lucky.roll(); 85 System.out.print(lucky.top() + " "); 86 while (lucky.top() != 1) { 87 lucky.roll(); 88 System.out.print(lucky.top() + " "); 89 } 90 System.out.println(); 91 } 92 93 private static void createAndRollTwelveSidedDieFor1() { 94 System.out.println("Roll a twelve sided die until you get a 1"); 95 Die lucky = new Die(12); 96 lucky.roll(); 97 System.out.print(lucky.top() + " "); 98 while (lucky.top() != 1) { 99 lucky.roll(); 100 System.out.print(lucky.top() + " "); 101 } 102 System.out.println(); 103 } 104 105 106 107 } 108