🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of rpottsoh

rpottsoh's solution

to Bowling in the Delphi Pascal Track

Published at Jul 13 2018 · 1 comment
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.

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 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]
    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));
    (*{
        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

type
   IBowlingGame = interface(IInvokable)
   ['{D4A292B6-BC15-48ED-AE04-2D34759017CB}']
     function Roll(aPins: integer): Boolean;
     function Score: integer;
   end;

   function NewBowlingGame: IBowlingGame;

implementation
uses System.SysUtils, System.Math, System.Generics.Collections;

type
   TBowlingGame = class(TInterfacedObject, IBowlingGame)
   private
     var
       fRolls: TList<integer>;
     const
       fNumberOfFrames = 10;
       fMaximumFrameScore = 10;
     function IsStrike(aFrameIndex: integer): Boolean;
     function IsSpare(aFrameIndex: integer): Boolean;
     function StrikeBonus(aFrameIndex: integer): integer;
     function SpareBonus(aFrameIndex: integer): integer;
     function SumOfPinsInFrame(aFrameIndex: integer): integer;
     function CorrectNumberOfRolls(aFrameIndex: integer): boolean;
     function ValidInput(aPin: integer): Boolean;
   public
     constructor create;
     function Score: integer;
     function Roll(aPins: integer): Boolean;
   end;

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

constructor TBowlingGame.create;
begin
  fRolls := TList<integer>.Create;
end;

function TBowlingGame.Roll(aPins: Integer): Boolean;
begin
  if not ValidInput(aPins) then
    exit(false);

  fRolls.Add(aPins);
  result := true;
end;

function TBowlingGame.Score: integer;
var lFrameIndex: integer;
    i: integer;
    lScore: integer;
    lStrikeBonus: integer;
    lFrameScore: integer;
begin
  lScore := 0;
  lFrameIndex := 0;
  try
    if (fRolls.Count < 12) or (fRolls.Count > 21) then
      raise EArgumentException.Create('Not a proper game');
    for i := 1 to fNumberOfFrames do
    begin
      if fRolls.Count <= lFrameIndex then
        raise EArgumentException.Create('Not a proper game');

      if IsStrike(lFrameIndex) then
      begin
        if (fRolls.Count <= lFrameIndex + 2) then
          raise EArgumentException.Create('Not a proper game');

        lStrikeBonus := StrikeBonus(lFrameIndex);
        if (lStrikeBonus > fMaximumFrameScore) and not IsStrike(lFrameIndex + 1) or (lStrikeBonus > 20) then
          raise EArgumentException.Create('Not a proper game');

        lScore := lScore + 10 + lStrikeBonus;
        inc(lFrameIndex, ifthen(i = fNumberOfFrames, 3, 1));
      end
      else
      if IsSpare(lFrameIndex) then
      begin
        if (fRolls.Count <= lFrameIndex + 2) then
          raise EArgumentException.Create('Not a proper game');

        lScore := lScore + 10 + SpareBonus(lFrameIndex);
        inc(lFrameIndex, ifthen(i = fNumberOfFrames, 3, 2));
      end
      else
      begin
        lFrameScore := SumOfPinsInFrame(lFrameIndex);
        if (lFrameScore < 0) or (lFrameScore > 10) then
          raise EArgumentException.Create('Not a proper game');

        lScore := lScore + lFrameScore;
        inc(lFrameIndex, 2);
      end;
    end;
    result := ifthen(CorrectNumberOfRolls(lFrameIndex), lScore, -1);
  except
    result := -1;
  end;
end;

function TBowlingGame.IsStrike(aFrameIndex: Integer): Boolean;
begin
  result := fRolls[aFrameIndex] = fMaximumFrameScore;
end;

function TBowlingGame.IsSpare(aFrameIndex: Integer): Boolean;
begin
  result := fRolls[aFrameIndex] + fRolls[aFrameIndex + 1] = FMaximumFrameScore;
end;

function TBowlingGame.StrikeBonus(aFrameIndex: Integer): integer;
begin
  result := fRolls[aFrameIndex + 1] + fRolls[aFrameIndex + 2];
end;

function TBowlingGame.SpareBonus(aFrameIndex: Integer): integer;
begin
  result := fRolls[aFrameIndex + 2];
end;

function TBowlingGame.SumOfPinsInFrame(aFrameIndex: Integer): integer;
begin
  result := fRolls[aFrameIndex] + fRolls[aFrameIndex + 1];
end;

function TBowlingGame.ValidInput(aPin: integer): Boolean;
begin
  result := true;
  if (fRolls.Count >= 21) or (aPin < 0) or (aPin > 10) or
      ((fRolls.Count + 1) mod 2 = 0) and (fRolls[fRolls.Count - 1] <> 10) and ((fRolls[fRolls.Count - 1] + aPin) > 10) then
    exit(false);

  if (fRolls.Count = 20) then
  begin
    if (fRolls[18] <> 10) and (fRolls[18] + fRolls[19] <> 10) then
      exit(false);

    if (aPin = 10) and ((fRolls[18] <> 10) or (fRolls[19] <> 10) or (fRolls[19] + aPin > 10) and (fRolls[19] + aPin <> 20)) and
        (fRolls[18] + fRolls[19] <> 10) then
      exit(false);

    if (aPin <> 10) and (fRolls[19] + aPin > 10) and (fRolls[19] <> 10) then
      exit(false);
  end;
end;

function TBowlingGame.CorrectNumberOfRolls(aFrameIndex: Integer): boolean;
begin
  result := aFrameIndex = fRolls.Count;
end;

end.

Community comments

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

added initialization for result of roll.

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?