This actually works. You might note that I pre-calculated the score rather than store the two pins. I might have done the latter but actually chose to precalculate because it is simpler, and because it throws away the individual pin values, which I expect to cause trouble right away, when I work on SpareFrame with my next test.
First, though, I want to get rid of some duplication in the tests:. And it will face me with the problem of getting access to the throws in a subsequent Frame. That seems daunting, but I have an idea. Let's code a bit, since we have a red bar:. The code doesn't run, though, because the cast to OpenFrame doesn't work for the SpareFrame.
We need to give them a common interface. The tests run! Of course we have not solved the NextBall issue for the SpareFrame, so we'll write a test for that, just like Spare but with different values:. This fails, as expected. What shall we do??? Here is my cunning plan. We will create the ArrayList of throws again, and we will give each Frame the list, and the index of its first ball in the list.
I foresee some duplication coming, but for now let's try it. I'm sort of refactoring here -- changing the algorithm a bit -- but since I'm not at a green bar, I'm going to call it implementing. I hope that my readers will give some useful feedback on how they would work in this situation. The above didn't work, even when I changed the Score method in SpareFrame. The reason was that I added the throws, and then used the Count.
Wrong, should have done that first. I decided to move the throws. Add inside the Frame classes as well, resulting in this:. The oddest thing here is that BowlingGame holds on to the throws list but doesn't use it.
I think he is asking to have a FrameFactory extracted or something like that. But the tests now run! Since the list is inside the Frames, I can remove the count parameter, resulting in this:.
Tests still run! This is starting to look nearly OK. I think I'll change the Score calc on OpenFrame to be dynamic, thus:. This gives me some duplication that I don't like, " int throws[ First I'll do the StrikeFrame, then remove duplication.
And before that, I'm taking a nap. I flew in on the red-eye last night and boy are my arms tired. The tests work! I'll make that change to SpareFrame, but notice that the definition will be different, as the StrikeFrame refers to the current ball plus one, while the SpareFrame will have plus two.
We could address that, perhaps, by having a separate constant for frame size in the two classes. That seems a bit too speculative, and anyway we need to work to remove the duplication. Taking OpenFrame as an example, they all share this duplicated code:.
We have to ask ourselves whether this duplication is worth eliminating. We'll have to set up a Frame superclass with those variables in it, and the first two lines of the constructor duplicated.
Then the subclasses will include some separate code in the constructor and of course their unique Score method. The superclass subclass thing will let us replace the interface with the superclass if we want to, but that's pretty much a wash. There are two ways one could go. One could choose one of the concrete classes to be the base class, then override behavior in the subclasses, or we could make an abstract superclass.
I'll go with the latter, on the grounds that it is safer. In the subclasses, if we forgot to override the Score method from a concrete superclass, we would have a mistake. Our tests would find it, of course, but the abstract class is still the "right" thing to do, I believe.
I'll manufacture the abstract class from IFrame and then add the subclasses one at a time, removing duplication as I go. I'll start by creating the Frame class:. Here are some things that the program will not do:. Depending on the application, this might or might not be a valid way to define a complete story, but we do it here for purposes of keeping the kata light. More info on the rules at: How to Score for Bowling.
What makes this game interesting to score is the lookahead in the scoring for strike and spare. At the time we throw a strike or spare, we cannot calculate the frame score: we have to wait one or two frames to find out what the bonus is.
Throw 10 ; bowling. Throw 3 ; bowling. Equal 24, bowling. Example 5. Throw pins1 ; bowling. Throw pins2 ; bowling. Equal 16, bowling. Example 6. Game ; bowling. Equal 10, score ; Assert. True scoreBoard. Example 7. True bowling. Example 8. Throw 1 ; Assert. Equal 1, bowling. Implementation of the bowling kata in python.
Updated Oct 24, Python. Bowling score calculator sample code. The Raddest and Baddest console bowling simulator. Updated Feb 23, C. Updated Jun 21, C.
Updated Oct 28, Java. Updated May 5, JavaScript. Updated Mar 14, Java. Updated Jan 30, Java. Open Print special characters into the score board for a spare or strike. Strike: Instead of showing "10", show a "X" instead. Read more.
A bowling game for lucky people. No skills are required! Updated Feb 9, JavaScript. Updated Oct 22, Java. Updated Jan 25, JavaScript.
0コメント