-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseballController.java
More file actions
69 lines (58 loc) · 2.4 KB
/
BaseballController.java
File metadata and controls
69 lines (58 loc) · 2.4 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
package baseball.controller;
import baseball.dto.ResultDTO;
import baseball.generator.BaseballNumberGenerator;
import baseball.model.Baseball;
import baseball.model.Referee;
import baseball.model.Result;
import baseball.validation.InputNumberValidation;
import baseball.validation.InputUserChoiceValidation;
import baseball.view.InputView;
import baseball.view.OutputView;
import java.util.List;
public class BaseballController {
private final InputView inputView;
private final OutputView outputView;
private final InputNumberValidation inputNumberValidationImpl;
private final BaseballNumberGenerator baseballNumberGeneratorImpl;
private final InputUserChoiceValidation inputUserChoiceValidation;
public BaseballController(InputView inputView, OutputView outputView, InputNumberValidation inputNumberValidationImpl, BaseballNumberGenerator baseballNumberGeneratorImpl, InputUserChoiceValidation inputUserChoiceValidation) {
this.inputView = inputView;
this.outputView = outputView;
this.inputNumberValidationImpl = inputNumberValidationImpl;
this.baseballNumberGeneratorImpl = baseballNumberGeneratorImpl;
this.inputUserChoiceValidation = inputUserChoiceValidation;
}
public void start() {
inputView.printBaseballGameStart();
boolean keepPlaying = true;
while (keepPlaying) {
keepPlaying = playGame();
}
}
private boolean playGame() {
List<Integer> baseballNumber = baseballNumberGeneratorImpl.generate();
Baseball baseball = new Baseball(baseballNumber);
boolean gameFinished = false;
while (!gameFinished) {
gameFinished = playTurn(baseball);
}
return processUserChoice();
}
private boolean playTurn(Baseball baseball) {
String userNumber = inputNumberValidationImpl.validate(inputView.inputBasebalNumber());
Result result = new Referee(baseball, userNumber).getResult();
ResultDTO resultDTO = new ResultDTO(result.getStrikes(), result.getBalls());
outputView.printResult(resultDTO);
return result.getStrikes() == 3;
}
private boolean processUserChoice() {
int userChoice = inputUserChoiceValidation.validate(inputView.restartGame());
if (userChoice == 1) {
return true;
}
if (userChoice == 2) {
return false;
}
return false;
}
}