ソースを参照

Dice thing done

NedRedmond 5 年 前
コミット
4aa91b6369
共有7 個のファイルを変更した215 個の追加1 個の削除を含む
  1. 8
    0
      pom.xml
  2. 30
    0
      src/main/java/Bins.java
  3. バイナリ
      src/main/java/Dice.class
  4. 24
    1
      src/main/java/Dice.java
  5. 7
    0
      src/main/java/Main.java
  6. 49
    0
      src/main/java/Simulation.java
  7. 97
    0
      src/main/test/DiceTest.java

+ 8
- 0
pom.xml ファイルの表示

@@ -8,5 +8,13 @@
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+    </dependencies>
11 19
 
12 20
 </project>

+ 30
- 0
src/main/java/Bins.java ファイルの表示

@@ -1,4 +1,34 @@
1
+import java.util.Arrays;
1 2
 
2 3
 public class Bins {
4
+    int[][] binArray;
5
+    int minRoll;
6
+    int maxRoll;
7
+    int binCount;
3 8
 
9
+    Bins(int minRoll, int maxRoll) {
10
+        this.minRoll = minRoll;
11
+        this.maxRoll = maxRoll;
12
+
13
+        //create bins to fill; first int is roll amount, second will be number of rolls at that amount
14
+        this.binCount = maxRoll - minRoll + 1;
15
+        binArray = new int[binCount][2];
16
+        int binNumber = minRoll;
17
+
18
+        for (int i = 0; i < binCount; i++) {
19
+            binArray[i][0] = binNumber;
20
+            binNumber++;
21
+        }
22
+//        System.out.println(Arrays.deepToString(binArray));
23
+    }
24
+
25
+    public int getBin(int currentRoll) {
26
+        return binArray[currentRoll-minRoll][1];
27
+    }
28
+
29
+    public void incrementBin (int currentRoll) {
30
+        int currentValue = getBin(currentRoll);
31
+        currentValue++;
32
+        binArray[currentRoll-minRoll][1] = currentValue;
33
+    }
4 34
 }

バイナリ
src/main/java/Dice.class ファイルの表示


+ 24
- 1
src/main/java/Dice.java ファイルの表示

@@ -1,4 +1,27 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
4
+    public int dieCount;
5
+
6
+    Dice(int dieCount) {
7
+        this.dieCount = dieCount;
8
+    }
2 9
 
10
+    public int tossAndSum() {
11
+        Random random = new Random();
3 12
 
4
-}
13
+        int rollSum = 0;
14
+        for (int i = 0; i < dieCount; i++) {
15
+            int roll = random.nextInt(6)+1;
16
+            rollSum += roll;
17
+        }
18
+        return rollSum;
19
+    }
20
+//
21
+//    public static long test() {
22
+//        Dice die = new Dice(1);
23
+//        return die.tossAndSum();
24
+//    }
25
+//
26
+//    public static void main(String[] args) { System.out.println(test()); }
27
+}

+ 7
- 0
src/main/java/Main.java ファイルの表示

@@ -0,0 +1,7 @@
1
+public class Main {
2
+    public static void main(String[] args) {
3
+        Simulation sim = new Simulation(2, 1000000);
4
+        sim.runSimulation();
5
+        sim.printResults();
6
+    }
7
+}

+ 49
- 0
src/main/java/Simulation.java ファイルの表示

@@ -1,5 +1,54 @@
1
+import java.util.Arrays;
2
+
1 3
 public class Simulation {
2 4
 
5
+    int dieCount;
6
+    int rollCount;
7
+
8
+    int minRoll;
9
+    int maxRoll;
10
+
11
+    Bins bins;
12
+    Dice dice;
13
+
14
+    public Simulation (int dieCount, int rollCount) {
15
+        this.dieCount = dieCount;
16
+        this.minRoll = dieCount;
17
+        this.maxRoll = dieCount*6;
18
+        this.rollCount = rollCount;
19
+        this.bins = new Bins(minRoll, maxRoll);
20
+        this.dice = new Dice(dieCount);
21
+    }
22
+
23
+    public void runSimulation(){
24
+        for (int i = 0; i < rollCount; i++) {
25
+            int currentRoll = dice.tossAndSum();
26
+            bins.incrementBin(currentRoll);
27
+        }
28
+//        System.out.println(Arrays.deepToString(bins.binArray));
29
+    }
30
+
31
+    public void printResults() {
32
+        StringBuilder builder = new StringBuilder("***\nSimulation of " + dieCount + " dice tossed " + rollCount + " times.\n***\n\n");
33
+
34
+
35
+        // roll chart
36
+        for (int i = 0; i < bins.binCount; i++) {
37
+            // populate numbers and statistics for each
38
+            int binNumber = i + minRoll;
39
+            int rollValue = bins.getBin(binNumber);
40
+            float rollStat = (float) rollValue / rollCount;
41
+            builder.append(String.format("%3d :" + "%8d:" + " %.2f ", binNumber, rollValue, rollStat));
42
+            // create graph
43
+            int starCount = (int) (rollStat * 100);
44
+            for (int j = 0; j < starCount; j++) {
45
+                builder.append("*");
46
+            }
47
+            // next line
48
+            builder.append("\n");
49
+        }
3 50
 
51
+        System.out.println(builder);
52
+    }
4 53
 
5 54
 }

+ 97
- 0
src/main/test/DiceTest.java ファイルの表示

@@ -0,0 +1,97 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.util.Arrays;
5
+
6
+public class DiceTest {
7
+
8
+    @Test
9
+    public void tossAndSumTest0() {
10
+        Dice dice = new Dice(2);
11
+
12
+        long actualNumber = dice.tossAndSum();
13
+        boolean test2To12 = 1 < actualNumber && actualNumber < 13;
14
+
15
+        Assert.assertTrue(test2To12);
16
+        System.out.println(actualNumber);
17
+
18
+    }
19
+
20
+    @Test
21
+    public void tossAndSumTest1() {
22
+        Dice dice = new Dice(10);
23
+
24
+        long actualNumber = dice.tossAndSum();
25
+        boolean test10To60 = 9 < actualNumber && actualNumber < 61;
26
+
27
+        Assert.assertTrue(test10To60);
28
+    }
29
+
30
+    @Test
31
+    public void binArrayTest1() {
32
+        Bins bins = new Bins(3,18);
33
+
34
+        int expectedValue = (3);
35
+
36
+        Assert.assertEquals(expectedValue, bins.binArray[0][0]);
37
+    }
38
+
39
+    @Test
40
+    public void binArrayTest2() {
41
+        int maxRoll = 12;
42
+        int minRoll = 2;
43
+        Bins bins = new Bins(minRoll,maxRoll);
44
+
45
+        int expectedValue = maxRoll;
46
+        int bindex = maxRoll-minRoll;
47
+
48
+        Assert.assertEquals(expectedValue, bins.binArray[bindex][0]);
49
+    }
50
+
51
+    @Test
52
+    public void binArrayTest3() {
53
+        int maxRoll = 6;
54
+        int minRoll = 1;
55
+        Bins bins = new Bins(minRoll,maxRoll);
56
+
57
+        int expectedValue = maxRoll;
58
+        int bindex = bins.binArray.length-1;
59
+
60
+        Assert.assertEquals(expectedValue, bins.binArray[bindex][0]);
61
+    }
62
+
63
+    @Test
64
+    public void getBinTest1() {
65
+        Bins bins = new Bins(3,18);
66
+
67
+        int expectedValue = (0);
68
+        int actualValue = bins.getBin(3);
69
+
70
+        Assert.assertEquals(expectedValue, actualValue);
71
+    }
72
+
73
+    @Test
74
+    public void getBinTest2() {
75
+        Bins bins = new Bins(2,12);
76
+
77
+        int expectedValue = (0);
78
+        int actualValue = bins.getBin(12);
79
+
80
+        Assert.assertEquals(expectedValue, actualValue);
81
+    }
82
+
83
+    @Test
84
+    public void incrementBinTest1() {
85
+        Bins bins = new Bins(3,18);
86
+
87
+        int expectedValue = (1);
88
+
89
+        bins.incrementBin(3);
90
+        int actualValue = bins.getBin(3);
91
+
92
+//        System.out.println(Arrays.deepToString(bins.binArray));
93
+
94
+        Assert.assertEquals(expectedValue, actualValue);
95
+    }
96
+
97
+}