# xdqc's solution

## to Bowling in the C# Track

Published at Dec 12 2018 · 1 comment
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.

## 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 Xunit;
using System;
using System.Collections.Generic;

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 = new int[0];
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 = new int[0];
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 = new int[0];
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")]
{
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(ICollection<int> rolls, BowlingGame sut)
{
foreach (var roll in rolls)
{
sut.Roll(roll);
}
}
}``````
``````using System;

public class BowlingGame
{
private int? frame = 0, score = 0, op = null, pT = 'o', ppT = 'o'; //op: knocked pins by first throw  //(previous of) previous roll Type: x-strike, s-spare, o-open, i-fillball
public void Roll(int pins) => (score, op, frame, (ppT, pT)) = (score + (pins < 0 || pins > 10 || pT == 'o' && frame == 10 || op + pins > 10 ? throw new ArgumentException() : pins * (1 + Convert.ToInt16(pT != 'o' && frame < 10) + Convert.ToInt16(ppT == 'x'))), !op.HasValue && pins < 10 ? (int?)pins : null, frame + Convert.ToInt16((!op.HasValue && pins < 10 ? (int?)pins : null) == null && frame < 10), (frame == 10 ? 'i' : pins == 10 ? 'x' : op + pins == 10 ? 's' : 'o') == 'i' ? ('o', pT == 'x' ? 's' : 'o') : (pT, (frame == 10 ? 'i' : pins == 10 ? 'x' : op + pins == 10 ? 's' : 'o')));
public int? Score() => frame == 10 && pT == 'o' ? score : throw new ArgumentException();
}``````