code blog foo - tag line bar

Awesome Code Katas

I've been doing quite a few Code Katas, especially at Dallas Hack Club. Here is a list of code katas that you may want to try out. I recommend tackling them in the order they are listed :-).

Bowling Kata - Reinforcement

Now that you've gone through the [Prime Factors Kata], reinforce what you've learned with the Bowling Kata. Keep the same things in mind. Write a failing test, write just enough code to make that specific test pass, keep things dry, keep things simple.

Explanation

Develop a function - not a complicated class structure ;-) - that when given a string representing a bowling score, it returns a number representing the bowling score. This is another kata I'll walk you through.

The First Test

In bowling, the bowler gets two attempts to clear the pins in a frame. If the player never rolls a spare or a strike, then the score is simply summed up.

Start with this test. If you are given these 10 frames: "31415390107133238009" the score is the sum of each digit, in this case the answer is 3+1+4+1...+8+0+0+9=63

The Next Test - Spares

Let's introduce the concept of a spare. If a bowler gets a spare (meaning he knocks down all 10 pins in two attempts, in a single frame), his score is doubled for the next roll. If you are given these 10 frames: "91415390107133238009", then the sum looks like the following 9+1(spare detected)+2*(4)+1+5+3+.....+8+0+0+9=73. Keep in mind that a value is considered a spare only if the pins belong to the same frame.

The Next Next Test - Strikes

Let's introduce the concept of a strike. If a bowler gets a strike (meaning he knocks down all 10 pins in one attempt in a single frame), his score is doubled for the next two rolls. If you are given these 10 frames: "X-415390107133238009" (the "-" is not considered a roll), then the sum looks like the following 10+0(the strike X-)+2*(4)+2*(1)+5+3+.....+8+0+0+9=74

You're on Your Own Now

Make sure your implementation works for any combination of strikes and spares. Chain strikes together and make sure the score is correct.

If the last frame ends up being a spare, you get one extra roll (which will be doubled). If the roll on the last frame is a strike, then you get two extra rolls (each of which are doubled).


Written: 3/10/2012