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

eferim's solution

to Bowling in the Delphi Pascal Track

Published at Sep 20 2018 · 0 comments
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.

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 receive assistance 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));
    (*{
        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
    function Roll(ARoll : integer) : boolean;
    function Score : integer;
  end;

  function NewBowlingGame : IBowlingGame;

implementation

type
  TNewBowlingGame = class(TInterfacedObject,IBowlingGame)
  private
    Fir, Sec : integer;
    FullFrame, Complete : boolean;
    FrameCounter, State, Sum, Additional, Additional2 : Integer;
    procedure AddBonus(ARoll : integer);
    function CheckRoll(ARoll : integer) : boolean;
    function CheckFinal(ARoll : integer) : boolean;
  public
    function Roll(ARoll : integer) : boolean;
    function Score : integer;
  end;

{$REGION 'TNewBowlingGame' }

procedure TNewBowlingGame.AddBonus(ARoll: integer);
begin
  inc(Sum, ARoll + ARoll * Additional );
  Additional := Additional2;
  Additional2 := 0;
end;

function TNewBowlingGame.CheckFinal(ARoll: integer): boolean;
begin
  Result := true;
  case state of
    0 : Fir := ARoll;
    1 : begin
      Sec := ARoll;
      if Fir + Sec < 10  then Complete := true;
    end;
    2 : begin
      Result := (Fir + Sec = 10) or ((Fir + Sec > 10) and ((sec + ARoll <= 10) or (Sec = 10)));
      Complete := Result;
    end
  else
    Result:= false;
  end;
  inc(State);
  if Result then
    AddBonus(ARoll);
end;

function TNewBowlingGame.CheckRoll(ARoll: integer): boolean;
begin
  Result := true;
  AddBonus(ARoll);
  if not FullFrame then
  begin
    Fir := ARoll;
    FullFrame := Fir < 10;
    if not FullFrame then
    begin
      inc(Additional);
      inc(Additional2);
    end;
  end
  else
  begin
    if Fir + ARoll > 10 then
      exit(false);
    if Fir + ARoll = 10 then
      inc(Additional);
    FullFrame := false;
  end;
  if not FullFrame then
    inc(FrameCounter);
end;

function TNewBowlingGame.Roll(ARoll: integer): boolean;
begin
  if not (ARoll in [0 .. 10]) then
    exit(false);
  if FrameCounter < 9 then
    Result := CheckRoll(ARoll)
  else
    Result := CheckFinal(ARoll);
end;

function TNewBowlingGame.Score: integer;
begin
  Result := -1;
  if Complete then
    Result := Sum;
end;

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

end.

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?