diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..4ebc8aea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage 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..ca697bd1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +# gem "rails" + +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 new file mode 100644 index 00000000..e00caa4f --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,61 @@ +GEM + remote: https://rubygems.org/ + 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) + 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) + 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 + arm64-darwin-21 + +DEPENDENCIES + rspec (~> 3.12) + rubocop (~> 1.51) + simplecov (~> 0.22.0) + simplecov-cobertura (~> 2.1) + +BUNDLED WITH + 2.4.13 diff --git a/design/class_recipe.md b/design/class_recipe.md new file mode 100644 index 00000000..65aef9b2 --- /dev/null +++ b/design/class_recipe.md @@ -0,0 +1,164 @@ + +# 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 + @final = [] + end + + def result(first_roll, second_roll) + if first_roll == 10 + @final << first_roll, 'X' + return @final + else + @final << first_roll, 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 + + +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 "#{all_frames} - #{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 + 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 +``` + +## 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/frame.rb new file mode 100644 index 00000000..256698ad --- /dev/null +++ b/lib/frame.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class Frame + def initialize + @final = [] + end + + def result(first_roll, second_roll) + @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 + + attr_reader :final +end diff --git a/lib/scorecard.rb b/lib/scorecard.rb new file mode 100644 index 00000000..34e2fafd --- /dev/null +++ b/lib/scorecard.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require_relative 'frame' + +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 + elsif @all_frames.last.strike? == true + strike_bonus_rolls(frame) + elsif @all_frames.last.spare? == true + spare_bonus_roll(frame) + else + @all_frames << frame + calculate_total + end + end + + def calculate_total + @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 + @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 + + 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 + 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 + 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/frame_spec.rb b/spec/frame_spec.rb new file mode 100644 index 00000000..f01f3dfe --- /dev/null +++ b/spec/frame_spec.rb @@ -0,0 +1,34 @@ +# 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 + + 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_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_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 diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb new file mode 100644 index 00000000..03936344 --- /dev/null +++ b/spec/integration_spec.rb @@ -0,0 +1,119 @@ +# frozen_string_literal: true + +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) + scorecard.frame_input(frame_six) + + 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 diff --git a/spec/scorecard_spec.rb b/spec/scorecard_spec.rb new file mode 100644 index 00000000..fe6503f1 --- /dev/null +++ b/spec/scorecard_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..19e25ca8 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,101 @@ +require 'simplecov' +SimpleCov.start + +# 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