-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathBaseballGameController.java
More file actions
65 lines (55 loc) · 1.55 KB
/
BaseballGameController.java
File metadata and controls
65 lines (55 loc) · 1.55 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
package controller;
import java.util.List;
import model.computer.Computer;
import model.game.GameCommand;
import model.game.Result;
import model.generator.NumberGenerator;
import model.player.Player;
import view.InputView;
import view.OutputView;
public class BaseballGameController {
private static final int GAME_CLEAR_STRIKES = 3;
private final InputView inputView;
private final OutputView outputView;
private final NumberGenerator numberGenerator;
public BaseballGameController(InputView inputView, OutputView outputView, NumberGenerator numberGenerator) {
this.inputView = inputView;
this.outputView = outputView;
this.numberGenerator = numberGenerator;
}
public void run() {
do {
start();
} while (shouldRestart());
}
private void start() {
Computer computer = new Computer(numberGenerator);
Result result;
do {
List<Integer> playerNumber = getPlayer().getNumber();
result = computer.calculate(playerNumber);
outputView.printResult(result);
} while (result.strikes() != GAME_CLEAR_STRIKES);
outputView.printGameClearMessage();
}
private boolean shouldRestart() {
while (true) {
try {
String input = inputView.readRestartOrQuit();
return GameCommand.from(input).isRestart();
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
private Player getPlayer() {
while (true) {
try {
String input = inputView.readPlayerNumber();
return new Player(input);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
}