-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathGameController.java
More file actions
61 lines (54 loc) · 1.79 KB
/
Copy pathGameController.java
File metadata and controls
61 lines (54 loc) · 1.79 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
package baseball.controller;
import baseball.domain.Game;
import baseball.domain.Matches;
import baseball.exception.NoMatchResultException;
import baseball.service.BaseballService;
import baseball.view.InputView;
import baseball.view.OutputView;
/**
* 프로그램의 전체 흐름을 조율하는 클래스
*/
public class GameController {
private final InputView inputView;
private final OutputView outputView;
private final BaseballService service;
public GameController(InputView inputView, OutputView outputView, BaseballService service) {
this.inputView = inputView;
this.outputView = outputView;
this.service = service;
};
public void run() {
outputView.printGameStartInstruction();
boolean flag = false;
do {
startGame();
outputView.printGameEndInstruction();
flag = isStartingNewGame();
} while (flag);
}
private void startGame() {
Game game = service.createGame();
boolean flag = false;
do {
boolean isGameOver = guess(game);
flag = !isGameOver;
} while (flag);
}
private boolean guess(Game game) {
outputView.printNumberInputPrompt();
String guessNumber = inputView.readGuessingNumber();
try {
Matches matchResult = service.match(game, guessNumber);
outputView.printMatchResults(matchResult);
return service.isGameOver(matchResult);
} catch (NoMatchResultException e) {
outputView.printMatchResults();
}
return false;
}
private boolean isStartingNewGame() {
outputView.printGameProceedPrompt();
String gameProceed = inputView.readGameProceed();
return service.isStartingNewGame(gameProceed);
}
}