-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBowling.java
More file actions
49 lines (39 loc) Β· 1.15 KB
/
Bowling.java
File metadata and controls
49 lines (39 loc) Β· 1.15 KB
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
package bowling;
import bowling.frame.FirstPitchingFrame;
import bowling.frame.Frame;
import bowling.frame.result.FrameResult;
import bowling.score.Score;
import bowling.score.ScoreBoard;
import java.util.ArrayList;
import java.util.List;
public class Bowling {
private final List<FrameResult> frameResults = new ArrayList<>();
public void bowl(final Score score) {
if (frameResults.isEmpty()) {
firstBowl(score);
return;
}
final Frame frame = getLastFrameResult().nextFrame();
bowl(score, frame);
}
private void firstBowl(final Score score) {
frameResults.clear();
final Frame firstFrame = new FirstPitchingFrame(Frame.MIN_NO);
bowl(score, firstFrame);
}
private void bowl(final Score score, final Frame frame) {
frameResults.add(frame.pitch(score));
}
public boolean hasNext() {
return frameResults.isEmpty() || getLastFrameResult().getNextFrameNo() <= Frame.MAX_NO;
}
public int getNextFrameNo() {
return getLastFrameResult().getNextFrameNo();
}
private FrameResult getLastFrameResult() {
return frameResults.get(frameResults.size() - 1);
}
public ScoreBoard getScoreBoard() {
return new ScoreBoard(frameResults);
}
}