Skip to content
Katabench
Try free

Test the Bowling Scorer

Hard About 30 min Free, no account needed

You're given a correct ten-pin bowling scorer. Write the tests that exercise spares, strikes, and the bonus rolls.

The brief

Ten-pin scoring is deceptively fiddly: spares and strikes borrow pins from future rolls. The scorer here is already written and correct. Your job is to write the tests that prove it.

Here is the code under test:

C#
public class BowlingGame
{
    // A spare adds the next roll as a bonus; a strike adds the next two rolls.
    public int Score(int[] rolls)
    {
        int score = 0, i = 0;
        for (int frame = 0; frame < 10; frame++)
        {
            if (rolls[i] == 10)                       // strike
            { score += 10 + rolls[i + 1] + rolls[i + 2]; i += 1; }
            else if (rolls[i] + rolls[i + 1] == 10)   // spare
            { score += 10 + rolls[i + 2]; i += 2; }
            else                                      // open frame
            { score += rolls[i] + rolls[i + 1]; i += 2; }
        }
        return score;
    }
}

Write a Tests class. Each public, parameterless method is one test: it passes if it runs without throwing and fails when an assertion throws. A tiny assertion library is in scope (no using needed):

C#
Assert.True(condition);          Assert.False(condition);
Assert.Equal(expected, actual);  Assert.NotEqual(a, b);
Assert.Null(x);                  Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);

The input is the array of rolls for a finished game. new int[20] is twenty gutter balls. A spare or strike near the end is followed by its bonus rolls, so a game can be 12 to 21 rolls long.

How you're graded

Your suite is run twice over:

  1. Against the correct BowlingGame above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken scorers you never see. A mutant is caught when at least one of your tests fails against it.

You pass when your suite is green on the correct code and catches at least 5 of the 6 mutants. The mutants drop a spare or strike bonus, count the wrong number of frames, or misread an open frame. A gutter game catches almost none of them. Construct small games that isolate the spare bonus, the strike bonus, and the perfect game. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `BowlingGame` class. Each public, parameterless method is one test; it passes
// if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy versions.
// A gutter game proves almost nothing. The real logic is in the spare and strike bonuses.
public class Tests
{
    public void A_gutter_game_scores_zero()
    {
        Assert.Equal(0, new BowlingGame().Score(new int[20]));
    }

    // TODO: add tests for open frames, a single spare, a single strike, and a perfect game.
}

How it is graded

You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.

C# unit testing exercises →

What it practices

Equivalence partitioning
Cover each class of input and every branch with one deliberate case.
Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas