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 TWENTY 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 NINE SIDED DIE AND ROLL IT 20 TIMES 21 createAndRollNineSidedDie(20); 22 //CREATE A NINE SIDED DIE AND ROLL IT 30 TIMES 23 createAndRollNineSidedDie(30); 24 //TEN 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 //TEN TIMES, CREATE A TWELVE 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 twenty sided die 5 times ..."); 49 Die die = new Die(20); 50 die.roll(); System.out.print(die.top()+" "); 51 die.roll(); System.out.print(die.top()+" "); 52 die.roll(); System.out.print(die.top()+" "); 53 die.roll(); System.out.print(die.top()+" "); 54 die.roll(); System.out.print(die.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 nine sided die " + nrOfTimes + " times..."); 72 Die lucky = new Die(9); 73 int i=1; 74 while(i<=nrOfTimes) { 75 lucky.roll(); 76 System.out.print(lucky.top() + " "); 77 i=i+1; 78 } 79 System.out.println(); 80 } 81 82 private static void createAndRollStandardDieFor1() { 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 Die lucky=new Die(20); 95 lucky.roll(); 96 System.out.print(lucky.top() + " "); 97 while (lucky.top() != 1) { 98 lucky.roll(); 99 System.out.print(lucky.top() + " "); 100 } 101 System.out.println(); 102 } 103 } 104