Bowling

Aug 25, 2022 12:25:16 PM · Last updated: 28 Sep 2023

Summary

This kata will help you to practise the “driving” part of Test Driven Development. By moving in small steps and reverting back when you make a bad decision, you are able to move forward more confidently, knowing that your previous tests make it easy to recover from a mistake. Small steps mean fast feedback, so it will be easy to identify where you went wrong, and you will be more comfortable experimenting with your approach.

Instructions

Write a program to score a game of Ten-Pin Bowling.

You should create a class or module that exposes two methods or functions:

void roll(int pins)
int score()

Ten-Pin Bowling Rules

A game of bowling is made up of ten "frames". In each frame a player has two rolls of a ball to try to knock down all 10 pins. 

The number of pins knocked down in a frame is the score for that frame, and the game score is the total of the scores from each frame.

Spare

If a player knocks down all 10 pins with two rolls in a frame, this is a Spare. The score from their next roll is added to the 10 points for the spare.

Frame 1 2
Rolls 6 4 5 3
Score 15 (6 + 4 + 5) 8
 
Strike

If a player knocks down all 10 pins with a single roll, this is a Strike. The frame is over, and the score from their next two rolls is added to the 10 points for the strike.

Frame 1 2
Rolls 10 - 5 3
Score 18 (10 + 5 + 3) 8

 

Note: since the true score for a Strike or Spare is dependent on the next ball or two, if a player scores a Strike or a Spare in the final (10th) frame, then they can roll the additional balls required to finish calculating the score.

To see the scoring system in action, you can use this app: http://www.bowlinggenius.com/

Example Games

[9,0] [9,0] [9,0] [9,0] [9,0] [9,0] [9,0] [9,0] [9,0] [9,0] = 90

[6,3] [5,2] [8,1] [3,2] [5,1] [5,3] [4,0] [4,3] [2,1] [6,2] = 66

[10] [10] [10] [10] [10] [10] [10] [10] [10] [10, 10, 10] = 300

A perfect game - strikes in every frame, plus the two balls required to finish calculating the score for the final frame. 10 frames, each scoring 30 points.

[5,5] [5,5] [5,5] [5,5] [5,5] [5,5] [5,5] [5,5] [5,5] [5,5,5] = 150

Five pins hit with every roll (making each frame a Spare), plus the extra ball required to finish calculating the score for the final frame, which also hits five pins. 10 frames, each scoring 15 points.

Extra Credit - The Outside-In Approach

If you'd like an extra challenge, or just to practise Outside-In TDD, then consider trying the exercise with some adjusted constraints:

Do not expose the void roll(int pins) and int score() methods described in the instructions above. Instead, your class/module should expose only a single public method:

int score(string game)

The input to this function should be a string describing a bowling game, in the following format:

"X|7/|9-|X|-8|8/|-6|X|X|X||81"

1-9 : Number of pins hit
X : Strike
/ : Spare
- : Miss, no pins hit

| : Frame separator

|| : Marks start of 'bonus' balls at end of 10th frame

Here, you can begin with an expected input and output, but the next test you need to write is not always so obvious. It is advised you try this additional exercise only once you are comfortable with the basic skills of classical TDD such as moving in baby steps and employing the red-green-refactor cycle, and you may want to look at using the Outside-In style of TDD to guide you.

If you need to hone these skills, try the FizzBuzz kata or Leap Year kata. The Roman Numerals kata may also be useful, as the obvious progression of test cases helps teach you to evolve an algorithm in small steps.

Credit: Robert C. Martin, and Kata-Log

Articles

Solutions

Videos