Avatar of w1zeman1p

w1zeman1p's solution

to Bowling in the Ruby Track

Published at Mar 06 2019 · 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.

For installation and learning resources, refer to the Ruby resources 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.2.0 1806718
class BowlingTest < Minitest::Test
  def test_should_be_able_to_score_a_game_with_all_zeros
    # skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 0, game.score
  end

  def test_should_be_able_to_score_a_game_with_no_strikes_or_spares
    skip
    game = Game.new
    rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 90, game.score
  end

  def test_a_spare_followed_by_zeros_is_worth_ten_points
    skip
    game = Game.new
    rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 10, game.score
  end

  def test_points_scored_in_the_roll_after_a_spare_are_counted_twice
    skip
    game = Game.new
    rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 16, game.score
  end

  def test_consecutive_spares_each_get_a_one_roll_bonus
    skip
    game = Game.new
    rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 31, game.score
  end

  def test_a_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 17, game.score
  end

  def test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll
    skip
    game = Game.new
    rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 10, game.score
  end

  def test_points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus
    skip
    game = Game.new
    rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 26, game.score
  end

  def test_consecutive_strikes_each_get_the_two_roll_bonus
    skip
    game = Game.new
    rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 81, game.score
  end

  def test_a_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 18, game.score
  end

  def test_rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 20, game.score
  end

  def test_strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 20, game.score
  end

  def test_all_strikes_is_a_perfect_game
    skip
    game = Game.new
    rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
    rolls.each { |pins| game.roll(pins) }
    assert_equal 300, game.score
  end

  def test_rolls_cannot_score_negative_points
    skip
    game = Game.new
    rolls = []
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.roll(-1)
    end
  end

  def test_a_roll_cannot_score_more_than_10_points
    skip
    game = Game.new
    rolls = []
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.roll(11)
    end
  end

  def test_two_rolls_in_a_frame_cannot_score_more_than_10_points
    skip
    game = Game.new
    rolls = [5]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.roll(11)
    end
  end

  def test_an_unstarted_game_cannot_be_scored
    skip
    game = Game.new
    rolls = []
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.score
    end
  end

  def test_an_incomplete_game_cannot_be_scored
    skip
    game = Game.new
    rolls = [0, 0]
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.score
    end
  end

  def test_cannot_roll_if_game_already_has_ten_frames
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]
    rolls.each { |pins| game.roll(pins) }
    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
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3]
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.score
    end
  end

  def test_cannot_roll_after_bonus_roll_for_spare
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2]
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.roll(2)
    end
  end

  def test_cannot_roll_after_bonus_rolls_for_strike
    skip
    game = Game.new
    rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2]
    rolls.each { |pins| game.roll(pins) }
    assert_raises Game::BowlingError do
      game.roll(2)
    end
  end
end
class Frame
  def initialize(next_frame=nil)
    @next_frame = next_frame
    @rolls = []
  end

  def validate_roll!(pins)
    raise Game::BowlingError.new("frame complete!") if complete?
    raise Game::BowlingError.new("invalid roll") if !pins.between?(0, 10)
    if !(rolls.sum + pins).between?(0, 10)
      raise Game::BowlingError.new("invalid roll")
    end
  end

  def knock_down(pins)
    validate_roll!(pins)

    @rolls << pins
  end

  def complete?
    rolls.length == 2 || strike?
  end

  def strike?
    rolls[0] == 10
  end

  def spare?
    rolls.sum == 10 && rolls.length == 2
  end

  def score
    rolls.sum + strike_bonus + spare_bonus
  end

  protected

  def spare_bonus
    spare? ? next_frame.rolls[0] : 0
  end

  def strike_bonus
    return 0 if !strike?
    if !next_frame.next_frame.nil? && next_frame.strike?
      10 + next_frame.next_frame.rolls[0]
    else
      next_frame.rolls[0..1].sum
    end
  end

  attr_reader :rolls, :next_frame
end

class TenthFrame < Frame

  def validate_roll!(pins)
    raise Game::BowlingError.new("frame complete!") if complete?
    raise Game::BowlingError.new("invalid roll") if !pins.between?(0, 10)
    if rolls.length == 2
      if rolls[0] == 10 && rolls[1] != 10 && rolls[1] + pins > 10
        raise Game::BowlingError.new
      end
    end
  end

  def complete?
    rolls.length == 2 && rolls.sum < 10 || rolls.length == 3
  end

  def score
    rolls.sum
  end
end

class Game
  class BowlingError < ArgumentError; end

  def initialize
    @frame_cursor = 0
    @frames = [TenthFrame.new]
    9.times do
      frames.unshift(Frame.new(frames.first))
    end
  end

  def roll(pins)
    raise Game::BowlingError.new if current_frame.nil?

    current_frame.knock_down(pins)

    @frame_cursor += 1 if current_frame.complete?
  end

  def score
    raise BowlingError.new if !frames.all?(&:complete?)

    frames.sum {|f| f.score}
  end

  private

  attr_reader :frames, :frame_cursor

  def current_frame
    frames[frame_cursor]
  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?