lundi 11 février 2019

That is a dice game and I am using a testfile to check and cant seem to know how to fix the errors , mentioned at the bottom of my article

import java.util.Random;

public class Zork {

public int DICE_IN_HAND = 6;

public static int rollDice() {
    return (new Random()).nextInt(6) + 1;
}

public static int[] rollHand(int times) {
    int dice[] = new int[times];
    for (int i = 0; i < dice.length; i++) {
        dice[i] = rollDice();
    }
    return dice;
}

/**
 * count the occurrence of each side of the dice (0 to 5) where 0 is the
 * side that has 1 on it and 5 is the side that has 6
 * 
 * @param roll
 *            an array or dice rolls
 * @return and array of count for each side of dice
 */
public static int[] countDice(int[] roll) {
    int[] count = new int[6];
    int i = 0;

    while (i < roll.length) {
        count[roll[i]]++;
        i++;
    }
    return count;
}

public static int getScore(int[] count) {
    int points = 0;
    // for every 3 ones you get 1000 points

    while(count[0] >= 3) {
        count[0] -= 3;
        points += 1000;
    }
    int i = 1;
    // for every three of anything else you get 100 * the dice
    while (i < count.length) {
        if (count[i] >= 3) {
            points += (i + 1) * 100;
            count[i] -= 3;
        }
        i++;
    }
    // for every 5 after removing all 3s you should get 50 points
    points = points + count[4] * 50;
    // for every 1 after removing all 3s you should get 100 points
    points = points + count[0] * 100;

    return points;
}

public static void main(String[] args) {

    int x = 3;
    System.out.println("x is" + x);
    int roll = Zork.rollDice();
    System.out.println("Roll dice and you get " + roll);

    // Hint: Needed for debuggibg the first bug
       //       int[] dice = Zork.rollHand(7);

     int[] dice = Zork.rollHand(8);
     int[] count = Zork.countDice(dice);
     int score = Zork.getScore(count);
     System.out.println(score);
}
       }

and that is for the test case :

 import static org.junit.Assert.*;
  import lab0.Zork;

    import org.junit.Test;


 public class ZorkPublicTest {

@Test
public void rollingDice() {
    int dice = Zork.rollDice();
    int high = 6;
    int low = 1;
    assertTrue("Error, dice is to high", high >= dice);
    assertTrue("Error, dice is to low", low <= dice);
}

@Test
public void rollSevenDice() {
    int[] dice = Zork.rollHand(7);
    assertEquals("roll Hand should roll seven dice", 7, dice.length);
}
@Test
public void rollSixDice() {
    int[] dice = Zork.rollHand(6);
    assertEquals("roll Hand should roll six dice", 6, dice.length);
}

@Test
public void countDice() {
    int[] dice = {1,2,4,4,5,5};
    int[] count = {1,1,0,2,2,0};

    assertArrayEquals("Should Count dice", count, Zork.countDice(dice));
}

@Test
public void computeScoreWithThreeOnes() {
    int[] dice = {1,1,1,2,4,4,5,5};
    int[] count = Zork.countDice(dice);
    assertEquals("Should compute score based on dice", 1100, Zork.getScore(count));
}

@Test
public void computeScoreWithAllSixOnes() {
    int[] dice = {1,1,1,1,1,1};
    int[] count = Zork.countDice(dice);
    assertEquals("Should compute score based on dice", 2000, Zork.getScore(count));
}

@Test
public void computeScoreWithThreeFours() {
    int[] dice = {4,4,4,6,3,2};
    int[] count = Zork.countDice(dice);
    assertEquals("Should compute score based on dice", 400, Zork.getScore(count));
}

@Test
public void computeScoreWithAllFiveFive() {
    int[] dice = {5,5,5,5,5,2};
    int[] count = Zork.countDice(dice);
    assertEquals("Should compute score based on dice", 600,    Zork.getScore(count));
}
           }

There Are error that appear at

  1. the testcase computeScoreWithThreeOnes method that it's excpected 1100 , but the answer is 300

    1. the testcase computerScoreWithThreeFours : Arrayindexoutofbound : 6

    2. the testcase computerScoreWithAllSixones : should compute 2000 but its

    3. 200 the testcase contDice :Array first element was expected 1 but is zero.

Aucun commentaire:

Enregistrer un commentaire