Browse Source

Strike counts next two partial frames

master
Thomas Papendieck 7 months ago
parent
commit
7b23e4649b
  1. 60
      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

60
pom.xml

@ -1,24 +1,38 @@
<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>
<groupId>de.edu.hsfulda.ciip</groupId>
<artifactId>tdd-pure-function</artifactId>
<version>0.1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<groupId>de.edu.hsfulda.ciip</groupId>
<artifactId>tdd-pure-function</artifactId>
<version>0.1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
</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>

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;
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;
}
}

33
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));
}
}
Loading…
Cancel
Save