Avatar of paulfioravanti

paulfioravanti's solution

to Bowling in the Ruby Track

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

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby bowling_test.rb

To include color from the command line:

ruby -r minitest/pride bowling_test.rb

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.

bowling_test.rb

require 'minitest/autorun'
require_relative 'bowling'

# Common test data version: 1.0.1 26e345e
class BowlingTest < Minitest::Test
  def setup
    @game = Game.new
  end

  def record(rolls)
    rolls.each { |pins| @game.roll(pins) }
  end

  def test_should_be_able_to_score_a_game_with_all_zeros
    # skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 0, @game.score
  end

  def test_should_be_able_to_score_a_game_with_no_strikes_or_spares
    skip
    record([3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6])
    assert_equal 90, @game.score
  end

  def test_a_spare_followed_by_zeros_is_worth_ten_points
    skip
    record([6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 10, @game.score
  end

  def test_points_scored_in_the_roll_after_a_spare_are_counted_twice
    skip
    record([6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 16, @game.score
  end

  def test_consecutive_spares_each_get_a_one_roll_bonus
    skip
    record([5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 31, @game.score
  end

  def test_a_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7])
    assert_equal 17, @game.score
  end

  def test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll
    skip
    record([10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 10, @game.score
  end

  def test_points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus
    skip
    record([10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 26, @game.score
  end

  def test_consecutive_strikes_each_get_the_two_roll_bonus
    skip
    record([10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_equal 81, @game.score
  end

  def test_a_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1])
    assert_equal 18, @game.score
  end

  def test_rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3])
    assert_equal 20, @game.score
  end

  def test_strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10])
    assert_equal 30, @game.score
  end

  def test_a_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10])
    assert_equal 20, @game.score
  end

  def test_all_strikes_is_a_perfect_game
    skip
    record([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10])
    assert_equal 300, @game.score
  end

  def test_rolls_cannot_score_negative_points
    skip
    record([])
    assert_raises Game::BowlingError do
      @game.roll(-1)
    end
  end

  def test_a_roll_cannot_score_more_than_10_points
    skip
    record([])
    assert_raises Game::BowlingError do
      @game.roll(11)
    end
  end

  def test_two_rolls_in_a_frame_cannot_score_more_than_10_points
    skip
    record([5])
    assert_raises Game::BowlingError do
      @game.roll(6)
    end
  end

  def test_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10])
    assert_raises Game::BowlingError do
      @game.roll(11)
    end
  end

  def test_two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5])
    assert_raises Game::BowlingError do
      @game.roll(6)
    end
  end

  def test_two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6])
    assert_equal 26, @game.score
  end

  def test_the_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6])
    assert_raises Game::BowlingError do
      @game.roll(10)
    end
  end

  def test_second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10])
    assert_raises Game::BowlingError do
      @game.roll(11)
    end
  end

  def test_an_unstarted_game_cannot_be_scored
    skip
    record([])
    assert_raises Game::BowlingError do
      @game.score
    end
  end

  def test_an_incomplete_game_cannot_be_scored
    skip
    record([0, 0])
    assert_raises Game::BowlingError do
      @game.score
    end
  end

  def test_cannot_roll_if_game_already_has_ten_frames
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert_raises Game::BowlingError do
      @game.roll(0)
    end
  end

  def test_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10])
    assert_raises Game::BowlingError do
      @game.score
    end
  end

  def test_both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10])
    assert_raises Game::BowlingError do
      @game.score
    end
  end

  def test_bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
    skip
    record([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3])
    assert_raises Game::BowlingError do
      @game.score
    end
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
  def test_bookkeeping
    skip
    assert_equal 3, BookKeeping::VERSION
  end
end
class Game
  class BowlingError < StandardError; end

  module BowlingRules
    NUM_FRAMES = 10
    START_FRAME = 0
    FRAME_INCREMENT = 1
    MAX_PINS = 10

    FINAL_FRAME = NUM_FRAMES - 1
    private_constant :FINAL_FRAME
    STANDARD_FRAME_SIZE = 2
    private_constant :STANDARD_FRAME_SIZE
    FINAL_FRAME_SIZE = 3
    private_constant :FINAL_FRAME_SIZE
    STANDARD_FRAME_STRIKE = [MAX_PINS].freeze
    private_constant :STANDARD_FRAME_STRIKE

    module Referee
      MIN_PINS = 0
      private_constant :MIN_PINS

      module_function

      def invalid_roll?(frame_number, pins, remaining_pins)
        !pins.between?(MIN_PINS, MAX_PINS) ||
          pins > remaining_pins ||
          frame_number > FINAL_FRAME
      end

      def unscoreable?(frames)
        frames.any?(&:empty?) ||
          frames[FINAL_FRAME].length < FINAL_FRAME_SIZE &&
            frames[FINAL_FRAME].sum >= MAX_PINS
      end
    end
    private_constant :Referee

    module Scorer
      FINAL_STANDARD_FRAME = FINAL_FRAME - 1
      private_constant :FINAL_STANDARD_FRAME

      module_function

      def score(frames, frame, index)
        score = 0
        if standard_frame_strike?(frame)
          score += strike_bonus(frames, index)
        elsif spare?(frame)
          score += spare_bonus(frames, index)
        end
        score + frame.sum
      end

      def standard_frame_strike?(frame)
        frame == STANDARD_FRAME_STRIKE
      end
      private_class_method :standard_frame_strike?

      def strike_bonus(frames, index)
        if before_final_standard_frame?(index)
          if next_standard_frame_is_a_strike?(frames, index)
            double_strike_bonus(frames, index)
          else
            single_strike_bonus(frames, index)
          end
        else
          strike_bonus_from_final_frame(frames, index)
        end
      end
      private_class_method :strike_bonus

      def before_final_standard_frame?(index)
        index < FINAL_STANDARD_FRAME
      end
      private_class_method :before_final_standard_frame?

      def next_standard_frame_is_a_strike?(frames, index)
        frames[index + FRAME_INCREMENT] == STANDARD_FRAME_STRIKE
      end
      private_class_method :next_standard_frame_is_a_strike?

      def double_strike_bonus(frames, index)
        next_frame = index + FRAME_INCREMENT
        frames[next_frame].first + frames[next_frame + FRAME_INCREMENT].first
      end
      private_class_method :double_strike_bonus

      def single_strike_bonus(frames, index)
        frames[index + FRAME_INCREMENT].sum
      end
      private_class_method :single_strike_bonus

      def strike_bonus_from_final_frame(frames, index)
        frames[index + FRAME_INCREMENT].first(STANDARD_FRAME_SIZE).sum
      end
      private_class_method :strike_bonus_from_final_frame

      def spare?(frame)
        frame.sum == MAX_PINS
      end
      private_class_method :spare?

      def spare_bonus(frames, index)
        frames[index + FRAME_INCREMENT].first
      end
      private_class_method :spare_bonus
    end
    private_constant :Scorer

    module_function

    def invalid_roll?(frame_number, pins, remaining_pins)
      Referee.invalid_roll?(frame_number, pins, remaining_pins)
    end

    def unscoreable?(frames)
      Referee.unscoreable?(frames)
    end

    def score(frames, frame, index)
      Scorer.score(frames, frame, index)
    end

    def next_actions(frames, frame_number, pins)
      if standard_frame?(frame_number)
        standard_frame_roll(frames, frame_number, pins)
      elsif can_roll_next_final_frame_ball?(frames, frame_number, pins)
        [:reset_pins]
      elsif frame_finished_without_fill_ball?(frames, frame_number)
        [:game_over]
      else
        [[:knock_down, pins]]
      end
    end

    def standard_frame?(frame_number)
      frame_number < FINAL_FRAME
    end
    private_class_method :standard_frame?

    def standard_frame_roll(frames, frame_number, pins)
      if all_pins_knocked_down?(pins)
        [:start_next_frame]
      elsif end_of_standard_frame?(frames, frame_number)
        [[:start_next_frame], [:reset_pins]]
      else
        [[:knock_down, pins]]
      end
    end
    private_class_method :standard_frame_roll

    def all_pins_knocked_down?(pins)
      pins == MAX_PINS
    end
    private_class_method :all_pins_knocked_down?

    def end_of_standard_frame?(frames, frame_number)
      frames[frame_number].size == STANDARD_FRAME_SIZE
    end
    private_class_method :end_of_standard_frame?

    def can_roll_next_final_frame_ball?(frames, frame_number, pins)
      pins == MAX_PINS ||
        frames[frame_number].first(STANDARD_FRAME_SIZE).sum == MAX_PINS
    end
    private_class_method :can_roll_next_final_frame_ball?

    def frame_finished_without_fill_ball?(frames, frame_number)
      frames[frame_number].length == STANDARD_FRAME_SIZE &&
        frames[frame_number].sum < MAX_PINS
    end
    private_class_method :frame_finished_without_fill_ball?
  end
  private_constant :BowlingRules

  GAME_OVER = 10
  private_constant :GAME_OVER

  def initialize
    @frames = Array.new(BowlingRules::NUM_FRAMES) { [] }
    @frame_number = BowlingRules::START_FRAME
    @remaining_pins = BowlingRules::MAX_PINS
  end

  def roll(pins)
    if BowlingRules.invalid_roll?(frame_number, pins, remaining_pins)
      raise BowlingError
    end
    frames[frame_number] << pins
    BowlingRules.next_actions(frames, frame_number, pins).each do |action|
      send(*action)
    end
  end

  def score
    raise BowlingError if BowlingRules.unscoreable?(frames)
    frames.each.with_index.reduce(0) do |acc, (frame, index)|
      acc + BowlingRules.score(frames, frame, index)
    end
  end

  private

  attr_reader :frames
  attr_accessor :frame_number, :remaining_pins

  def start_next_frame
    self.frame_number = frame_number + BowlingRules::FRAME_INCREMENT
  end

  def reset_pins
    self.remaining_pins = BowlingRules::MAX_PINS
  end

  def knock_down(pins)
    self.remaining_pins = remaining_pins - pins
  end

  def game_over
    self.frame_number = GAME_OVER
  end
end

module BookKeeping
  VERSION = 3
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?