Browse Source

refactor implementation for the roll after spare counts twice test

master
binsky 3 years ago
parent
commit
940be19623
  1. 17
      src/main/java/BowlingGameCalculator.java
  2. BIN
      target/classes/BowlingGameCalculator.class

17
src/main/java/BowlingGameCalculator.java

@ -1,3 +1,4 @@
import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -10,16 +11,28 @@ public class BowlingGameCalculator {
public int score(String playerResult) { public int score(String playerResult) {
int playerScore = INITIAL_SCORE_VALUE; int playerScore = INITIAL_SCORE_VALUE;
/*
Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(playerResult); 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); Matcher spare = SPARE_PATTERN.matcher(playerResult);
while (spare.find()) {
playerScore += 10 + Integer.parseInt(spare.group(1));
Function<Matcher, Integer> spareScorer = (spareMatcher) -> 10 + Integer.parseInt(spare.group(1));
playerScore = scoreMatch(playerScore, spare, spareScorer);
*/
playerScore += scoreMatch(SINGLE_VALUE_PATTERN.matcher(playerResult), (spareMatcher) -> Integer.parseInt(spareMatcher.group()));
playerScore += scoreMatch(SPARE_PATTERN.matcher(playerResult), (spareMatcher) -> 10 + Integer.parseInt(spareMatcher.group(1)));
return playerScore;
} }
private int scoreMatch(Matcher scoreMatch, Function<Matcher, Integer> scorer) {
int playerScore = INITIAL_SCORE_VALUE;
while (scoreMatch.find()) {
playerScore += scorer.apply(scoreMatch);
}
return playerScore; return playerScore;
} }
} }

BIN
target/classes/BowlingGameCalculator.class

Loading…
Cancel
Save