From 940be1962308c030eb6a64bd38a3e9df29692bef Mon Sep 17 00:00:00 2001 From: binsky Date: Wed, 24 Nov 2021 11:01:08 +0100 Subject: [PATCH] refactor implementation for the roll after spare counts twice test --- src/main/java/BowlingGameCalculator.java | 19 ++++++++++++++++--- target/classes/BowlingGameCalculator.class | Bin 1253 -> 2583 bytes 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/BowlingGameCalculator.java b/src/main/java/BowlingGameCalculator.java index 8ea8bd1..1fda65b 100644 --- a/src/main/java/BowlingGameCalculator.java +++ b/src/main/java/BowlingGameCalculator.java @@ -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 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 scorer) { + int playerScore = INITIAL_SCORE_VALUE; + while (scoreMatch.find()) { + playerScore += scorer.apply(scoreMatch); + } + return playerScore; + } } diff --git a/target/classes/BowlingGameCalculator.class b/target/classes/BowlingGameCalculator.class index e06f1b25c447aefc91fb5e08799630fd35bd51b1..649e59fadff4f972234c4cd71fbcfeb33ebd7283 100644 GIT binary patch literal 2583 zcmbVOZC4vb6n-WN1j14v1uCxvq%{dIX|1(l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