-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathUtility.java
More file actions
133 lines (109 loc) · 3.63 KB
/
Utility.java
File metadata and controls
133 lines (109 loc) · 3.63 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
import java.util.HashSet;
import java.util.Scanner;
public class Utility {
private Utility() {
}
public static int[] makeRandomNumbers() {
boolean[] flags = new boolean[10];
int[] randomNumbers = new int[3];
int count = 0;
while (count < 3) {
int temp = (int) (Math.random() * 9) + 1;
if (flags[temp]) {
continue;
} else {
randomNumbers[count] = temp;
flags[temp] = true;
count += 1;
}
}
return randomNumbers;
}
public static String getUserString() {
Scanner sc = new Scanner(System.in);
String userString = sc.nextLine();
return userString;
}
public static void checkStringLength(String userString) {
if (userString.length() != 3) {
throw new IllegalArgumentException();
}
}
public static void checkStringDistinct(String userString) {
HashSet<Character> tempSet = new HashSet<>();
for (int i = 0; i < userString.length(); i++) {
tempSet.add(userString.charAt(i));
}
if (tempSet.size() != userString.length()) {
throw new IllegalArgumentException();
}
}
public static void checkStringDigit(String userString) {
for (int i = 0; i < userString.length(); i++) {
int temp = (int) userString.charAt(i) - (int) '0';
if (temp < 1 || temp > 9) {
throw new IllegalArgumentException();
}
}
}
public static int[] stringToIntArray(String userString) {
int[] userNumbers = new int[3];
for (int i = 0; i < 3; i++) {
int temp = (int) userString.charAt(i) - (int) '0';
userNumbers[i] = temp;
}
return userNumbers;
}
public static int strikeCount(int[] randomNumbers, int[] userNumbers) {
int strike = 0;
for (int i = 0; i < 3; i++) {
if (randomNumbers[i] == userNumbers[i]) {
strike += 1;
}
}
return strike;
}
public static int ballCount(int[] randomNumbers, int[] userNumbers) {
int ball = 0;
if (userNumbers[0] == randomNumbers[1] || userNumbers[0] == randomNumbers[2]) {
ball += 1;
}
if (userNumbers[1] == randomNumbers[0] || userNumbers[1] == randomNumbers[2]) {
ball += 1;
}
if (userNumbers[2] == randomNumbers[0] || userNumbers[2] == randomNumbers[1]) {
ball += 1;
}
return ball;
}
public static String getStrikeBall(int strike, int ball) {
if (strike == 0 && ball == 0) {
return "낫싱";
} else if (ball == 0) {
return strike + "스트라이크";
} else if (strike == 0) {
return ball + "볼";
} else {
return ball + "볼 " + strike + "스트라이크";
}
}
public static void printEndMessage(int strike) {
if (strike == 3) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
}
}
public static boolean checkKeepGoing(String userString, int strike) {
if (strike != 3) {
return true;
} else {
if (userString.equals("1")) {
return true;
} else if (userString.equals("2")) {
return false;
} else {
throw new IllegalArgumentException();
}
}
}
}