Browse Source

Strike counts next two partial frames

master
Thomas Papendieck 4 years ago
parent
commit
7b23e4649b
  1. 16
      pom.xml
  2. 31
      src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java
  3. 33
      src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java

16
pom.xml

@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.edu.hsfulda.ciip</groupId> <groupId>de.edu.hsfulda.ciip</groupId>
<artifactId>tdd-pure-function</artifactId> <artifactId>tdd-pure-function</artifactId>
@ -21,4 +23,16 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project> </project>

31
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; import java.util.regex.Pattern;
public class BowlingCalculator { 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 @FunctionalInterface
interface FrameScorer { interface FrameScorer {
int score(Matcher frameMatcher); int score(Matcher frameMatcher);
@ -12,6 +17,7 @@ public class BowlingCalculator {
private static final int INITIAL_SCORE = 0; private static final int INITIAL_SCORE = 0;
private static final Pattern SINGLE_ROLL_PATTERN = Pattern.compile("\\d"); private static final Pattern SINGLE_ROLL_PATTERN = Pattern.compile("\\d");
private static final Pattern SPARE_PATTERN = Pattern.compile("(\\d)/ ([-\\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) { public int score(String rolls) {
int score = INITIAL_SCORE; int score = INITIAL_SCORE;
@ -21,18 +27,26 @@ public class BowlingCalculator {
Matcher spareMatcher = SPARE_PATTERN.matcher(rolls); Matcher spareMatcher = SPARE_PATTERN.matcher(rolls);
score = scoreFrameType(score, spareMatcher, matcher -> scoreSpareFrame(spareMatcher)); score = scoreFrameType(score, spareMatcher, matcher -> scoreSpareFrame(spareMatcher));
Matcher strikeMatcher = STRIKE_PATTERN.matcher(rolls);
score = scoreFrameType(score, strikeMatcher, matcher -> scoreStrike(strikeMatcher));
return score; 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; 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) { private int scoreFrameType(int score, Matcher scoreMatcher, FrameScorer frameScorer) {
while (scoreMatcher.find()) { while (scoreMatcher.find()) {
score += frameScorer.score(scoreMatcher); score += frameScorer.score(scoreMatcher);
@ -40,4 +54,11 @@ public class BowlingCalculator {
return score; 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;
}
} }

33
src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java

@ -49,6 +49,7 @@ class BowlingCalculatorTest {
// assert // assert
assertThat("score", score, CoreMatchers.equalTo(expectedScore)); assertThat("score", score, CoreMatchers.equalTo(expectedScore));
} }
@Test @Test
void frameWithSpareAddsNextRollTwice() { void frameWithSpareAddsNextRollTwice() {
// arrange // arrange
@ -59,4 +60,36 @@ class BowlingCalculatorTest {
// assert // assert
assertThat("score", score, CoreMatchers.equalTo(expectedScore)); 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));
}
} }
Loading…
Cancel
Save