Sfoglia il codice sorgente

completed dicey lab

Jonathan Hinds 5 anni fa
parent
commit
ac009b7aba

BIN
.DS_Store Vedi File


+ 10
- 0
pom.xml Vedi File

@@ -8,5 +8,15 @@
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+        <dependency>
14
+            <groupId>junit</groupId>
15
+            <artifactId>junit</artifactId>
16
+            <version>4.12</version>
17
+            <scope>test</scope>
18
+        </dependency>
19
+
20
+    </dependencies>
11 21
 
12 22
 </project>

BIN
src/.DS_Store Vedi File


+ 29
- 2
src/main/java/Bins.java Vedi File

@@ -1,4 +1,31 @@
1
-
2 1
 public class Bins {
3 2
 
4
-}
3
+    //array to store the results of the dice rolls
4
+    private int[] result;
5
+    //amount of dice whos results are being stored in this bin
6
+    private int amount;
7
+    //the amount of sides on the dice
8
+    private int sides;
9
+
10
+    public Bins(int amount, int sides){
11
+        this.amount = amount;
12
+        this.sides = sides;
13
+        this.result = new int[((this.amount * this.sides) - (amount - 1))];
14
+    }
15
+
16
+    //mutator function to manipulate the results from the dice rolls
17
+    public void addToBin(int resultGiven){
18
+        this.result[(resultGiven - this.amount)] ++;
19
+    }
20
+
21
+    //accessor function to retrieve the amount of a given result
22
+    public int retrieveSums(int resultGiven){
23
+        return this.result[(resultGiven - this.amount)];
24
+    }
25
+
26
+    //accessor method to get results of the roll
27
+    public int[] getResults()
28
+    {
29
+        return this.result;
30
+    }
31
+}

+ 34
- 2
src/main/java/Dice.java Vedi File

@@ -1,4 +1,36 @@
1
-public class Dice {
1
+public class Dice
2
+{
3
+    //the amount of dice being created
4
+    private int amount = 0;
5
+    //the amount of sides the dice has
6
+    private int sides = 0;
2 7
 
8
+    public Dice(int amount, int sides)
9
+    {
10
+        this.amount = amount;
11
+        this.sides = sides;
12
+    }
3 13
 
4
-}
14
+    //method to roll the dice, and store the results in the bin
15
+    public int roll(){
16
+        //default the sum of these dice rolls to 0
17
+        int sum = 0;
18
+        //for each dice created
19
+        for(int i = 0; i < this.amount; i ++) {
20
+            //roll the dice
21
+            int random = (int) (Math.floor(Math.random() * this.sides) + 1);
22
+            //add the random number to the sum of the current roll
23
+            sum += random;
24
+        }
25
+        return sum;
26
+    }
27
+
28
+    //accessor function to access the dices sides
29
+    public int getSides(){
30
+        return this.sides;
31
+    }
32
+    //accessor function to get the dice amount;
33
+    public int getAmount(){
34
+        return this.amount;
35
+    }
36
+}

+ 14
- 0
src/main/java/Main.java Vedi File

@@ -0,0 +1,14 @@
1
+public class Main
2
+{
3
+    public static void main(String[] args)
4
+    {
5
+        //create a new simulation
6
+        Simulation sim = new Simulation(100, 6,100);
7
+
8
+        //run the new simulation
9
+        sim.runSimulation();
10
+
11
+        //print the results of simulation
12
+        sim.printResult();
13
+    }
14
+}

+ 46
- 0
src/main/java/Simulation.java Vedi File

@@ -1,5 +1,51 @@
1
+import java.math.RoundingMode;
2
+import java.math.BigDecimal;
3
+
1 4
 public class Simulation {
2 5
 
6
+    private Dice dice;
7
+    private Bins bin;
8
+    private int rolls = 0;
9
+
10
+    public Simulation(int numberOfDice, int diceSides, int timesROlled){
11
+        this.dice = new Dice(numberOfDice, diceSides);
12
+        this.bin = new Bins(numberOfDice, diceSides);
13
+        this.rolls = timesROlled;
14
+    }
15
+
16
+    public static double round(double value, int places) {
17
+        if (places < 0) throw new IllegalArgumentException();
18
+
19
+        BigDecimal bd = new BigDecimal(value);
20
+        bd = bd.setScale(places, RoundingMode.HALF_UP);
21
+        return bd.doubleValue();
22
+    }
3 23
 
24
+    //method to run the dice simulation
25
+    public void runSimulation()
26
+    {
27
+        //for each amount of time we roll
28
+        for(int i = 0; i < this.rolls; i ++){
29
+            //roll the dice and store the values
30
+            int sum = this.dice.roll();
31
+            //store the sum in the bin
32
+            bin.addToBin(sum);
33
+        }
34
+    }
4 35
 
36
+    //method to print the results of the roll
37
+    public void printResult(){
38
+        //store the results of the bin
39
+        int[] results = this.bin.getResults();
40
+        //loop through the bin
41
+        for(int i = 0; i < this.bin.getResults().length; i ++)
42
+        {
43
+            //for each value in the bin
44
+            int currentResult = this.bin.getResults()[i];
45
+            //get the statistics
46
+            double stats = ((double)currentResult/(double)this.rolls);
47
+            stats = round(stats, 2);
48
+            System.out.println("index of the array: " +i + " | value rolled : " + (i + this.dice.getAmount()) + " | amount of times this value was rolled: " + currentResult + " | percent : " + stats);
49
+        }
50
+    }
5 51
 }

+ 170
- 0
src/test/java/TestClass.java Vedi File

@@ -0,0 +1,170 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class TestClass {
5
+
6
+    //BIN TESTING
7
+
8
+    @Test
9
+    public void test01()
10
+    {
11
+        //GIVEN
12
+        //initialize the amount of rolls to 100;
13
+        int rollAmount = 100;
14
+        //create a new dice to test the bin;
15
+        Dice dice = new Dice(1, 6);
16
+        //create a new bin to test
17
+        Bins bin = new Bins(1, 6);
18
+        boolean expted = true;
19
+        boolean actual = false;
20
+        int counter = 0;
21
+
22
+        //WHEN
23
+        //loop one headred times
24
+        for(int i = 0; i < rollAmount; i++)
25
+        {
26
+            //create a new random number between 1 and 6 each time
27
+            int random = (int) (Math.floor(Math.random() * 6) + 1);
28
+            //add it to the bin to test addToBin
29
+            bin.addToBin(random);
30
+        }
31
+        //loop for each result in the bin // testing getResults();
32
+        for(int m = 0; m < bin.getResults().length; m++)
33
+        {
34
+            //store the current result
35
+            int binresult = bin.getResults()[m];
36
+            //if the result is not a negative number, and all results are less than
37
+            //the max amount of results
38
+            if(binresult >= 0 && binresult < 100)
39
+            {
40
+                //increase the counter
41
+                counter ++;
42
+            }
43
+        }
44
+        //if the counter was increased for every iteration
45
+        if(counter == bin.getResults().length)
46
+        {
47
+            //then pass the test
48
+            actual = true;
49
+        }
50
+        Assert.assertEquals(expted, actual);
51
+    }
52
+
53
+    //DICE TESTING
54
+
55
+    @Test
56
+    public void test02() {
57
+        //GIVEN
58
+        //amount of times rolling
59
+        int rollAmount = 100;
60
+        //amount of dice being rolled
61
+        int amountOfDice = 3;
62
+        //sum of the dice roll
63
+        int sum = 0;
64
+        //dice to be rolled
65
+        Dice dice = new Dice(amountOfDice, 6);
66
+        //bin results will be stored in
67
+        Bins bin = new Bins(amountOfDice, 6);
68
+        //result to pass the test
69
+        boolean expected = true;
70
+        //default result
71
+        boolean result = false;
72
+
73
+        //WHEN
74
+        //perform 100 rolls, testing every roll
75
+        for(int i = 0; i < rollAmount; i ++)
76
+        {
77
+            //reset the sum each time it is rolled
78
+            sum = 0;
79
+            //roll the dice and collect the sum
80
+            sum = dice.roll();
81
+            //check if the roll is between the correct numbers
82
+            bin.addToBin(sum);
83
+            if(sum >= 3 && sum <= 18)
84
+            {
85
+                result = true;
86
+
87
+            }
88
+        }
89
+
90
+        //RESULT
91
+        Assert.assertEquals(expected, result);
92
+    }
93
+
94
+    @Test
95
+    public void test03() {
96
+        //GIVEN
97
+        //amount of times rolling
98
+        int rollAmount = 100;
99
+        //amount of dice being rolled
100
+        int amountOfDice = 100;
101
+        //sum of the dice roll
102
+        int sum = 0;
103
+        //dice to be rolled
104
+        Dice dice = new Dice(amountOfDice, 6);
105
+        //bin results will be stored in
106
+        Bins bin = new Bins(amountOfDice, 6);
107
+        //result to pass the test
108
+        boolean expected = true;
109
+        //default result
110
+        boolean result = false;
111
+
112
+        //WHEN
113
+        //perform 100 rolls, testing every roll
114
+        for(int i = 0; i < rollAmount; i ++)
115
+        {
116
+            //reset the sum each time it is rolled
117
+            sum = 0;
118
+            //roll the dice and collect the sum
119
+            sum = dice.roll();
120
+            //check if the roll is between the correct numbers
121
+            //at least 1 for every dice, and 600 for if every dice landed 6
122
+            if(sum >= dice.getAmount() && sum <= 600)
123
+            {
124
+                result = true;
125
+            }
126
+        }
127
+
128
+        //RESULT
129
+        Assert.assertEquals(expected, result);
130
+    }
131
+
132
+    @Test
133
+    public void test04() {
134
+        //GIVEN
135
+        //amount of times rolling
136
+        int rollAmount = 10000;
137
+        //amount of dice being rolled
138
+        int amountOfDice = 100;
139
+        //sum of the dice roll
140
+        int sum = 0;
141
+        //dice to be rolled
142
+        Dice dice = new Dice(amountOfDice, 100);
143
+        //bin results will be stored in
144
+        Bins bin = new Bins(amountOfDice, 100);
145
+        //result to pass the test
146
+        boolean expected = true;
147
+        //default result
148
+        boolean result = false;
149
+
150
+        //WHEN
151
+        //perform 100 rolls, testing every roll
152
+        for(int i = 0; i < rollAmount; i ++)
153
+        {
154
+            //reset the sum each time it is rolled
155
+            sum = 0;
156
+            //roll the dice and collect the sum
157
+            sum = dice.roll();
158
+            //check if the roll is between the correct numbers
159
+            //at least 1 for every dice, and 10000 for if every dice landed 100
160
+            if(sum >= dice.getAmount() && sum <= 10000)
161
+            {
162
+                result = true;
163
+            }
164
+        }
165
+
166
+        //RESULT
167
+        Assert.assertEquals(expected, result);
168
+    }
169
+}
170
+