-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathBowlingController.java
More file actions
70 lines (56 loc) · 2.02 KB
/
BowlingController.java
File metadata and controls
70 lines (56 loc) · 2.02 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bowling.controller;
import bowling.domain.Ball;
import bowling.domain.FrameBoard;
import bowling.domain.PlayerName;
import bowling.view.InputView;
import bowling.view.OutputView;
import java.util.function.Supplier;
public class BowlingController {
private final InputView inputView;
private final OutputView outputView;
public BowlingController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void run() {
PlayerName playerName = inputView.inputPlayerName();
FrameBoard frameBoard = FrameBoard.init(playerName);
outputView.printBoard(frameBoard);
playFrames(frameBoard);
}
private void playFrames(FrameBoard frameBoard) {
int frameIndex = 0;
while (!frameBoard.hasEmptyFrame()) {
playFrame(frameBoard, frameIndex);
frameIndex++;
}
playBonusBall(frameBoard);
}
private void playBonusBall(FrameBoard frameBoard) {
playFrame(frameBoard, 9);
if (frameBoard.needLastFrameBonus()) {
Ball bonusBall = inputView.inputBall(9);
frameBoard.applyBonusBall(bonusBall);
outputView.printBoard(frameBoard);
}
}
private void playFrame(FrameBoard frameBoard, int frameIndex) {
frameIndex = frameBoard.getNextFrameIndes();
Ball firstBall = inputView.inputBall(frameIndex);
frameBoard.applyFirstBallOf(frameIndex, firstBall);
outputView.printBoard(frameBoard);
if (!firstBall.isStrike()) {
Ball secondBall = inputView.inputBall(frameIndex);
frameBoard.applySecondBallOf(frameIndex, secondBall);
outputView.printBoard(frameBoard);
}
}
private <T> T readWithRetry(Supplier<T> supplier) {
try {
return supplier.get();
} catch (IllegalArgumentException e) {
outputView.printExceptionMessage(e.getMessage());
return readWithRetry(supplier);
}
}
}