#36 Completed Dicey

開啟中
pchai7 請求將 1 次程式碼提交從 pchai7/ZCW-DiceyLab:master 合併至 master

+ 8
- 0
pom.xml 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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);
         }

      }

二進制
src/test/java/.DS_Store 查看文件


+ 1
- 0
src/test/java/BinsTest.java 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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);
    }
}