From 1fa51203ae1dbcdebcd3e4523de632fa3d8b64de Mon Sep 17 00:00:00 2001 From: JRSkates Date: Mon, 22 May 2023 18:57:06 +0100 Subject: [PATCH 1/8] first commit --- .rspec | 1 + Gemfile | 7 ++++ Gemfile.lock | 26 ++++++++++++ spec/spec_helper.rb | 98 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 Gemfile.lock 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/Gemfile b/Gemfile new file mode 100644 index 00000000..41df210d --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# gem "rails" + +gem "rspec", "~> 3.12" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..b39934ea --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,26 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.5.0) + 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) + +PLATFORMS + arm64-darwin-21 + +DEPENDENCIES + rspec (~> 3.12) + +BUNDLED WITH + 2.4.13 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 fa711d772f8ee9307b23c25241085477369b6b66 Mon Sep 17 00:00:00 2001 From: JRSkates Date: Mon, 22 May 2023 18:59:41 +0100 Subject: [PATCH 2/8] Project Files set-up --- lib/frame.rb | 0 lib/scorecard.rb | 0 spec/frame_spec.rb | 0 spec/integration/app_spec.rb | 0 spec/scorecard_spec.rb | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/frame.rb create mode 100644 lib/scorecard.rb create mode 100644 spec/frame_spec.rb create mode 100644 spec/integration/app_spec.rb create mode 100644 spec/scorecard_spec.rb diff --git a/lib/frame.rb b/lib/frame.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/scorecard.rb b/lib/scorecard.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/scorecard_spec.rb b/spec/scorecard_spec.rb new file mode 100644 index 00000000..e69de29b From 78a7835085bfd4a011677108faeb412ff9b9038e Mon Sep 17 00:00:00 2001 From: JRSkates Date: Tue, 23 May 2023 15:06:27 +0100 Subject: [PATCH 3/8] Class Design Complete --- design/class_recipe.md | 169 +++++++++++++++++++++++++ lib/{frame.rb => scores.rb} | 0 spec/{frame_spec.rb => scores_spec.rb} | 0 3 files changed, 169 insertions(+) create mode 100644 design/class_recipe.md rename lib/{frame.rb => scores.rb} (100%) rename spec/{frame_spec.rb => scores_spec.rb} (100%) diff --git a/design/class_recipe.md b/design/class_recipe.md new file mode 100644 index 00000000..b529eb77 --- /dev/null +++ b/design/class_recipe.md @@ -0,0 +1,169 @@ + +# Multi-Class Planned Design Recipe + +## 1. Describe the Problem + +As a user +I want to be able to see +a scorecard of my bowling game +for one player + +As a user +I want to see results +frame by frame in a 10 +frame game of bowling + +As a user +I will inputthe score of each roll, +of each frame + +As a user +I want to see a running +total score of all the points +I have total frame by frame + +As a user +I want to acurrately +calcuate a strike so I get +10 points for knocking down all pins +and x amount of extra points, x being +the number of pins knocked down in the +BOTH rolls of next frame + +As a user +I want to acurrately +calcuate a spare 10 points for knocking down all pins +over two rolls, and x amount of extra points, x being +the number of pins knocked down in the +FIRST roll of next frame + +As a user +if i roll a strike on the tenth frame +i want to be able to roll twice more +for my bonus points + +As a user +if I roll a spare on the tenth frame +i want to be able to roll once more +for my bonus points + +## 2. Design the Class System + +nouns: + +frame, scorecard, points, rolls, strike, spare, tenth frame, bonus points + +```ruby + +class Frame + def initialize(io) + @io = io + @final = [] + end + + def start_frame + first_roll = roll(1) + if first_roll == 10 + @final << first_roll, 'X' + return @final + else + second_roll = roll(2) + @final << first_roll, second_roll + return @final + end + end + + def roll(roll_number) + @io.puts "Roll #{roll_number} - enter number of pins knocked down" + pins_knocked_down = @io.gets.chomp.to_i + return pins_knocked_down + end + + def strike? + @final.first == 10 + end + + def spare? + !strike? && @final.sum == 10 + end + + def final + return @final + end +end + + +class ScoreCard + def initialize + @frame_count = 0 + @all_frames = [] + end + + def frame_input(frame) + if @frame_count == 9 + return tenth_frame + else + @all_frames << frame.final + @frame_count += 1 + return "#{10 - @frame_count} frames left" + end + end + + def calculate_total + + end + + def tenth_frame + + end + + def final_format + return "Your game isn't over yet!" if @frame_count < 10 + end + + private + + def spare_bonus_roll + + end + + def stike_bonus_rolls + + end +end +``` + +## 3. Create Examples as Integration Tests + +```ruby +RSpec.describe 'integration' do + context 'Inputting a Frame' do + it 'correctly inputs one Frame object into the total array' do + + end + end +end +``` + +## 4. Create Examples as Unit Tests + +Create examples, where appropriate, of the behaviour of each relevant class at +a more granular level of detail. + +Encode one of these as a test and move to step 5. + +## 5. Implement the Behaviour + +For each example you create as a test, implement the behaviour that allows the +class to behave according to your example. + +Then return to step 3 until you have addressed the problem you were given. You +may also need to revise your design, for example if you realise you made a +mistake earlier. + +Copy and fill out [this template](../resources/multi_class_recipe_template.md) +for each of the below exercises. + +## Demonstration + +[A video demonstration](https://www.youtube.com/watch?v=CkyhW3pNTUY&t=0s) diff --git a/lib/frame.rb b/lib/scores.rb similarity index 100% rename from lib/frame.rb rename to lib/scores.rb diff --git a/spec/frame_spec.rb b/spec/scores_spec.rb similarity index 100% rename from spec/frame_spec.rb rename to spec/scores_spec.rb From 565704d1218d7f645fdb282650fefb3d6c7e2d38 Mon Sep 17 00:00:00 2001 From: JRSkates Date: Tue, 23 May 2023 17:24:28 +0100 Subject: [PATCH 4/8] Frame Objects TDD, Can handle normal, strikes and spares --- design/class_recipe.md | 21 ++++------ lib/frame.rb | 29 +++++++++++++ lib/scorecard.rb | 65 +++++++++++++++++++++++++++++ lib/scores.rb | 0 spec/frame_spec.rb | 32 ++++++++++++++ spec/integration/app_spec.rb | 0 spec/integration_spec.rb | 81 ++++++++++++++++++++++++++++++++++++ spec/scorecard_spec.rb | 10 +++++ spec/scores_spec.rb | 0 9 files changed, 225 insertions(+), 13 deletions(-) create mode 100644 lib/frame.rb delete mode 100644 lib/scores.rb create mode 100644 spec/frame_spec.rb delete mode 100644 spec/integration/app_spec.rb create mode 100644 spec/integration_spec.rb delete mode 100644 spec/scores_spec.rb diff --git a/design/class_recipe.md b/design/class_recipe.md index b529eb77..65aef9b2 100644 --- a/design/class_recipe.md +++ b/design/class_recipe.md @@ -56,29 +56,20 @@ frame, scorecard, points, rolls, strike, spare, tenth frame, bonus points ```ruby class Frame - def initialize(io) - @io = io + def initialize @final = [] end - def start_frame - first_roll = roll(1) + def result(first_roll, second_roll) if first_roll == 10 @final << first_roll, 'X' return @final else - second_roll = roll(2) @final << first_roll, second_roll return @final end end - def roll(roll_number) - @io.puts "Roll #{roll_number} - enter number of pins knocked down" - pins_knocked_down = @io.gets.chomp.to_i - return pins_knocked_down - end - def strike? @final.first == 10 end @@ -105,7 +96,7 @@ class ScoreCard else @all_frames << frame.final @frame_count += 1 - return "#{10 - @frame_count} frames left" + return "#{all_frames} - #{10 - @frame_count} frames left" end end @@ -139,7 +130,11 @@ end RSpec.describe 'integration' do context 'Inputting a Frame' do it 'correctly inputs one Frame object into the total array' do - + scorecard = Scorecard.new + frame_one = Frame.new + frame_one.result(3, 5) + + expect(scorecard..frame_input(frame_one)).to eq "[3, 5] - 9 frames left" end end end diff --git a/lib/frame.rb b/lib/frame.rb new file mode 100644 index 00000000..ee129906 --- /dev/null +++ b/lib/frame.rb @@ -0,0 +1,29 @@ +class Frame + def initialize + @final = [] + end + + def result(first_roll, second_roll) + if first_roll == 10 + @final << first_roll + @final << 0 + return @final + else + @final << first_roll + @final << second_roll + return @final + end + end + + def strike? + @final.first == 10 + end + + def spare? + !strike? && @final.sum == 10 + end + + def final + return @final + end +end \ No newline at end of file diff --git a/lib/scorecard.rb b/lib/scorecard.rb index e69de29b..7d26cb13 100644 --- a/lib/scorecard.rb +++ b/lib/scorecard.rb @@ -0,0 +1,65 @@ +require_relative 'frame' + +class ScoreCard + def initialize + @frame_count = 10 + @all_frames = [] + end + + def frame_input(frame) + #p @all_frames + @frame_count -= 1 + if @all_frames.empty? + @all_frames << frame + return calculate_total + elsif @all_frames.last.strike? == true + return strike_bonus_rolls(frame) + elsif @all_frames.last.spare? == true + return spare_bonus_roll(frame) + else + @all_frames << frame + return calculate_total + end + end + + def calculate_total + total_frames = @all_frames.map do |frame| + if frame.is_a?(Array) + frame + else + frame.final + end + end + total_frames.compact! + total = total_frames.flatten.reduce(:+) + return "Current Total: #{total} - #{@frame_count} frames left\n#{total_frames}" + end + + def tenth_frame + + end + + def final_format + return "Your game isn't over yet! You have #{@frame_count} frames left" if @frame_count > 0 + end + + private + + def spare_bonus_roll(frame) + @all_frames.delete_at(@all_frames.length - 1) + total = (frame.final.first) + 10 + spare_bonus = [total, 0] + @all_frames << spare_bonus + @all_frames << frame + return calculate_total + end + + def strike_bonus_rolls(frame) + @all_frames.delete_at(@all_frames.length - 1) + total = (frame.final.sum) + 10 + strike_bonus = [total, 0] + @all_frames << strike_bonus + @all_frames << frame + return calculate_total + end +end \ No newline at end of file diff --git a/lib/scores.rb b/lib/scores.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb new file mode 100644 index 00000000..23e64cba --- /dev/null +++ b/spec/frame_spec.rb @@ -0,0 +1,32 @@ +require 'frame' + +RSpec.describe Frame do + context 'Constructing a Frame Object' do + it 'should create a normal Frame' do + frame = Frame.new + + expect(frame.result(3, 5)).to eq [3, 5] + expect(frame.final).to eq [3, 5] + end + + it 'should construct a Stike Frame object' do + frame = Frame.new + + expect(frame.result(10, 3)).to eq [10, 0] + expect(frame.final).to eq [10, 0] + expect(frame.strike?).to eq true + end + + it 'should construct a Spare Frame object' do + frame = Frame.new + frame_2 = Frame.new + + expect(frame.result(7, 3)).to eq [7, 3] + expect(frame.final).to eq [7, 3] + expect(frame.spare?).to eq true + expect(frame_2.result(4, 6)).to eq [4, 6] + expect(frame_2.final).to eq [4, 6] + expect(frame_2.spare?).to eq true + end + end +end \ No newline at end of file diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb new file mode 100644 index 00000000..b0745e52 --- /dev/null +++ b/spec/integration_spec.rb @@ -0,0 +1,81 @@ +require 'frame_spec' +require 'scorecard' + +RSpec.describe 'ScoreCard and Frame Integration' do + context 'Inputting Frames to a ScoreCard' do + it 'correctly inputs one Frame object into the total array' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(3, 5) + + expect(scorecard.frame_input(frame_one)).to eq "Current Total: 8 - 9 frames left\n[[3, 5]]" + end + + it 'correctly inputs two Frame object into the total array' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(3, 5) + scorecard.frame_input(frame_one) + frame_two = Frame.new + frame_two.result(7, 1) + expect(scorecard.frame_input(frame_two)).to eq "Current Total: 16 - 8 frames left\n[[3, 5], [7, 1]]" + end + + it 'correctly identifies a Frame Object that is a Strike' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(10, 0) + scorecard.frame_input(frame_one) + frame_two = Frame.new + frame_two.result(3, 2) + expect(scorecard.frame_input(frame_two)).to eq "Current Total: 20 - 8 frames left\n[[15, 0], [3, 2]]" + end + + it 'correctly identifies a Frame Object that is a Spare' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(8, 2) + scorecard.frame_input(frame_one) + frame_two = Frame.new + frame_two.result(3, 2) + expect(scorecard.frame_input(frame_two)).to eq "Current Total: 18 - 8 frames left\n[[13, 0], [3, 2]]" + end + end + + context 'correctly returns a total score of frames so far' do + it 'correctly returns the result of two frames' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(3, 5) + scorecard.frame_input(frame_one) + frame_two = Frame.new + frame_two.result(7, 1) + scorecard.frame_input(frame_two) + + expect(scorecard.calculate_total).to eq "Current Total: 16 - 8 frames left\n[[3, 5], [7, 1]]" + end + + it 'correctly returns the result of six frames, with Spares and Strikes' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(3, 5) + scorecard.frame_input(frame_one) + frame_two = Frame.new + frame_two.result(7, 1) + scorecard.frame_input(frame_two) + frame_three = Frame.new + frame_three.result(10, 0) + scorecard.frame_input(frame_three) + frame_four = Frame.new + frame_four.result(4, 3) + scorecard.frame_input(frame_four) + frame_five = Frame.new + frame_five.result(2, 8) + scorecard.frame_input(frame_five) + frame_six = Frame.new + frame_six.result(3, 5) + + expect(scorecard.frame_input(frame_six)).to eq "Current Total: 61 - 4 frames left\n[[3, 5], [7, 1], [17, 0], [4, 3], [13, 0], [3, 5]]" + end + end +end \ No newline at end of file diff --git a/spec/scorecard_spec.rb b/spec/scorecard_spec.rb index e69de29b..977a5c29 100644 --- a/spec/scorecard_spec.rb +++ b/spec/scorecard_spec.rb @@ -0,0 +1,10 @@ +require 'scorecard' + +RSpec.describe ScoreCard do + context 'final format method' do + it "Returns early if the ScoreCard doesn't have 10 Frames" do + scorecard = ScoreCard.new + expect(scorecard.final_format).to eq "Your game isn't over yet! You have 10 frames left" + end + end +end \ No newline at end of file diff --git a/spec/scores_spec.rb b/spec/scores_spec.rb deleted file mode 100644 index e69de29b..00000000 From 2b6dfa9f587dbee016b24a45f473125e38bf57ad Mon Sep 17 00:00:00 2001 From: JRSkates Date: Tue, 23 May 2023 18:35:38 +0100 Subject: [PATCH 5/8] rubocop --- Gemfile | 6 ++- Gemfile.lock | 23 +++++++++ lib/frame.rb | 32 ++++++------ lib/scorecard.rb | 62 ++++++++++++------------ spec/frame_spec.rb | 16 +++--- spec/integration_spec.rb | 7 ++- spec/scorecard_spec.rb | 4 +- spec/spec_helper.rb | 102 +++++++++++++++++++-------------------- 8 files changed, 141 insertions(+), 111 deletions(-) diff --git a/Gemfile b/Gemfile index 41df210d..e9dfc3e6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,9 @@ # frozen_string_literal: true -source "https://rubygems.org" +source 'https://rubygems.org' # gem "rails" -gem "rspec", "~> 3.12" +gem 'rspec', '~> 3.12' + +gem 'rubocop', '~> 1.51' diff --git a/Gemfile.lock b/Gemfile.lock index b39934ea..8d1b1638 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,15 @@ 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) @@ -15,12 +23,27 @@ GEM 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 arm64-darwin-21 DEPENDENCIES rspec (~> 3.12) + rubocop (~> 1.51) BUNDLED WITH 2.4.13 diff --git a/lib/frame.rb b/lib/frame.rb index ee129906..256698ad 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -1,29 +1,27 @@ +# frozen_string_literal: true + class Frame def initialize @final = [] end - + def result(first_roll, second_roll) - if first_roll == 10 - @final << first_roll - @final << 0 - return @final - else - @final << first_roll - @final << second_roll - return @final - end + @final << first_roll + @final << if first_roll == 10 + 0 + else + second_roll + end + @final end - + def strike? @final.first == 10 end - + def spare? !strike? && @final.sum == 10 end - - def final - return @final - end -end \ No newline at end of file + + attr_reader :final +end diff --git a/lib/scorecard.rb b/lib/scorecard.rb index 7d26cb13..23b86463 100644 --- a/lib/scorecard.rb +++ b/lib/scorecard.rb @@ -1,65 +1,65 @@ +# frozen_string_literal: true + require_relative 'frame' class ScoreCard - def initialize + def initialize @frame_count = 10 @all_frames = [] end - + def frame_input(frame) - #p @all_frames @frame_count -= 1 + return tenth_frame if @frame_count.zero? + if @all_frames.empty? @all_frames << frame - return calculate_total + calculate_total elsif @all_frames.last.strike? == true - return strike_bonus_rolls(frame) + strike_bonus_rolls(frame) elsif @all_frames.last.spare? == true - return spare_bonus_roll(frame) + spare_bonus_roll(frame) else - @all_frames << frame - return calculate_total + @all_frames << frame + calculate_total end end - + def calculate_total - total_frames = @all_frames.map do |frame| - if frame.is_a?(Array) - frame - else + total_frames = @all_frames.map do |frame| + if frame.is_a?(Array) + frame + else frame.final - end + end end - total_frames.compact! - total = total_frames.flatten.reduce(:+) - return "Current Total: #{total} - #{@frame_count} frames left\n#{total_frames}" - end - - def tenth_frame - + total = total_frames.compact.flatten.reduce(:+) + "Current Total: #{total} - #{@frame_count} frames left\n#{total_frames}" end - + + def tenth_frame; end + def final_format - return "Your game isn't over yet! You have #{@frame_count} frames left" if @frame_count > 0 + return "Your game isn't over yet! You have #{@frame_count} frames left" if @frame_count.positive? end - + private - + def spare_bonus_roll(frame) @all_frames.delete_at(@all_frames.length - 1) - total = (frame.final.first) + 10 + total = frame.final.first + 10 spare_bonus = [total, 0] @all_frames << spare_bonus @all_frames << frame - return calculate_total + calculate_total end - + def strike_bonus_rolls(frame) @all_frames.delete_at(@all_frames.length - 1) - total = (frame.final.sum) + 10 + total = frame.final.sum + 10 strike_bonus = [total, 0] @all_frames << strike_bonus @all_frames << frame - return calculate_total + calculate_total end -end \ No newline at end of file +end diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 23e64cba..f01f3dfe 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require 'frame' RSpec.describe Frame do context 'Constructing a Frame Object' do it 'should create a normal Frame' do frame = Frame.new - + expect(frame.result(3, 5)).to eq [3, 5] expect(frame.final).to eq [3, 5] end @@ -18,15 +20,15 @@ end it 'should construct a Spare Frame object' do - frame = Frame.new - frame_2 = Frame.new + frame = Frame.new + frame_two = Frame.new expect(frame.result(7, 3)).to eq [7, 3] expect(frame.final).to eq [7, 3] expect(frame.spare?).to eq true - expect(frame_2.result(4, 6)).to eq [4, 6] - expect(frame_2.final).to eq [4, 6] - expect(frame_2.spare?).to eq true + expect(frame_two.result(4, 6)).to eq [4, 6] + expect(frame_two.final).to eq [4, 6] + expect(frame_two.spare?).to eq true end end -end \ No newline at end of file +end diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index b0745e52..6a4aa009 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'frame_spec' require 'scorecard' @@ -74,8 +76,9 @@ scorecard.frame_input(frame_five) frame_six = Frame.new frame_six.result(3, 5) + scorecard.frame_input(frame_six) - expect(scorecard.frame_input(frame_six)).to eq "Current Total: 61 - 4 frames left\n[[3, 5], [7, 1], [17, 0], [4, 3], [13, 0], [3, 5]]" + expect(scorecard.calculate_total).to eq "Current Total: 61 - 4 frames left\n[[3, 5], [7, 1], [17, 0], [4, 3], [13, 0], [3, 5]]" end end -end \ No newline at end of file +end diff --git a/spec/scorecard_spec.rb b/spec/scorecard_spec.rb index 977a5c29..fe6503f1 100644 --- a/spec/scorecard_spec.rb +++ b/spec/scorecard_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'scorecard' RSpec.describe ScoreCard do @@ -7,4 +9,4 @@ expect(scorecard.final_format).to eq "Your game isn't over yet! You have 10 frames left" end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c80d44b9..4f8c8d91 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# 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 @@ -44,55 +46,53 @@ # 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 + # 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 From 7f1c5573ccf2c797d9a418d723fc4bf85807813e Mon Sep 17 00:00:00 2001 From: JRSkates Date: Tue, 23 May 2023 18:41:57 +0100 Subject: [PATCH 6/8] simplecov 100% coverage --- .gitignore | 1 + Gemfile | 4 ++++ Gemfile.lock | 12 ++++++++++++ spec/spec_helper.rb | 3 +++ 4 files changed, 20 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..4ebc8aea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage diff --git a/Gemfile b/Gemfile index e9dfc3e6..ca697bd1 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,7 @@ source 'https://rubygems.org' gem 'rspec', '~> 3.12' gem 'rubocop', '~> 1.51' + +gem 'simplecov', '~> 0.22.0' + +gem 'simplecov-cobertura', '~> 2.1' diff --git a/Gemfile.lock b/Gemfile.lock index 8d1b1638..e00caa4f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ GEM specs: ast (2.4.2) diff-lcs (1.5.0) + docile (1.4.0) json (2.6.3) parallel (1.23.0) parser (3.2.2.1) @@ -36,6 +37,15 @@ GEM rubocop-ast (1.28.1) parser (>= 3.2.1.0) ruby-progressbar (1.13.0) + simplecov (0.22.0) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-cobertura (2.1.0) + rexml + simplecov (~> 0.19) + simplecov-html (0.12.3) + simplecov_json_formatter (0.1.4) unicode-display_width (2.4.2) PLATFORMS @@ -44,6 +54,8 @@ PLATFORMS DEPENDENCIES rspec (~> 3.12) rubocop (~> 1.51) + simplecov (~> 0.22.0) + simplecov-cobertura (~> 2.1) BUNDLED WITH 2.4.13 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4f8c8d91..19e25ca8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require 'simplecov' +SimpleCov.start + # frozen_string_literal: true # This file was generated by the `rspec --init` command. Conventionally, all From 80084d84cd92e74a5d17d3888045fc4b7846abcb Mon Sep 17 00:00:00 2001 From: JRSkates Date: Tue, 23 May 2023 21:02:18 +0100 Subject: [PATCH 7/8] tenth frame and final format to complete --- lib/scorecard.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/scorecard.rb b/lib/scorecard.rb index 23b86463..65330b2f 100644 --- a/lib/scorecard.rb +++ b/lib/scorecard.rb @@ -10,7 +10,6 @@ def initialize def frame_input(frame) @frame_count -= 1 - return tenth_frame if @frame_count.zero? if @all_frames.empty? @all_frames << frame @@ -37,7 +36,9 @@ def calculate_total "Current Total: #{total} - #{@frame_count} frames left\n#{total_frames}" end - def tenth_frame; end + def tenth_frame + return + end def final_format return "Your game isn't over yet! You have #{@frame_count} frames left" if @frame_count.positive? From b7f92f2058f931e03454c5c81c35559a915053b2 Mon Sep 17 00:00:00 2001 From: JRSkates Date: Fri, 26 May 2023 17:29:06 +0100 Subject: [PATCH 8/8] app.rb and 10th Frame Stike/Spare functionality needed --- lib/scorecard.rb | 44 +++++++++++++++++++++++++++++----------- spec/integration_spec.rb | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/lib/scorecard.rb b/lib/scorecard.rb index 65330b2f..34e2fafd 100644 --- a/lib/scorecard.rb +++ b/lib/scorecard.rb @@ -6,11 +6,13 @@ class ScoreCard def initialize @frame_count = 10 @all_frames = [] + @final_frames = [] + @tenth_frame = false + @total = nil end def frame_input(frame) @frame_count -= 1 - if @all_frames.empty? @all_frames << frame calculate_total @@ -25,23 +27,28 @@ def frame_input(frame) end def calculate_total - total_frames = @all_frames.map do |frame| - if frame.is_a?(Array) - frame - else - frame.final - end - end - total = total_frames.compact.flatten.reduce(:+) - "Current Total: #{total} - #{@frame_count} frames left\n#{total_frames}" + @final_frames = final_arr_mapping + @total = @final_frames.compact.flatten.reduce(:+) + return tenth_frame if @frame_count == 0 && @tenth_frame == false + "Current Total: #{@total} - #{@frame_count} frames left\n#{@final_frames}" end - def tenth_frame - return + def tenth_frame + @tenth_frame = true + if @all_frames.last.strike? == true + 'strike tenth frame bonus roll functionality needed' + elsif @all_frames.last.spare? == true + 'spare tenth frame bonus roll functionality needed' + else + final_format + end end def final_format return "Your game isn't over yet! You have #{@frame_count} frames left" if @frame_count.positive? + @final_frames = final_arr_mapping + @total = @final_frames.compact.flatten.reduce(:+) + return "Final Total: #{@total} - Frames:\n#{@final_frames}" end private @@ -63,4 +70,17 @@ def strike_bonus_rolls(frame) @all_frames << frame calculate_total end + + def final_arr_mapping + result = @all_frames.map do |frame| + if frame.is_a?(Array) + frame + else + frame.final + end + end + + return result + end + end diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 6a4aa009..03936344 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -80,5 +80,40 @@ expect(scorecard.calculate_total).to eq "Current Total: 61 - 4 frames left\n[[3, 5], [7, 1], [17, 0], [4, 3], [13, 0], [3, 5]]" end + + it 'should return the tenth frame method' do + scorecard = ScoreCard.new + frame_one = Frame.new + frame_one.result(3, 5) + scorecard.frame_input(frame_one) + frame_two = Frame.new + frame_two.result(7, 1) + scorecard.frame_input(frame_two) + frame_three = Frame.new + frame_three.result(10, 0) + scorecard.frame_input(frame_three) + frame_four = Frame.new + frame_four.result(4, 3) + scorecard.frame_input(frame_four) + frame_five = Frame.new + frame_five.result(2, 8) + scorecard.frame_input(frame_five) + frame_six = Frame.new + frame_six.result(3, 5) + scorecard.frame_input(frame_six) + frame_seven = Frame.new + frame_seven.result(10, 0) + scorecard.frame_input(frame_seven) + frame_eight = Frame.new + frame_eight.result(4, 3) + scorecard.frame_input(frame_eight) + frame_nine = Frame.new + frame_nine.result(2, 8) + scorecard.frame_input(frame_nine) + frame_ten = Frame.new + frame_ten.result(3, 5) + + expect(scorecard.frame_input(frame_ten)).to eq "Final Total: 106 - Frames:\n[[3, 5], [7, 1], [17, 0], [4, 3], [13, 0], [3, 5], [17, 0], [4, 3], [13, 0], [3, 5]]" + end end end