-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathBaseball.java
More file actions
86 lines (66 loc) · 2.05 KB
/
Baseball.java
File metadata and controls
86 lines (66 loc) · 2.05 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
package baseball.model;
import java.util.*;
public class Baseball {
private Boolean stop;
private String answer;
private Set<Character> set;
public Baseball() {
this.stop = false;
this.answer = generateRandomNumber();
this.set = createSet(answer);
// System.out.println("컴퓨터의 정답 숫자 3개가 생성되었습니다." + this.answer);
}
public Baseball(String answer) {
this.stop = false;
this.answer = answer;
this.set = createSet(answer);
}
public String getAnswer() {
return this.answer;
}
public Boolean getStop() {
return this.stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public void setAnswer() {
this.answer = generateRandomNumber();
this.set = createSet(answer);
// System.out.println("컴퓨터의 정답 숫자 3개가 생성되었습니다." + this.answer);
}
private Set<Character> createSet(String answer) {
Set<Character> set = new HashSet<>();
for (char c : answer.toCharArray()) {
set.add(c);
}
return set;
}
private String generateRandomNumber() {
List<Integer> numbers = new ArrayList<>();
for (int i = 1; i <= 9; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
return "" + numbers.get(0) + numbers.get(1) + numbers.get(2);
}
public Result calculate(String input) {
int ball = calculateBall(input);
int strike = calculateStrike(input);
return new Result(strike, ball - strike);
}
private int calculateBall(String input) {
int ball = 0;
for (char num : input.toCharArray()) {
if (set.contains(num)) ball++;
}
return ball;
}
private int calculateStrike(String input) {
int strike = 0;
for (int i = 0; i < 3; i++) {
if (answer.substring(i, i + 1).equals(input.substring(i, i + 1))) strike++;
}
return strike;
}
}