ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# Greycoat21's solution

## to Bowling in the Delphi Pascal Track

Published at Feb 19 2020 · 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.

## Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

### Loading Exercises into Delphi

If Delphi is properly installed, and `*.dpr` file types have been associated with Delphi, then double clicking the supplied `*.dpr` file will start Delphi and load the exercise/project. `control + F9` is the keyboard shortcut to compile the project or pressing `F9` will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the `File` drop down menu.

### When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>` directory.

For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`.

## 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 may request help from a mentor.

### uBowlingTests.pas

``````unit uBowlingTests;

interface
uses
DUnitX.TestFramework,
uBowling;

const
CanonicalVersion = '1.2.0';

type

[TestFixture]
BowlingTests = class(TObject)
private
class function RollMany(pins: array of integer; game: IBowlingGame): IBowlingGame; static;
public
[Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
procedure Should_be_able_to_score_a_game_with_all_zeros;

[Test]
[Ignore]
procedure Should_be_able_to_score_a_game_with_no_strikes_or_spares;

[Test]
[Ignore]
procedure A_spare_followed_by_zeros_is_worth_ten_points;

[Test]
[Ignore]
procedure Points_scored_in_the_roll_after_a_spare_are_counted_twice;

[Test]
[Ignore]
procedure Consecutive_spares_each_get_a_one_roll_bonus;

[Test]
[Ignore]
procedure A_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once;

[Test]
[Ignore]
procedure A_strike_earns_ten_points_in_frame_with_a_single_roll;

[Test]
[Ignore]
procedure Points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus;

[Test]
[Ignore]
procedure Consecutive_strikes_each_get_the_two_roll_bonus;

[Test]
[Ignore]
procedure A_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once;

[Test]
[Ignore]
procedure Rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll;

[Test]
[Ignore]
procedure Strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls;

[Test]
[Ignore]
procedure A_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus;

[Test]
[Ignore]
procedure All_strikes_is_a_perfect_game;

[Test]
[Ignore]
procedure Rolls_cannot_score_negative_points;

[Test]
[Ignore]
procedure A_roll_cannot_score_more_than_10_points;

[Test]
[Ignore]
procedure Two_rolls_in_a_frame_cannot_score_more_than_10_points;

[Test]
[Ignore]
procedure Bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;

[Test]
[Ignore]
procedure Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;

[Test]
[Ignore]
procedure Two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike;

[Test]
[Ignore]
procedure The_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike;

[Test]
[Ignore]
procedure Second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;

[Test]
[Ignore]
procedure An_unstarted_game_cannot_be_scored;

[Test]
[Ignore]
procedure An_incomplete_game_cannot_be_scored;

[Test]
[Ignore]
procedure Cannot_roll_if_game_already_has_ten_frames;

[Test]
[Ignore]
procedure Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;

[Test]
[Ignore]
procedure Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;

[Test]
[Ignore]
procedure Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;

[Test]
[Ignore]
procedure Cannot_roll_after_bonus_roll_for_spare;

[Test]
[Ignore]
procedure Cannot_roll_after_bonus_rolls_for_strike;
end;

implementation
uses System.SysUtils;

procedure BowlingTests.Second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10], NewBowlingGame);
Assert.IsFalse(game.Roll(11));
end;

procedure BowlingTests.Should_be_able_to_score_a_game_with_all_zeros;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(0, game.Score);
end;

procedure BowlingTests.Should_be_able_to_score_a_game_with_no_strikes_or_spares;
var game: IBowlingGame;
begin
game := RollMany([3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6], NewBowlingGame);
assert.AreEqual(90, game.Score);
end;

procedure BowlingTests.A_spare_followed_by_zeros_is_worth_ten_points;
var game: IBowlingGame;
begin
game := RollMany([6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(10, game.Score);
end;

procedure BowlingTests.Points_scored_in_the_roll_after_a_spare_are_counted_twice;
var game: IBowlingGame;
begin
game := RollMany([6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(16, game.Score);
end;

procedure BowlingTests.Consecutive_spares_each_get_a_one_roll_bonus;
var game: IBowlingGame;
begin
game := RollMany([5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(31, game.Score);
end;

procedure BowlingTests.A_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7], NewBowlingGame);
assert.AreEqual(17, game.Score);
end;

procedure BowlingTests.A_strike_earns_ten_points_in_frame_with_a_single_roll;
var game: IBowlingGame;
begin
game := RollMany([10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(10, game.Score);
end;

procedure BowlingTests.Points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus;
var game: IBowlingGame;
begin
game := RollMany([10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(26, game.Score);
end;

procedure BowlingTests.Consecutive_strikes_each_get_the_two_roll_bonus;
var game: IBowlingGame;
begin
game := RollMany([10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
assert.AreEqual(81, game.Score);
end;

procedure BowlingTests.A_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1], NewBowlingGame);
assert.AreEqual(18, game.Score);
end;

procedure BowlingTests.Rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3], NewBowlingGame);
assert.AreEqual(20, game.Score);
end;

procedure BowlingTests.Strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10], NewBowlingGame);
assert.AreEqual(30, game.Score);
end;

procedure BowlingTests.A_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10], NewBowlingGame);
assert.AreEqual(20, game.Score);
end;

procedure BowlingTests.All_strikes_is_a_perfect_game;
var game: IBowlingGame;
begin
game := RollMany([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], NewBowlingGame);
assert.AreEqual(300, game.Score);
end;

procedure BowlingTests.Rolls_cannot_score_negative_points;
var game: IBowlingGame;
begin
game := RollMany([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.A_roll_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
game := RollMany([11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Two_rolls_in_a_frame_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
game := RollMany([5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.The_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6], NewBowlingGame);
Assert.IsFalse(game.Roll(10));
end;

procedure BowlingTests.Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 6], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6], NewBowlingGame);
Assert.AreEqual(26, game.Score);
end;

procedure BowlingTests.An_unstarted_game_cannot_be_scored;
var game: IBowlingGame;
begin
game := RollMany([],NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.An_incomplete_game_cannot_be_scored;
var game: IBowlingGame;
begin
game := RollMany([0, 0], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Cannot_roll_after_bonus_rolls_for_strike;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2], NewBowlingGame);
Assert.IsFalse(game.Roll(2));
end;

procedure BowlingTests.Cannot_roll_after_bonus_roll_for_spare;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2], NewBowlingGame);
Assert.IsFalse(game.Roll(2));
end;

procedure BowlingTests.Cannot_roll_if_game_already_has_ten_frames;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
Assert.IsFalse(game.Roll(0));
end;

procedure BowlingTests.Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10], NewBowlingGame);
Assert.IsFalse(game.Roll(11));
end;

procedure BowlingTests.Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
var game: IBowlingGame;
begin
game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3], NewBowlingGame);
Assert.AreEqual(-1, game.Score);
end;

class function BowlingTests.RollMany(pins: array of integer; game: IBowlingGame): IBowlingGame;
var count: integer;
begin
for count in pins do
game.Roll(count);
result := game;
end;

initialization
TDUnitX.RegisterTestFixture(BowlingTests);
end.``````
``````unit uBowling;

interface

type
IBowlingGame = interface
function Roll(Pins: Integer): Boolean;
function Score: Integer;
end;

function NewBowlingGame: IBowlingGame;

implementation

uses
Generics.Collections;

type
{ fsBonus frames have their scores ignored, they are only used for
determing any bonus points in the event that bonus rolls are earned on
the last frame.
fsNormal frames are the regular 10 frames of a bowling game whose points
are totaled for the final game score.
fsError frames are any frames with bad/invalid rolls such rolls
'knocking down' either negative pins, or more pins than are available in
the lane. }
TFrameStatus = (fsBonus, fsNormal, fsError);

TFrame = class
private const
kPinsPerFrame = 10;
kNormalRollsPerFrame = 2;
private var
FScore, FPinsRemaining, FRollsRemaining, FBonusRollsRemaining: Integer;
FFrameStatus: TFrameStatus;
public
function Roll(Pins: Integer): Boolean;
function AddBonus(Pins: Integer): Boolean;
constructor Create(FrameStatus: TFrameStatus = fsNormal);
end;

TBowlingGame = class(TInterfacedObject, IBowlingGame)
public
function Roll(Pins: Integer): Boolean;
function Score: Integer;
constructor Create;
private const
kFramesPerGame = 10;
var
FFramesRemaining: Integer;
// Frames with Pending Bonus Rolls
FBonusFrames: TList<TFrame>;
// Frames Rolled, including 'bonus' end-of-game frames for processing
FFrames: TList<TFrame>;
private
function AddBonusPins(Pins: Integer): Boolean;
function CurrentFrame: TFrame;
procedure SetUpPins;
end;

function NewBowlingGame: IBowlingGame;
begin
Result := TBowlingGame.Create;
end;

{ TBowlingGame }

procedure TBowlingGame.SetUpPins;
begin
if FFrames.Count < kFramesPerGame then
FFrames.Add(TFrame.Create)
else if (CurrentFrame.FPinsRemaining = 0) then
FFrames.Add(TFrame.Create(fsBonus));
end;

constructor TBowlingGame.Create;
begin
FFramesRemaining := kFramesPerGame;
FBonusFrames := TList<TFrame>.Create;
FFrames := TList<TFrame>.Create;
SetUpPins;
end;

function TBowlingGame.AddBonusPins(Pins: Integer): Boolean;
var
i: Integer;
Frame: TFrame;
begin
Result := False;
// Add any bonus points to any frames with known outstanding bonus rolls.
for i := 0 to FBonusFrames.Count - 1 do begin
Frame := FBonusFrames[i];
Result := Frame.AddBonus(Pins);
// Remove any frames whose last bonus rolls have just been rolled.
if Frame.FBonusRollsRemaining = 0 then
Frame := nil;
end;
FBonusFrames.Pack;
end;

function TBowlingGame.CurrentFrame: TFrame;
begin
Result := FFrames.Last;
end;

function TBowlingGame.Roll(Pins: Integer): Boolean;
begin
Result := CurrentFrame.Roll(Pins) and AddBonusPins(Pins);

if (CurrentFrame.FRollsRemaining = 0) then begin

if CurrentFrame.FBonusRollsRemaining > 0 then
FBonusFrames.Add(CurrentFrame);

SetUpPins;
end;
end;

function TBowlingGame.Score: Integer;
var
Frame: TFrame;
NormalFrameCount: Integer;
begin
Result := 0;
NormalFrameCount := 0;
for Frame in FFrames do begin
if (Frame.FFrameStatus = fsError)
or (Frame.FBonusRollsRemaining > 0) then
Exit(-1)
else if Frame.FFrameStatus = fsNormal then begin
Inc(Result, Frame.FScore);
Inc(NormalFrameCount);
end;
end;

if NormalFrameCount <> kFramesPerGame then
Exit(-1);
end;

{ TFrame }

constructor TFrame.Create(FrameStatus: TFrameStatus = fsNormal);
begin
FFrameStatus := FrameStatus;
FScore := 0;
FBonusRollsRemaining := 0;
FRollsRemaining := kNormalRollsPerFrame;
FPinsRemaining := kPinsPerFrame;
end;

function TFrame.AddBonus(Pins: Integer): Boolean;
begin
Result := FBonusRollsRemaining > 0;
if Result then begin
Inc(FScore, Pins);
Dec(FBonusRollsRemaining);
end;
end;

function TFrame.Roll(Pins: Integer): Boolean;
begin
Result := (FRollsRemaining > 0) and (FPinsRemaining >= Pins) and (Pins >= 0);

if (FPinsRemaining < Pins) or (Pins < 0) then
FFrameStatus := fsError;

if Result then begin

Inc(FScore, Pins);

Dec(FRollsRemaining);
Dec(FPinsRemaining, Pins);

if FPinsRemaining = 0 then begin
FRollsRemaining := 0;
end;

if (FFrameStatus <> fsBonus) then begin
// Spare or Strike
if FPinsRemaining = 0 then begin
Inc(FBonusRollsRemaining);
end;
if Pins = kPinsPerFrame then begin
Inc(FBonusRollsRemaining);
end;
end;
end;
end;

end.``````

## Community comments

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

### Greycoat21's Reflection

Some private fields should be made properties. More comments may be helpful.