Unit Testing – C# – Part 01 – Tenpin Bowling Scorer

In this tutorial series I will be showing how we can develop a ten pin bowling class to calculate a players score using Unit Testing. This makes a difference from the usual calculator demos you normally see. There are myriad of sources that explain Unit Testing and implementing classes using Test Driven Development (TDD), so my aim is to follow these processes and produce a class. This is not intended to teach you Unit Testing or TDD from scratch.

Why am I building this Class using Unit Testing

Whilst it helps start our developing classes one of the usual answers is that you can be comfortable that future updates do not break existing functionality. The biggest benefit for me however; is that I don’t have to wait for someone to throw a 300 game to know if my code works. I can plug in all the difference permutations and know if it all works immediately. That’s the big win. If your class has external dependencies, trying to get it to produce everything it may throw at you could almost be time consuming impossible if you had to do it through the application itself. A good example could be testing for 90% disk free space on your C: drive and then carry out an action. You don’t want to be filling your own C: drive with dummy data just to test this.

Scoring Rules

The first thing we need to do is be clear on the rules for scoring.

  • Each game consists of 10 frames.
  • A bowler will throw either 1 or 2 balls in each frame. Only 1 ball if he knocks down all ten pins with the first ball. This is known as a Strike.
  • If he knocks down all 10 pins with 2 balls, this is known as a Spare.
  • In 10th (Last) Frame if a strike is thrown, two extra bowls are allowed to be thrown at 10 new pins.
  • In 10th (Last) Frame if a spare is thrown, one extra bowl is allowed to be thrown at 10 new pins.

In order to score the bowls we add them as follows:

  • The scores start a zero and each pin is worth 1 point.
  • If 2 bowls thrown do not knock down all the pins, the sum of the pins knocked down by those two bowls are added to score.
  • If a Strike is thrown, you add 10 points plus the value of the next 2 bowls (Maximum of 20) to the score.
  • If a Spare is thrown, you add 10 points plus the value of the next bowl (Maximum of 10) to the score.
  • If a player does 12 Strikes in a frame (Perfect Game), the maximum score is 300 points.

Business Requirements

We need a Scorer class which has the following requirements:

  • Enter the player’s name.
  • Enter the number of pins knocked down by each bowl by frame.
  • Get the player’s name.
  • Get the total Score.
  • Get the score card for a frame.
  • Get the number of Strikes and Spares.

Notice there is no display aspects in the business rules. We don’t need to know if the data will be displayed on a web page, in a WPF application or extracted by a REST API for example. This is the beauty of layered architectures.

Unit Testing Framework

I will be developing the class in C# using Visual Studio 2017. The blog will show the examples using NUnit however; I will be including XUnit and MSTest examples as well.

In the next blog I will explain how we setup the visual studio project.