Bladeren bron

Simulation Created

Trinh Tong 6 jaren geleden
bovenliggende
commit
2f3e3accfe

+ 25
- 0
src/main/java/Bins.java Bestand weergeven

@@ -1,9 +1,11 @@
1 1
 import java.util.HashMap;
2
+import java.util.Set;
2 3
 
3 4
 public class Bins {
4 5
     public HashMap<Integer, Integer> results;
5 6
     int min = 0;
6 7
     int max = 0;
8
+    Set<Integer> allKeys;
7 9
     // creates an array of required length
8 10
     // increments the array @ index roll properly
9 11
 
@@ -11,10 +13,33 @@ public class Bins {
11 13
         this.min = min;
12 14
         this.max = max;
13 15
 
16
+
14 17
         this.results = new HashMap<Integer, Integer>();
15 18
 
16 19
         for (int i = min; i <= max; i++) {
17 20
             this.results.put(i, 0);
18 21
         }
22
+
23
+        allKeys = this.results.keySet();
24
+    }
25
+
26
+    public boolean hasKey(int key) {
27
+
28
+        boolean hasKeyToken = false;
29
+
30
+        for (int diceRoll: allKeys) {
31
+            if (key == diceRoll) {
32
+                hasKeyToken = true;
33
+                break;
34
+            }
35
+        }
36
+
37
+        return hasKeyToken;
38
+    }
39
+
40
+    public void incrementValue(int key) {
41
+
42
+        this.results.put(key, this.results.get(key) + 1);
43
+
19 44
     }
20 45
 }

+ 4
- 0
src/main/java/Dice.java Bestand weergeven

@@ -24,5 +24,9 @@ public class Dice {
24 24
         return result;
25 25
     }
26 26
 
27
+    public int getNumberOfDice() {
28
+        return this.numberOfDice;
29
+    }
30
+
27 31
 }
28 32
 

+ 2
- 0
src/main/java/Main.java Bestand weergeven

@@ -0,0 +1,2 @@
1
+public class Main {
2
+}

+ 18
- 0
src/main/java/Simulation.java Bestand weergeven

@@ -1,5 +1,23 @@
1 1
 public class Simulation {
2 2
 
3
+    int dice = 0;
4
+    int tosses = 0;
5
+    Dice simulationDie;
6
+    Bins simulationBins;
3 7
 
8
+    public Simulation(int numberofDie, int numberOfTosses) {
9
+        this.dice = numberofDie;
10
+        this.tosses = numberOfTosses;
4 11
 
12
+        simulationDie = new Dice(numberofDie);
13
+
14
+        simulationBins = new Bins(this.dice, this.dice * 6);
15
+
16
+        for (int i = 0; i < numberOfTosses; i++) {
17
+            int key = simulationDie.tossAndSum();
18
+            if (simulationBins.hasKey(key)) {
19
+                simulationBins.incrementValue(key);
20
+            }
21
+        }
22
+    }
5 23
 }

+ 22
- 0
src/test/java/BinsTest.java Bestand weergeven

@@ -7,12 +7,14 @@ public class BinsTest {
7 7
 
8 8
     Dice testDice;
9 9
     Bins testBins;
10
+    int key;
10 11
 
11 12
     public BinsTest() {
12 13
 
13 14
         testDice = new Dice(2);
14 15
         testBins = new Bins(2, 12);
15 16
 
17
+        key = testDice.getNumberOfDice();
16 18
     }
17 19
 
18 20
     @Test
@@ -38,4 +40,24 @@ public class BinsTest {
38 40
         // Then
39 41
         Assert.assertTrue(keyMatches);
40 42
     }
43
+
44
+    @Test
45
+    public void testCheckBinValue() {
46
+
47
+        // Given
48
+        int max = 100;
49
+        int actual = 0;
50
+
51
+        // When
52
+        for (int i = 0; i < 100; i++) {
53
+            testBins.incrementValue(key);
54
+        }
55
+
56
+        if (testBins.hasKey(key)) {
57
+            actual = testBins.results.get(key);
58
+        }
59
+
60
+        // Then
61
+        Assert.assertEquals(max, actual);
62
+    }
41 63
 }

+ 6
- 0
src/test/java/SimulationTest.java Bestand weergeven

@@ -1,2 +1,8 @@
1
+import org.junit.Test;
2
+
1 3
 public class SimulationTest {
4
+    // Simulation
5
+
2 6
 }
7
+
8
+