Skip to content

Commit 18950b1

Browse files
committed
BAEL-8198: full impl
1 parent 3ea27e1 commit 18950b1

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

patterns-modules/data-oriented-programming/src/main/java/com/baeldung/patterns/dataoriented/ScoringRules.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.Optional;
56

67
import static java.util.Comparator.reverseOrder;
78
import static java.util.function.Function.identity;
89
import static java.util.stream.Collectors.counting;
910
import static java.util.stream.Collectors.groupingBy;
1011

1112
public class ScoringRules {
12-
static int calculatePairs(List<Integer> dices, int nrOfPairs) {
13+
static int pairs(List<Integer> dices, int nrOfPairs) {
1314
Map<Integer, Long> frequency = dices.stream()
1415
.collect(groupingBy(identity(), counting()));
1516

@@ -30,4 +31,23 @@ static int calculatePairs(List<Integer> dices, int nrOfPairs) {
3031
.sum();
3132
}
3233

34+
public static Integer moreOfSameKind(List<Integer> roll, int nrOfDicesOfSameKind) {
35+
Map<Integer, Long> frequency = roll.stream()
36+
.collect(groupingBy(identity(), counting()));
37+
38+
Optional<Integer> diceValue = frequency.entrySet().stream()
39+
.filter(entry -> entry.getValue() >= nrOfDicesOfSameKind)
40+
.map(Map.Entry::getKey)
41+
.max(Integer::compare);
42+
43+
return diceValue.map(it -> it * nrOfDicesOfSameKind)
44+
.orElse(0);
45+
}
46+
47+
public static Integer specificValue(List<Integer> dices, Integer value) {
48+
return dices.stream()
49+
.filter(value::equals)
50+
.mapToInt(it -> it)
51+
.sum();
52+
}
3353
}

patterns-modules/data-oriented-programming/src/main/java/com/baeldung/patterns/dataoriented/YahtzeeDO.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.baeldung.patterns.dataoriented;
22

33

4+
import com.baeldung.patterns.dataoriented.plays.MoreOfTheSameKind;
45
import com.baeldung.patterns.dataoriented.plays.Pairs;
56
import com.baeldung.patterns.dataoriented.plays.PlayedHand;
7+
import com.baeldung.patterns.dataoriented.plays.SpecificDiceValue;
68

79
import java.util.List;
810

@@ -12,11 +14,11 @@ public enum Strategy {
1214
ONES, TWOS, THREES, FOURS, FIVES, SIXES, PAIR, TWO_PAIRS, THREE_OF_A_KIND, FOUR_OF_A_KIND;
1315
}
1416

15-
public Integer calculateScore(List<Integer> dices, Strategy strategy) {
16-
PlayedHand hand = PlayFactory.createPlayedHand(dices, strategy);
17-
return switch (hand) {
18-
case Pairs p -> ScoringRules.calculatePairs(p.dices(), p.nrOfPairs());
19-
default -> 0;
17+
public static int calculateScore(List<Integer> dices, Strategy strategy) {
18+
return switch (PlayFactory.createPlayedHand(dices, strategy)) {
19+
case SpecificDiceValue(List<Integer> roll, int value) -> ScoringRules.specificValue(roll, value);
20+
case Pairs(List<Integer> roll, int pairsCount) -> ScoringRules.pairs(roll, pairsCount);
21+
case MoreOfTheSameKind(List<Integer> roll, int sameKindCount) -> ScoringRules.moreOfSameKind(roll, sameKindCount);
2022
};
2123
}
2224

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package com.baeldung.patterns.objectoriented;
1+
package com.baeldung.patterns;
22

33
import com.baeldung.patterns.dataoriented.YahtzeeDO;
4-
import com.baeldung.patterns.objectoriented.YahtzeeOO.Strategy;
4+
import com.baeldung.patterns.objectoriented.YahtzeeOO;
55
import org.junit.jupiter.params.ParameterizedTest;
66
import org.junit.jupiter.params.provider.Arguments;
77
import org.junit.jupiter.params.provider.MethodSource;
@@ -40,7 +40,7 @@ void testCalculateScore_OO(List<Integer> dices, String strategyStr, Integer expe
4040
void testCalculateScore_DO(List<Integer> dices, String strategyStr, Integer expectedScore) {
4141
YahtzeeDO.Strategy strategy = YahtzeeDO.Strategy.valueOf(strategyStr);
4242

43-
Integer actualScore = new YahtzeeDO().calculateScore(dices, strategy);
43+
Integer actualScore = YahtzeeDO.calculateScore(dices, strategy);
4444

4545
assertEquals(expectedScore, actualScore,
4646
() -> String.format("Failed for roll %s and category %s", dices, strategy));

0 commit comments

Comments
 (0)