#36 Completed Dicey

Open
pchai7 wants to merge 1 commits from pchai7/ZCW-DiceyLab:master into master

+ 8
- 0
pom.xml View File

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

+ 1
- 4
src/main/java/Bins.java View File

@@ -1,4 +1 @@
1
-
2
-public class Bins {
3
-
4
-}
1
+import java.util.HashMap;

public class Bins {
    private int numOfDiceRolled;
    private int maxVal;

    public static HashMap<Integer, Integer> binStore = new HashMap<Integer, Integer>();

    public Bins(int numOfDiceRolled, int maxVal) {
        this.numOfDiceRolled = numOfDiceRolled;
        this.maxVal = maxVal;


        for (int i = numOfDiceRolled; i <= maxVal; i++) {
            binStore.put(i, 0);
        }
    }

    public int getBin(int binNum){
            return binStore.get(binNum);
        }
     public int count(int binNum){
         int current = getBin(binNum);
            current += 1;
          binStore.put(binNum, current);
          return binStore.get(binNum);
    }

    public int getNumOfDice() {
        return numOfDiceRolled;
    }

    public int getMaxVal(){
        return maxVal;
     }

}

+ 1
- 4
src/main/java/Dice.java View File

@@ -1,4 +1 @@
1
-public class Dice {
2
-
3
-
4
-}
1
+import java.math.*;


public class Dice {
    private int numDiceRolled;

    public Dice(int num) {
        this.numDiceRolled = num;
    }

    public int diceRoll() {
        int sumOfRoll = 0;
        for (int i = 0; i < numDiceRolled; i++) {
            sumOfRoll += (int) ((6 * Math.random()) + 1);
        }


        return sumOfRoll;
    }

}

+ 1
- 5
src/main/java/Simulation.java View File

@@ -1,5 +1 @@
1
-public class Simulation {
2
-
3
-
4
-
5
-}
1
+
public class Simulation{
    private int numOfDice;
    private int numOfThrows;
    public static void main(String[] args){
        Simulation sim = new Simulation(2, 1000000);
        Bins bin1 = sim.statusOfSim();
        sim.print(bin1);
        }

     public Simulation(int numOfDice, int numOfThrows){
        this.numOfDice = numOfDice;
        this.numOfThrows = numOfThrows;
        }

        public int getTotal(Bins binStore){
        int tot = 0;
        for(int i= binStore.getNumOfDice(); i <= binStore.getMaxVal(); i++){
           tot += binStore.getBin(i);

        }
        return tot;
        }

        public Bins statusOfSim(){
        Dice dice = new Dice(2);
        Bins binStore = new Bins(2,12);

       for(int i = 0; i < numOfThrows ; i++) {
           int output = dice.diceRoll();
           binStore.count(output);
       }
         return binStore;
       }

     public void print(Bins inBin){
     StringBuilder build = new StringBuilder();
         int binTotal = getTotal(inBin);
       StringBuilder starbuilder = new StringBuilder();
         String totalSims = String.format("Simulation of %d dice rolled %d times \n", + numOfDice, numOfThrows);
             build.append(totalSims);
      for(int i = inBin.getNumOfDice(); i <= inBin.getMaxVal(); i++){
         int currentVal = inBin.getBin(i);
         float percent = (float)currentVal/binTotal;
        for(int q = 0; q <= (int)(percent); q++){
               starbuilder.append("*");
        }

        String finalOutput = String.format(" %3d : %7d %.2f %s \n", +
                  i, currentVal, percent, starbuilder);
          build.append(finalOutput);
         }

         System.out.println(build);
         }

      }

BIN
src/test/java/.DS_Store View File


+ 1
- 0
src/test/java/BinsTest.java View File

@@ -0,0 +1 @@
1
+import org.junit.Test;
import static org.junit.Assert.*;


public class BinsTest {

    @Test
    public void binHashTest() {
        Bins bins = new Bins(20,185);
        int expected = bins.getBin(20);

        assertEquals(expected,0);
    }

    @Test
    public void binHashTest2() {
        Bins bins = new Bins(40,221);
        int expected = bins.getBin(40);

        assertEquals(expected,0);
    }

    @Test
   public void binIncrementTest(){
        Bins bins = new Bins(20,185);
        int expected = bins.count(20);
        assertEquals(expected,1);
    }
}

+ 1
- 0
src/test/java/DiceTest.java View File

@@ -0,0 +1 @@
1
+import org.junit.Test;
import static org.junit.Assert.*;


public class DiceTest {
    private Dice dice;


    @Test
    public void diceTestCraps() {
        Dice dice = new Dice(2);
        int min = 2;
        int max = 12;

        int actual = dice.diceRoll();


        assertTrue(min <= actual && actual <= max);

    }

    @Test
     public void diceTestYahtzee() {
        Dice dice = new Dice(5);
        int min = 5;
        int max = 27;
        //int expected = 27;

        int actual = dice.diceRoll();

        assertTrue(min <= actual && actual <= max);
    }

}

+ 1
- 0
src/test/java/SimulationTest.java View File

@@ -0,0 +1 @@
1
+import org.junit.Test;

import static junit.framework.TestCase.assertNotNull;

public class SimulationTest {
    private  Simulation sim;

    @Test
    public void simTest() {
        Simulation sim = new Simulation(2,100000);

        Bins actual = sim.statusOfSim();
        assertNotNull(actual);
    }
}