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

## to Bowling in the Delphi Pascal Track

Published at Jul 13 2018 · 10 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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.

## Testing

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

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 can see how others have completed the exercise.

### 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]

[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;

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));
(*{
var sut = new BowlingGame();
var previousRolls = new int[] {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));
} (**)

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
uses System.Generics.Collections;
type

IBowlingGame = interface
procedure Roll(pins: integer);
function Score:integer;
end;

TBowlingFrame = class
FrameIndex:integer;
FirstBall:integer;
SecondBall:integer;
IsStrike:boolean;
IsSpare:boolean;
FMinIndex:integer;
FMaxIndex:integer;
public
constructor Create(Index:integer);
end;

TBowlingGame = class(TInterfacedObject, IBowlingGame)
private
FScore:integer;
FRoll:integer;
FPins: TList<integer>;
FFrames: TList<TBowlingFrame>;
FIndex:integer;
public
procedure Roll(pins: integer);
function Score:integer;
constructor Create;
end;

function NewBowlingGame:IBowLingGame;

implementation

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

constructor TBowlingFrame.Create(Index:integer);
begin
FrameIndex := Index;
FirstBall := 0;
SecondBall := 0;
IsStrike := False;
IsSpare := False;
FMinIndex := Index;
FMaxIndex := Index;
end;

constructor TBowlingGame.Create;
begin
FScore := 0;
FIndex := 0;
FPins := TList<integer>.Create;
FFrames := TList<TBowlingFrame>.Create;
end;

procedure TBowlingGame.Roll(pins: integer);
begin
inc(FIndex);
if (pins=10) then
inc(FIndex);
end;

function TBowlingGame.Score: integer;
var i, pins, iFrame, iFrames, gameLen,CurrScore: integer;
aBowlingFrame, CurrFrame :TBowlingFrame;
FNew:boolean;
begin
gameLen := FIndex;
if (gameLen < 20) then
result := -1
else
begin
FRoll := 0;
iFrame := 0;
aBowlingFrame := nil;
FNew := true;
for i := 0 to FPins.Count-1 do
begin
pins := FPins[i];
if FNew then
begin
aBowlingFrame := TBowlingFrame.Create(iFrame);
aBowlingFrame.FirstBall := pins;
aBowlingFrame.IsStrike := (pins = 10);
FNew := aBowlingFrame.IsStrike;
if (aBowlingFrame.IsStrike) then
begin
aBowlingFrame.FMaxIndex := aBowlingFrame.FMinIndex + 2;
inc(iFrame);
end
end
else
begin
aBowlingFrame.SecondBall := pins;
if (not (aBowlingFrame.IsStrike)) then
begin
aBowlingFrame.IsSpare:= (aBowlingFrame.FirstBall + pins = 10);
aBowlingFrame.FMaxIndex := aBowlingFrame.FMinIndex + 2;
end;
FNew := true;
inc(iFrame);
end;
end;

for CurrFrame in FFrames do
begin
CurrScore := (CurrFrame.FirstBall + CurrFrame.SecondBall);
if ((CurrScore >= 0) and (CurrScore <=10)) then
begin
FScore := FScore + CurrScore;
if ((CurrFrame.IsStrike) and (CurrFrame.FrameIndex<iFrame) and (CurrFrame.FMaxIndex < iFrame)) then
begin
FScore := FScore + FPins[CurrFrame.FMaxIndex-1]+ FPins[CurrFrame.FMaxIndex];
end;

if ((CurrFrame.IsSpare) and (CurrFrame.FrameIndex<iFrame) and (CurrFrame.FMaxIndex < iFrame)) then
FScore := FScore + (FPins[CurrFrame.FMaxIndex]);
end
else
begin
FScore := -1;
break;
end;
end;
result := FScore;
end;

end;

end.``````

Solution Author
commented over 3 years ago

Partial solution

Tests Found : 24 Tests Ignored : 0 Tests Passed : 17 Tests Leaked : 0 Tests Failed : 7 Tests Errored : 0

Thanks for posting your code. I'll take a look at this in a little bit.

I think you might be confusing rolls and frames. Just keep a list of rolls, not frames. The roll method will be a simple one liner that simply adds the roll to the list of rolls. Score will take care of processing all the rolls. Yes there are always 10 frames, but there are not always 2 rolls per frame. Sometimes there is only 1 and sometimes there is three. A perfect game will have 12 rolls, while a game with no spares or strikes will have 20 rolls. You can't determine (on the fly) what frame the roll is for, so don't try. :)

You should iterate based on the number of frames, which is 10. Based on the current roll you have to determine how to include the next roll(s) for the current frame. Also the current frame index will not always increment by 1, could increment by 2 or 3.

I want to be helpful, but at the same time I don't want to tell you what changes to make to your code. I am limiting my advice to what I think is happening in your code.

Also, try not to activate the next test until the current failing test passes. Consecutive_spares_each_get_a_one_roll_bonus is the test you should be trying to get to pass.

The test suite has been updated. There are now 30 test cases......

Solution Author
commented over 3 years ago

I tested and I saw that Roll is now a function

I tested and I saw that Roll is now a function

Some of the new tests are checking that not too many rolls have taken place for the entire game and I think there may be a test in there checking to make sure that a roll doesn't exceed 10 pins. This will some checking to be performed in Roll.

Solution Author
commented over 3 years ago

I changed to function returning boolean and I got same results: .........F.F...............F...........F...........F.F.F....

Tests Found : 30 Tests Ignored : 0 Tests Passed : 23 Tests Leaked : 0 Tests Failed : 7 Tests Errored : 0

So which tests are failing? I suggest reinstating the Ignore attribute for all the passing tests. From the remain failing tests I would ignore all but the first test and focus on it getting it to pass. Once it is passing I would reinstate the previously passing (now ignored) tests to verify that they are still passing. If you iterate in this way you should eventually knock out the list of failing tests.

You may now want to, but is OK to look at solutions from others for inspiration, which reminds me, I need to update mine!

Solution Author
commented over 3 years ago

Thanks I will try this way. I proceeded like this on BookStore, focusing on 2x4 and 4x4.

The test suite was recently updated for this exercise.

### 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?