forked from woowacourse/java-baseball-precourse
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathComputerFunction.java
More file actions
56 lines (45 loc) · 1.79 KB
/
ComputerFunction.java
File metadata and controls
56 lines (45 loc) · 1.79 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
package baseball;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;
import static baseball.NumberInfo.*;
import static baseball.Message.*;
public class ComputerFunction {
UserFunction userFunction = new UserFunction();
public List<Integer> createRandomNumber(){
List<Integer> randomNumber = new ArrayList<>();
while(randomNumber.size() < SIZE_OF_NUMBER.getNumberInfo()){
int tmp = Randoms.pickNumberInRange(START_OF_RANGE.getNumberInfo(), END_OF_RANGE.getNumberInfo());
if(!randomNumber.contains(tmp)){
randomNumber.add(tmp);
}
}
return randomNumber;
}
public void countCalculate(List<Integer> randomNumber){
while(true) {
String userNumber = userFunction.getUserNumber();
int ballCount = 0;
int strikeCount = 0;
for (int i=0; i<SIZE_OF_NUMBER.getNumberInfo(); i++) {
int tmp = userNumber.charAt(i) - '0';
if (randomNumber.indexOf(tmp) == i) {
strikeCount++;
}
if (randomNumber.contains(tmp)) {
ballCount++;
}
}
if (strikeCount == SIZE_OF_NUMBER.getNumberInfo()) {
System.out.println(strikeCount + STRIKE_MESSAGE.getMessage());
System.out.println(CORRECT_MESSAGE.getMessage());
break;
} else if (strikeCount == 0 && ballCount == 0) {
System.out.println(NOTHING_MESSAGE.getMessage());
continue;
}
System.out.println((ballCount-strikeCount) + BALL_MESSAGE.getMessage() +
strikeCount + STRIKE_MESSAGE.getMessage());
}
}
}