diff --git a/diceRollResults.txt b/diceRollResults.txt new file mode 100644 index 0000000..d16bc72 --- /dev/null +++ b/diceRollResults.txt @@ -0,0 +1,11 @@ + 2 : 27650: 0.03 ** + 3 : 55639: 0.06 ***** + 4 : 83133: 0.08 ******** + 5 : 111209: 0.11 *********** + 6 : 139205: 0.14 ************* + 7 : 166772: 0.17 **************** + 8 : 139358: 0.14 ************* + 9 : 110858: 0.11 *********** +10 : 83034: 0.08 ******** +11 : 55398: 0.06 ***** +12 : 27744: 0.03 ** \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7219542..1121264 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,43 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT - + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + org.junit.jupiter + junit-jupiter-engine + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..b7dab41 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,31 @@ +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; public class Bins { -} + Integer[] bins; + Integer min; + Integer max; + + public Bins(Integer min, Integer max) { + bins = new Integer[max - min + 1]; + this.min = min; + this.max = max; + Arrays.fill(bins, 0); + } + + // looking for one index array [6] + public Integer getBinValue(Integer diceRoll) { + + return bins[diceRoll - min]; + } + + public Integer[] getBins() { + return bins; + } + + public Integer incrementBins(Integer diceRoll) { + return bins[diceRoll - min] ++; + } +} \ No newline at end of file diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..8512dd2 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,53 @@ public class Dice { + // private static final int MAX_VALUE = 6; + private int numberOfDie; + int[] allTossedDice; + + + public static void main(String[] args) { + Dice die = new Dice(1); + + Integer toss = die.tossAndSum(); + + System.out.println(toss); + + } + + public Dice(int numberOfDice) { + numberOfDie = numberOfDice; + } + + public int tossAndSum() { + int sumOfDice = 0; + for (int i = 0; i < numberOfDie; i++) { + sumOfDice += rollDie(); + } + + return sumOfDice; + } + + public int rollDie() { + + return (int) (Math.random() * 6 + 1); + } + + public int getNumberOfDie() { + return numberOfDie; + } + + public void setNumberOfDie(int numberOfDie) { + this.numberOfDie = numberOfDie; + } + +/* + public String toString() + { + String result = Integer.toString(faceValue); + return result; + } +*/ + + } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..bca6ded 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,50 @@ public class Simulation { + Integer numberOfDice; + Integer numberOfTosses; + Dice dice; + Bins bins; + public Simulation(Integer numberOfDice, Integer numberOfTosses) { + this.numberOfDice = numberOfDice; + this.numberOfTosses = numberOfTosses; + } + public void runSimulation() { + this.dice = new Dice(2); + this.bins = new Bins(2, 12); + + for (int i = 0; i < numberOfTosses; i++) { + Integer sumUp = dice.tossAndSum(); + bins.incrementBins(sumUp); + } + System.out.println(""); + } + + public void printResults() { + for(Integer i = 2; i <= 12; i++) { + Double percentage = (double) bins.getBinValue(i) / numberOfTosses; + System.out.printf("%2d : %7d: %.2f ", i, bins.getBinValue(i), percentage); + for(int j = 1; j < percentage * 100; j++) + { + System.out.print("*"); + } + System.out.println(); + } + } + + + public double getPercentage(Integer numberOfTosses, Bins bins) { + double percentage = 0; + for( int element : bins.getBins()) { + percentage = element/ numberOfTosses; + } + return percentage; + } + + public static void main(String[] args) { + Simulation simulation = new Simulation(2, 1000000); + simulation.runSimulation(); + simulation.printResults(); + } } diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..1f8c107 --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,16 @@ +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class BinsTest { + @Test + public void testBin1() { + + + + } + +} \ No newline at end of file diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..beedb10 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,17 @@ +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +public class DiceTest { + + @Test + public void testGetNumberOfDice() { + int expected = 2; + + Dice die = new Dice(expected); + int actual = die.getNumberOfDie(); + + Assertions.assertEquals(expected, actual); + + } +}