From 42bd3bc2d0ad516c70ecc31d5d1d0ca631112b52 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 13:43:39 +0100 Subject: [PATCH 01/21] Initial commit --- .rspec | 2 + design.md | 130 ++++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 98 +++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 .rspec create mode 100644 design.md create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..775c62b0 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--require spec_helper +--format documentation \ No newline at end of file diff --git a/design.md b/design.md new file mode 100644 index 00000000..786e5a83 --- /dev/null +++ b/design.md @@ -0,0 +1,130 @@ +# Bowling Challenge Multi-Class Planned Design Recipe + +## 1. Describe the Problem + +Count and sum the scores of a bowling game for one player. + +### Bowling scoring + +Scoring it bowling is pretty tricky. This is a representation of how the logic works. + +```mermaid +flowchart TD + F1[Frame 1] --> B1.1{Ball 1} + B1.1 -->|Score < 10| B1.1S[Record B1 score] + B1.1 -->|Score == 10| NF1.1[Strike: FS pending and
move to next frame] + NF1.1 -->|Carry strike from F1| F2 + B1.2 -->|Frame score == 10| NF1.2[Spare: BS2 pending and move
to next fame] + NF1.2 -->|Carry spare from F1| F2 + B1.1S --> B1.2{Ball 2} + B1.2 -->|Frame score < 10| FS1.2[Record F1 score and
move to next frame] + FS1.2 --> F2 + + F2[Frame 2] --> B2.1{Ball 1} + B2.1 -->|Score < 10| B2.1S{B1 score} + B2.1 -->|Score == 10| NF2.1{Strike:
FS pending
and move to
next frame} + NF2.1 -->|If strike from F1,
carry strikes from F1 & F2| F3 + NF2.1 -->|If spare from F1,
record F1 bonus| F3 + NF2.1 -->|If no strike from F1,
carry strike from F2| F3 + B2.2 -->|Frame score == 10| NF2.2[Spare: BS2 pending and move
to next fame] + NF2.2 -->|Carry spare from F2| F3 + B2.1S -->|Record score.
Next ball.| B2.2{Ball 2} + B2.1S -->|Spare from F1| B2.1Ss[Add bonus to
F1 score] + B2.1Ss --> B2.2 + B2.2 -->|Strike from F1| B2.2Ss[Add bonus to
F1 score] + B2.2Ss --> F3[Add bonus to
F1 score] + B2.2 -->|Frame score < 10| FS2.2[Record frame score and
move to next frame] + FS2.2 --> F3[Frame 3] +``` + +## 2. Design the Class System + +```mermaid +classDiagram + Application <|-- Frame + Application <|-- ScoreCard + Application : +array frames + Application : +int current_frame + Application : +int current_ball + Application : +run() + Application : +create_frames() + class Frame{ + +int frame_number + +int ball1 + +int ball2 + +int ball3 + } + class ScoreCard{ + -int game_score + +TotalScore() + } +``` + +```ruby +class Application + def initialize + @frames = [] + @current_frame = 1 + @current_frame_ball = 1 + create_frames + end + + def run + # Runs an IO loop and calls the other classes + end + + def create_frames + # Runs once when .run starts to create the `@frames` array of frame objects + end + + def return_frames + # Returns the `@frames` array + end +end + +class Frame + # A model class with attributes: + # :frame_number, :ball_1, :ball_2, :ball_3 +end + +class ScoreCard + def GameScore + end + + def FrameScore + end +end +``` + +## 3. Create Examples as Integration Tests + +_Create examples of the classes being used together in different situations and +combinations that reflect the ways in which the system will be used._ + +```ruby +# # Gets all tracks +# library = MusicLibrary.new +# track_1 = Track.new("Carte Blanche", "Veracocha") +# track_2 = Track.new("Synaesthesia", "The Thrillseekers") +# library.add(track_1) +# library.add(track_2) +# library.all # => [track_1, track_2] +``` + +## 4. Create Examples as Unit Tests + +_Create examples, where appropriate, of the behaviour of each relevant class at +a more granular level of detail._ + +```ruby +# Creates the `@frames` array of `frame` objects +app = Application.new +app.return_frames.length # => 10 +``` + +_Encode each example as a test. You can add to the above list as you go._ + +## 5. Implement the Behaviour + +_After each test you write, follow the test-driving process of red, green, +refactor to implement the behaviour._ \ 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 8755d79acd73032e22aab7fdd737cac7d971399d Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 13:44:18 +0100 Subject: [PATCH 02/21] Create Application.create_frames, .return_frames and tests --- lib/app.rb | 21 +++++++++++++++++++++ lib/frame.rb | 3 +++ spec/application_spec.rb | 8 ++++++++ 3 files changed, 32 insertions(+) create mode 100644 lib/app.rb create mode 100644 lib/frame.rb create mode 100644 spec/application_spec.rb diff --git a/lib/app.rb b/lib/app.rb new file mode 100644 index 00000000..65cc6b13 --- /dev/null +++ b/lib/app.rb @@ -0,0 +1,21 @@ +require_relative 'frame' + +class Application + def initialize + @frames = [] + @current_frame = 1 + @current_frame_ball = 1 + create_frames + end + + def create_frames + 10.times do + frame = Frame.new + @frames.push(frame) + end + end + + def return_frames + return @frames + end +end \ No newline at end of file diff --git a/lib/frame.rb b/lib/frame.rb new file mode 100644 index 00000000..603d0e1c --- /dev/null +++ b/lib/frame.rb @@ -0,0 +1,3 @@ +class Frame + attr_accessor :frame_number, :ball_1, :ball_2, :ball_3 +end \ No newline at end of file diff --git a/spec/application_spec.rb b/spec/application_spec.rb new file mode 100644 index 00000000..06377a69 --- /dev/null +++ b/spec/application_spec.rb @@ -0,0 +1,8 @@ +require 'app' + +RSpec.describe Application do + it "creates the @frames array" do + app = Application.new + expect(app.return_frames.length).to eq 10 + end +end \ No newline at end of file From 00306c3c53f37a9ef7773feba3d2bb791a840485 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 14:10:28 +0100 Subject: [PATCH 03/21] Add Fram methods and tests --- lib/app.rb | 4 ++-- lib/frame.rb | 17 ++++++++++++++++- spec/frame_spec.rb | 11 +++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 spec/frame_spec.rb diff --git a/lib/app.rb b/lib/app.rb index 65cc6b13..e4c3e513 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -9,8 +9,8 @@ def initialize end def create_frames - 10.times do - frame = Frame.new + 10.times do |i| + frame = Frame.new(i + 1) @frames.push(frame) end end diff --git a/lib/frame.rb b/lib/frame.rb index 603d0e1c..581df102 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -1,3 +1,18 @@ class Frame - attr_accessor :frame_number, :ball_1, :ball_2, :ball_3 + def initialize(frame_number) + @frame_number = frame_number + @ball_scores = [0, 0, 0] + end + + def add_ball_score(ball, score) + @ball_scores[ball - 1] = score + end + + def get_ball_score(ball) + return @ball_scores[ball - 1] + end + + def frame_number + return @frame_number + end end \ No newline at end of file diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb new file mode 100644 index 00000000..b5f2592f --- /dev/null +++ b/spec/frame_spec.rb @@ -0,0 +1,11 @@ +require 'frame' + +RSpec.describe Frame do + context ".add_ball_score" do + it "adds a ball score to the frame" do + frame = Frame.new(1) + frame.add_ball_score(1, 5) + expect(frame.get_ball_score(1)).to eq 5 + end + end +end \ No newline at end of file From ab6d446f78819ac7f7bba4b0ceb63ab6a62f5156 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 15:03:06 +0100 Subject: [PATCH 04/21] Add Frame.score and tests --- lib/frame.rb | 6 ++++++ spec/frame_spec.rb | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/frame.rb b/lib/frame.rb index 581df102..d3c69370 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -2,6 +2,8 @@ class Frame def initialize(frame_number) @frame_number = frame_number @ball_scores = [0, 0, 0] + @frame_score = 0 + @bonus_score = 0 end def add_ball_score(ball, score) @@ -15,4 +17,8 @@ def get_ball_score(ball) def frame_number return @frame_number end + + def score + return @ball_scores.sum + @bonus_score + end end \ No newline at end of file diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index b5f2592f..156e3177 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -8,4 +8,21 @@ expect(frame.get_ball_score(1)).to eq 5 end end + + context ".get_ball_score" do + it "returns the score for a given ball in the frame" do + frame = Frame.new(1) + frame.add_ball_score(2, 3) + expect(frame.get_ball_score(2)).to eq 3 + end + end + + context ".score" do + it "returns the score for the frame" do + frame = Frame.new(1) + frame.add_ball_score(1, 4) + frame.add_ball_score(2, 3) + expect(frame.score).to eq 7 + end + end end \ No newline at end of file From 0b764bbb39b4cb2cc53092bab997153fe607cbd8 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 17:45:03 +0100 Subject: [PATCH 05/21] Add methods and tests for updating pending bonuses --- lib/frame.rb | 28 +++++++++++++++++-- lib/scorecard.rb | 28 +++++++++++++++++++ spec/frame_spec.rb | 8 ++++++ spec/scorecard_spec.rb | 63 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 lib/scorecard.rb create mode 100644 spec/scorecard_spec.rb diff --git a/lib/frame.rb b/lib/frame.rb index d3c69370..a945d5f6 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -2,7 +2,6 @@ class Frame def initialize(frame_number) @frame_number = frame_number @ball_scores = [0, 0, 0] - @frame_score = 0 @bonus_score = 0 end @@ -14,11 +13,36 @@ def get_ball_score(ball) return @ball_scores[ball - 1] end + def update_bonus_score(score) + @bonus_score = score + end + + def bonus_score + return @bonus_score + end + + # Returns true if this is a complete frame with no strike + def two_bowls? + return true if get_ball_score(1) != 0 && get_ball_score(2) != 0 + end + + def strike? + return true if get_ball_score(1) == 'X' + end + + def spare? + return true if get_ball_score(2) == '/' + end + def frame_number return @frame_number end def score - return @ball_scores.sum + @bonus_score + scores = @ball_scores + scores[0] = 10 if scores[0] == 'X' + scores[1] = 10 - scores[0] if scores[1] == '/' + + return scores.sum + @bonus_score end end \ No newline at end of file diff --git a/lib/scorecard.rb b/lib/scorecard.rb new file mode 100644 index 00000000..eb1998de --- /dev/null +++ b/lib/scorecard.rb @@ -0,0 +1,28 @@ +class ScoreCard + def update_pending_bonuses(frames, current_frame) + update_pending_strikes(frames, current_frame) + update_pending_spares(frames, current_frame) + end + + private + + def update_pending_strikes(frames, current_frame) + previous_frame = current_frame - 1 + frame_before_last = current_frame -2 + + if (frames[current_frame].two_bowls?) && (frames[previous_frame].strike?) + frames[previous_frame].update_bonus_score(frames[current_frame].score) + elsif (frames[current_frame].strike?) && (frames[previous_frame].strike?) && (frames[frame_before_last].strike?) + frames[frame_before_last].update_bonus_score(20) + end + end + + def update_pending_spares(frames, current_frame) + previous_frame = current_frame - 1 + + if (frames[previous_frame].spare?) + frames[previous_frame].update_bonus_score(frames[current_frame].score) + end + end + +end \ No newline at end of file diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 156e3177..4334bc8e 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -25,4 +25,12 @@ expect(frame.score).to eq 7 end end + + context ".update_bonus_score" do + it "updates the bonus score for the frame" do + frame = Frame.new(1) + frame.update_bonus_score(5) + expect(frame.bonus_score).to eq 5 + end + end end \ No newline at end of file diff --git a/spec/scorecard_spec.rb b/spec/scorecard_spec.rb new file mode 100644 index 00000000..a0780689 --- /dev/null +++ b/spec/scorecard_spec.rb @@ -0,0 +1,63 @@ +require 'scorecard' +require 'frame' + +RSpec.describe ScoreCard do + + context ".update_pending_bonuses" do + it "updates the bonus for current_frame-1 if two balls have been played in this frame" do + + frame_1 = Frame.new(1) + frame_1.add_ball_score(1, 'X') + + frame_2 = Frame.new(2) + frame_2.add_ball_score(1, 3) + frame_2.add_ball_score(2, 4) + + frames = {1 => frame_1, 2 => frame_2} + current_frame = 2 + + score_card = ScoreCard.new + score_card.update_pending_bonuses(frames, current_frame) + + expect(frame_1.score).to eq 17 + end + + it "updates the bonus for current_frame-2 if current_frame and current_frame-1 are strikes" do + + frame_1 = Frame.new(1) + frame_1.add_ball_score(1, 'X') + + frame_2 = Frame.new(2) + frame_2.add_ball_score(1, 'X') + + frame_3 = Frame.new(3) + frame_3.add_ball_score(1, 'X') + + frames = {1 => frame_1, 2 => frame_2, 3 => frame_3} + current_frame = 3 + + score_card = ScoreCard.new + score_card.update_pending_bonuses(frames, current_frame) + + expect(frame_1.score).to eq 30 + end + + it "updates the bonus for current_frame-1 if it had a spare" do + + frame_1 = Frame.new(1) + frame_1.add_ball_score(1, 3) + frame_1.add_ball_score(2, '/') + + frame_2 = Frame.new(2) + frame_2.add_ball_score(1, 5) + + frames = {1 => frame_1, 2 => frame_2} + current_frame = 2 + + score_card = ScoreCard.new + score_card.update_pending_bonuses(frames, current_frame) + + expect(frame_1.score).to eq 15 + end + end +end \ No newline at end of file From bd353f2422e4ffb15e171fd0dfea8ce2dab99faf Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 17:51:37 +0100 Subject: [PATCH 06/21] Add tests for Frame .strike? .two_bowls? and .spare? --- spec/frame_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 4334bc8e..5893ec7b 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -33,4 +33,30 @@ expect(frame.bonus_score).to eq 5 end end + + context ".strike?" do + it "returns true if the frame has a strike" do + frame = Frame.new(1) + frame.add_ball_score(1, 'X') + expect(frame.strike?).to eq true + end + end + + context ".two_bowls?" do + it "returns true if the frame has two two_bowls" do + frame = Frame.new(1) + frame.add_ball_score(1, 2) + frame.add_ball_score(2, 3) + expect(frame.two_bowls?).to eq true + end + end + + context ".spare?" do + it "returns true if the frame has a spare" do + frame = Frame.new(1) + frame.add_ball_score(1, '3') + frame.add_ball_score(2, '/') + expect(frame.spare?).to eq true + end + end end \ No newline at end of file From 308da828bafa5d6883c4f3b3bee568184c05011d Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 18:54:04 +0100 Subject: [PATCH 07/21] Add Gameplay.next_ball and .continue? methods and tests --- lib/gameplay.rb | 29 +++++++++++++++++++++++++++++ spec/gameplay_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/gameplay.rb create mode 100644 spec/gameplay_spec.rb diff --git a/lib/gameplay.rb b/lib/gameplay.rb new file mode 100644 index 00000000..963dd0ed --- /dev/null +++ b/lib/gameplay.rb @@ -0,0 +1,29 @@ +class Gameplay + attr_accessor :current_frame, :current_ball + + def initialize + @current_frame = 1 + @current_ball = 1 + end + + def prompt + + end + + def next_ball + if @current_frame < 10 + if @current_ball == 1 + @current_ball = 2 + else + @current_frame = @current_frame + 1 + @current_ball = 1 + end + else + @current_ball = @current_ball + 1 + end + end + + def continue? + return true if @current_frame <= 10 && @current_ball <= 3 + end +end \ No newline at end of file diff --git a/spec/gameplay_spec.rb b/spec/gameplay_spec.rb new file mode 100644 index 00000000..9513ec1d --- /dev/null +++ b/spec/gameplay_spec.rb @@ -0,0 +1,24 @@ +require 'gameplay' + +RSpec.describe Gameplay do + context ".continue?" do + it "returns true if the game should continue" do + game = Gameplay.new + expect(game.continue?).to eq true + end + end + + context ".next_ball" do + it "increments the current ball and frame" do + game = Gameplay.new + expect(game.current_frame).to eq 1 + expect(game.current_ball).to eq 1 + game.next_ball + expect(game.current_frame).to eq 1 + expect(game.current_ball).to eq 2 + game.next_ball + expect(game.current_frame).to eq 2 + expect(game.current_ball).to eq 1 + end + end +end \ No newline at end of file From 466dfa5b5fa043164db00a60619ce80a4ccf73db Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sat, 10 Jun 2023 19:11:45 +0100 Subject: [PATCH 08/21] Add Application.run --- lib/app.rb | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index e4c3e513..aa37e271 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -1,21 +1,36 @@ require_relative 'frame' +require_relative 'gameplay' class Application def initialize - @frames = [] - @current_frame = 1 - @current_frame_ball = 1 + @frames = {} + @gameplay = Gameplay.new create_frames end + def run + loopy = true + while loopy do + @gameplay.score_prompt + @gameplay.next_ball + loopy = @gameplay.continue? + end + + @gameplay.final_score(@frames) + return + end + def create_frames 10.times do |i| frame = Frame.new(i + 1) - @frames.push(frame) + @frames[i + 1] = frame end end def return_frames return @frames end -end \ No newline at end of file +end + +app = Application.new +app.run \ No newline at end of file From e8262709e56ca3e750fe7a2881f065e78a85f003 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 08:51:50 +0100 Subject: [PATCH 09/21] Change Frame.complete_frame? to .two_balls? to make it clearer --- lib/app.rb | 13 +++++++++---- lib/frame.rb | 8 +++++--- spec/frame_spec.rb | 8 ++++---- spec/spec_helper.rb | 2 ++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index aa37e271..b8a03c8e 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -1,5 +1,6 @@ require_relative 'frame' require_relative 'gameplay' +require_relative 'scorecard' class Application def initialize @@ -11,12 +12,14 @@ def initialize def run loopy = true while loopy do - @gameplay.score_prompt - @gameplay.next_ball + @gameplay.score_prompt(@frames) + @gameplay.next_ball(@frames) + loopy = @gameplay.continue? end @gameplay.final_score(@frames) + puts @frames return end @@ -32,5 +35,7 @@ def return_frames end end -app = Application.new -app.run \ No newline at end of file +unless ENV['ENV'] == 'test' + app = Application.new + app.run +end diff --git a/lib/frame.rb b/lib/frame.rb index a945d5f6..bb71608f 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -22,8 +22,8 @@ def bonus_score end # Returns true if this is a complete frame with no strike - def two_bowls? - return true if get_ball_score(1) != 0 && get_ball_score(2) != 0 + def two_balls? + return true if (get_ball_score(1) != 0 && get_ball_score(2) != 0) end def strike? @@ -41,8 +41,10 @@ def frame_number def score scores = @ball_scores scores[0] = 10 if scores[0] == 'X' + scores[1] = 10 if scores[1] == 'X' + scores[2] = 10 if scores[2] == 'X' scores[1] = 10 - scores[0] if scores[1] == '/' return scores.sum + @bonus_score end -end \ No newline at end of file +end diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 5893ec7b..0c78671d 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -42,12 +42,12 @@ end end - context ".two_bowls?" do - it "returns true if the frame has two two_bowls" do + context ".two_balls?" do + it "returns true if the frame has two two balls bowled" do frame = Frame.new(1) frame.add_ball_score(1, 2) frame.add_ball_score(2, 3) - expect(frame.two_bowls?).to eq true + expect(frame.two_balls?).to eq true end end @@ -59,4 +59,4 @@ expect(frame.spare?).to eq true end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c80d44b9..066b79aa 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +ENV['ENV'] = 'test' + # 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 From 756fc3cf25f485e195308d0f33fb37b98bf85dd2 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 12:35:51 +0100 Subject: [PATCH 10/21] Add Frame.strike and .spare methods --- lib/frame.rb | 32 +++++++++++++++++++------------- spec/application_spec.rb | 2 +- spec/frame_spec.rb | 19 +++++++++---------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/frame.rb b/lib/frame.rb index bb71608f..f6dfae99 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -1,12 +1,13 @@ class Frame - def initialize(frame_number) - @frame_number = frame_number + def initialize @ball_scores = [0, 0, 0] @bonus_score = 0 + @strike = false + @spare = false end - def add_ball_score(ball, score) - @ball_scores[ball - 1] = score + def add_ball_score(ball, ball_score) + @ball_scores[ball - 1] = ball_score end def get_ball_score(ball) @@ -25,13 +26,24 @@ def bonus_score def two_balls? return true if (get_ball_score(1) != 0 && get_ball_score(2) != 0) end + + def strike + @strike = true + add_ball_score(1, 10) + end def strike? - return true if get_ball_score(1) == 'X' + return true if @strike == true + end + + def spare + @spare = true + score = 10 - get_ball_score(1) + add_ball_score(2, score) end def spare? - return true if get_ball_score(2) == '/' + return true if @spare == true end def frame_number @@ -39,12 +51,6 @@ def frame_number end def score - scores = @ball_scores - scores[0] = 10 if scores[0] == 'X' - scores[1] = 10 if scores[1] == 'X' - scores[2] = 10 if scores[2] == 'X' - scores[1] = 10 - scores[0] if scores[1] == '/' - - return scores.sum + @bonus_score + return @ball_scores.sum + @bonus_score end end diff --git a/spec/application_spec.rb b/spec/application_spec.rb index 06377a69..20a585fa 100644 --- a/spec/application_spec.rb +++ b/spec/application_spec.rb @@ -5,4 +5,4 @@ app = Application.new expect(app.return_frames.length).to eq 10 end -end \ No newline at end of file +end diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 0c78671d..707dcbce 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -3,7 +3,7 @@ RSpec.describe Frame do context ".add_ball_score" do it "adds a ball score to the frame" do - frame = Frame.new(1) + frame = Frame.new frame.add_ball_score(1, 5) expect(frame.get_ball_score(1)).to eq 5 end @@ -11,7 +11,7 @@ context ".get_ball_score" do it "returns the score for a given ball in the frame" do - frame = Frame.new(1) + frame = Frame.new frame.add_ball_score(2, 3) expect(frame.get_ball_score(2)).to eq 3 end @@ -19,7 +19,7 @@ context ".score" do it "returns the score for the frame" do - frame = Frame.new(1) + frame = Frame.new frame.add_ball_score(1, 4) frame.add_ball_score(2, 3) expect(frame.score).to eq 7 @@ -28,7 +28,7 @@ context ".update_bonus_score" do it "updates the bonus score for the frame" do - frame = Frame.new(1) + frame = Frame.new frame.update_bonus_score(5) expect(frame.bonus_score).to eq 5 end @@ -36,15 +36,15 @@ context ".strike?" do it "returns true if the frame has a strike" do - frame = Frame.new(1) - frame.add_ball_score(1, 'X') + frame = Frame.new + frame.strike expect(frame.strike?).to eq true end end context ".two_balls?" do it "returns true if the frame has two two balls bowled" do - frame = Frame.new(1) + frame = Frame.new frame.add_ball_score(1, 2) frame.add_ball_score(2, 3) expect(frame.two_balls?).to eq true @@ -53,9 +53,8 @@ context ".spare?" do it "returns true if the frame has a spare" do - frame = Frame.new(1) - frame.add_ball_score(1, '3') - frame.add_ball_score(2, '/') + frame = Frame.new + frame.spare expect(frame.spare?).to eq true end end From 48d69aa0923798ee998f829980ed79760d18d4e9 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 12:40:10 +0100 Subject: [PATCH 11/21] Update Gameplay tests --- spec/gameplay_spec.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/spec/gameplay_spec.rb b/spec/gameplay_spec.rb index 9513ec1d..90bbfe2c 100644 --- a/spec/gameplay_spec.rb +++ b/spec/gameplay_spec.rb @@ -1,4 +1,5 @@ require 'gameplay' +require 'frame' RSpec.describe Gameplay do context ".continue?" do @@ -10,15 +11,30 @@ context ".next_ball" do it "increments the current ball and frame" do + frame_1 = Frame.new + frame_1.add_ball_score(1, '3') + frame_1.add_ball_score(2, '4') + + frame_2 = Frame.new + frame_2.strike + + frame_3 = Frame.new + frame_3.add_ball_score(1, '2') + + frames = {1 => frame_1, 2 => frame_2, 3 => frame_3} + game = Gameplay.new expect(game.current_frame).to eq 1 expect(game.current_ball).to eq 1 - game.next_ball + game.next_ball(frames) expect(game.current_frame).to eq 1 expect(game.current_ball).to eq 2 - game.next_ball + game.next_ball(frames) expect(game.current_frame).to eq 2 expect(game.current_ball).to eq 1 + game.next_ball(frames) + expect(game.current_frame).to eq 3 + expect(game.current_ball).to eq 1 end end -end \ No newline at end of file +end From 522c77c803dfbea58cce712f6aa3dec1003a8f05 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 14:40:22 +0100 Subject: [PATCH 12/21] Split Frame scoring methods into two (with and without bonus) --- lib/frame.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/frame.rb b/lib/frame.rb index f6dfae99..7e64181d 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -27,13 +27,14 @@ def two_balls? return true if (get_ball_score(1) != 0 && get_ball_score(2) != 0) end - def strike + def strike(ball) @strike = true - add_ball_score(1, 10) + add_ball_score(ball, 10) end def strike? return true if @strike == true + return false end def spare @@ -44,13 +45,14 @@ def spare def spare? return true if @spare == true + return false end - - def frame_number - return @frame_number + + def frame_score + return @ball_scores.sum end - def score + def total_frame_score return @ball_scores.sum + @bonus_score end end From f509c8ec5b79ef4a9647de32e15d6b1e7a34389d Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 14:41:01 +0100 Subject: [PATCH 13/21] ScoreCard handles frame 10 and edge cases better --- lib/scorecard.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/scorecard.rb b/lib/scorecard.rb index eb1998de..311b6fcd 100644 --- a/lib/scorecard.rb +++ b/lib/scorecard.rb @@ -1,3 +1,5 @@ +require_relative 'frame' + class ScoreCard def update_pending_bonuses(frames, current_frame) update_pending_strikes(frames, current_frame) @@ -8,21 +10,30 @@ def update_pending_bonuses(frames, current_frame) def update_pending_strikes(frames, current_frame) previous_frame = current_frame - 1 - frame_before_last = current_frame -2 + frame_before_last = current_frame - 2 - if (frames[current_frame].two_bowls?) && (frames[previous_frame].strike?) - frames[previous_frame].update_bonus_score(frames[current_frame].score) - elsif (frames[current_frame].strike?) && (frames[previous_frame].strike?) && (frames[frame_before_last].strike?) + return if previous_frame == 0 + if (frames[current_frame].two_balls?) && (frames[previous_frame].strike?) && (frames[previous_frame].bonus_score == 0) + frames[previous_frame].update_bonus_score(frames[current_frame].frame_score) + end + + return if frame_before_last == 0 + if (frames[current_frame].strike?) && (frames[previous_frame].strike?) && (frames[frame_before_last].strike?) && (frames[frame_before_last].bonus_score == 0) frames[frame_before_last].update_bonus_score(20) end + + return if frame_before_last == 0 + if (frames[current_frame].get_ball_score(1) > 0) && (frames[previous_frame].strike?) && (frames[frame_before_last].strike?) && (frames[frame_before_last].bonus_score == 0) + frames[frame_before_last].update_bonus_score(10 + frames[current_frame].frame_score) + end end def update_pending_spares(frames, current_frame) previous_frame = current_frame - 1 + return if previous_frame == 0 if (frames[previous_frame].spare?) - frames[previous_frame].update_bonus_score(frames[current_frame].score) + frames[previous_frame].update_bonus_score(frames[current_frame].get_ball_score(1)) end end - -end \ No newline at end of file +end From 6b7ff76d1485919a3627823f35feaf2948b50948 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 14:41:30 +0100 Subject: [PATCH 14/21] Update tests to reflect changed method names --- spec/frame_spec.rb | 4 +- spec/gameplay_spec.rb | 6 ++- spec/scorecard_spec.rb | 85 ++++++++++++++++++++++++++++++++---------- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 707dcbce..d6bbdfa9 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -22,7 +22,7 @@ frame = Frame.new frame.add_ball_score(1, 4) frame.add_ball_score(2, 3) - expect(frame.score).to eq 7 + expect(frame.frame_score).to eq 7 end end @@ -37,7 +37,7 @@ context ".strike?" do it "returns true if the frame has a strike" do frame = Frame.new - frame.strike + frame.strike(1) expect(frame.strike?).to eq true end end diff --git a/spec/gameplay_spec.rb b/spec/gameplay_spec.rb index 90bbfe2c..d30dad98 100644 --- a/spec/gameplay_spec.rb +++ b/spec/gameplay_spec.rb @@ -4,8 +4,10 @@ RSpec.describe Gameplay do context ".continue?" do it "returns true if the game should continue" do + frame_1 = Frame.new + frames = { 1 => frame_1 } game = Gameplay.new - expect(game.continue?).to eq true + expect(game.continue?(frames)).to eq true end end @@ -16,7 +18,7 @@ frame_1.add_ball_score(2, '4') frame_2 = Frame.new - frame_2.strike + frame_2.strike(1) frame_3 = Frame.new frame_3.add_ball_score(1, '2') diff --git a/spec/scorecard_spec.rb b/spec/scorecard_spec.rb index a0780689..b81206a5 100644 --- a/spec/scorecard_spec.rb +++ b/spec/scorecard_spec.rb @@ -3,13 +3,13 @@ RSpec.describe ScoreCard do - context ".update_pending_bonuses" do + context ".update_pending_strikes" do it "updates the bonus for current_frame-1 if two balls have been played in this frame" do - frame_1 = Frame.new(1) - frame_1.add_ball_score(1, 'X') + frame_1 = Frame.new + frame_1.strike(1) - frame_2 = Frame.new(2) + frame_2 = Frame.new frame_2.add_ball_score(1, 3) frame_2.add_ball_score(2, 4) @@ -19,36 +19,81 @@ score_card = ScoreCard.new score_card.update_pending_bonuses(frames, current_frame) - expect(frame_1.score).to eq 17 + expect(frame_1.total_frame_score).to eq 17 end it "updates the bonus for current_frame-2 if current_frame and current_frame-1 are strikes" do - frame_1 = Frame.new(1) - frame_1.add_ball_score(1, 'X') + frame_1 = Frame.new + frame_1.strike(1) - frame_2 = Frame.new(2) - frame_2.add_ball_score(1, 'X') + frame_2 = Frame.new + frame_2.strike(1) - frame_3 = Frame.new(3) - frame_3.add_ball_score(1, 'X') + frame_3 = Frame.new + frame_3.strike(1) frames = {1 => frame_1, 2 => frame_2, 3 => frame_3} current_frame = 3 - score_card = ScoreCard.new score_card.update_pending_bonuses(frames, current_frame) - - expect(frame_1.score).to eq 30 + expect(frame_1.total_frame_score).to eq 30 end - + + it "updates the bonus for frame 9 if frame 9 had a strike and the first two balls of frame 10 are strikes" do + frame_8 = Frame.new + + frame_9 = Frame.new + frame_9.strike(1) + + frame_10 = Frame.new + frame_10.strike(1) + frame_10.strike(2) + + frames = {8 => frame_8, 9 => frame_9, 10 => frame_10} + current_frame = 10 + + score_card = ScoreCard.new + score_card.update_pending_bonuses(frames, current_frame) + + expect(frame_9.total_frame_score).to eq 30 + end + + it "doesn't apply a bonus if ball 3 of frame 10 is a strike" do + frame_8 = Frame.new + + frame_9 = Frame.new + frame_9.strike(1) + + frame_10 = Frame.new + frame_10.strike(1) + frame_10.strike(2) + + frames = {8 => frame_8, 9 => frame_9, 10 => frame_10} + current_frame = 10 + + score_card = ScoreCard.new + score_card.update_pending_bonuses(frames, current_frame) + + expect(frame_9.total_frame_score).to eq 30 + expect(frame_10.total_frame_score).to eq 20 + + frame_10.strike(3) + + score_card.update_pending_bonuses(frames, current_frame) + + expect(frame_9.total_frame_score).to eq 30 + expect(frame_10.total_frame_score).to eq 30 + end + end + context ".update_pending_spares" do it "updates the bonus for current_frame-1 if it had a spare" do - frame_1 = Frame.new(1) + frame_1 = Frame.new frame_1.add_ball_score(1, 3) - frame_1.add_ball_score(2, '/') + frame_1.spare - frame_2 = Frame.new(2) + frame_2 = Frame.new frame_2.add_ball_score(1, 5) frames = {1 => frame_1, 2 => frame_2} @@ -57,7 +102,7 @@ score_card = ScoreCard.new score_card.update_pending_bonuses(frames, current_frame) - expect(frame_1.score).to eq 15 + expect(frame_1.total_frame_score).to eq 15 end end -end \ No newline at end of file +end From bb6235ecf2cc8bef637e078e2f21a98a4235a783 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 14:42:17 +0100 Subject: [PATCH 15/21] Add Gameplay.score_prompt, update .continue? --- lib/gameplay.rb | 51 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/gameplay.rb b/lib/gameplay.rb index 963dd0ed..503bc33a 100644 --- a/lib/gameplay.rb +++ b/lib/gameplay.rb @@ -1,18 +1,39 @@ +require_relative 'scorecard' + class Gameplay attr_accessor :current_frame, :current_ball def initialize @current_frame = 1 @current_ball = 1 + @scorecard = ScoreCard.new end - def prompt - + def score_prompt(frames) + puts "Enter score for frame #{current_frame}, ball #{current_ball}:" + input = gets.chomp + input = input.to_i if input != "X" && input != "/" + frame = frames[@current_frame] + process_current_ball(frame, input) + + end + + def process_current_ball(frame, input) + if input == "X" + frame.strike(@current_ball) + elsif input == "/" + frame.spare + else + frame.add_ball_score(@current_ball, input) + # binding.irb + end end - def next_ball + def next_ball(frames) + @scorecard.update_pending_bonuses(frames, @current_frame) + if @current_frame < 10 - if @current_ball == 1 + if @current_ball == 1 && frames[@current_frame].strike? != true @current_ball = 2 else @current_frame = @current_frame + 1 @@ -23,7 +44,23 @@ def next_ball end end - def continue? - return true if @current_frame <= 10 && @current_ball <= 3 + def continue?(frames) + frame = frames[@current_frame] + # binding.irb + if @current_frame == 10 && @current_ball == 4 + return false + elsif @current_frame == 10 && frame.two_balls? == true && frame.spare? == false && frame.strike? == false + return false + else + return true + end + end + + def final_score(frames) + score = 0 + frames.values.each do |frame| + score = score + frame.total_frame_score + end + puts "\nYour final score is: #{score}" end -end \ No newline at end of file +end From c62f8a988587a4400611bb0b9f2e6a7138d44398 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 16:45:20 +0100 Subject: [PATCH 16/21] Create visual scorecard --- lib/app.rb | 6 ++--- lib/gameplay.rb | 2 +- lib/scorecard.rb | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index b8a03c8e..6efbfcd0 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -15,17 +15,15 @@ def run @gameplay.score_prompt(@frames) @gameplay.next_ball(@frames) - loopy = @gameplay.continue? + loopy = @gameplay.continue?(@frames) end - @gameplay.final_score(@frames) - puts @frames return end def create_frames 10.times do |i| - frame = Frame.new(i + 1) + frame = Frame.new @frames[i + 1] = frame end end diff --git a/lib/gameplay.rb b/lib/gameplay.rb index 503bc33a..0a26ffbc 100644 --- a/lib/gameplay.rb +++ b/lib/gameplay.rb @@ -15,7 +15,7 @@ def score_prompt(frames) input = input.to_i if input != "X" && input != "/" frame = frames[@current_frame] process_current_ball(frame, input) - + @scorecard.show_scorecard(frames) end def process_current_ball(frame, input) diff --git a/lib/scorecard.rb b/lib/scorecard.rb index 311b6fcd..9c5cbbd3 100644 --- a/lib/scorecard.rb +++ b/lib/scorecard.rb @@ -5,6 +5,29 @@ def update_pending_bonuses(frames, current_frame) update_pending_strikes(frames, current_frame) update_pending_spares(frames, current_frame) end + + def show_scorecard(frames) + segments = [] + 9.times do |i| + segments << scorecard_segment(frames[i + 1], i + 1) + end + + segments << scorecard_segment_frame_10(frames[10]) + + lines = [] + + 6.times do |i| + elements = [] + 10.times do |l| + elements.push(segments[l][i]) + end + lines << elements + end + + 6.times do |i| + puts lines[i].join + end + end private @@ -36,4 +59,51 @@ def update_pending_spares(frames, current_frame) frames[previous_frame].update_bonus_score(frames[current_frame].get_ball_score(1)) end end + + def scorecard_segment(frame, frame_number) + ball_1 = frame.get_ball_score(1) + ball_1 = " " if ball_1 == 0 + ball_1 = "X" if ball_1 == 10 + ball_1 = ball_1.to_s.ljust(2, " ") + + ball_2 = frame.get_ball_score(2) + ball_2 = " " if ball_2 == 0 + ball_2 = "/" if frame.spare? + ball_2 = ball_2.to_s.ljust(2, " ") + + total = frame.total_frame_score + total = " " if total == 0 + total = total.to_s.ljust(3, " ") + + template = [" #{frame_number} ", '┌────┬────┐', '│ 1 │ 2 │', '│ └────┤', '│ TTT │', '└─────────┘'] + template.map! {|s| s.gsub(/\│ 1 \│ 2 \│/, "│ #{ball_1} │ #{ball_2} │")} + template.map! {|s| s.gsub(/\│ TTT \│/, "│ #{total} │")} + return template + end + + def scorecard_segment_frame_10(frame) + ball_1 = frame.get_ball_score(1) + ball_1 = " " if ball_1 == 0 + ball_1 = "X" if ball_1 == 10 + ball_1 = ball_1.to_s.ljust(2, " ") + + ball_2 = frame.get_ball_score(2) + ball_2 = " " if ball_2 == 0 + ball_2 = "X" if ball_2 == 10 + ball_2 = ball_2.to_s.ljust(2, " ") + + ball_3 = frame.get_ball_score(3) + ball_3 = " " if ball_3 == 0 + ball_3 = "/" if ball_3 == 10 + ball_3 = ball_3.to_s.ljust(2, " ") + + total = frame.total_frame_score + total = " " if total == 0 + total = total.to_s.ljust(3, " ") + + template = [" 10 ", "┌────┬────┬────┐", "│ 1 │ 2 │ 3 │", "│ └────┴────┤", "│ TTT │", "└──────────────┘"] + template.map! {|s| s.gsub(/\│ 1 \│ 2 \│ 3 \│/, "│ #{ball_1} │ #{ball_2} │ #{ball_3} │")} + template.map! {|s| s.gsub(/\│ TTT \│/, "│ #{total} │")} + return template + end end From c5f0469a28797e37ac5d3e5bdfb9c1868830f31b Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 17:21:45 +0100 Subject: [PATCH 17/21] Add input validation --- lib/app.rb | 5 ++--- lib/gameplay.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index 6efbfcd0..2c120f48 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -12,9 +12,8 @@ def initialize def run loopy = true while loopy do - @gameplay.score_prompt(@frames) - @gameplay.next_ball(@frames) - + success = @gameplay.score_prompt(@frames) + @gameplay.next_ball(@frames) if success == true loopy = @gameplay.continue?(@frames) end @gameplay.final_score(@frames) diff --git a/lib/gameplay.rb b/lib/gameplay.rb index 0a26ffbc..3a3d07ba 100644 --- a/lib/gameplay.rb +++ b/lib/gameplay.rb @@ -12,10 +12,13 @@ def initialize def score_prompt(frames) puts "Enter score for frame #{current_frame}, ball #{current_ball}:" input = gets.chomp + input = input.upcase if input.is_a? String + return false if validate_input(input, frames) == false input = input.to_i if input != "X" && input != "/" frame = frames[@current_frame] process_current_ball(frame, input) @scorecard.show_scorecard(frames) + return true end def process_current_ball(frame, input) @@ -63,4 +66,13 @@ def final_score(frames) end puts "\nYour final score is: #{score}" end + + private + + def validate_input(input, frames) + return false if input == "" + return false if input == "/" && @current_ball == 1 + return false if input == "X" && @current_ball == 2 + return false if /[X\/1-9]/.match(input) == nil + end end From 859c322ed87888201d59671d2d978058ce1f2659 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 17:37:35 +0100 Subject: [PATCH 18/21] Remove unused methods --- lib/app.rb | 4 ---- lib/frame.rb | 2 +- spec/application_spec.rb | 5 +---- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index 2c120f48..300ebb48 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -26,10 +26,6 @@ def create_frames @frames[i + 1] = frame end end - - def return_frames - return @frames - end end unless ENV['ENV'] == 'test' diff --git a/lib/frame.rb b/lib/frame.rb index 7e64181d..e5b377ee 100644 --- a/lib/frame.rb +++ b/lib/frame.rb @@ -51,7 +51,7 @@ def spare? def frame_score return @ball_scores.sum end - + def total_frame_score return @ball_scores.sum + @bonus_score end diff --git a/spec/application_spec.rb b/spec/application_spec.rb index 20a585fa..03e0a7a8 100644 --- a/spec/application_spec.rb +++ b/spec/application_spec.rb @@ -1,8 +1,5 @@ require 'app' RSpec.describe Application do - it "creates the @frames array" do - app = Application.new - expect(app.return_frames.length).to eq 10 - end + end From 42ffbae9616629b865689123c046318a2cb0bb82 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 17:58:44 +0100 Subject: [PATCH 19/21] Update design.md --- design.md | 82 ++++++++++++++++++++++++------------------------------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/design.md b/design.md index 786e5a83..4352e576 100644 --- a/design.md +++ b/design.md @@ -43,60 +43,50 @@ flowchart TD classDiagram Application <|-- Frame Application <|-- ScoreCard + Application <|-- Gameplay Application : +array frames - Application : +int current_frame - Application : +int current_ball + Application : +object gameplay Application : +run() Application : +create_frames() class Frame{ - +int frame_number - +int ball1 - +int ball2 - +int ball3 + +array ball_scores + +int bonus_score + +bool strike + +bool spare + +add_ball_score(ball, ball_score) + +get_ball_score(ball) + +update_bonus_score(score) + +bonus_score() + +two_balls?() + +strike(ball) + +strike?() + +spare() + +spare?() + +frame_score() + +total_frame_score() } class ScoreCard{ - -int game_score - +TotalScore() + +update_pending_bonuses(frames, current_frame) + +show_scorecard(frames) + -update_pending_strikes(frames, current_frame) + -update_pending_spares(frames, current_frame) + -scorecard_segment(frame, frame_number) + -scorecard_segment_frame_10(frame) + } + class Gameplay{ + +int current_frame + +int current_ball + +object scorecard + +score_prompt(frames) + +process_current_ball(frame, input) + +next_ball(frames) + +continue?(frames) + +final_score(frames) + -validate_input(input, frames) } ``` -```ruby -class Application - def initialize - @frames = [] - @current_frame = 1 - @current_frame_ball = 1 - create_frames - end - - def run - # Runs an IO loop and calls the other classes - end - - def create_frames - # Runs once when .run starts to create the `@frames` array of frame objects - end - - def return_frames - # Returns the `@frames` array - end -end - -class Frame - # A model class with attributes: - # :frame_number, :ball_1, :ball_2, :ball_3 -end - -class ScoreCard - def GameScore - end - - def FrameScore - end -end -``` - -## 3. Create Examples as Integration Tests + From bb36e97d1c1b2300e8c3f1e935d6d842dddec5cc Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Sun, 11 Jun 2023 20:42:18 +0100 Subject: [PATCH 20/21] Fix incorrect input validation --- lib/gameplay.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gameplay.rb b/lib/gameplay.rb index 3a3d07ba..88e0a616 100644 --- a/lib/gameplay.rb +++ b/lib/gameplay.rb @@ -72,7 +72,7 @@ def final_score(frames) def validate_input(input, frames) return false if input == "" return false if input == "/" && @current_ball == 1 - return false if input == "X" && @current_ball == 2 + return false if input == "X" && @current_ball == 2 && @current_frame != 10 return false if /[X\/1-9]/.match(input) == nil end end From a8f9b25a081e29def9efec5a6012c62be7576089 Mon Sep 17 00:00:00 2001 From: Sean Peters Date: Tue, 13 Jun 2023 09:12:38 +0100 Subject: [PATCH 21/21] Update bonuses before showing scorecard --- lib/gameplay.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/gameplay.rb b/lib/gameplay.rb index 88e0a616..10bd6238 100644 --- a/lib/gameplay.rb +++ b/lib/gameplay.rb @@ -17,6 +17,7 @@ def score_prompt(frames) input = input.to_i if input != "X" && input != "/" frame = frames[@current_frame] process_current_ball(frame, input) + @scorecard.update_pending_bonuses(frames, @current_frame) @scorecard.show_scorecard(frames) return true end @@ -33,8 +34,6 @@ def process_current_ball(frame, input) end def next_ball(frames) - @scorecard.update_pending_bonuses(frames, @current_frame) - if @current_frame < 10 if @current_ball == 1 && frames[@current_frame].strike? != true @current_ball = 2