-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowerSectionCombination.java
More file actions
55 lines (46 loc) · 1.94 KB
/
Copy pathLowerSectionCombination.java
File metadata and controls
55 lines (46 loc) · 1.94 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class LowerSectionCombination extends Combination{
private List<Integer> pointsPatterns;
public LowerSectionCombination(String name, int pointsGained, List<Integer> pointsPatterns) {
super(name, pointsGained);
this.pointsPatterns = new ArrayList<>(pointsPatterns);
}
@Override
public boolean isFormed(List<Integer> dice) {
switch (name) {
case "Three of a kind", "Four of a kind", "Full House", "YAHTZEE": {
Map<Integer, Long> counting = dice.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
int conditions[] = new int[pointsPatterns.size()];
Arrays.fill(conditions, 0);
for (long v : counting.values())
for (int i = 0; i < pointsPatterns.size(); i++)
if (v >= pointsPatterns.get(i)) {
conditions[i] = 1;
break;
}
return Arrays.stream(conditions).allMatch(s -> s == 1);
}
case "Small straight": {
if (dice.equals(Arrays.asList(1, 2, 3, 4)) || dice.equals(Arrays.asList(2, 3, 4, 5)) || dice.equals(Arrays.asList(3, 4, 5, 6)))
return true;
}
case "Large straight": {
if (dice.equals(Arrays.asList(1, 2, 3, 4, 5)) || dice.equals(Arrays.asList(2, 3, 4, 5, 6)))
return true;
}
default: return false;
}
}
@Override
public int getPoints(List<Integer> dice) {
return switch (name) {
case "Three of a kind", "Four of a kind", "Chance" -> dice.stream().mapToInt(Integer::intValue).sum();
default -> 0;
};
}
}