forked from woowacourse/java-baseball-precourse
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApplication.java
More file actions
105 lines (98 loc) · 3.57 KB
/
Application.java
File metadata and controls
105 lines (98 loc) · 3.57 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
99
100
101
102
103
104
105
package baseball;
import baseball.ball.Ball;
import baseball.ball.BallBoundary;
import camp.nextstep.edu.missionutils.Console;
public class Application {
public static String createRandomBall(){
Ball ball = new Ball(BallBoundary.MAX_VALUE.getValue(), BallBoundary.MIN_VALUE.getValue());
return ball.getBallNumber();
}
public static String input(){
System.out.print("숫자를 입력해주세요 : ");
String temp = Console.readLine();
return temp;
}
public static void checkException(String number){
if(number.length() != Numbers.LENGTH.getValue()) {
throw new IllegalArgumentException(); // 세자리 수
}
if(number.contains("0")) {
throw new IllegalArgumentException(); // 0이 있는지
}
for(int i = 0; i < number.length(); i++){
if(!(number.charAt(i) > '0' && number.charAt(i) <= '9')) throw new IllegalArgumentException(); //숫자가 아닌지
}
checkRepeated(number);
}
private static void checkRepeated(String number) {
for(int i = 0; i < number.length()-1; i++){
for(int j = i+1; j < number.length(); j++){
if(number.charAt(i) == number.charAt(j))throw new IllegalArgumentException();
}
}
}
public static int countStrike(String randomNum, String inputNum){
int count = 0;
for(int i = 0; i < Numbers.LENGTH.getValue(); i++){
if(randomNum.charAt(i) == inputNum.charAt(i)) count++;
}
return count;
}
public static int countBall(String randomNum, String inputNum){
int count = 0;
for(int i = 0; i < Numbers.LENGTH.getValue(); i++) {
for(int j = 0; j < Numbers.LENGTH.getValue(); j++){
if(randomNum.charAt(i) == inputNum.charAt(j) && i != j) count++;
}
}
return count;
}
public static String printHint(int strike, int ball){
String str;
if(strike != 0 && ball != 0){
str = ball + HintString.BALL.getValue() + strike + HintString.STRIKE.getValue();;
return str;
}
else if(strike == 0 && ball != 0){
str = ball +HintString.BALL.getValue();;
return str;
}
else if(strike != 0 && ball == 0){
str = strike + HintString.STRIKE.getValue();
return str;
}
return HintString.NOTHING.getValue();
}
public static void startGame(){
String randNum = createRandomBall();
String inputNum = "";
while(!randNum.equals(inputNum)) {
inputNum = input();
checkException(inputNum);
int strike = countStrike(randNum, inputNum);
int ball = countBall(randNum, inputNum);
String result = printHint(strike, ball);
System.out.println(result);
if (strike == Numbers.LENGTH.getValue()) {
System.out.println(Numbers.LENGTH.getValue() + "개의 숫자를 모두 맞히셨습니다! 게임 종료");
return;
}
}
}
public static boolean repeatGame(){
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String num = Console.readLine();
if(num.equals("1")) return true;
return false;
}
public static void game(){
while(true){
startGame();
if(!repeatGame())return;
}
}
public static void main(String[] args) {
//TODO: 숫자 야구 게임 구현
game();
}
}