Avatar of josephfash11

josephfash11's solution

to Bowling in the C# Track

Published at Mar 15 2019 · 0 comments
Instructions
Test suite
Solution

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.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Bowling.cs

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

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));
    }

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


public class BowlingGame
{
    private class Frame
    {
        public int FirstBowl { get; set; } = 0;
        public int SecondBowl { get; set; } = 0;
        public int Score => FirstBowl + SecondBowl;
        public bool IsSpare => (Score == 10) && (FirstBowl != 10);
        public bool IsStrike => FirstBowl == 10;
        public bool IsClosed { get; set; } = false;
        public Frame(int firstBowl) => FirstBowl = firstBowl;
    }

    private readonly List<Frame> rolls = new List<Frame>();
    private bool isFirstBowl = true;


    public void Roll(int pins)
    {
        if ((pins < 0) || (pins > 10))
        {
            throw new ArgumentException($"Score '{pins}' is not in range [0,10]."); ;
        }

        if ((rolls.Count == 11) && rolls[9].IsSpare)
        {
            throw new ArgumentException("Cannot roll after bonus roll for spare.");
        }

        if ((rolls.Count == 11) && rolls[10].IsClosed && !rolls[10].IsStrike)
        {
            throw new ArgumentException("Cannot roll after bonus rolls for strike.");
        }

        if ((rolls.Count == 10) && rolls[9].IsClosed && !rolls[9].IsSpare && !rolls[9].IsStrike)
        {
            throw new ArgumentException("Cannot roll if game already has ten frames.");
        }

        if (isFirstBowl)
        {
            rolls.Add(new Frame(pins));
            isFirstBowl = pins == 10;
            rolls.Last().IsClosed = pins == 10;
        }
        else
        {
            var last = rolls.Last();

            if ((last.FirstBowl + pins) > 10)
            {
                throw new ArgumentException("Two rolls in a frame cannot score more than 10 points.");
            }

            last.SecondBowl = pins;
            last.IsClosed = true;
            isFirstBowl = true;
        }
    }

    public int? Score()
    {
        if (rolls.Count == 0)
        {
            throw new ArgumentException("An unstarted game cannot be scored.");
        }

        if (rolls.Count < 10)
        {
            throw new ArgumentException("An incomplete game cannot be scored.");
        }

        if ((rolls.Count == 10) && (rolls.Last().IsStrike || rolls.Last().IsSpare))
        {
            throw new ArgumentException("Bonus rolls for a strike in the last frame must be rolled before score can be calculated.");
        }

        var score = 0;
        Frame frame;

        for (var i = 0; i < 10; ++i)
        {
            frame = rolls[i];

            if (frame.IsStrike)
            {
                score += 10 + GetStrikeBonus(i);
            }
            else if (frame.IsSpare)
            {
                score += 10 + rolls[i + 1].FirstBowl;
            }
            else
            {
                score += frame.Score;
            }
        }

        return score;
    }


    private int GetStrikeBonus(int roll)
    {
        var bonus = rolls[roll + 1].FirstBowl;

        if (bonus == 10)
        {
            if (rolls.Count <= roll + 2)
            {
                throw new ArgumentException("Not enough rolls to compute strike bonus.");
            }

            bonus += rolls[roll + 2].FirstBowl;
        }
        else
        {
            bonus += rolls[roll + 1].SecondBowl;
        }

        return bonus;
    }
}

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?