-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathGameController.java
More file actions
91 lines (75 loc) · 2.27 KB
/
GameController.java
File metadata and controls
91 lines (75 loc) · 2.27 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package baseball.controller;
import baseball.domain.Hint;
import baseball.domain.NumbersGenerator;
import baseball.domain.PlayerGuess;
import baseball.domain.Umpire;
import baseball.view.InputView;
import baseball.view.OutputView;
public class GameController {
private final NumbersGenerator numbersGenerator;
private final Umpire umpire;
private final InputView inputView;
private final OutputView outputView;
public GameController(NumbersGenerator numbersGenerator, Umpire umpire,
InputView inputView, OutputView outputView) {
this.numbersGenerator = numbersGenerator;
this.umpire = umpire;
this.inputView = inputView;
this.outputView = outputView;
}
public void run() {
while (true) {
playOneGame();
if (isRestart()) {
continue;
}
return;
}
}
private void playOneGame() {
int[] answer = numbersGenerator.generate();
while (true) {
Hint hint = playOneTurn(answer);
if (hint == null) {
continue;
}
if (hint.isThreeStrike()) {
outputView.printGameEnd();
return;
}
}
}
private Hint playOneTurn(int[] answer) {
try {
PlayerGuess guess = readGuess();
Hint hint = umpire.judge(answer, guess.asArray());
outputView.printHint(hint);
return hint;
} catch (IllegalArgumentException e) {
outputView.printErrorMessage();
return null;
}
}
private PlayerGuess readGuess() {
String input = inputView.readGuess();
return PlayerGuess.from(input);
}
private boolean isRestart() {
try {
String input = inputView.readRestartCommand();
return parseRestartCommand(input);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage();
return isRestart();
}
}
private boolean parseRestartCommand(String input) {
if ("1".equals(input)) {
return true;
}
if ("2".equals(input)) {
return false;
}
throw new IllegalArgumentException();
}
}