Avatar of ChrisPritchard

ChrisPritchard's solution

to Bowling in the C# Track

Published at Sep 21 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Score a bowling game.

Bowling is a game where players roll a heavy ball to knock down pins arranged in a triangle. Write code to keep track of the score of a game of bowling.

Scoring Bowling

The game consists of 10 frames. A frame is composed of one or two ball throws with 10 pins standing at frame initialization. There are three cases for the tabulation of a frame.

  • An open frame is where a score of less than 10 is recorded for the frame. In this case the score for the frame is the number of pins knocked down.

  • A spare is where all ten pins are knocked down by the second throw. The total value of a spare is 10 plus the number of pins knocked down in their next throw.

  • A strike is where all ten pins are knocked down by the first throw. The total value of a strike is 10 plus the number of pins knocked down in the next two throws. If a strike is immediately followed by a second strike, then the value of the first strike cannot be determined until the ball is thrown one more time.

Here is a three frame example:

Frame 1 Frame 2 Frame 3
X (strike) 5/ (spare) 9 0 (open frame)

Frame 1 is (10 + 5 + 5) = 20

Frame 2 is (5 + 5 + 9) = 19

Frame 3 is (9 + 0) = 9

This means the current running total is 48.

The tenth frame in the game is a special case. If someone throws a strike or a spare then they get a fill ball. Fill balls exist to calculate the total of the 10th frame. Scoring a strike or spare on the fill ball does not give the player more fill balls. The total value of the 10th frame is the total number of pins knocked down.

For a tenth frame of X1/ (strike and a spare), the total value is 20.

For a tenth frame of XXX (three strikes), the total value is 30.

Requirements

Write code to keep track of the score of a game of bowling. It should support two operations:

  • roll(pins : int) is called each time the player rolls a ball. The argument is the number of pins knocked down.
  • score() : int is called only at the very end of the game. It returns the total score for that game.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

The Bowling Game Kata at but UncleBob http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

BowlingTest.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using System;
using System.Collections.Generic;
using Xunit;

public class BowlingTest
{
    [Fact]
    public void Should_be_able_to_score_a_game_with_all_zeros()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(0, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Should_be_able_to_score_a_game_with_no_strikes_or_spares()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(90, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_spare_followed_by_zeros_is_worth_ten_points()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(10, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Points_scored_in_the_roll_after_a_spare_are_counted_twice()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(16, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Consecutive_spares_each_get_a_one_roll_bonus()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(31, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(17, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_strike_earns_ten_points_in_a_frame_with_a_single_roll()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(10, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(26, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Consecutive_strikes_each_get_the_two_roll_bonus()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(81, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(18, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(20, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(30, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(20, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void All_strikes_is_a_perfect_game()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(300, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rolls_cannot_score_negative_points()
    {
        var sut = new BowlingGame();
        var previousRolls = Array.Empty<int>();
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(-1));
    }

    [Fact(Skip = "Remove to run test")]
    public void A_roll_cannot_score_more_than_10_points()
    {
        var sut = new BowlingGame();
        var previousRolls = Array.Empty<int>();
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(11));
    }

    [Fact(Skip = "Remove to run test")]
    public void Two_rolls_in_a_frame_cannot_score_more_than_10_points()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 5 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(6));
    }

    [Fact(Skip = "Remove to run test")]
    public void Bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(11));
    }

    [Fact(Skip = "Remove to run test")]
    public void Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(6));
    }

    [Fact(Skip = "Remove to run test")]
    public void Two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6 };
        DoRoll(previousRolls, sut);
        var actual = sut.Score();
        Assert.Equal(26, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void The_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(11));
    }

    [Fact(Skip = "Remove to run test")]
    public void An_unstarted_game_cannot_be_scored()
    {
        var sut = new BowlingGame();
        var previousRolls = Array.Empty<int>();
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Score());
    }

    [Fact(Skip = "Remove to run test")]
    public void An_incomplete_game_cannot_be_scored()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Score());
    }

    [Fact(Skip = "Remove to run test")]
    public void Cannot_roll_if_game_already_has_ten_frames()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Score());
    }

    [Fact(Skip = "Remove to run test")]
    public void Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Score());
    }

    [Fact(Skip = "Remove to run test")]
    public void Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Score());
    }

    [Fact(Skip = "Remove to run test")]
    public void Cannot_roll_after_bonus_roll_for_spare()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Cannot_roll_after_bonus_rolls_for_strike()
    {
        var sut = new BowlingGame();
        var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2 };
        DoRoll(previousRolls, sut);
        Assert.Throws<ArgumentException>(() => sut.Roll(2));
    }

    public void DoRoll(IEnumerable<int> rolls, BowlingGame sut)
    {
        foreach (var roll in rolls)
        {
            sut.Roll(roll);
        }
    }
}
using System;
using System.Linq;
using System.Collections.Generic;

public class BowlingGame
{
    private List<int> currentFrame;
    private readonly List<int[]> frames = new List<int[]>();

    private bool IsFinished() => frames.Count >= 10 && (frames.Count != 10 || frames[9].Sum() != 10);

    public void Roll(int pins) 
    {
        if(pins < 0 || pins > 10 || IsFinished())
            throw new ArgumentException();

        if(currentFrame == null)
            currentFrame = new List<int> { pins };
        else
            currentFrame.Add(pins);
        
        var frameSum = currentFrame.Sum();
        if(frameSum > 10 && (frames.Count != 10 || currentFrame[0] != 10))
            throw new ArgumentException();
        
        if((frameSum == 10 && frames.Count != 10) 
        || currentFrame.Count == 2 
        || (frames.Count == 10 && frames[9].Length != 1))
        {
            frames.Add(currentFrame.ToArray());
            currentFrame = null;
        }
    }

    public int? Score()
    {
        if(!IsFinished())
            throw new ArgumentException();

        return Enumerable.Range(0, 10).Select(i => 
        {
            var frame = frames[i];
            var score = frame.Sum();
            if(score == 10 && frame.Length == 2)
                return score + frames[i+1][0];
            else if(score == 10 && frame.Length == 1)
                return 
                    frames[i+1].Length == 1 
                    ? score + frames[i+1][0] + frames[i+2][0]
                    : score + frames[i+1].Sum();
            else return score;
        }).Sum();
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?