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