-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.java
More file actions
52 lines (40 loc) · 1.69 KB
/
GameController.java
File metadata and controls
52 lines (40 loc) · 1.69 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
package baseball.controller;
import static baseball.constant.SystemMessage.RESTART_FLAG;
import static baseball.util.NumberUtil.convertStringToList;
import baseball.application.NumberService;
import baseball.domain.Hint;
import baseball.domain.Player;
import baseball.view.InputView;
import baseball.view.OutputView;
import java.util.Objects;
public class GameController {
private final NumberService numberService = NumberService.getInstance();
private final InputView inputView = InputView.getInstance();
private final OutputView outputView = OutputView.getInstance();
public void run() {
do {
Player computer = prepareComputer();
play(computer);
} while (isGameRestarted()); // 종료(2)를 누르지 않으면 게임을 다시 시작한다.
}
private void play(Player computer) {
while (true) {
Player player = preparePlayer();
Hint hint = numberService.generateHint(computer, player); // 컴퓨터와 플레이어의 숫자를 비교하여 힌트를 얻는다.
outputView.printHintMessage(hint); // 힌트를 출력, 만약 숫자가 맞는다면 게임 종료 메시지도 출력한다.
if (hint.isThreeStrike()) { // 숫자를 맞췄다면 끝낸다.
break;
}
}
}
private boolean isGameRestarted() {
return !Objects.equals(inputView.readNumber(), RESTART_FLAG.getMessage());
}
private Player prepareComputer() {
return new Player(numberService.getRandomNumber());
}
private Player preparePlayer() {
outputView.printReadMessage();
return new Player(convertStringToList(inputView.readNumber()));
}
}