diff --git a/src/main/java/BowlingGameCalculator.java b/src/main/java/BowlingGameCalculator.java index 27ce370..8ea8bd1 100644 --- a/src/main/java/BowlingGameCalculator.java +++ b/src/main/java/BowlingGameCalculator.java @@ -4,14 +4,22 @@ import java.util.regex.Pattern; public class BowlingGameCalculator { private static final Pattern SINGLE_VALUE_PATTERN = Pattern.compile("\\d"); + private static final Pattern SPARE_PATTERN = Pattern.compile("/[ -,]*(\\d)"); private static final int INITIAL_SCORE_VALUE = 0; public int score(String playerResult) { - Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(playerResult); int playerScore = INITIAL_SCORE_VALUE; + + Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(playerResult); while (singleDigit.find()) { playerScore += Integer.parseInt(singleDigit.group()); } + + Matcher spare = SPARE_PATTERN.matcher(playerResult); + while (spare.find()) { + playerScore += 10 + Integer.parseInt(spare.group(1)); + } + return playerScore; } } diff --git a/src/test/java/BowlingCalculatorTest.java b/src/test/java/BowlingCalculatorTest.java index 91755ee..a63a75a 100644 --- a/src/test/java/BowlingCalculatorTest.java +++ b/src/test/java/BowlingCalculatorTest.java @@ -22,7 +22,8 @@ public class BowlingCalculatorTest { return Stream.of( Arguments.of("worst game", "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", 0), // 20x 0 Arguments.of("some incomplete frames", "0,1,2,3,0,0,4,0,0,5,0,0,6,0,0,0,0,0,0,0", 21), - Arguments.of("some other incomplete frames", "9- 9- 9- 9- 9- 9- 9- 9- 9- 9-", 90) + Arguments.of("some other incomplete frames", "9- 9- 9- 9- 9- 9- 9- 9- 9- 9-", 90), + Arguments.of("roll after spare counts twice", "0,0 ,0,/ ,1,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0", 12) // Wurf nach Spare (/) zählt doppelt ); } } diff --git a/target/classes/BowlingGameCalculator.class b/target/classes/BowlingGameCalculator.class index cb9921d..e06f1b2 100644 Binary files a/target/classes/BowlingGameCalculator.class and b/target/classes/BowlingGameCalculator.class differ diff --git a/target/test-classes/BowlingCalculatorTest.class b/target/test-classes/BowlingCalculatorTest.class index fd22f84..b58c7dd 100644 Binary files a/target/test-classes/BowlingCalculatorTest.class and b/target/test-classes/BowlingCalculatorTest.class differ