-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferee.java
More file actions
55 lines (46 loc) · 1.72 KB
/
Referee.java
File metadata and controls
55 lines (46 loc) · 1.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
package baseball.model;
import java.util.List;
import java.util.stream.IntStream;
import static baseball.utils.BaseballConstants.BASEBALL_SIZE;
public class Referee {
private Referee() {
}
public static Result judge(
final List<Integer> computerBaseballs,
final List<Integer> userBaseballs
) {
final int strikeCount = calculateStrikeCount(computerBaseballs, userBaseballs);
final int ballCount = calculateBallCount(computerBaseballs, userBaseballs);
return new Result(strikeCount, ballCount);
}
private static int calculateStrikeCount(
final List<Integer> computerBaseballs,
final List<Integer> userBaseballs
) {
return (int) IntStream.range(0, BASEBALL_SIZE)
.filter(index -> isExactlyMatchAtIndex(computerBaseballs, userBaseballs, index))
.count();
}
private static boolean isExactlyMatchAtIndex(
final List<Integer> computerBaseballs,
final List<Integer> userBaseballs,
final int index
) {
return computerBaseballs.get(index).equals(userBaseballs.get(index));
}
private static int calculateBallCount(
final List<Integer> computerBaseballs,
final List<Integer> userBaseballs
) {
return getContainedElementsCount(computerBaseballs, userBaseballs)
- calculateStrikeCount(computerBaseballs, userBaseballs);
}
private static int getContainedElementsCount(
final List<Integer> computerBaseballs,
final List<Integer> userBaseballs
) {
return (int) userBaseballs.stream()
.filter(computerBaseballs::contains)
.count();
}
}