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