Activity 5-1: TDD Practice 1 - Bowling Game Scorer

Practicing test-driven development (TDD)

This is a paired-programming exercise.

The goal of this activity is to practice the red-green-refactor cycle:

Use the red-green-refactor prop as you work through this activity to help you internalize the process.

General Rules to follow (TDD Rules):

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and not compiling is failing.
  3. You are not allowed to write any more production code than is sufficient to pass the failing test.

Bowling game scoring rules

  1. A bowling game consists of 10 frames.
  2. On each frame the player makes up to two throws.
  3. Each throw/roll knocks down a number of pins. Each pin knocked down scores a point.
  4. There is a total of 10 pins that can be knocked down on each frame.
  5. Knocking all 10 pins down with the two rolls of the frame is called a spare.
  6. The pins in the next roll after the spare are added to the spare frame’s score.
  7. Knocking all 10 pins down with the first of the frame rolls (and subsequently NOT doing a second roll) is called a strike.
  8. The pins in the next two rolls after a strike are added to the strike frame’s score.
  9. More rolls may occur after all 10 frames are completed, to account for the extra points allotted to spares/strikes on the last frame.
  10. Your score is the sum of your scores in all the frames.

 

Example: Typical game

Frame Roll 1 Roll 2 Special Extra Points Frame Score Game Score
1 1 4 0 5 5
2 4 5 0 9 14
3 6 4 Spare 5 15 29
4 5 5 Spare 10 20 49
5 10 Strike 1 11 60
6 0 1 0 1 61
7 7 3 Spare 6 16 77
8 6 4 Spare 10 20 97
9 10 Strike 10 20 117
10 2 8 Spare 6 16 133
6 133

 

Example: Perfect game

Frame Roll 1 Roll 2 Special Extra Points Frame Score Game Score
1 10 Strike 20 30 30
2 10 Strike 20 30 60
3 10 Strike 20 30 90
4 10 Strike 20 30 120
5 10 Strike 20 30 150
6 10 Strike 20 30 180
7 10 Strike 20 30 210
8 10 Strike 20 30 240
9 10 Strike 20 30 270
10 10 Strike 20 30 300
10 10 300

Development Steps

These are the steps (as represented by test cases) that you will be going through to develop the bowling game scorer.

  1. Getting empty test to compile
  2. Can create game
  3. Can roll
  4. Score a gutter game
  5. Score all ones game
  6. Score game with one spare and rest gutter balls
  7. Score game with one strike and rest gutter balls
  8. Score a perfect game

Step 1: Getting empty test to compile

RED
GREEN
REFACTOR

Step 2: Can create game

RED
GREEN
REFACTOR

Step 3: Can roll

RED
GREEN
REFACTOR

Step 4: Score a gutter game

RED
GREEN
REFACTOR

Step 5: Score all ones game

RED
GREEN
REFACTOR

Step 6: Score game with one spare and rest gutter balls

PRE-REFACTOR
RED
GREEN
REFACTOR

Step 7: Score game with one strike and rest gutter balls

RED
GREEN
REFACTOR

Step 8: Score a perfect game

RED
GREEN
REFACTOR