-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathGameController.java
More file actions
68 lines (57 loc) · 2.08 KB
/
GameController.java
File metadata and controls
68 lines (57 loc) · 2.08 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
package controller;
import model.BaseballGameJudge;
import model.GameResult;
import model.InputValidator;
import model.RandomNumberGenerator;
import model.exception.UserException;
import view.InputView;
import view.OutputView;
import java.util.List;
public class GameController {
private static final int NUMBER_SIZE = 3;
private static final int MIN_NUMBER = 1;
private static final int MAX_NUMBER = 9;
private final RandomNumberGenerator randomNumberGenerator;
private final BaseballGameJudge baseballGameJudge;
private final InputValidator inputValidator;
private final InputView inputView;
private final OutputView outputView;
public GameController() {
this.randomNumberGenerator = new RandomNumberGenerator();
this.baseballGameJudge = new BaseballGameJudge();
this.inputValidator = new InputValidator();
this.inputView = new InputView();
this.outputView = new OutputView();
}
public void run() {
do{
List<Integer> result = randomNumberGenerator.pickUniqueNumbers(MIN_NUMBER, MAX_NUMBER, NUMBER_SIZE);
// List<Integer> result = List.of(1,2,3);
while(!playRound(result)){
}
outputView.printGameEnd(NUMBER_SIZE);
}while(handleRestart());
}
public boolean playRound(List<Integer> answer) {
try{
String input = inputView.readNumber();
List<Integer> inputList = inputValidator.parseToNumbers(input, MIN_NUMBER, MAX_NUMBER, NUMBER_SIZE);
GameResult judge = baseballGameJudge.judge(answer, inputList);
outputView.printResult(judge);
return judge.isGameOver(NUMBER_SIZE);
}catch(UserException e){
outputView.printError(e);
}
return false;
}
public boolean handleRestart() {
while(true){
try{
String s = inputView.readRestartOption();
return inputValidator.validateRestartInput(s);
}catch(UserException e){
outputView.printError(e);
}
}
}
}