-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice.java
76 lines (69 loc) · 2.01 KB
/
Dice.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
/**
* {@link Dice} represents {@link Yahtzee#DICE_COUNT} number of {@link Die}.
*
* @see Die
* @see Yahtzee#DICE_COUNT
* @see #roll(Boolean[])
* @author Arjun Subramanian
*/
public class Dice {
private Die[] dice;
/**
* Construct {@link Yahtzee#DICE_COUNT} number of {@link Die}.
*/
public Dice() {
dice = new Die[Yahtzee.DICE_COUNT];
for (int i = 0; i < Yahtzee.DICE_COUNT; i++)
dice[i] = new Die();
}
/**
* Getter for the {@code index}th {@link Die} in the {@link #dice} array.
*
* @param index The index of the desired {@link Die}.
* @return {@link Die} object at {@code index} position.
*/
public Die getDie(int index) {
return dice[index];
}
/**
* Roll all {@link Yahtzee#DICE_COUNT} dice.
*
* @see Die#roll()
*/
public void roll() {
for (int i = 0; i < Yahtzee.DICE_COUNT; i++)
dice[i].roll();
}
/**
* Roll those dice in the array whose index is set to false in the toKeep array.
*
* @param toKeep {@code true} if the die shouldn't be rerolled.
* @see Die#roll()
*/
public void roll(Boolean[] toKeep) {
for (int i = 0; i < Yahtzee.DICE_COUNT; i++)
if (!toKeep[i]) // Don't keep dice i
dice[i].roll();
}
/**
* Shows all {@link Yahtzee#DICE_COUNT} number of dice separated by spaces.
*
* @return String representation of {@link Dice}.
* @see Die#toString()
* @see java.lang.Object#toString()
*/
public String toString() {
// Initialize it with the 0th dice
StringBuilder sb = new StringBuilder(dice[0].toString());
for (int i = 1; i < Yahtzee.DICE_COUNT; i++)
// every additional dice will lead with a space
sb.append(' ' + dice[i].toString());
return sb.toString();
}
/**
* Print the {@link #toString()} to System.out.
*/
public void display() {
System.out.println(this.toString());
}
}