From 21a354fa9a6e4da47228592219505c94fc7729d3 Mon Sep 17 00:00:00 2001 From: Tom Carmichael-Mhanna Date: Sun, 23 Apr 2023 17:46:57 +0100 Subject: [PATCH 1/5] Project setup and creation of initial tests --- .rspec | 1 + Gemfile | 7 ++++ Gemfile.lock | 26 ++++++++++++ app.rb | 5 +++ spec/app_spec.rb | 12 ++++++ spec/frame_spec.rb | 10 +++++ spec/spec_helper.rb | 98 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 159 insertions(+) create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 app.rb create mode 100644 spec/app_spec.rb create mode 100644 spec/frame_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/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..8a9766fa --- /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-22 + +DEPENDENCIES + rspec (~> 3.12) + +BUNDLED WITH + 2.4.12 diff --git a/app.rb b/app.rb new file mode 100644 index 00000000..0787e899 --- /dev/null +++ b/app.rb @@ -0,0 +1,5 @@ +class Application + def initialize(io=Kernel) + @io = io + end +end \ No newline at end of file diff --git a/spec/app_spec.rb b/spec/app_spec.rb new file mode 100644 index 00000000..e853e2d0 --- /dev/null +++ b/spec/app_spec.rb @@ -0,0 +1,12 @@ +require 'app' + +describe Application do + it "asks user for a roll and moves to next frame if they enter 10" do + app = Application.new(@io) + expect(@io).to receive(:puts).with("Frame 1:").ordered + expect(@io).to receive(:gets).and_return(10).ordered + expect(@io).to receive(:puts).with("Frame 2:").ordered + + + 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..58aa4fa1 --- /dev/null +++ b/spec/frame_spec.rb @@ -0,0 +1,10 @@ +require 'frame' + + +describe Frame do + it "initializes with an empty array @rolls" do + frame = Frame.new + expect(frame.rolls).to be_an_instance_of Array + expect(frame.rolls.empty?).to be_truthy + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..c80d44b9 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,98 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 3e30f6f1e998859bba094fecb2ee9bad92dc571a Mon Sep 17 00:00:00 2001 From: Tom Carmichael-Mhanna Date: Sun, 23 Apr 2023 21:32:46 +0100 Subject: [PATCH 2/5] Test Drive correct scoring for perfect game --- app.rb | 12 ++++--- lib/frame.rb | 68 +++++++++++++++++++++++++++++++++++++ lib/game.rb | 83 ++++++++++++++++++++++++++++++++++++++++++++++ spec/app_spec.rb | 12 ------- spec/frame_spec.rb | 7 +++- spec/game_spec.rb | 37 +++++++++++++++++++++ 6 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 lib/frame.rb create mode 100644 lib/game.rb delete mode 100644 spec/app_spec.rb create mode 100644 spec/game_spec.rb diff --git a/app.rb b/app.rb index 0787e899..572d2cbb 100644 --- a/app.rb +++ b/app.rb @@ -1,5 +1,7 @@ -class Application - def initialize(io=Kernel) - @io = io - end -end \ No newline at end of file +require_relative 'lib/game' +require_relative 'lib/frame' + +game = Game.new +game.run_game +game.calculate_score +puts "Total score: #{game.grand_total}" diff --git a/lib/frame.rb b/lib/frame.rb new file mode 100644 index 00000000..9065a643 --- /dev/null +++ b/lib/frame.rb @@ -0,0 +1,68 @@ +class Frame + attr_accessor :rolls, :bonus_points + + def initialize(io=Kernel) + @io = io + @rolls = [] + @bonus_points = 0 + end + + def play_regular_frame + roll_first_ball + if @rolls.first == 10 + print_strike_message + return + end + roll_second_ball + print_spare_message if @rolls.inject(:+) == 10 + end + + def play_last_frame + roll_first_ball + if @rolls.first == 10 + print_strike_message + roll_bonus_ball + roll_second_bonus_ball + return + end + roll_second_ball + if @rolls.inject(:+) == 10 + print_spare_message + roll_bonus_ball + end + end + + private + + def roll_first_ball + @io.puts "Roll first ball:" + roll = @io.gets.chomp.to_i + @rolls << roll + end + + def print_strike_message + @io.puts "Strike!" + end + + def roll_second_ball + @io.puts "Roll second ball:" + roll = @io.gets.chomp.to_i + @rolls << roll + end + + def print_spare_message + @io.puts "Spare!" + end + + def roll_bonus_ball + @io.puts "Roll bonus ball:" + bonus_roll = @io.gets.chomp.to_i + @rolls << bonus_roll + end + + def roll_second_bonus_ball + @io.puts "Roll second bonus ball:" + bonus_roll = @io.gets.chomp.to_i + @rolls << bonus_roll + end +end \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 00000000..f972ba86 --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,83 @@ +require_relative './frame' + +class Game + attr_accessor :frames + attr_reader :grand_total + + def initialize(io=Kernel) + @io = io + @frames = [] + 10.times { @frames << Frame.new(@io) } + @gutter_game = false + @perfect_game = false + @grand_total = 0 + end + + def gutter_game + @gutter_game = true + end + + def gutter_game? + @gutter_game + end + + def perfect_game + @perfect_game = true + end + + def perfect_game? + @perfect_game + end + + def run_game + for i in 1..9 do + @io.puts "Frame #{i}:" + @frames[i-1].play_regular_frame + end + + @io.puts "Frame 10:" + @frames.last.play_last_frame + end + + def calculate_score + award_bonus_points + award_bonus_points_in_penultimate_frame + sum_all_points + end + + def award_bonus_points + for i in 0..7 do + # If player made a strike + if @frames[i].rolls.include?(10) + @frames[i].bonus_points += @frames[i+1].rolls.first + # If subsequent frame had two rolls + if @frames[i+1].rolls.length == 2 + @frames[i].bonus_points += @frames[i+1].rolls.last + # Else they must have bowled a strike + else + @frames[i].bonus_points += @frames[i+2].rolls.first + end + # If player made a spare + elsif @frames[i].rolls.inject(:+) == 10 + @frames[i].bonus_points += @frames[i+1].rolls.first + end + end + end + + def award_bonus_points_in_penultimate_frame + # If player made a strike + if @frames[8].rolls.include?(10) + @frames[8].bonus_points += (@frames.last.rolls.first + @frames.last.rolls[1]) + # If player made a spare + elsif @frames[8].rolls.inject(:+) == 10 + @frames[8].bonus_points += @frames.last.rolls.first + end + end + + def sum_all_points + @frames.each do |frame| + @grand_total += frame.rolls.inject(0, :+) + @grand_total += frame.bonus_points + end + end +end \ No newline at end of file diff --git a/spec/app_spec.rb b/spec/app_spec.rb deleted file mode 100644 index e853e2d0..00000000 --- a/spec/app_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'app' - -describe Application do - it "asks user for a roll and moves to next frame if they enter 10" do - app = Application.new(@io) - expect(@io).to receive(:puts).with("Frame 1:").ordered - expect(@io).to receive(:gets).and_return(10).ordered - expect(@io).to receive(:puts).with("Frame 2:").ordered - - - end -end \ No newline at end of file diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 58aa4fa1..2e5c9ced 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -2,9 +2,14 @@ describe Frame do + let(:frame) { Frame.new } it "initializes with an empty array @rolls" do - frame = Frame.new expect(frame.rolls).to be_an_instance_of Array expect(frame.rolls.empty?).to be_truthy end + + it "initializes with bonus points set to 0" do + expect(frame.bonus_points).to eq 0 + end + end diff --git a/spec/game_spec.rb b/spec/game_spec.rb new file mode 100644 index 00000000..f858adf8 --- /dev/null +++ b/spec/game_spec.rb @@ -0,0 +1,37 @@ +require 'game' +require 'frame' + +describe Game do + let(:io) { double(:io) } + let(:game) { Game.new(double(:io)) } + + it "intializes with an array of 10 frames" do + expect(game.frames.first).to be_an_instance_of Frame + expect(game.frames.first.rolls.empty?).to eq true + expect(game.frames.length).to eq 10 + end + + it "intializes with gutter_game? and perfect_game? set to false" do + expect(game.gutter_game?).to eq false + expect(game.perfect_game?).to eq false + end + + it "scores a perfect game 300" do + io = double(:io) + for i in 1..10 + expect(io).to receive(:puts).with("Frame #{i}:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('10') + expect(io).to receive(:puts).with("Strike!") + end + expect(io).to receive(:puts).with("Roll bonus ball:") + expect(io).to receive(:gets).and_return('10') + expect(io).to receive(:puts).with("Roll second bonus ball:") + expect(io).to receive(:gets).and_return('10') + game = Game.new(io) + game.run_game + game.calculate_score + expect(game.grand_total).to eq 300 + end + +end \ No newline at end of file From 481ee694d4033639b2b27e51774b7f7b2dc6b09b Mon Sep 17 00:00:00 2001 From: Tom Carmichael-Mhanna Date: Sun, 23 Apr 2023 21:42:33 +0100 Subject: [PATCH 3/5] Additional test for alternative scoring game --- lib/game.rb | 2 ++ spec/game_spec.rb | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/lib/game.rb b/lib/game.rb index f972ba86..d4f764a0 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -43,6 +43,8 @@ def calculate_score award_bonus_points award_bonus_points_in_penultimate_frame sum_all_points + @gutter_game = true if @grand_total == 0 + @perfect_game = true if @grand_total == 300 end def award_bonus_points diff --git a/spec/game_spec.rb b/spec/game_spec.rb index f858adf8..132cef6a 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -32,6 +32,84 @@ game.run_game game.calculate_score expect(game.grand_total).to eq 300 + expect(game.perfect_game?).to eq true end + + it "scores an example game correctly" do + io = double(:io) + expect(io).to receive(:puts).with("Frame 1:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('1') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('4') + + expect(io).to receive(:puts).with("Frame 2:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('4') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('5') + + expect(io).to receive(:puts).with("Frame 3:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('6') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('4') + expect(io).to receive(:puts).with("Spare!") + + expect(io).to receive(:puts).with("Frame 4:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('5') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('5') + expect(io).to receive(:puts).with("Spare!") + + expect(io).to receive(:puts).with("Frame 5:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('10') + expect(io).to receive(:puts).with("Strike!") + + expect(io).to receive(:puts).with("Frame 6:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('0') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('1') + + expect(io).to receive(:puts).with("Frame 7:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('7') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('3') + expect(io).to receive(:puts).with("Spare!") + + expect(io).to receive(:puts).with("Frame 8:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('6') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('4') + expect(io).to receive(:puts).with("Spare!") + + expect(io).to receive(:puts).with("Frame 9:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('10') + expect(io).to receive(:puts).with("Strike!") + + expect(io).to receive(:puts).with("Frame 10:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('2') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('8') + expect(io).to receive(:puts).with("Spare!") + expect(io).to receive(:puts).with("Roll bonus ball:") + expect(io).to receive(:gets).and_return('6') + + + game = Game.new(io) + game.run_game + game.calculate_score + expect(game.grand_total).to eq 133 + expect(game.perfect_game?).to eq false + expect(game.gutter_game?).to eq false + + end end \ No newline at end of file From a4561bd18784c18906132420db866f5ef3d15afd Mon Sep 17 00:00:00 2001 From: Tom Carmichael-Mhanna Date: Sun, 23 Apr 2023 21:43:57 +0100 Subject: [PATCH 4/5] Test gutter game scoring --- spec/game_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/game_spec.rb b/spec/game_spec.rb index 132cef6a..c97dc6ea 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -35,6 +35,22 @@ expect(game.perfect_game?).to eq true end + it "scores a gutter game 0" do + io = double(:io) + for i in 1..10 + expect(io).to receive(:puts).with("Frame #{i}:") + expect(io).to receive(:puts).with("Roll first ball:") + expect(io).to receive(:gets).and_return('0') + expect(io).to receive(:puts).with("Roll second ball:") + expect(io).to receive(:gets).and_return('0') + end + game = Game.new(io) + game.run_game + game.calculate_score + expect(game.grand_total).to eq 0 + expect(game.perfect_game?).to eq false + expect(game.gutter_game?).to eq true + end it "scores an example game correctly" do io = double(:io) From 91b8af9050086446c23b8de74552404af6d8d3de Mon Sep 17 00:00:00 2001 From: Tom Carmichael-Mhanna Date: Fri, 16 Jun 2023 13:19:03 +0100 Subject: [PATCH 5/5] Updated README --- README.md | 57 ++++++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 15dc4762..eaaf99d4 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,36 @@ Bowling Challenge in Ruby ================= -* Feel free to use google, your notes, books, etc. but work on your own -* If you refer to the solution of another coach or student, please put a link to that in your README -* If you have a partial solution, **still check in a partial solution** -* You must submit a pull request to this repo with your code by 9am Monday week +A weekend challenge as part of the Makers Academy software engineering bootcamp. ## The Task +We were given the following brief: + **THIS IS NOT A BOWLING GAME, IT IS A BOWLING SCORECARD PROGRAM. DO NOT GENERATE RANDOM ROLLS. THE USER INPUTS THE ROLLS.** -Count and sum the scores of a bowling game for one player. For this challenge, you do _not_ need to build a web app with a UI, instead, just focus on the logic for bowling (you also don't need a database). Next end-of-unit challenge, you will have the chance to translate the logic to Javascript and build a user interface. +Count and sum the scores of a bowling game for one player. A bowling game consists of 10 frames in which the player tries to knock down the 10 pins. In every frame the player can roll one or two times. The actual number depends on strikes and spares. The score of a frame is the number of knocked down pins plus bonuses for strikes and spares. After every frame the 10 pins are reset. -As usual please start by - -* Forking this repo - -* Finally submit a pull request before Monday week at 9am with your solution or partial solution. However much or little amount of code you wrote please please please submit a pull request before Monday week at 9am. - -___STRONG HINT, IGNORE AT YOUR PERIL:___ Bowling is a deceptively complex game. Careful thought and thorough diagramming — both before and throughout — will save you literal hours of your life. - -## Focus for this challenge The focus for this challenge is to write high-quality code. -In order to do this, you may pay particular attention to the following: -* Using diagramming to plan your approach to the challenge -* TDD your code -* Focus on testing behaviour rather than state -* Commit often, with good commit messages -* Single Responsibility Principle and encapsulation -* Clear and readable code - -## Bowling — how does it work? - -### Strikes - -The player has a strike if he knocks down all 10 pins with the first roll in a frame. The frame ends immediately (since there are no pins left for a second roll). The bonus for that frame is the number of pins knocked down by the next two rolls. That would be the next frame, unless the player rolls another strike. - -### Spares - -The player has a spare if the knocks down all 10 pins with the two rolls of a frame. The bonus for that frame is the number of pins knocked down by the next roll (first roll of next frame). - -### 10th frame +## Getting started -If the player rolls a strike or spare in the 10th frame they can roll the additional balls for the bonus. But they can never roll more than 3 balls in the 10th frame. The additional rolls only count for the bonus not for the regular frame count. +`git clone https://github.com/tomcarmichael/bowling-challenge-ruby.git` - 10, 10, 10 in the 10th frame gives 30 points (10 points for the regular first strike and 20 points for the bonus). - 1, 9, 10 in the 10th frame gives 20 points (10 points for the regular spare and 10 points for the bonus). +Install dependencies: -### Gutter Game +`bundle install` -A Gutter Game is when the player never hits a pin (20 zero scores). +Run the tests: -### Perfect Game +`rspec` -A Perfect Game is when the player rolls 12 strikes (10 regular strikes and 2 strikes for the bonus in the 10th frame). The Perfect Game scores 300 points. +Run the program: -In the image below you can find some score examples. +`ruby app.rb` -More about ten pin bowling here: http://en.wikipedia.org/wiki/Ten-pin_bowling +Enter the score for each roll. A strike will immediately move the program onto the next frame. A strike or a spare in frame 10 results in additional bonus balls for the player. -![Ten Pin Score Example](images/example_ten_pin_scoring.png) +At the end of the game the score will be calculated and printed to the terminal.