diff --git a/pom.xml b/pom.xml index 7219542..d202c52 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,26 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + RELEASE + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..11fec90 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,23 @@ +import java.util.Arrays; public class Bins { + static Integer[] values; + private final Integer min; + private final Integer max; + + public Bins (Integer min, Integer max) { + this.min = min; + this.max = max; + this.values = new Integer[(max - min) + 1]; + // Arrays.fill(values, 0); + for (int i = 0; i < values.length; i++) { + values[i] = 0; + } + } + + public void incrementBin(int binIndex) { + int index = binIndex - min; + values[index]++; + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..07dfe18 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,57 @@ +import java.util.ArrayList; +import java.util.List; + public class Dice { + private ArrayList rollingResults; + private Integer numOfDice = 2; + private Integer sumOfDice; + + public Dice (Integer countOfDice) { + this.rollingResults = new ArrayList<>(); + for (int i = 0; i < countOfDice; i++) { + this.rollingResults.add((int) ((Math.random() * (7 - 1)) + 1)); + } + } + + public ArrayList getRollingResults() { + return rollingResults; + } + + public void setRollingResults(ArrayList rollingResults) { + this.rollingResults = rollingResults; + } + + public Integer tossAndSum () { + Integer sum = 0; + for (int i = 0; i < rollingResults.size(); i++) { + rollingResults.set(i, (int) ((Math.random() * (7 - 1)) + 1)); + sum += rollingResults.get(i); + } + return sum; + } + +// public void initializeDiceList () { +// Integer numOfDice = getNumOfDice(); +// List rollingResults = this.getRollingResults(); +// for (int i = 0; i < numOfDice; i++) { +// rollingResults.add(0); +// } + + + public Integer getNumOfDice() { + return numOfDice; + } + + public void setNumOfDice(Integer numOfDice) { + this.numOfDice = numOfDice; + } + + public Integer getSumOfDice() { + return sumOfDice; + } + public void setSumOfDice(Integer sumOfDice) { + this.sumOfDice = sumOfDice; + } } diff --git a/src/main/java/MainApplication.java b/src/main/java/MainApplication.java new file mode 100644 index 0000000..7aae62c --- /dev/null +++ b/src/main/java/MainApplication.java @@ -0,0 +1,5 @@ +public class MainApplication { + public static void main(String[] args) { + + } +} diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..122b51d 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,57 @@ +import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput; + +import java.io.PrintStream; +import java.util.Arrays; + public class Simulation { + public static void main(String[] args) { + run(2, 1000000); + } + + private Dice dice; + private Bins bin; + private Integer numOfToss; + private Integer numOfDice; + private Integer min; + private Integer max; + + public Simulation(Integer numOfDice, Integer numOfToss) { + this.numOfToss = numOfToss; + this.numOfDice = numOfDice; + this.min = numOfDice; + this.dice = new Dice(numOfDice); + this.bin = new Bins(numOfDice, numOfDice * 6); + } + public static void run (Integer numOfDice, Integer numOfToss) { + Dice dice = new Dice(numOfDice); + Bins bin = new Bins(numOfDice, numOfDice * 6); + for (int i = 0; i < numOfToss; i++) { + dice.tossAndSum(); + bin.incrementBin(dice.tossAndSum()); + } +// System.out.println(Arrays.toString(Bins.values)); +// System.out.println("2's: " + Bins.values[0] + " : %.2d"); + printer(); + } -} + public static void printer () { + String jawner = ""; + String jawnski = ""; + for (int i = 0; i < Bins.values.length; i++) { + if (i < 3) { + jawnski = " "; + } else { + jawnski = ""; + } + Double result = Bins.values[i] / 1000000.0; + jawner += (i + 2) + ": " + Bins.values[i] + jawnski + " : " + String.format("%.2f", result) + " "; + for (int j = 0; j < (result * 100) - 1; j++) { + jawner += "*"; + } + jawner += "\n"; + } + System.out.println(jawner); + } +} \ No newline at end of file diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..2aa3aaf --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,32 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class BinsTest { + @Test + public void testBinConstructor () { + int testMin = 2; + int testMax = 12; + int expected = testMax - testMin; + + Bins test = new Bins(testMin, testMax); + int actualLength = test.values.length; + + Assert.assertEquals(expected, actualLength); + } + + @Test + public void testBinConstructor1 () { + int testMin = 2; + int testMax = 12; + int expected = testMax - testMin; + + Bins test = new Bins(testMin, testMax); + int actualLength = test.values.length; + int actualValue = test.values[1]; + + Assert.assertEquals(expected, actualLength); + Assert.assertEquals(0, actualValue); + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..ff937a8 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,19 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class DiceTest { + + @Test + public void testDiceConstructor () { + int numOfDie = 2; + Dice dice = new Dice(numOfDie); + + Integer testInt = dice.tossAndSum(); + + System.out.println(testInt); + Assert.assertTrue(testInt < 13 && testInt > 1); + } +} diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java new file mode 100644 index 0000000..5b6e99e --- /dev/null +++ b/src/test/java/SimulationTest.java @@ -0,0 +1,5 @@ +import org.junit.Test; + +public class SimulationTest { + +}