Browse Source

add implementation for the roll after spare counts twice test

master
binsky 3 years ago
parent
commit
c097ddf4bc
  1. 10
      src/main/java/BowlingGameCalculator.java
  2. 3
      src/test/java/BowlingCalculatorTest.java
  3. BIN
      target/classes/BowlingGameCalculator.class
  4. BIN
      target/test-classes/BowlingCalculatorTest.class

10
src/main/java/BowlingGameCalculator.java

@ -4,14 +4,22 @@ import java.util.regex.Pattern;
public class BowlingGameCalculator { public class BowlingGameCalculator {
private static final Pattern SINGLE_VALUE_PATTERN = Pattern.compile("\\d"); 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; private static final int INITIAL_SCORE_VALUE = 0;
public int score(String playerResult) { public int score(String playerResult) {
Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(playerResult);
int playerScore = INITIAL_SCORE_VALUE; int playerScore = INITIAL_SCORE_VALUE;
Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(playerResult);
while (singleDigit.find()) { while (singleDigit.find()) {
playerScore += Integer.parseInt(singleDigit.group()); playerScore += Integer.parseInt(singleDigit.group());
} }
Matcher spare = SPARE_PATTERN.matcher(playerResult);
while (spare.find()) {
playerScore += 10 + Integer.parseInt(spare.group(1));
}
return playerScore; return playerScore;
} }
} }

3
src/test/java/BowlingCalculatorTest.java

@ -22,7 +22,8 @@ public class BowlingCalculatorTest {
return Stream.of( 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("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 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
); );
} }
} }

BIN
target/classes/BowlingGameCalculator.class

BIN
target/test-classes/BowlingCalculatorTest.class

Loading…
Cancel
Save