-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidityTester.java
88 lines (82 loc) · 3.03 KB
/
ValidityTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* This class tests whether the {@link ScoreRow#isValid(Dice)} method is
* accurate. By iterating though all dice combinations
* ({@link #diceCombinations(int)}) and determining the {@link #validDiceCount}
* for each row in {@link ScoreSheet}. It then compares the
* {@link #validDiceCount} values to {@link #correctCounts} to determine if
* {@link ScoreRow#isValid(Dice)} is correct for all scoring rows.
*
* @see ScoreRow#isValid(Dice)
* @see Yahtzee#SCORE_SHEET_ROWS
* @author Arjun Subramanian
*/
public class ValidityTester {
private static final int[] correctCounts = new int[] {
7776, 7776, 7776, 7776, 7776, 7776,
1500, 150, 300, 1200, 240, 6, 7776 };
private int[] validDiceCount;
private ScoreSheet ss;
private Dice dice;
/**
* Construct a {@link ValidityTester} to check whether
* {@link ScoreRow#isValid(Dice)} method is accurate.
*
* @see ScoreRow#isValid(Dice)
*/
public ValidityTester() {
validDiceCount = new int[Yahtzee.SCORE_SHEET_ROWS];
ss = new ScoreSheet();
dice = new Dice();
diceCombinations(0);
}
/**
* For a particular {@link #dice}, update the
* {@link #validDiceCount} for all {@link Yahtzee#SCORE_SHEET_ROWS}.
* Called by {@link #diceCombinations()} to count for all combinations.
*
* @see ScoreRow#isValid(Dice)
*/
private void updateCounter() {
for (int i = 0; i < Yahtzee.SCORE_SHEET_ROWS; i++)
if (ss.get(i).isValid(dice))
validDiceCount[i]++;
}
/**
* Recurse though all dice combinations and call {@link #updateCounter()}.
*
* @param depth The depth of the recursion, or the die number being changed.
* @see #updateCounter()
*/
private void diceCombinations(int depth) {
if (depth == Yahtzee.DICE_COUNT)
updateCounter();
else
// loop though all values of the dice
for (int i = 1; i <= Yahtzee.DICE_SIDES; i++) {
dice.getDie(depth).setValue(i);
diceCombinations(depth + 1);
}
}
/**
* Checks that the {@link ScoreRow#isValid(Dice)} is correct
* by comparing {@link #validDiceCount} to {@link #correctCounts}.
*
* @return Whether or not {@link ScoreRow#isValid(Dice)} is correct.
*/
public boolean isValid() {
boolean valid = true;
for (int i = 0; i < Yahtzee.SCORE_SHEET_ROWS && valid; i++)
valid = validDiceCount[i] == correctCounts[i];
return valid;
}
/**
* Print out the number of {@link #validDiceCount} for each {@link ScoreRow}.
*/
public void display() {
System.out.println("Number of valid dice combinations for each scoring type");
for (int i = 0; i < Yahtzee.SCORE_SHEET_ROWS; i++)
// print each scoring row's validCount vs. what is expected
System.out.println(String.format("%s: %d (expected %d)",
ss.get(i).getName(), validDiceCount[i], correctCounts[i]));
}
}