-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathGameController.java
More file actions
57 lines (50 loc) · 1.51 KB
/
GameController.java
File metadata and controls
57 lines (50 loc) · 1.51 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
package controller;
import domain.JudgeCount;
import service.GameService;
import util.Validator;
import view.InputView;
import view.OutputView;
public class GameController {
private final InputView inputView;
private final OutputView outputView;
private final GameService gameService;
public GameController(InputView inputView, OutputView outputView, GameService gameService) {
this.inputView = inputView;
this.outputView = outputView;
this.gameService = gameService;
}
public void run() {
outputView.printStartMessage();
while (true) {
playGame();
if (shouldStop()) {
return;
}
}
}
private void playGame() {
gameService.startNewGame();
playUntilWin();
}
private void playUntilWin() {
while (!gameService.isGameOver()) {
try {
JudgeCount count = gameService.play(inputView.readGuess());
outputView.printResult(count);
} catch (IllegalArgumentException exception) {
outputView.printError(exception.getMessage());
}
}
outputView.printGameEndMessage();
}
private boolean shouldStop() {
while (true) {
String input = inputView.readRestartCommand();
String error = Validator.restartError(input);
if (error == null) {
return "2".equals(input);
}
outputView.printError(error);
}
}
}