Roller.java
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 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 12 SIDED DIE AND ROLL IT UNTIL YOU GET A 1
30           System.out.println("Ten times, roll a 12 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.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           die.roll(); System.out.println(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.println(die.top() + " ");
51           die.roll(); System.out.println(die.top() + " ");
52           die.roll(); System.out.println(die.top() + " ");
53           die.roll(); System.out.println(die.top() + " ");
54           die.roll(); System.out.println(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   
62           //For Loop Style
63           //for (int i =1; i <= nrOfTimes; i++) {
64               //lucky.roll();
65               //System.out.println(lucky.top() + " ");
66           //}
67   
68           //While Loop Style
69           int i = 1;
70           while(i <= nrOfTimes) {
71               lucky.roll();
72               System.out.println(lucky.top() + " ");
73               i++;
74           }
75   
76           System.out.println();
77       }
78   
79       private static void createAndRollNineSidedDie(int nrOfTimes) {
80           System.out.println("Roll a 9-sided die " + nrOfTimes + " times ...");
81           Die lucky = new Die(9);
82   
83           for (int i =1; i <= nrOfTimes; i++) {
84               lucky.roll();
85               System.out.println(lucky.top() + " ");
86           }
87           System.out.println();
88       }
89   
90       private static void createAndRollStandardDieFor1() { //Roll a standard die until you get a 1
91           Die die = new Die(); //Create the die
92           die.roll(); //Roll the die
93           System.out.print(die.top() + " "); //print the top face of the die followed by a space -- using print not println
94   
95           while (die.top() != 1 ) { //while the top is not a 1,
96               die.roll(); //roll the die
97               System.out.print(die.top() + " "); //print the top face of the die followed by a space -- using print not println
98           } //end while
99   
100          System.out.println(); //issue a println command (just to terminate printing on the line)
101      }
102  
103      private static void createAndRollTwelveSidedDieFor1() { //Roll a 12 sided die until you get a 1
104          Die die = new Die(12); //Create the die
105          die.roll(); //Roll the die
106          System.out.print(die.top() + " "); //print the top face of the die followed by a space -- using print not println
107  
108          while (die.top() != 1 ) { //while the top is not a 1,
109              die.roll(); //roll the die
110              System.out.print(die.top() + " "); //print the top face of the die followed by a space -- using print not println
111          } //end while
112  
113          System.out.println(); //issue a println command (just to terminate printing on the line)
114      }
115  }
116