diff --git a/pom.xml b/pom.xml index 7219542..35793d4 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 + + 11 + 11 + + + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..642f898 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,87 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; public class Bins { + private List results = new ArrayList<>(); + private Integer min; + private Integer max; + + public Bins(Integer min, Integer max){ + this.min = min; + this.max = max; + List resultsList = this.getResults(); + for(int i = min; i <= max; i++){ + resultsList.add(0); + } + } + + public List getResults() { + return results; + } + + public Integer getBin(Integer binNumber){ + + return this.getResults().get(binNumber); + } + + public void incrementBin(Integer binNumber){ + if(binNumber >= this.min && binNumber <= this.max){ + Integer binIndex = binNumber - this.min; + Integer currentBinResult = this.getBin(binIndex); + this.results.set(binIndex, currentBinResult + 1); + } + + + } + + public List tallyResults(Integer numberOfTosses){ + List percentages = new ArrayList(); + for(int i = 0; i < results.size(); i++){ + double percentage = (double)results.get(i) / numberOfTosses; + BigDecimal bigDecimal = new BigDecimal(Double.toString(percentage)); + bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP); + percentages.add(bigDecimal.doubleValue()); + } + return percentages; + } + + public void printResults(Integer numberOfTosses){ + List talliedResultList = tallyResults(numberOfTosses); + Integer count = this.min; + Integer i = 0; + String printout = ""; + int numberOfStars = 0; + for(Double element : talliedResultList) { + numberOfStars = (int)(element * 100); + i = 1; + printout += String.format("%3d", count) + ": " + String.format("%7d", this.getBin(count - 2)) + + " " + String.format("%1.2f", element) + " "; + while(i < numberOfStars){ + printout += "*"; + i++; + } + count++; + printout += "\n"; + } + System.out.println(printout); + } + + public Integer getMin() { + return min; + } + + public void setMin(Integer min) { + this.min = min; + } + + public Integer getMax() { + return max; + } + + public void setMax(Integer max) { + this.max = max; + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..0f90d61 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,71 @@ +import java.util.ArrayList; +import java.util.List; + public class Dice { + private List diceContainer = new ArrayList(); + private Integer numberOfDice; + private Integer sumOfDice; + + public Dice(Integer numberOfDice){ + this.numberOfDice = numberOfDice; + } + + public Dice(){ + this.numberOfDice = 2; + } + + public Integer getNumberOfDice(){ + return this.numberOfDice; + } + + public void setNumberOfDice(Integer numberOfDice){ + this.numberOfDice = numberOfDice; + } + + public List getDiceList(){ + return this.diceContainer; + } + + public Integer getSumOfDice(){ + return this.sumOfDice; + } + + public void initializeDiceList(){ + Integer numberOfDice = this.getNumberOfDice(); + List diceList = this.getDiceList(); + for(int i = 0; i < numberOfDice; i++){ + diceList.add(0); + } + } + + + + public void tossDice(){ + Integer min = 1; + Integer max = 7; + List diceList = this.getDiceList(); + Integer length = diceList.size(); + for(int i = 0; i < length; i++){ + diceList.set(i, (int)(Math.random() * (max - min)) + min); + } + } + + public Integer tossAndSum(){ + Integer sum = 0; + this.tossDice(); + List diceList = this.getDiceList(); + for(int i = 0; i < diceList.size(); i++){ + sum += diceList.get(i); + } + this.sumOfDice = sum; + return sum; + } + public void printDice(){ + List diceList = this.getDiceList(); + for(int i = 0; i < diceList.size(); i++){ + System.out.println((i + 1) + ": " + diceList.get(i)); + } + } } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..cf31d86 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,23 @@ +import java.util.List; + public class Simulation { + public static void main(String[] args) { + runSimXTimes(2, 1000000); + + } + + public static void runSimXTimes(Integer numberOfDice, Integer numberOfTosses){ + Dice testDice = new Dice(numberOfDice); + Bins testBin = new Bins(testDice.getNumberOfDice(), testDice.getNumberOfDice() * 6); + testDice.initializeDiceList(); + + for(int i = 0; i < numberOfTosses; i++){ + testBin.incrementBin(testDice.tossAndSum()); + } + testBin.printResults(numberOfTosses); + System.out.println("testline"); + } }