From c097ddf4bcafe6e6e68a2efe64dca26bfb9704a0 Mon Sep 17 00:00:00 2001 From: binsky Date: Wed, 24 Nov 2021 10:50:59 +0100 Subject: [PATCH] add implementation for the roll after spare counts twice test --- src/main/java/BowlingGameCalculator.java | 10 +++++++++- src/test/java/BowlingCalculatorTest.java | 3 ++- target/classes/BowlingGameCalculator.class | Bin 1084 -> 1253 bytes .../test-classes/BowlingCalculatorTest.class | Bin 1867 -> 1982 bytes 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/BowlingGameCalculator.java b/src/main/java/BowlingGameCalculator.java index 27ce370..8ea8bd1 100644 --- a/src/main/java/BowlingGameCalculator.java +++ b/src/main/java/BowlingGameCalculator.java @@ -4,14 +4,22 @@ import java.util.regex.Pattern; public class BowlingGameCalculator { 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; public int score(String playerResult) { - Matcher singleDigit = SINGLE_VALUE_PATTERN.matcher(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)); + } + return playerScore; } } diff --git a/src/test/java/BowlingCalculatorTest.java b/src/test/java/BowlingCalculatorTest.java index 91755ee..a63a75a 100644 --- a/src/test/java/BowlingCalculatorTest.java +++ b/src/test/java/BowlingCalculatorTest.java @@ -22,7 +22,8 @@ public class BowlingCalculatorTest { 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("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 ); } } diff --git a/target/classes/BowlingGameCalculator.class b/target/classes/BowlingGameCalculator.class index cb9921db9820be5adf5e7eeb49e584fc0b00d9ad..e06f1b25c447aefc91fb5e08799630fd35bd51b1 100644 GIT binary patch delta 579 zcmX|8%}x_h7(I77wbSWTpe-SVY79{NL(2dnN=wy(je(GcP*fL0cEMI<1tbO(im!f7nu$X!va3@!?SeiV=}%} zPBd@Dk#EzszEKLbvq9u3MUMa0H_ec+OtI7#RX? zJYs#pFD6kC-N|!^V}~-%D#w6|m#jvMfLEB{NidsUR(biF3^#Gwl{;y`J9q|vF@1*M z0oo(P@CBZH%6tU7sfMiI@ugD>FT#&nap|SdC)(;s;2iouy z0mKlb-4OEVa(s=ocK`F$pbca(NoxuzIlfup^$EPj8w$q|!CQ7JXP~5d2VHm&3jTpc q2y^Gu|4`D-i%AP4E--isZ@_f0PX;6$lRDL9Od9{Y;ARRdc=rIm1XpPQ delta 389 zcmXYsyG{Z@6o$W<1$TE?ynr!Cg7L0g6vaCb9>B!P))K7oxEXoAET zAHer8R>mjra%ORg|C~AVpZ}l#-GA}S+t2eWP^I2B<)n{??_vTH1nqjJb68U;k6DF2 zI57+|AF#k;_@Sy{Mc;>?I&TseXU(hA%l+f?Fzf8LJhbR(vQ|R6EO`njiqW)$BYm6! z!6liLr7EfM!V{_!QI-YmGsNT>5s3(_uqro2g=s;%d@VLHsgn=5#V*M`r%`HkN#8J> zD|H#~kvnSK{(j}|gydj_$HW_q#7qbVjF4o4p~$Gv7yCC7S=Lw=D~}8tY>JtY{Vhr3 qupO!BZ;Z>RN(*PFpUM7sN|x;LQ!_p09&v3j75VprS**%TQ~Luuf-Hmp diff --git a/target/test-classes/BowlingCalculatorTest.class b/target/test-classes/BowlingCalculatorTest.class index fd22f8486adae85c756799510899648a4620ab5f..b58c7dd1dda9614c569fc88d09e88735eb8f07f2 100644 GIT binary patch delta 416 zcmX|+%Su9F6otS4=sB867YABWaA=frAftZr9J0b zb{b7Wz=J}^5S)=|NV<})$DdJ;LNsOtn`>xePB8c0wLWbhc$55!H@(vJSiK3CW$H9QFxF z9W+BlbdOIEYQ8#sw#TUBz8Twj+YqMNp3rE{_J+oDw%!$dVIcRypwz^JI8^Iv(bl11 Z35zfy@jLW#K@^8Dc~NN`it?z%>|an-J=g#M delta 333 zcmX|+JyHTu5QM+?*4-^uM8GQ0%+Mtjhs3BQGxcgX#ynuTm!cNl8giQc{^6W%t-%D1EQ4