-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullHouseRow.java
59 lines (52 loc) · 1.87 KB
/
FullHouseRow.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
/**
* {@link FullHouseRow} extends {@link ScoreRow} for the "Full House" category
* and implements the {@link #isValid(Dice)} function.
*
* @see Yahtzee#FULL_HOUSE_HIGH_COUNT
* @see Yahtzee#FULL_HOUSE_LOW_COUNT
* @see FixedAmountRow
* @see ScoreRow
* @author Arjun Subramanian
*/
public class FullHouseRow extends FixedAmountRow {
/**
* Construct scoring row for the Full House category.
*
* @param name The scoring row's name.
*/
public FullHouseRow(String name) {
super(name, Yahtzee.FULL_HOUSE_SCORE);
}
/**
* {@inheritDoc} {@link FullHouseRow} is valid if there are
* {@link Yahtzee#FULL_HOUSE_HIGH_COUNT} of one number and
* {@link Yahtzee#FULL_HOUSE_LOW_COUNT} of the other.
*
* @see ScoreRow#isValid(Dice)
*/
public boolean isValid(Dice dice) {
// num1 and num2 represent the two potential die values in a full house
// num1Count and num2Count are the number of times the num1 and num2 appear in
// the dice
int num1 = dice.getDie(0).getValue(), num1Count = 1;
int num2 = Yahtzee.UNSET, num2Count = 0;
// Determine the counts of num1 and num2
for (int i = 1; i < Yahtzee.DICE_COUNT; i++) {
if (dice.getDie(i).getValue() == num1)
num1Count++;
else if (num2 == Yahtzee.UNSET)
num2 = dice.getDie(i).getValue();
if (dice.getDie(i).getValue() == num2)
num2Count++;
}
// Swap the count so num1Count is the large one
if (num1Count < num2Count) {
int temp = num1Count;
num1Count = num2Count;
num2Count = temp;
}
// Check the count are consistent with that of a full house
return (num1Count == Yahtzee.FULL_HOUSE_HIGH_COUNT) &&
(num2Count == Yahtzee.FULL_HOUSE_LOW_COUNT);
}
}