-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathApplication.java
More file actions
98 lines (82 loc) · 3.11 KB
/
Application.java
File metadata and controls
98 lines (82 loc) · 3.11 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
92
93
94
95
96
97
98
import computer.Computer;
import game.BaseBallGameService;
import game.GameCondition;
import java.util.Scanner;
public class Application {
private final Computer computer;
private final BaseBallGameService gameService;
private final GameCondition gameCondition;
public Application(Computer computer, BaseBallGameService gameService, GameCondition gameCondition) {
this.computer = computer;
this.gameService = gameService;
this.gameCondition = gameCondition;
}
public Computer getComputer() {
return computer;
}
private boolean inputValidCheck(String clientNumber) {
try{
gameService.checkNumberValid(clientNumber);
return true;
}
catch (IllegalArgumentException e){
System.out.println(e.getMessage());
return false;
}
}
private void printGameInfo(int strike, int ball) {
if(strike == 3){
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
gameCondition.changeCollectAnswer(true);
}else{
String info = gameService.printGameInfo(strike, ball);
System.out.println(info);
}
}
private void checkRestartGame(String continueGame){
try{
if("1".equals(continueGame)){
computer.reGenerateNumber();
gameCondition.changeCollectAnswer(false);
}
else if("2".equals(continueGame)){
gameCondition.changePlayGame(false);
}
else{
throw new IllegalArgumentException("입력이 1과 2가 아닙니다. 애플리케이션을 종료합니다.");
}
}
catch (IllegalArgumentException e){
System.out.println(e.getMessage());
gameCondition.changePlayGame(false);
}
}
public void start(){
Scanner scanner = new Scanner(System.in);
while(gameCondition.canPlayGame()){
System.out.print("숫자를 입력해 주세요 : ");
String clientNumber = scanner.nextLine();
if (!inputValidCheck(clientNumber)) {
scanner.close();
break;
}
int strike = gameService.checkStrike(computer.getNumber(), clientNumber);
int ball = gameService.checkBall(computer.getNumber(), clientNumber);
printGameInfo(strike, ball);
if(gameCondition.canCollectAnswer()){
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String continueGame = scanner.nextLine();
checkRestartGame(continueGame);
}
}
scanner.close();
System.out.println("안녕히 가세요.");
}
public static void main(String[] args) {
Computer computer = new Computer();
BaseBallGameService gameService = new BaseBallGameService();
GameCondition gameCondition = new GameCondition();
Application app = new Application(computer, gameService, gameCondition);
app.start();
}
}