-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreSheet.java
78 lines (72 loc) · 2.51 KB
/
ScoreSheet.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
import java.util.ArrayList;
/**
* List of {@link ScoreRow} that contains are the scoring categories and their
* scores. For example: Threes, Four of a Kind, and Yahtzee are scoring rows.
*
* @see ScoreRow
* @see SumAllRow
* @see FixedAmountRow
* @see NumberRow
* @see NumOfAKindRow
* @see FullHouseRow
* @see StraightRow
* @see ChanceRow
* @author Arjun Subramanian
*/
public class ScoreSheet extends ArrayList<ScoreRow> {
/**
* Construct a {@link ScoreSheet} with all the scoring rows.
*/
public ScoreSheet() {
this.add(new NumberRow("Ones", 1));
this.add(new NumberRow("Twos", 2));
this.add(new NumberRow("Threes", 3));
this.add(new NumberRow("Fours", 4));
this.add(new NumberRow("Fives", 5));
this.add(new NumberRow("Sixes", 6));
this.add(new NumOfAKindRow("Three of a Kind", 3));
this.add(new NumOfAKindRow("Four of a Kind", 4));
this.add(new FullHouseRow("Full House"));
this.add(new StraightRow("Small Straight", Yahtzee.SMALL_STRAIGHT_COUNT));
this.add(new StraightRow("Large Straight", Yahtzee.LARGE_STRAIGHT_COUNT));
this.add(new YahtzeeRow("Yahtzee"));
this.add(new ChanceRow("Chance"));
}
/**
* Total score so far; used for {@link #display()}.
*
* @return Sum of all {@link ScoreRow#getScore()} for each row.
*/
public int getTotal() {
int total = 0;
for (int i = 0; i < this.size(); i++)
if (this.get(i).getScore() != Yahtzee.UNSET)
total += this.get(i).getScore();
return total;
}
/**
* Determine if the score sheet is finished.
*
* @return If the score sheet have been filled.
*/
public boolean isFull() {
boolean allUsed = true;
for (int i = 0; i < this.size() && allUsed; i++)
allUsed = this.get(i).getScore() != Yahtzee.UNSET;
return allUsed;
}
/**
* Print a visualized {@link ScoreSheet} to System.out.
*
* Shows the row number followed by {@link ScoreRow#toString()}.
*
* @see ScoreRow#toString()
*/
public void display() {
for (int i = 0; i < this.size(); i++)
// Show the row number followed by the ScoreRow.toString() string
System.out.println(String.format("%" + 2 + "d. %s", i + 1, this.get(i).toString()));
// Additionally print out that total score, calculated by ScoreSheet.getTotal()
System.out.println("Total = " + Integer.toString(this.getTotal()));
}
}