From e040420d9efeb7c4e94f69f12d34fb035bc86b0b Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 9 Jul 2021 17:36:01 -0400 Subject: [PATCH 1/2] Current workingish state --- pom.xml | 8 +++++++ src/main/java/Bins.java | 45 ++++++++++++++++++++++++++++++++++++- src/main/java/Dice.java | 41 +++++++++++++++++++++++++++++++++ src/test/java/BinsTest.java | 19 ++++++++++++++++ src/test/java/DiceTest.java | 33 +++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/test/java/BinsTest.java create mode 100644 src/test/java/DiceTest.java diff --git a/pom.xml b/pom.xml index 7219542..15e9f23 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + 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..07bfe04 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,47 @@ +import java.util.ArrayList; +import java.util.List; public class Bins { - + Dice dice = new Dice(); + // List savedDataOnRoll = new ArrayList(dice.diceContainerForCraps); + Integer bin2 = 0; + Integer bin3 = 0; + Integer bin4 = 0; + Integer bin5 = 0; + Integer bin6 = 0; + Integer bin7 = 0; + Integer bin8 = 0; + Integer bin9 = 0; + Integer bin10 = 0; + Integer bin11 = 0; + Integer bin12 = 0; + public void allocatingValues () { + dice.numberOfRolls = 1000000; + List theList = dice.tossAndSumForCraps(2, dice.numberOfRolls); + for (int i = 0; i < theList.size(); i++) { + if (theList.get(i).equals(2)) { + bin2++; + } else if (theList.get(i).equals(3)) { + bin3++; + } else if (theList.get(i).equals(4)) { + bin4++; + } else if (theList.get(i).equals(5)) { + bin5++; + } else if (theList.get(i).equals(6)) { + bin6++; + } else if (theList.get(i).equals(7)) { + bin7++; + } else if (theList.get(i).equals(8)) { + bin8++; + } else if (theList.get(i).equals(9)) { + bin9++; + } else if (theList.get(i).equals(10)) { + bin10++; + } else if (theList.get(i).equals(11)) { + bin11++; + } else { + bin12++; + } + } + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..bed4e04 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,45 @@ +import java.util.ArrayList; +import java.util.List; + public class Dice { + Integer numberOfRolls; + Integer numberOfDice; + List diceContainerForCraps = new ArrayList(); + + public Dice (Integer numberOfDice, Integer numberOfRolls) { + this.numberOfRolls = numberOfRolls; + this.numberOfDice = numberOfDice; + } + public Dice () { + } + + public List tossAndSumForCraps (Integer numberOfDice, Integer numberOfRolls) { + Integer result = 0; + List diceContainer = this.getDiceContainerForCraps(); + for (int i = 0; i < numberOfRolls; i++) { + Integer dice1 = (int) (Math.random() * (7 - 1) + 1); + Integer dice2 = (int) (Math.random() * (7 - 1) + 1); + result = dice1 + dice2; + diceContainer.add(i, result); + } + // diceContainer.addAll(diceContainerForCraps); + setDiceContainerForCraps(diceContainer); + return diceContainerForCraps; + } + + public Integer getNumberOfRolls() { + return this.numberOfRolls; + } + + public Integer getNumberOfDice() { + return this.numberOfRolls; + } + public List getDiceContainerForCraps() { + return diceContainerForCraps; + } + public void setDiceContainerForCraps(List diceContainerForCraps) { + this.diceContainerForCraps = diceContainerForCraps; + } } diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..3725feb --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,19 @@ +import org.junit.Test; + +import java.util.List; + +public class BinsTest { + + @Test + public void generatingBinsTest () { + Dice dice = new Dice(); + Bins bins = new Bins(); + + bins.allocatingValues(); + + System.out.println(bins.bin2 + "\n" + bins.bin3 + "\n" + bins.bin4 + "\n" + bins.bin5 + "\n" + + bins.bin6 + "\n" + bins.bin7 + "\n" + bins.bin8 + "\n" + bins.bin9 + "\n" + bins.bin10 + "\n" + + bins.bin11 + "\n" + bins.bin12); + } + +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..5c3bc2c --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,33 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class DiceTest { + + @Test + public void diceConstructorTest () { + // Given + Integer expectedRolls = 20; + Dice dice = new Dice(2, 20); + + // When + Integer actualRolls = dice.getNumberOfRolls(); + + // Then + Assert.assertEquals(expectedRolls, actualRolls); + } + + @Test + public void sumForCrapsInList () { + List diceContainerForCraps = new ArrayList(); + Integer numberOfRolls = 20; + Dice dice = new Dice(2, numberOfRolls); + + List result = dice.tossAndSumForCraps(2, numberOfRolls); + + // System.out.println(result); + System.out.println(dice.diceContainerForCraps); + } +} From 6be98dc2d2819d877d7770c66ced67546a184807 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 11 Jul 2021 20:25:02 -0400 Subject: [PATCH 2/2] Finished the lab --- pom.xml | 12 ++++++ src/main/java/Bins.java | 60 +++++++++------------------- src/main/java/Dice.java | 64 ++++++++++++++++++------------ src/main/java/MainApplication.java | 5 +++ src/main/java/Simulation.java | 54 ++++++++++++++++++++++++- src/test/java/BinsTest.java | 29 ++++++++++---- src/test/java/DiceTest.java | 26 +++--------- src/test/java/SimulationTest.java | 5 +++ 8 files changed, 158 insertions(+), 97 deletions(-) create mode 100644 src/main/java/MainApplication.java create mode 100644 src/test/java/SimulationTest.java diff --git a/pom.xml b/pom.xml index 15e9f23..d202c52 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + junit diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index 07bfe04..11fec90 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,47 +1,23 @@ -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; public class Bins { - Dice dice = new Dice(); - // List savedDataOnRoll = new ArrayList(dice.diceContainerForCraps); - Integer bin2 = 0; - Integer bin3 = 0; - Integer bin4 = 0; - Integer bin5 = 0; - Integer bin6 = 0; - Integer bin7 = 0; - Integer bin8 = 0; - Integer bin9 = 0; - Integer bin10 = 0; - Integer bin11 = 0; - Integer bin12 = 0; - public void allocatingValues () { - dice.numberOfRolls = 1000000; - List theList = dice.tossAndSumForCraps(2, dice.numberOfRolls); - for (int i = 0; i < theList.size(); i++) { - if (theList.get(i).equals(2)) { - bin2++; - } else if (theList.get(i).equals(3)) { - bin3++; - } else if (theList.get(i).equals(4)) { - bin4++; - } else if (theList.get(i).equals(5)) { - bin5++; - } else if (theList.get(i).equals(6)) { - bin6++; - } else if (theList.get(i).equals(7)) { - bin7++; - } else if (theList.get(i).equals(8)) { - bin8++; - } else if (theList.get(i).equals(9)) { - bin9++; - } else if (theList.get(i).equals(10)) { - bin10++; - } else if (theList.get(i).equals(11)) { - bin11++; - } else { - bin12++; - } + + 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 bed4e04..07dfe18 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -2,44 +2,56 @@ import java.util.List; public class Dice { - Integer numberOfRolls; - Integer numberOfDice; - List diceContainerForCraps = new ArrayList(); - public Dice (Integer numberOfDice, Integer numberOfRolls) { - this.numberOfRolls = numberOfRolls; - this.numberOfDice = numberOfDice; + 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 Dice () { + + public ArrayList getRollingResults() { + return rollingResults; } - public List tossAndSumForCraps (Integer numberOfDice, Integer numberOfRolls) { - Integer result = 0; - List diceContainer = this.getDiceContainerForCraps(); - for (int i = 0; i < numberOfRolls; i++) { - Integer dice1 = (int) (Math.random() * (7 - 1) + 1); - Integer dice2 = (int) (Math.random() * (7 - 1) + 1); - result = dice1 + dice2; - diceContainer.add(i, result); + 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); } - // diceContainer.addAll(diceContainerForCraps); - setDiceContainerForCraps(diceContainer); - return diceContainerForCraps; + return sum; } - public Integer getNumberOfRolls() { - return this.numberOfRolls; +// 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 Integer getNumberOfDice() { - return this.numberOfRolls; + public void setNumOfDice(Integer numOfDice) { + this.numOfDice = numOfDice; } - public List getDiceContainerForCraps() { - return diceContainerForCraps; + public Integer getSumOfDice() { + return sumOfDice; } - public void setDiceContainerForCraps(List diceContainerForCraps) { - this.diceContainerForCraps = diceContainerForCraps; + 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 index 3725feb..2aa3aaf 100644 --- a/src/test/java/BinsTest.java +++ b/src/test/java/BinsTest.java @@ -1,19 +1,32 @@ +import org.junit.Assert; import org.junit.Test; import java.util.List; public class BinsTest { - @Test - public void generatingBinsTest () { - Dice dice = new Dice(); - Bins bins = new Bins(); + public void testBinConstructor () { + int testMin = 2; + int testMax = 12; + int expected = testMax - testMin; - bins.allocatingValues(); + Bins test = new Bins(testMin, testMax); + int actualLength = test.values.length; - System.out.println(bins.bin2 + "\n" + bins.bin3 + "\n" + bins.bin4 + "\n" + bins.bin5 + "\n" + - bins.bin6 + "\n" + bins.bin7 + "\n" + bins.bin8 + "\n" + bins.bin9 + "\n" + bins.bin10 + "\n" + - bins.bin11 + "\n" + bins.bin12); + 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 index 5c3bc2c..ff937a8 100644 --- a/src/test/java/DiceTest.java +++ b/src/test/java/DiceTest.java @@ -7,27 +7,13 @@ public class DiceTest { @Test - public void diceConstructorTest () { - // Given - Integer expectedRolls = 20; - Dice dice = new Dice(2, 20); + public void testDiceConstructor () { + int numOfDie = 2; + Dice dice = new Dice(numOfDie); - // When - Integer actualRolls = dice.getNumberOfRolls(); + Integer testInt = dice.tossAndSum(); - // Then - Assert.assertEquals(expectedRolls, actualRolls); - } - - @Test - public void sumForCrapsInList () { - List diceContainerForCraps = new ArrayList(); - Integer numberOfRolls = 20; - Dice dice = new Dice(2, numberOfRolls); - - List result = dice.tossAndSumForCraps(2, numberOfRolls); - - // System.out.println(result); - System.out.println(dice.diceContainerForCraps); + 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 { + +}