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

dgeiger's solution

to Bowling in the Delphi Pascal Track

Published at Sep 12 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

uses
  System.Generics.Collections;

const
  FirstFrame = 1;
  LastFrame = 10;
  MinPins = 0;
  AllPins = 10;
  MinBallsPerFrame = 1;
  MaxBallsPerFrame = 2;
  MaxBallsPerGame = 21;
  ErrorScore = -1;

type
  TFrameTypes = (OpenFrame, SpareFrame, StrikeFrame);

  TGameStatus = (GameUnstarted, GameInProgress, GameFinished, GameError);

  IBowlingGame = Interface ['{7143B16F-D8DE-406A-A378-0B700A45C277}']
    function Roll(Frame: Integer): Boolean;
    function Score: Integer;

  end;

  TBowlingGame = class(TInterfacedObject, IBowlingGame)
    private
      FBall: Integer;
      FBallsInFrame: Integer;
      FPinsByFrame: Array[FirstFrame..LastFrame, MinBallsPerFrame..MaxBallsPerFrame] of Integer;
      FFrames: Array[FirstFrame..LastFrame] of Integer;
      FFrame: Integer;
      FFrameStatus: array[FirstFrame..LastFrame] of TFrameTypes;
      FScore: Integer;
      FFillBall: Integer;
      FGameStatus: TGameStatus;

      function NextBall(FrameNumber: Integer): Integer;
      function Next2Balls(FrameNumber: Integer): Integer;

      procedure SetFrameStatus(Pins: Integer);

    public
      constructor Create;

      function Roll(Pins: Integer): Boolean;
      function Score: Integer;

  end;

function NewBowlingGame: IBowlingGame;

implementation

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

{ TBowlingGame }

constructor TBowlingGame.Create;
begin
  // We start the game with the first frame, first ball, first ball in the frame,
  // zero pins for the fill ball, zero pins for the score, and the game unstarted.
  FFrame := FirstFrame;
  FBall  := MinBallsPerFrame;
  FBallsInFrame := MinBallsPerFrame;
  FFillBall := MinPins;
  FScore := MinPins;
  FGameStatus := GameUnstarted;
end;

function TBowlingGame.NextBall(FrameNumber: Integer): Integer;
begin
  // Is this the last frame?
  if FrameNumber = LastFrame then
    // Yes, the next ball is the fill ball
    Result := FFillBall
  else
    // No, get the first ball of the next frame
    Result := FPinsByFrame[Succ(FrameNumber), MinBallsPerFrame];
end;

function TBowlingGame.Next2Balls(FrameNumber: Integer): Integer;
begin
  // Is this the last frame?
  if FrameNumber = LastFrame then
    // Yes, the next ball is the fill ball
    Result := FPinsByFrame[FrameNumber, MaxBallsPerFrame] + FFillBall
  else
    // No, get the first ball of the next frame
    begin
      Result := FPinsByFrame[Succ(FrameNumber), MinBallsPerFrame];

      // Wass this ball a strike?
      if Result = AllPins then
        // Yes, then the second ball comes from the frame after the next
        Result := Result + FPinsByFrame[Succ(Succ(FrameNumber)), MinBallsPerFrame]
      else
        // No, get the second ball from the next frame
        Result := Result + FPinsByFrame[Succ(FrameNumber), MaxBallsPerFrame];
      end;
end;

function TBowlingGame.Roll(Pins: Integer): Boolean;
begin
  // Assume this is a valid roll
  Result := True;

  // Is this the first roll of the game?
  if FGameStatus = GameUnstarted then
    // Yes, so mark the game as being in progress
    FGameStatus := GameInProgress;

  // Is the number of pins on this roll valid?
  if (Pins < MinPins) or (Pins > AllPins) then
    begin
      // No, set the error flag for return,
      Result := False;

      // set the error score,
      FScore := ErrorScore;

      // and leave early
      Exit;
    end;

  // Is the game finished?
  if FGameStatus = GameFinished then
    begin
      // Yes, so we can't make any more rolls. So we need to set the error flag,
      Result := False;

      // set the error score,
      FScore := ErrorScore;

      // and leave early.
      Exit;
    end;

  // Are we past the final frame?
  if (FFrame > LastFrame) then
    begin
      // Yes, so either this is a fill ball or an invalid roll. Lets start
      // by treating this roll as a fill ball.
      FFillBall := Pins;

      // Either way, the game is done
      FGameStatus := GameFinished;

      // Is the last frame is an open frame?
      if FFrameStatus[LastFrame] = OpenFrame then
        // Yes, so this can't be a fill ball, it's an invalid roll.
        begin
          // Set the error flag,
          Result := False;

          // set the error score,
          FScore := ErrorScore;

          // and leave early.
          Exit;
        end;

      // Yes, so we need to make sure the pin count is valid for a fill ball.
      // If the first ball of the last frame was a strike,
      // and the second ball wasn't, then the total of the second ball and
      // the fill ball can't be more than the number of pins.
      if (FFrameStatus[LastFrame] = StrikeFrame) and
          (FPinsByFrame[LastFrame, MaxBallsPerFrame] <> AllPins) and
          (Pins + FPinsByFrame[LastFrame, MaxBallsPerFrame]  > AllPins) then
        begin
          // Too many pins were knocked down, so flag the error,
          Result := False;

          // set the error score,
          FScore := ErrorScore;

          // and flag the game as invalid.
          FGameStatus := GameError;
        end;

      // Since we're on the fill ball roll, we now leave early
      exit;
    end;

  // Set the pin count in the frame for this ball
  FPinsByFrame[FFrame][FBallsInFrame] := Pins;

  // Is this an open frame?
  if (FFrameStatus[FFrame] = OpenFrame) then
    begin
      // Yes, so we need to add the pins to the frame total
      FFrames[FFrame] := FFrames[FFrame] + Pins;

      // Is this a valid pin count?
      if FFrames[FFrame] > AllPins then
        begin
          // Set the error flag,
          Result := False;

          // set the error score,
          FScore := ErrorScore;

          // and leave early.
          Exit;
        end;

      // Did all of the pins get knocked down in this frame?
      if FFrames[FFrame] = AllPins then
        // Yes, so it's either a strike or a spare, based on the ball count
        // of the frame and how many pins that ball knocked down.
        SetFrameStatus(Pins);
    end;

  // Increment the ball count for the game
  Inc(FBall);

  // Increment the ball count for the frame
  Inc(FBallsInFrame);

  // If we're past the number of balls per frame,
  if FBallsInFrame > MaxBallsPerFrame then
    begin
      // we need to reset the ball count
      FBallsInFrame := MinBallsPerFrame;

      // and increment the frame count.
      Inc(FFrame);
    end;

  // Were all the pins knocked down?
  if (Pins = AllPins) then
    //Yes, is this the last frame
    if (FFrame <> LastFrame) then
      begin
        // No, so we reset the ball count for the frame
        FBallsInFrame := MinBallsPerFrame;

        // and move to the next frame.
        Inc(FFrame);
      end;

  // If we're past the last frame, and the last frame wasn't a strike or spare,
  if (FFrame > LastFrame) and (FFrameStatus[LastFrame] = OpenFrame) then
    // then the game is finished.
    FGameStatus := GameFinished;
end;

function TBowlingGame.Score: Integer;
var
  FrameNumber: Integer;
begin
  // It's time to calculate the score for the game, but only if the game
  // is actually finished.
  if (FGameStatus = GameFinished) then
    // Okay, the game if finished.
    for FrameNumber := FirstFrame to LastFrame do
      // We need to add the pins and take care of any strikes and spares
      case FFrameStatus[FrameNumber] of
        OpenFrame:
          // An open frame only get the pins knocked down in this frame
          FScore := FScore + FFrames[FrameNumber];

        SpareFrame:
          // A spare also get the pins from the next ball
          FScore := FScore + FFrames[FrameNumber] + NextBall(FrameNumber);

        StrikeFrame:
          // A strike also get the pins from the next two balls
          FScore := FScore + FFrames[FrameNumber] + Next2Balls(FrameNumber);

      end
  else
    // The game isn't finished, so return the error score
    FScore := ErrorScore;

  Result := FScore;
end;

procedure TBowlingGame.SetFrameStatus(Pins: Integer);
begin
  // This frame isn't an open frame, so we need to mark it as a spare or a strike
  if Pins = AllPins then
    // All pins were knocked down, so it's a strike only if it's the first ball
    // of the frame
    if FBallsInFrame = MinBallsPerFrame then
      // This was the first ball, so it's a strike
      FFrameStatus[FFrame] := StrikeFrame
    else
      // This was the second ball, so it's a spare
      FFrameStatus[FFrame] := SpareFrame
  else
    // The current ball only got some of the pins, so it's a spare
    FFrameStatus[FFrame] := SpareFrame;
end;

end.

Community comments

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

dgeiger's Reflection

This really needs some refactoring for the edge cases.