|
|
@ -4,6 +4,10 @@ import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
public class BownlingCalculator { |
|
|
|
private static final int SINGLE_DIGIT = 0; |
|
|
|
private static final int STRIKE_SECOND_NEXT_THROW = 2; |
|
|
|
private static final int STRIKE_NEXT_THROW = 1; |
|
|
|
private static final String STRIKE = "X (\\d)(\\d)"; |
|
|
|
private static final int PINS_IN_FRAME = 10; |
|
|
|
private static final String SPARE = "(\\d)/ (\\d)"; |
|
|
|
private static final String INCOMPLETE_FRAME = "\\d"; |
|
|
@ -18,16 +22,23 @@ public class BownlingCalculator { |
|
|
|
|
|
|
|
public int evaluate(String listOfThrows) { |
|
|
|
int sum = 0; |
|
|
|
sum += evaluateFrames(listOfThrows, INCOMPLETE_FRAME, m -> Integer.parseInt(m.group())); |
|
|
|
sum += evaluateFrames(listOfThrows, SPARE, m -> PINS_IN_FRAME// |
|
|
|
- Integer.parseInt(m.group(SPARE_FRAME_FIRST_THROW)) |
|
|
|
+ Integer.parseInt(m.group(SPARE_FRAME_FOLLOWING_THROW))); |
|
|
|
sum += evaluateFrames(listOfThrows, "X (\\d)(\\d)", m -> PINS_IN_FRAME// |
|
|
|
+ Integer.parseInt(m.group(1)) |
|
|
|
+ Integer.parseInt(m.group(2))); |
|
|
|
sum += evaluateFrames(listOfThrows, INCOMPLETE_FRAME, |
|
|
|
singleDigit -> convertMatchToInt(singleDigit, SINGLE_DIGIT)); |
|
|
|
sum += evaluateFrames(listOfThrows, SPARE, // trailing line comments for formatting |
|
|
|
spare -> PINS_IN_FRAME // |
|
|
|
- convertMatchToInt(spare, SPARE_FRAME_FIRST_THROW) |
|
|
|
+ convertMatchToInt(spare, SPARE_FRAME_FOLLOWING_THROW)); |
|
|
|
sum += evaluateFrames(listOfThrows, STRIKE, // |
|
|
|
strike -> PINS_IN_FRAME // |
|
|
|
+ convertMatchToInt(strike, STRIKE_NEXT_THROW) // |
|
|
|
+ convertMatchToInt(strike, STRIKE_SECOND_NEXT_THROW)); |
|
|
|
return sum; |
|
|
|
} |
|
|
|
|
|
|
|
private int convertMatchToInt(Matcher strike, int groupIndex) { |
|
|
|
return Integer.parseInt(strike.group(groupIndex)); |
|
|
|
} |
|
|
|
|
|
|
|
private int evaluateFrames(String listOfThrows, String frameStructurePattern, FrameCalculator frameCalculator) { |
|
|
|
Matcher singleDigit = Pattern.compile(frameStructurePattern).matcher(listOfThrows); |
|
|
|
int frameValue = 0; |
|
|
|