From fe3b62f8eeb7e923ee83565c37f256d4d6eb96e1 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 16:02:44 +0200 Subject: [PATCH 01/16] Basic bowling game works without spares or strikes --- .rspec | 1 + scores.rb | 43 ++++++++++++++++++ spec/scores_spec.rb | 103 ++++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 98 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 .rspec create mode 100644 scores.rb create mode 100644 spec/scores_spec.rb create mode 100644 spec/spec_helper.rb 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/scores.rb b/scores.rb new file mode 100644 index 00000000..c02ea0ce --- /dev/null +++ b/scores.rb @@ -0,0 +1,43 @@ +class Scores + + def initialize(io) + @io = io + @total_score = 0 + end + + def roll + @io.puts 'Enter score:' + input = @io.gets.chomp.to_i + if input < 10 + @frame_score += input + end + end + + def frame + @frame_score = 0 + self.roll + self.roll + @total_score += @frame_score + @io.puts "Frame score: #{@frame_score}" + end + + def last_frame + @frame_score = 0 + self.roll + self.roll + if @frame_score == 10 + self.roll + end + @total_score += @frame_score + @io.puts "Frame score: #{@frame_score}" + end + + def run + 9.times{self.frame} + self.last_frame + @io.puts "Total score: #{@total_score}" + end +end + +# game = Scores.new(Kernel) +# game.run diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb new file mode 100644 index 00000000..abea1429 --- /dev/null +++ b/spec/scores_spec.rb @@ -0,0 +1,103 @@ +require_relative '../scores' + +RSpec.describe Scores do + it 'Frame - Adds scores of two rolls together when they amount to less than 10' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('2').ordered + expect(io).to receive(:puts).with('Frame score: 3').ordered + + game = Scores.new(io) + game.frame + end + + it 'Shows total score for full game, no spares no strikes' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + + expect(io).to receive(:puts).with('Total score: 20').ordered + + game = Scores.new(io) + game.run + end + + xit 'Spare in a frame' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('9') + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('1') + expect(io).to receive(:puts).with('Frame score: 10') + + game = Scores.new(io) + game.run + end + + xit 'Strike in a frame' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('1') + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('2') + expect(io).to receive(:puts).with('Frame score: 3') + + game = Scores.new(io) + game.run + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..c80d44b9 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,98 @@ +# 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. +=begin + # 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 +end From 60d57a3dcf31fba24d60169dcbf32bc11b96fc9b Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 16:35:38 +0200 Subject: [PATCH 02/16] Revert "Basic bowling game works without spares or strikes" This reverts commit fe3b62f8eeb7e923ee83565c37f256d4d6eb96e1. --- .rspec | 1 - scores.rb | 43 ------------------ spec/scores_spec.rb | 103 -------------------------------------------- spec/spec_helper.rb | 98 ----------------------------------------- 4 files changed, 245 deletions(-) delete mode 100644 .rspec delete mode 100644 scores.rb delete mode 100644 spec/scores_spec.rb delete mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec deleted file mode 100644 index c99d2e73..00000000 --- a/.rspec +++ /dev/null @@ -1 +0,0 @@ ---require spec_helper diff --git a/scores.rb b/scores.rb deleted file mode 100644 index c02ea0ce..00000000 --- a/scores.rb +++ /dev/null @@ -1,43 +0,0 @@ -class Scores - - def initialize(io) - @io = io - @total_score = 0 - end - - def roll - @io.puts 'Enter score:' - input = @io.gets.chomp.to_i - if input < 10 - @frame_score += input - end - end - - def frame - @frame_score = 0 - self.roll - self.roll - @total_score += @frame_score - @io.puts "Frame score: #{@frame_score}" - end - - def last_frame - @frame_score = 0 - self.roll - self.roll - if @frame_score == 10 - self.roll - end - @total_score += @frame_score - @io.puts "Frame score: #{@frame_score}" - end - - def run - 9.times{self.frame} - self.last_frame - @io.puts "Total score: #{@total_score}" - end -end - -# game = Scores.new(Kernel) -# game.run diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb deleted file mode 100644 index abea1429..00000000 --- a/spec/scores_spec.rb +++ /dev/null @@ -1,103 +0,0 @@ -require_relative '../scores' - -RSpec.describe Scores do - it 'Frame - Adds scores of two rolls together when they amount to less than 10' do - io = double(:io) - - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('2').ordered - expect(io).to receive(:puts).with('Frame score: 3').ordered - - game = Scores.new(io) - game.frame - end - - it 'Shows total score for full game, no spares no strikes' do - io = double(:io) - - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered - expect(io).to receive(:puts).with('Frame score: 2').ordered - - expect(io).to receive(:puts).with('Total score: 20').ordered - - game = Scores.new(io) - game.run - end - - xit 'Spare in a frame' do - io = double(:io) - - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('9') - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('1') - expect(io).to receive(:puts).with('Frame score: 10') - - game = Scores.new(io) - game.run - end - - xit 'Strike in a frame' do - io = double(:io) - - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('1') - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('2') - expect(io).to receive(:puts).with('Frame score: 3') - - game = Scores.new(io) - game.run - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb deleted file mode 100644 index c80d44b9..00000000 --- a/spec/spec_helper.rb +++ /dev/null @@ -1,98 +0,0 @@ -# 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. -=begin - # 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 -end From 75385bb8979f4b7aa72583c20911e70155576249 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 16:38:07 +0200 Subject: [PATCH 03/16] basic bowling game scorecard works without strikes or spares --- .rspec | 1 + lib/scores.rb | 43 ++++++++++++++++++ spec/scores_spec.rb | 103 ++++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 98 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 .rspec create mode 100644 lib/scores.rb create mode 100644 spec/scores_spec.rb create mode 100644 spec/spec_helper.rb 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/lib/scores.rb b/lib/scores.rb new file mode 100644 index 00000000..5f179139 --- /dev/null +++ b/lib/scores.rb @@ -0,0 +1,43 @@ +class Scores + + def initialize(io) + @io = io + @total_score = 0 + end + + def roll + @io.puts 'Enter score:' + input = @io.gets.chomp.to_i + if input < 10 + @frame_score += input + end + end + + def frame + @frame_score = 0 + self.roll + self.roll + @total_score += @frame_score + @io.puts "Frame score: #{@frame_score}" + end + + def last_frame + @frame_score = 0 + self.roll + self.roll + if @frame_score == 10 + self.roll + end + @total_score += @frame_score + @io.puts "Frame score: #{@frame_score}" + end + + def run + 9.times{self.frame} + self.last_frame + @io.puts "Total score: #{@total_score}" + end +end + +# game = Scores.new(Kernel) +# game.run \ No newline at end of file diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb new file mode 100644 index 00000000..dc7378ad --- /dev/null +++ b/spec/scores_spec.rb @@ -0,0 +1,103 @@ +require 'scores' + +RSpec.describe Scores do + it 'Frame - Adds scores of two rolls together when they amount to less than 10' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('2').ordered + expect(io).to receive(:puts).with('Frame score: 3').ordered + + game = Scores.new(io) + game.frame + end + + it 'Shows total score for full game, no spares no strikes' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 2').ordered + + expect(io).to receive(:puts).with('Total score: 20').ordered + + game = Scores.new(io) + game.run + end + + xit 'Spare in a frame' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('9') + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('1') + expect(io).to receive(:puts).with('Frame score: 10') + + game = Scores.new(io) + game.run + end + + xit 'Strike in a frame' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('1') + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('2') + expect(io).to receive(:puts).with('Frame score: 3') + + game = Scores.new(io) + game.run + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..c80d44b9 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,98 @@ +# 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. +=begin + # 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 +end From 2df69912a6c8f30bba3d2727023871ebdd301e8a Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 16:48:29 +0200 Subject: [PATCH 04/16] added a working scoreboard up until frame 9 --- lib/scores.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 5f179139..7c2c8fb1 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -3,6 +3,7 @@ class Scores def initialize(io) @io = io @total_score = 0 + @scoreboard = [] end def roll @@ -10,14 +11,17 @@ def roll input = @io.gets.chomp.to_i if input < 10 @frame_score += input + return input end end def frame @frame_score = 0 - self.roll - self.roll + roll_1 = self.roll + roll_2 = self.roll @total_score += @frame_score + @frame = [[roll_1, roll_2], @total_score] + @scoreboard << @frame @io.puts "Frame score: #{@frame_score}" end @@ -40,4 +44,4 @@ def run end # game = Scores.new(Kernel) -# game.run \ No newline at end of file +# game.run From 20cd77b28f613e3dda4d7e4740417443b8772194 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 16:49:02 +0200 Subject: [PATCH 05/16] testing commit revert --- lib/scores.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 7c2c8fb1..c70c4b3d 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -43,5 +43,5 @@ def run end end -# game = Scores.new(Kernel) -# game.run +game = Scores.new(Kernel) +game.run From 17cc6d9341f5b73727b4d364de3f801e2f6598e4 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 17:12:22 +0200 Subject: [PATCH 06/16] added logic for last frame --- lib/scores.rb | 15 ++++++++------- spec/scores_spec.rb | 13 +++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index c70c4b3d..475b6172 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -9,7 +9,7 @@ def initialize(io) def roll @io.puts 'Enter score:' input = @io.gets.chomp.to_i - if input < 10 + if (@frame_score + input) < 10 @frame_score += input return input end @@ -27,11 +27,12 @@ def frame def last_frame @frame_score = 0 - self.roll - self.roll - if @frame_score == 10 - self.roll + rolls = [self.roll, self.roll] + if @frame_score >= 10 + rolls << self.roll end + @frame = [rolls, @total_score] + @scoreboard << @frame @total_score += @frame_score @io.puts "Frame score: #{@frame_score}" end @@ -43,5 +44,5 @@ def run end end -game = Scores.new(Kernel) -game.run +# game = Scores.new(Kernel) +# game.last_frame diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index dc7378ad..791002f3 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -75,6 +75,19 @@ game.run end + xit 'Last frame' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('9') + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('1') + expect(io).to receive(:puts).with('Frame score: 10') + + game = Scores.new(io) + game.run + end + xit 'Spare in a frame' do io = double(:io) From 56caaf8e8d843a8312f678c73de03e123a21ba70 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 17:13:55 +0200 Subject: [PATCH 07/16] last frame test works when no spare or strike --- spec/scores_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index 791002f3..257fe343 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -75,17 +75,17 @@ game.run end - xit 'Last frame' do + it 'Last frame - 2 rolls if no spare or strike' do io = double(:io) - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('9') expect(io).to receive(:puts).with('Enter score:') expect(io).to receive(:gets).and_return('1') - expect(io).to receive(:puts).with('Frame score: 10') + expect(io).to receive(:puts).with('Enter score:') + expect(io).to receive(:gets).and_return('2') + expect(io).to receive(:puts).with('Frame score: 3') game = Scores.new(io) - game.run + game.last_frame end xit 'Spare in a frame' do From 73fdd61e14c86f4a3c4fe0bc1c6c21a19e1cc8c8 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 17:18:32 +0200 Subject: [PATCH 08/16] Last frame passes test when there is a spare --- lib/scores.rb | 6 ++---- spec/scores_spec.rb | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 475b6172..3aec44b8 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -9,10 +9,8 @@ def initialize(io) def roll @io.puts 'Enter score:' input = @io.gets.chomp.to_i - if (@frame_score + input) < 10 - @frame_score += input - return input - end + @frame_score += input + return input end def frame diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index 257fe343..9e484cdf 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -78,11 +78,26 @@ it 'Last frame - 2 rolls if no spare or strike' do io = double(:io) - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('1') - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('2') - expect(io).to receive(:puts).with('Frame score: 3') + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('2').ordered + expect(io).to receive(:puts).with('Frame score: 3').ordered + + game = Scores.new(io) + game.last_frame + end + + it 'Last frame - 3 rolls if there is a spare or strike' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('3').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('7').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('3').ordered + expect(io).to receive(:puts).with('Frame score: 13').ordered game = Scores.new(io) game.last_frame @@ -91,11 +106,11 @@ xit 'Spare in a frame' do io = double(:io) - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('9') - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('1') - expect(io).to receive(:puts).with('Frame score: 10') + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('9').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Frame score: 10').ordered game = Scores.new(io) game.run @@ -104,11 +119,11 @@ xit 'Strike in a frame' do io = double(:io) - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('1') - expect(io).to receive(:puts).with('Enter score:') - expect(io).to receive(:gets).and_return('2') - expect(io).to receive(:puts).with('Frame score: 3') + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('2').ordered + expect(io).to receive(:puts).with('Frame score: 3').ordered game = Scores.new(io) game.run From 900d64096a1e32db876ed8e4991ac5382adc1243 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 18:10:20 +0200 Subject: [PATCH 09/16] trying to make spares bonus count, commented lines are no longer working --- lib/scores.rb | 41 ++++++++++++++++++++++++++++++++--------- spec/scores_spec.rb | 10 +++++++++- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 3aec44b8..1260c8af 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -3,7 +3,9 @@ class Scores def initialize(io) @io = io @total_score = 0 - @scoreboard = [] + # @scoreboard = [] + @strike = false + @spare = false end def roll @@ -15,22 +17,43 @@ def roll def frame @frame_score = 0 + # rolls = [] roll_1 = self.roll - roll_2 = self.roll + # rolls << roll_1 + if @frame_score < 10 + roll_2 = self.roll + # rolls << roll_2 + else + + end @total_score += @frame_score - @frame = [[roll_1, roll_2], @total_score] - @scoreboard << @frame + + # @frame = [rolls, @total_score] + # @scoreboard << @frame + @io.puts "Frame score: #{@frame_score}" end def last_frame @frame_score = 0 - rolls = [self.roll, self.roll] + + # rolls = [] + + roll_1 = self.roll + roll_2 = self.roll + + # rolls << roll_1 + # rolls << roll_2 + if @frame_score >= 10 - rolls << self.roll + roll_3 = self.roll + + # rolls << roll_3 end - @frame = [rolls, @total_score] - @scoreboard << @frame + + # @frame = [rolls, @total_score] + # @scoreboard << @frame + @total_score += @frame_score @io.puts "Frame score: #{@frame_score}" end @@ -43,4 +66,4 @@ def run end # game = Scores.new(Kernel) -# game.last_frame +# game.run diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index 9e484cdf..c7f47275 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -103,7 +103,7 @@ game.last_frame end - xit 'Spare in a frame' do + xit 'Spare in a previous frame adds to previous frame score from current first roll' do io = double(:io) expect(io).to receive(:puts).with('Enter score:').ordered @@ -112,6 +112,14 @@ expect(io).to receive(:gets).and_return('1').ordered expect(io).to receive(:puts).with('Frame score: 10').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('2').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('3').ordered + expect(io).to receive(:puts).with('Frame score: 10').ordered + + expect(@scoreboard[]) + game = Scores.new(io) game.run end From ffabe45e99433ae68a04d12591c709f9afc1c015 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 20:02:30 +0200 Subject: [PATCH 10/16] creating scoreboard with rolls and current total score works --- lib/scores.rb | 24 ++++++++++++------------ spec/scores_spec.rb | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 1260c8af..d25cdd85 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -3,7 +3,7 @@ class Scores def initialize(io) @io = io @total_score = 0 - # @scoreboard = [] + @scoreboard = [] @strike = false @spare = false end @@ -17,19 +17,19 @@ def roll def frame @frame_score = 0 - # rolls = [] + rolls = [] roll_1 = self.roll - # rolls << roll_1 + rolls << roll_1 if @frame_score < 10 roll_2 = self.roll - # rolls << roll_2 + rolls << roll_2 else end @total_score += @frame_score - # @frame = [rolls, @total_score] - # @scoreboard << @frame + @frame = [rolls, @total_score] + @scoreboard << @frame @io.puts "Frame score: #{@frame_score}" end @@ -37,22 +37,22 @@ def frame def last_frame @frame_score = 0 - # rolls = [] + rolls = [] roll_1 = self.roll roll_2 = self.roll - # rolls << roll_1 - # rolls << roll_2 + rolls << roll_1 + rolls << roll_2 if @frame_score >= 10 roll_3 = self.roll - # rolls << roll_3 + rolls << roll_3 end - # @frame = [rolls, @total_score] - # @scoreboard << @frame + @frame = [rolls, @total_score] + @scoreboard << @frame @total_score += @frame_score @io.puts "Frame score: #{@frame_score}" diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index c7f47275..5b2b96bd 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -103,7 +103,7 @@ game.last_frame end - xit 'Spare in a previous frame adds to previous frame score from current first roll' do + xit 'Spare adds bonus points from first roll' do io = double(:io) expect(io).to receive(:puts).with('Enter score:').ordered From 2deb878aa7a3c50368f2dc8ba13ff101162f0beb Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 23:34:26 +0200 Subject: [PATCH 11/16] scoreboard part of new method --- lib/scores.rb | 13 ++++++++----- spec/scores_spec.rb | 4 +--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index d25cdd85..058a4a56 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -24,12 +24,11 @@ def frame roll_2 = self.roll rolls << roll_2 else - + end @total_score += @frame_score - @frame = [rolls, @total_score] - @scoreboard << @frame + score(rolls) @io.puts "Frame score: #{@frame_score}" end @@ -51,13 +50,17 @@ def last_frame rolls << roll_3 end - @frame = [rolls, @total_score] - @scoreboard << @frame + score(rolls) @total_score += @frame_score @io.puts "Frame score: #{@frame_score}" end + def score(rolls) + @frame = [rolls, @total_score] + @scoreboard << @frame + end + def run 9.times{self.frame} self.last_frame diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index 5b2b96bd..11f72f46 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -116,9 +116,7 @@ expect(io).to receive(:gets).and_return('2').ordered expect(io).to receive(:puts).with('Enter score:').ordered expect(io).to receive(:gets).and_return('3').ordered - expect(io).to receive(:puts).with('Frame score: 10').ordered - - expect(@scoreboard[]) + expect(io).to receive(:puts).with('Frame score: 5').ordered game = Scores.new(io) game.run From 409145157be72c030b43cb133780efcd6bca7477 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 23:44:18 +0200 Subject: [PATCH 12/16] created new method for total --- lib/scores.rb | 7 ++++++- spec/scores_spec.rb | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 058a4a56..2dd03bc1 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -59,12 +59,17 @@ def last_frame def score(rolls) @frame = [rolls, @total_score] @scoreboard << @frame + return @scoreboard + end + + def total + @io.puts "Total score: #{@total_score}" end def run 9.times{self.frame} self.last_frame - @io.puts "Total score: #{@total_score}" + self.total end end diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index 11f72f46..fd572290 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -103,7 +103,7 @@ game.last_frame end - xit 'Spare adds bonus points from first roll' do + it 'Spare adds bonus points from first roll' do io = double(:io) expect(io).to receive(:puts).with('Enter score:').ordered @@ -119,7 +119,8 @@ expect(io).to receive(:puts).with('Frame score: 5').ordered game = Scores.new(io) - game.run + game.frame + game.frame end xit 'Strike in a frame' do From c0a227e98e9c69e5d21db19a096217faaf742825 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Sun, 21 May 2023 23:46:13 +0200 Subject: [PATCH 13/16] correct red error for spare test --- spec/scores_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index fd572290..ff1d0b6c 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -118,9 +118,12 @@ expect(io).to receive(:gets).and_return('3').ordered expect(io).to receive(:puts).with('Frame score: 5').ordered + expect(io).to receive(:puts).with('Total score: 17').ordered + game = Scores.new(io) game.frame game.frame + game.total end xit 'Strike in a frame' do From da9f6a4e7125bcdeb4cc65c7d38f88a788d94ef4 Mon Sep 17 00:00:00 2001 From: Sharmine Date: Mon, 22 May 2023 00:28:47 +0200 Subject: [PATCH 14/16] spares working correctly, only strikes are not yet working --- lib/scores.rb | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index 2dd03bc1..ea81a41c 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -4,33 +4,37 @@ def initialize(io) @io = io @total_score = 0 @scoreboard = [] - @strike = false - @spare = false + @strike = 1 + @spare = 1 + @frame = 1 end def roll @io.puts 'Enter score:' input = @io.gets.chomp.to_i @frame_score += input + @total_score += input * @spare return input end def frame + @frame += 1 @frame_score = 0 rolls = [] + roll_1 = self.roll rolls << roll_1 + @spare = 1 + if @frame_score < 10 roll_2 = self.roll rolls << roll_2 - else - end - @total_score += @frame_score score(rolls) @io.puts "Frame score: #{@frame_score}" + bonus(roll_1, roll_2) end def last_frame @@ -46,22 +50,30 @@ def last_frame if @frame_score >= 10 roll_3 = self.roll - rolls << roll_3 end score(rolls) - @total_score += @frame_score @io.puts "Frame score: #{@frame_score}" end def score(rolls) - @frame = [rolls, @total_score] - @scoreboard << @frame + @scoreboard << rolls return @scoreboard end + def bonus(roll_1, roll_2) + if roll_1 == 10 + @strike = 2 + elsif roll_1 + roll_2 == 10 + @spare = 2 + else + @strike = 1 + @spare = 1 + end + end + def total @io.puts "Total score: #{@total_score}" end @@ -74,4 +86,6 @@ def run end # game = Scores.new(Kernel) +# game.frame +# game.frame # game.run From 2041b695d1981319a9e4dc6eadc6e6db599d097f Mon Sep 17 00:00:00 2001 From: Sharmine Date: Mon, 22 May 2023 00:46:19 +0200 Subject: [PATCH 15/16] got strikes to work, further refactoring for many spares and strikes in a row required --- lib/scores.rb | 6 ++++-- spec/scores_spec.rb | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/scores.rb b/lib/scores.rb index ea81a41c..20db67d0 100644 --- a/lib/scores.rb +++ b/lib/scores.rb @@ -13,7 +13,7 @@ def roll @io.puts 'Enter score:' input = @io.gets.chomp.to_i @frame_score += input - @total_score += input * @spare + @total_score += input * @spare * @strike return input end @@ -26,9 +26,10 @@ def frame rolls << roll_1 @spare = 1 - if @frame_score < 10 + if roll_1 < 10 roll_2 = self.roll rolls << roll_2 + @strike = 1 end score(rolls) @@ -38,6 +39,7 @@ def frame end def last_frame + #refactor to count bonus @frame_score = 0 rolls = [] diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index ff1d0b6c..e50ff3b7 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -126,16 +126,26 @@ game.total end - xit 'Strike in a frame' do + it 'Strike adds bonus points from first and second roll' do io = double(:io) expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('1').ordered + expect(io).to receive(:gets).and_return('10').ordered + + + expect(io).to receive(:puts).with('Frame score: 10').ordered + expect(io).to receive(:puts).with('Enter score:').ordered - expect(io).to receive(:gets).and_return('2').ordered - expect(io).to receive(:puts).with('Frame score: 3').ordered + expect(io).to receive(:gets).and_return('4').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('5').ordered + expect(io).to receive(:puts).with('Frame score: 9').ordered + + expect(io).to receive(:puts).with('Total score: 28').ordered game = Scores.new(io) - game.run + game.frame + game.frame + game.total end end \ No newline at end of file From 03fd08e203df73c8620a042041c5a308a321d32a Mon Sep 17 00:00:00 2001 From: Sharmine Date: Mon, 22 May 2023 00:49:21 +0200 Subject: [PATCH 16/16] started writing test case for multiple spares and strikes in a row, red --- spec/scores_spec.rb | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb index e50ff3b7..dab1f93e 100644 --- a/spec/scores_spec.rb +++ b/spec/scores_spec.rb @@ -131,8 +131,6 @@ expect(io).to receive(:puts).with('Enter score:').ordered expect(io).to receive(:gets).and_return('10').ordered - - expect(io).to receive(:puts).with('Frame score: 10').ordered expect(io).to receive(:puts).with('Enter score:').ordered @@ -148,4 +146,31 @@ game.frame game.total end + + it 'Multiples spares and strikes in a row' do + io = double(:io) + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('3').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('7').ordered + expect(io).to receive(:puts).with('Frame score: 10').ordered + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('10').ordered + expect(io).to receive(:puts).with('Frame score: 10').ordered + + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('3').ordered + expect(io).to receive(:puts).with('Enter score:').ordered + expect(io).to receive(:gets).and_return('4').ordered + expect(io).to receive(:puts).with('Frame score: 7').ordered + + expect(io).to receive(:puts).with('Total score: 44').ordered + + game = Scores.new(io) + game.frame + game.frame + game.total + end end \ No newline at end of file