diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile new file mode 100644 index 00000000..e9dfc3e6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +# gem "rails" + +gem 'rspec', '~> 3.12' + +gem 'rubocop', '~> 1.51' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..b8760bf9 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,49 @@ +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + diff-lcs (1.5.0) + json (2.6.3) + parallel (1.23.0) + parser (3.2.2.1) + ast (~> 2.4.1) + rainbow (3.1.1) + regexp_parser (2.8.0) + rexml (3.2.5) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.2) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.3) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-support (3.12.0) + rubocop (1.51.0) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.28.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.28.1) + parser (>= 3.2.1.0) + ruby-progressbar (1.13.0) + unicode-display_width (2.4.2) + +PLATFORMS + x86_64-darwin-20 + +DEPENDENCIES + rspec (~> 3.12) + rubocop (~> 1.51) + +BUNDLED WITH + 2.4.13 diff --git a/app.rb b/app.rb new file mode 100644 index 00000000..a7c909a4 --- /dev/null +++ b/app.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require_relative './lib/bowling_score_sheet' + +class BowlingApp + def initialize(io, score_sheet) + @io = io + @score_sheet = score_sheet + end + + def run + while @score_sheet.complete == false + @io.puts 'Enter number of knocked down pins separated by commas (eg 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1):' + user_input = @io.gets.chomp + roll(user_input) + print_score_sheet + @io.puts "Total: #{score}" + end + end + + def roll(string_of_pins) + rolls = string_of_pins.split(',').map(&:to_i) + i = 0 + while i < rolls.length + @score_sheet.add_roll(rolls[i]) + i += 1 + end + end + + def score + @score_sheet.all_frames.sum do |frame| + frame.round <= 10 ? frame.total_score : 0 + end + end + + def print_score_sheet + @score_sheet.all_frames.each do |frame| + if frame.round.positive? && frame.round <= 10 + @io.puts "Frame: #{frame.round}, scores: #{frame.score}, total score: #{frame.total_score}, notes: #{frame.status}" + end + end + end +end + +if __FILE__ == $PROGRAM_NAME + app = BowlingApp.new( + Kernel, + BowlingScoreSheet.new + ) + app.run +end diff --git a/lib/bowling_score_sheet.rb b/lib/bowling_score_sheet.rb new file mode 100644 index 00000000..8dce9b2b --- /dev/null +++ b/lib/bowling_score_sheet.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require_relative 'frame' + +class BowlingScoreSheet + def initialize + @frames = [Frame.new] + end + + def add_roll(pins) + add_bonus_to_previous_frames(pins) + add_score_to_current_frame(pins) + end + + def all_frames + @frames + end + + def complete + frame_10 = @frames.filter { |frame| frame.round == 10 } + if frame_10 == [] + false + elsif frame_10[0].complete == true && frame_10[0].bonuses.zero? + true + else + false + end + end + + private + + def add_bonus_to_previous_frames(pins) + @frames.each do |frame| + if frame.bonuses.positive? && frame.complete + frame.total_score += pins + frame.bonuses -= 1 + end + end + end + + def add_score_to_current_frame(pins) + current_frame = @frames.last + if current_frame.complete + new_frame = create_new_frame(current_frame, pins) + @frames << new_frame + else + current_frame.score << pins + current_frame.total_score += pins + set_frame_status(current_frame) + current_frame.complete = true + end + end + + def create_new_frame(current_frame, pins) + new_frame = Frame.new + new_frame.round = current_frame.round + 1 + new_frame.score = [pins] + new_frame.total_score = pins + new_frame.complete = false if pins != 10 + set_frame_status(new_frame) + + new_frame + end + + def set_frame_status(frame) + return unless frame.total_score == 10 + + if frame.score[0] == 10 + frame.status = 'strike' + frame.bonuses = 2 + else + frame.status = 'spare' + frame.bonuses = 1 + end + end +end diff --git a/lib/frame.rb b/lib/frame.rb new file mode 100644 index 00000000..bc6ba15f --- /dev/null +++ b/lib/frame.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class Frame + attr_accessor :round, :score, :total_score, :status, :complete, :bonuses + + def initialize + @complete = true + @round = 0 + @score = 0 + @total_score = 0 + @bonuses = 0 + end +end diff --git a/spec/app_spec.rb b/spec/app_spec.rb new file mode 100644 index 00000000..c3eabc43 --- /dev/null +++ b/spec/app_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require_relative '../app' +require_relative '../lib/bowling_score_sheet' + +RSpec.describe BowlingApp do + it 'calculates the running score of a game with no strikes or spares' do + io = double :io + game = BowlingApp.new(io, BowlingScoreSheet.new) + game.roll('1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1') + expect(game.score).to eq 20 + + game = BowlingApp.new(io, BowlingScoreSheet.new) + game.roll('2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2') + expect(game.score).to eq 40 + + game = BowlingApp.new(io, BowlingScoreSheet.new) + game.roll('2,2,2,2,2,2,2,2,2,2') + game.roll('2,2,2,2,2,2,2,2,2,2') + expect(game.score).to eq 40 + end + + it 'calculates the running score of a game with one spare (not in final frame)' do + io = double :io + game = BowlingApp.new(io, BowlingScoreSheet.new) + game.roll('1,1,2,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1') + expect(game.score).to eq 29 + end + + it 'calculates the running score of a game with a mix of spares and stikes' do + io = double :io + BowlingScoreSheet.new + + game = BowlingApp.new(io, BowlingScoreSheet.new) + game.roll('1,1,1,1,1,1,1,1,1,1,10,10,2,8,8,2,10,4,5') + expect(game.score).to eq 109 + end +end diff --git a/spec/bowling_score_sheet_spec.rb b/spec/bowling_score_sheet_spec.rb new file mode 100644 index 00000000..4bae18ac --- /dev/null +++ b/spec/bowling_score_sheet_spec.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +require 'bowling_score_sheet' + +RSpec.describe BowlingScoreSheet do + it 'calculates the running score of a game with no strikes or spares' do + double :io + score_sheet = BowlingScoreSheet.new + 20.times do + score_sheet.add_roll(1) + end + + total_score = score_sheet.all_frames.sum(&:total_score) + expect(total_score).to eq 20 + end + + it 'calculates the running score of a game with a spare (not in final frame) and no strikes' do + double :io + score_sheet = BowlingScoreSheet.new + + score_sheet.add_roll(2) + score_sheet.add_roll(8) + + 18.times do + score_sheet.add_roll(1) + end + + total_score = score_sheet.all_frames.sum(&:total_score) + expect(total_score).to eq 29 + end + + it 'calculates the running score of a game with a strike (not in final frame) and no spares' do + double :io + score_sheet = BowlingScoreSheet.new + + score_sheet.add_roll(10) + score_sheet.add_roll(2) + score_sheet.add_roll(3) + 16.times do + score_sheet.add_roll(1) + end + + total_score = score_sheet.all_frames.sum(&:total_score) + expect(total_score).to eq 36 + end + + it 'calculates the running score of a game with multiple strikes (not in final frame) and no spares' do + double :io + score_sheet = BowlingScoreSheet.new + + score_sheet.add_roll(10) + score_sheet.add_roll(10) + score_sheet.add_roll(2) + score_sheet.add_roll(3) + + 14.times do + score_sheet.add_roll(1) + end + + total_score = score_sheet.all_frames.sum(&:total_score) + expect(total_score).to eq 56 + end + + it 'calculates the running score of a game with multiple strikes and spares (not in final frame)' do + double :io + score_sheet = BowlingScoreSheet.new + + score_sheet.add_roll(10) + score_sheet.add_roll(10) + score_sheet.add_roll(2) + score_sheet.add_roll(8) + score_sheet.add_roll(8) + score_sheet.add_roll(2) + score_sheet.add_roll(10) + + 10.times do + score_sheet.add_roll(1) + end + + total_score = score_sheet.all_frames.sum(&:total_score) + expect(total_score).to eq 102 + end + + it 'calculates the running score of a game with a strike in final frame' do + double :io + score_sheet = BowlingScoreSheet.new + + 18.times do + score_sheet.add_roll(1) + end + score_sheet.add_roll(10) + score_sheet.add_roll(2) + score_sheet.add_roll(3) + + total_score = score_sheet.all_frames.sum do |frame| + frame.round <= 10 ? frame.total_score : 0 + end + expect(total_score).to eq 33 + end + + it 'calculates the running score of a game with a spare in final frame' do + double :io + score_sheet = BowlingScoreSheet.new + + 19.times do + score_sheet.add_roll(1) + end + score_sheet.add_roll(9) + score_sheet.add_roll(2) + + total_score = score_sheet.all_frames.sum do |frame| + frame.round <= 10 ? frame.total_score : 0 + end + expect(total_score).to eq 30 + end + + it 'calculates the running score of a game with a mix of spares and stikes' do + double :io + score_sheet = BowlingScoreSheet.new + + 10.times do + score_sheet.add_roll(1) + end + + score_sheet.add_roll(10) + score_sheet.add_roll(10) + score_sheet.add_roll(2) + score_sheet.add_roll(8) + score_sheet.add_roll(8) + score_sheet.add_roll(2) + score_sheet.add_roll(10) + score_sheet.add_roll(4) + score_sheet.add_roll(5) + + total_score = score_sheet.all_frames.sum do |frame| + frame.round <= 10 ? frame.total_score : 0 + end + expect(total_score).to eq 109 + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..4f8c8d91 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ + # config.disable_monkey_patching! + # + # # This setting enables warnings. It's recommended, but in some cases may + # # be too noisy due to issues in dependencies. + # config.warnings = true + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end