diff --git a/pom.xml b/pom.xml index 55013c1..ced75d2 100644 --- a/pom.xml +++ b/pom.xml @@ -1,24 +1,38 @@ - - 4.0.0 - de.edu.hsfulda.ciip - tdd-pure-function - 0.1.0-SNAPSHOT - - 11 - 11 - - - - org.junit.jupiter - junit-jupiter-engine - 5.5.2 - test - - - org.junit.platform - junit-platform-runner - 1.5.2 - test - - + + 4.0.0 + de.edu.hsfulda.ciip + tdd-pure-function + 0.1.0-SNAPSHOT + + 11 + 11 + + + + org.junit.jupiter + junit-jupiter-engine + 5.5.2 + test + + + org.junit.platform + junit-platform-runner + 1.5.2 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 2.22.2 + + + + diff --git a/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java b/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java index e2a5450..655e166 100644 --- a/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java +++ b/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java @@ -4,6 +4,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class BowlingCalculator { + private static final String FOUL = "-"; + private static final int SPLIT_NEXT_ROLL = 2; + private static final int STRIKE_NEXT_ROLL = 1; + private static final int MAX_PIN_COUNT = 10; + @FunctionalInterface interface FrameScorer { int score(Matcher frameMatcher); @@ -12,6 +17,7 @@ public class BowlingCalculator { private static final int INITIAL_SCORE = 0; private static final Pattern SINGLE_ROLL_PATTERN = Pattern.compile("\\d"); private static final Pattern SPARE_PATTERN = Pattern.compile("(\\d)/ ([-\\d])"); + private static final Pattern STRIKE_PATTERN = Pattern.compile("X ([-\\d])([-\\d])"); public int score(String rolls) { int score = INITIAL_SCORE; @@ -21,18 +27,26 @@ public class BowlingCalculator { Matcher spareMatcher = SPARE_PATTERN.matcher(rolls); score = scoreFrameType(score, spareMatcher, matcher -> scoreSpareFrame(spareMatcher)); + Matcher strikeMatcher = STRIKE_PATTERN.matcher(rolls); + score = scoreFrameType(score, strikeMatcher, matcher -> scoreStrike(strikeMatcher)); + return score; } - private int scoreSpareFrame(Matcher spareMatcher) { - int frameScore = 10 - Integer.parseInt(spareMatcher.group(1)); - String nextRoll = spareMatcher.group(2); - if(!"-".equalsIgnoreCase(nextRoll)) { - frameScore += Integer.parseInt(spareMatcher.group(1)); + private int scoreStrike(Matcher strikeMatcher) { + int frameScore = MAX_PIN_COUNT; + for (int nextRollIndex = STRIKE_NEXT_ROLL; nextRollIndex <= STRIKE_NEXT_ROLL + 1; nextRollIndex++) { + frameScore = addNextRollToCurrentFrame(strikeMatcher, frameScore, nextRollIndex); } return frameScore; } + private int scoreSpareFrame(Matcher spareMatcher) { + int frameScore = MAX_PIN_COUNT - Integer.parseInt(spareMatcher.group(1)); + int nextRollIndex = SPLIT_NEXT_ROLL; + return addNextRollToCurrentFrame(spareMatcher, frameScore, nextRollIndex); + } + private int scoreFrameType(int score, Matcher scoreMatcher, FrameScorer frameScorer) { while (scoreMatcher.find()) { score += frameScorer.score(scoreMatcher); @@ -40,4 +54,11 @@ public class BowlingCalculator { return score; } + private int addNextRollToCurrentFrame(Matcher frameMatcher, int frameScore, int nextRollIndex) { + String nextRoll = frameMatcher.group(nextRollIndex); + if (!FOUL.equalsIgnoreCase(nextRoll)) { + frameScore += Integer.parseInt(nextRoll); + } + return frameScore; + } } diff --git a/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java b/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java index 470c22d..0090bc1 100644 --- a/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java +++ b/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java @@ -49,6 +49,7 @@ class BowlingCalculatorTest { // assert assertThat("score", score, CoreMatchers.equalTo(expectedScore)); } + @Test void frameWithSpareAddsNextRollTwice() { // arrange @@ -59,4 +60,36 @@ class BowlingCalculatorTest { // assert assertThat("score", score, CoreMatchers.equalTo(expectedScore)); } + + @Test + void frameWithStrikeCounts10() { + // arrange + String rolls = "-- -- X -- X -- -- -- -- --"; + int expectedScore = 20; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + } + + @Test + void frameWithStrikeAddsNextRollTwice() { + // arrange + String rolls = "-- -- X 2- X 8- -- -- -- --"; + int expectedScore = 40; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + }@Test + + void frameWithStrikeAddsSecondNextRollTwice() { + // arrange + String rolls = "-- -- X -2 X -8 -- -- -- --"; + int expectedScore = 40; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + } }