-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathApplication.java
More file actions
135 lines (118 loc) · 3.72 KB
/
Copy pathApplication.java
File metadata and controls
135 lines (118 loc) · 3.72 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package baseball;
import java.util.ArrayList;
import java.util.List;
import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;
public class Application {
public static void main(String[] args) {
System.out.println("숫자 야구 게임을 시작합니다.");
boolean play = true;
while (play) {
playGame();
play = isRestartGame();
}
}
private static void playGame() {
List<Integer> computer = getComputer();
boolean gameWon = false;
while (!gameWon) {
List<Integer> me = getNum();
List<Integer> checked = check(computer, me);
int ball = checked.get(0), strike = checked.get(1);
gameWon = printBallStrike(ball, strike);
System.out.print("\n");
}
}
private static boolean isRestartGame() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int ans = Integer.parseInt(Console.readLine());
checkAnswer(ans);
if (ans == 1) {
return true;
}
else {
return false;
}
}
private static boolean printBallStrike(int ball, int strike) {
if (ball == 0 && strike == 0) {
System.out.print("낫싱");
}
if (ball != 3 && strike != 3) {
if (ball != 0) {
System.out.printf("%d볼 ", ball);
}
if (strike != 0) {
System.out.printf("%d스트라이크", strike);
}
}
else if (ball == 3) {
System.out.printf("%d볼", ball);
}
else {
System.out.printf("%d스트라이크\n", strike);
System.out.print("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
return true;
}
return false;
}
private static boolean checkNum(String num) {
if (num.length() != 3) {
return false;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
continue;
}
if (num.charAt(i) == num.charAt(j)) {
return false;
}
}
}
return true;
}
private static void checkAnswer(int ans) {
if (ans != 1 && ans != 2) {
throw new IllegalArgumentException();
}
}
private static List<Integer> getComputer() {
List<Integer> computer = new ArrayList<>();
while (computer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}
return computer;
}
private static List<Integer> getNum() {
System.out.print("숫자를 입력해주세요 : ");
String num = Console.readLine();
if (!checkNum(num)) {
throw new IllegalArgumentException();
}
List<Integer> me = new ArrayList<>();
for (int i = 0; i < num.length(); i++) {
me.add(Integer.parseInt(num.substring(i, i+1)));
}
return me;
}
private static List<Integer> check(List<Integer> computer, List<Integer> me) {
List<Integer> ans = new ArrayList<>(); // strike, ball
int strike = 0, ball = 0;
for (int i = 0; i < 3; i++) {
int m = me.get(i);
if (computer.get(i).equals(m)) {
strike++;
}
else if (computer.contains(m)) {
ball++;
}
}
ans.add(ball);
ans.add(strike);
return ans;
}
}