diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..5ae7ed87 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +scratchpad.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..49590388 --- /dev/null +++ b/Gemfile @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# gem "rails" + +gem "rspec", "~> 3.12" + +gem "simplecov", "~> 0.22.0" + +gem "simplecov-cobertura", "~> 2.1" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..b4695496 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,39 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.5.0) + docile (1.4.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) + 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) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + rspec (~> 3.12) + simplecov (~> 0.22.0) + simplecov-cobertura (~> 2.1) + +BUNDLED WITH + 2.4.13 diff --git a/coverage/.last_run.json b/coverage/.last_run.json new file mode 100644 index 00000000..52d2bf29 --- /dev/null +++ b/coverage/.last_run.json @@ -0,0 +1,5 @@ +{ + "result": { + "line": 100.0 + } +} diff --git a/coverage/.resultset.json b/coverage/.resultset.json new file mode 100644 index 00000000..a1a50e6e --- /dev/null +++ b/coverage/.resultset.json @@ -0,0 +1,773 @@ +{ + "RSpec": { + "coverage": { + "/Users/wimdavies/Projects/bowling-challenge-ruby/spec/frame_spec.rb": { + "lines": [ + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 2, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + 1, + null, + 2, + null, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + null + ] + }, + "/Users/wimdavies/Projects/bowling-challenge-ruby/lib/frame.rb": { + "lines": [ + 1, + 1, + 1, + null, + 1, + 284, + 284, + 284, + 284, + 284, + 284, + null, + null, + 1, + 391, + 390, + null, + 389, + null, + null, + 1, + 97, + 28, + null, + 69, + null, + null, + null, + 1, + 47, + null, + 47, + null, + null, + 1, + 556, + null, + null, + 1, + 173, + null, + null + ] + }, + "/Users/wimdavies/Projects/bowling-challenge-ruby/spec/score_card_integration_spec.rb": { + "lines": [ + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + null, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + null, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + null, + 1, + null, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + null, + 1, + null, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 1, + 18, + null, + null, + 1, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 16, + null, + null, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 16, + null, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 16, + null, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 18, + null, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 21, + null, + 1, + null, + null, + 1, + 1, + null, + 21, + null, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + null, + null, + 1, + 1, + null, + 13, + null, + 1, + null, + null + ] + }, + "/Users/wimdavies/Projects/bowling-challenge-ruby/lib/score_card.rb": { + "lines": [ + 1, + null, + 1, + 1, + null, + 1, + 27, + 27, + 27, + null, + 27, + 270, + 270, + 270, + null, + null, + null, + 1, + 362, + null, + 362, + null, + 362, + 43, + 28, + null, + 15, + null, + null, + 319, + null, + null, + 362, + null, + null, + 1, + 14, + 140, + 140, + null, + 140, + null, + 14, + null, + 14, + null, + 112, + null, + null, + null, + null, + 1, + 4, + null, + 4, + 4, + 40, + 40, + null, + 40, + null, + null, + 4, + null, + null, + 1, + null, + 1, + 14, + 2, + 12, + 4, + null, + null, + null, + 1, + 14, + 2, + 12, + 2, + null, + null, + null, + 1, + 112, + 4, + 108, + 9, + 99, + 3, + null, + null, + null + ] + } + }, + "timestamp": 1685437384 + } +} diff --git a/coverage/.resultset.json.lock b/coverage/.resultset.json.lock new file mode 100644 index 00000000..e69de29b diff --git a/coverage/coverage.xml b/coverage/coverage.xml new file mode 100644 index 00000000..e4a07c31 --- /dev/null +++ b/coverage/coverage.xml @@ -0,0 +1,478 @@ + + + + + + /Users/wimdavies/Projects/bowling-challenge-ruby + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/frame.rb b/lib/frame.rb new file mode 100644 index 00000000..9231a3a3 --- /dev/null +++ b/lib/frame.rb @@ -0,0 +1,42 @@ +class Frame + attr_reader :rolls, :frame_score, :total_score + attr_accessor :frame_number, :bonus_score, :complete + + def initialize + @frame_number = 0 + @rolls = [] + @frame_score = 0 + @bonus_score = 0 + @total_score = 0 + @complete = false + end + + def add_roll(pins) + fail 'Only the tenth frame may consist of more than two rolls' if @frame_number < 10 && @rolls.length == 2 + fail 'The tenth frame may consist of no more than three rolls' if @frame_number == 10 && @rolls.length == 3 + + @rolls << pins + end + + def calculate_frame_score + if is_strike? + @frame_score = @rolls[0] + else + @frame_score = @rolls[0] + @rolls[1] + end + end + + def calculate_total_score + calculate_frame_score + + @total_score = (@frame_score + @bonus_score) + end + + def is_strike? + @rolls[0] == 10 + end + + def is_spare? + @rolls.length > 1 && (@rolls[0] + @rolls[1]) == 10 && @rolls[0] != 10 + end +end diff --git a/lib/score_card.rb b/lib/score_card.rb new file mode 100644 index 00000000..1959c7e5 --- /dev/null +++ b/lib/score_card.rb @@ -0,0 +1,93 @@ +require_relative 'frame' + +class ScoreCard + attr_reader :frames, :current_frame_index + + def initialize + @frames = [] + @current_frame_index = 0 + @game_score = 0 + + 10.times do + frame = Frame.new + frame.frame_number = @frames.length + 1 + @frames << frame + end + end + + def roll_current_frame(pins) + frame = @frames[@current_frame_index] + + frame.add_roll(pins) + + if @current_frame_index == 9 + if frame.is_strike? || frame.is_spare? + frame.complete = frame.rolls.length == 3 + else + frame.complete = frame.rolls.length == 2 + end + else + frame.complete = frame.rolls.length == 2 || frame.is_strike? + end + + @current_frame_index += 1 if @current_frame_index < 9 && frame.complete + end + + def calculate_bonus_scores + @frames.each_with_index do |frame, index| + next_frame = @frames[index + 1] + subsequent_frame = @frames[index + 2] + + case frame.frame_number + when 10 + calculate_bonus_for_frame_10(frame) + when 9 + calculate_bonus_for_frame_9(frame, next_frame) + else + calculate_bonus_for_other_frames(frame, next_frame, subsequent_frame) + end + end + end + + def calculate_game_score + calculate_bonus_scores + + frame_totals = 0 + @frames.each do |frame| + frame.calculate_frame_score + frame.calculate_total_score + + frame_totals += frame.total_score + end + + return @game_score = frame_totals + end + + private + + def calculate_bonus_for_frame_10(frame) + if frame.is_spare? + frame.bonus_score = frame.rolls[2] + elsif frame.is_strike? + frame.bonus_score = frame.rolls[1] + frame.rolls[2] + end + end + + def calculate_bonus_for_frame_9(frame, next_frame) + if frame.is_spare? + frame.bonus_score = next_frame.rolls[0] + elsif frame.is_strike? + frame.bonus_score = next_frame.rolls[0] + next_frame.rolls[1] + end + end + + def calculate_bonus_for_other_frames(frame, next_frame, subsequent_frame) + if frame.is_spare? + frame.bonus_score = next_frame.rolls[0] + elsif frame.is_strike? && next_frame.is_strike? + frame.bonus_score = next_frame.rolls[0] + subsequent_frame.rolls[0] + elsif frame.is_strike? + frame.bonus_score = next_frame.rolls[0] + next_frame.rolls[1] + end + end +end diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb new file mode 100644 index 00000000..6f7eaa1f --- /dev/null +++ b/spec/frame_spec.rb @@ -0,0 +1,159 @@ +require 'frame' + +RSpec.describe Frame do + context '#rolls' do + it 'initializes with an empty rolls array' do + frame = Frame.new + + expect(frame.rolls).to eq [] + end + end + + context '#add_rolls' do + it 'adds rolls to the array' do + frame = Frame.new + + frame.add_roll(1) + frame.add_roll(1) + + expect(frame.rolls).to eq [1, 1] + end + + it 'fails when adding a third roll to a non-tenth frame' do + frame = Frame.new + + frame.frame_number = 1 + frame.add_roll(1) + frame.add_roll(1) + + expect{frame.add_roll(1)}.to raise_error 'Only the tenth frame may consist of more than two rolls' + end + + it 'fails when a fourth add_roll attempted on tenth frame' do + frame = Frame.new + + frame.frame_number = 10 + frame.add_roll(1) + frame.add_roll(1) + frame.add_roll(1) + + expect{frame.add_roll(1)}.to raise_error 'The tenth frame may consist of no more than three rolls' + end + end + + context '#calculate_frame_score' do + it 'sets frame_score to 0 when rolls = [0, 0]' do + frame = Frame.new + + frame.add_roll(0) + frame.add_roll(0) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 0 + end + + it 'sets frame_score to 5 when rolls = [2, 3]' do + frame = Frame.new + + frame.add_roll(2) + frame.add_roll(3) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 5 + end + + it 'sets frame_score to 10 when rolls = [1, 9, 5] (tenth frame issue)' do + frame = Frame.new + frame.frame_number = 10 + + frame.add_roll(1) + frame.add_roll(9) + frame.add_roll(5) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 10 + end + + it 'sets frame_score to 10 when rolls = [10, 10, 10] (tenth frame issue)' do + frame = Frame.new + frame.frame_number = 10 + + frame.add_roll(10) + frame.add_roll(10) + frame.add_roll(10) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 10 + end + end + + context '#calculate_total_score' do + it 'sets total_score to 0 when frame_score and bonus_score are 0' do + frame = Frame.new + + frame.add_roll(0) + frame.add_roll(0) + frame.calculate_frame_score + frame.calculate_total_score + + expect(frame.total_score).to eq 0 + end + + it 'sets total_score to 10 when frame_score is 9 and bonus_score is 1' do + frame = Frame.new + + frame.add_roll(8) + frame.add_roll(1) + + frame.bonus_score = 1 + frame.calculate_total_score + + expect(frame.total_score).to eq 10 + end + end + + context '#is_strike?' do + it 'returns true when first roll = 10' do + frame = Frame.new + + frame.frame_number = 2 + frame.add_roll(10) + + expect(frame.is_strike?).to eq true + end + end + + context '#is_spare?' do + it 'returns true when sum of first two rolls = 10' do + frame = Frame.new + + frame.add_roll(5) + frame.add_roll(5) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 10 + expect(frame.is_spare?).to eq true + end + + it 'returns false when sum correct but only one roll' do + frame = Frame.new + + frame.add_roll(10) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 10 + expect(frame.is_spare?).to eq false + end + + it 'returns false when first roll 10 and second roll 0' do + frame = Frame.new + + frame.add_roll(10) + frame.add_roll(0) + frame.calculate_frame_score + + expect(frame.frame_score).to eq 10 + expect(frame.is_spare?).to eq false + end + end +end \ No newline at end of file diff --git a/spec/score_card_integration_spec.rb b/spec/score_card_integration_spec.rb new file mode 100644 index 00000000..4249bd0e --- /dev/null +++ b/spec/score_card_integration_spec.rb @@ -0,0 +1,456 @@ +require 'frame' +require 'score_card' + +RSpec.describe 'integration' do + it 'ScoreCard initializes with 10 frames, correctly numbered' do + score_card = ScoreCard.new + + expect(score_card.frames.length).to eq(10) + expect(score_card.frames[0].frame_number).to eq(1) + expect(score_card.frames[1].frame_number).to eq(2) + expect(score_card.frames[2].frame_number).to eq(3) + expect(score_card.frames[3].frame_number).to eq(4) + expect(score_card.frames[4].frame_number).to eq(5) + expect(score_card.frames[5].frame_number).to eq(6) + expect(score_card.frames[6].frame_number).to eq(7) + expect(score_card.frames[7].frame_number).to eq(8) + expect(score_card.frames[8].frame_number).to eq(9) + expect(score_card.frames[9].frame_number).to eq(10) + end + + it 'can enter "regular" rolls into the first frame' do + score_card = ScoreCard.new + + score_card.roll_current_frame(5) + score_card.roll_current_frame(2) + score_card.frames[0].calculate_frame_score + + expect(score_card.frames[0].rolls).to eq [5, 2] + expect(score_card.frames[0].frame_score).to eq 7 + end + + it 'marks a "regular" frame completed after two non-strike rolls' do + score_card = ScoreCard.new + + score_card.roll_current_frame(5) + expect(score_card.frames[0].complete).to eq false + + score_card.roll_current_frame(2) + expect(score_card.frames[0].complete).to eq true + end + + it 'marks a "regular" frame complete after a single strike' do + score_card = ScoreCard.new + + score_card.roll_current_frame(10) + + expect(score_card.frames[0].is_strike?).to eq true + expect(score_card.frames[0].complete).to eq true + end + + it 'does not mark tenth frame complete after a spare' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + expect(score_card.current_frame_index).to eq 9 + + score_card.roll_current_frame(9) + score_card.roll_current_frame(1) + expect(score_card.current_frame_index).to eq 9 + expect(score_card.frames[score_card.current_frame_index].frame_number).to eq 10 + + expect(score_card.frames[9].is_spare?).to eq true + expect(score_card.frames[9].rolls.length).to eq 2 + expect(score_card.frames[9].complete).to eq false + end + + it 'marks tenth frame spare complete after bonus roll' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + + # tenth frame spare + score_card.roll_current_frame(9) + score_card.roll_current_frame(1) + expect(score_card.current_frame_index).to eq 9 + expect(score_card.frames[score_card.current_frame_index].frame_number).to eq 10 + expect(score_card.frames[9].is_spare?).to eq true + expect(score_card.frames[9].complete).to eq false + + # tenth frame bonus roll + score_card.roll_current_frame(1) + expect(score_card.current_frame_index).to eq 9 + expect(score_card.frames[score_card.current_frame_index].frame_number).to eq 10 + expect(score_card.frames[9].is_spare?).to eq true + expect(score_card.frames[9].rolls.length).to eq 3 + expect(score_card.frames[9].complete).to eq true + end + + it 'does not mark tenth frame complete after a single strike' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + + # tenth frame strike + score_card.roll_current_frame(10) + expect(score_card.current_frame_index).to eq 9 + expect(score_card.frames[score_card.current_frame_index].frame_number).to eq 10 + expect(score_card.frames[9].is_strike?).to eq true + expect(score_card.frames[9].is_spare?).to eq false + expect(score_card.frames[9].complete).to eq false + end + + it 'does not mark tenth frame complete after two strikes' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + + # tenth frame strikes + score_card.roll_current_frame(10) + # one bonus roll + score_card.roll_current_frame(10) + expect(score_card.current_frame_index).to eq 9 + expect(score_card.frames[score_card.current_frame_index].frame_number).to eq 10 + expect(score_card.frames[9].is_strike?).to eq true + expect(score_card.frames[9].is_spare?).to eq false + expect(score_card.frames[9].complete).to eq false + end + + it 'marks tenth frame strike complete after two bonus rolls' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + + # tenth frame strike + score_card.roll_current_frame(10) + # two bonus rolls + score_card.roll_current_frame(2) + score_card.roll_current_frame(3) + expect(score_card.current_frame_index).to eq 9 + expect(score_card.frames[score_card.current_frame_index].frame_number).to eq 10 + expect(score_card.frames[9].is_strike?).to eq true + expect(score_card.frames[9].is_spare?).to eq false + expect(score_card.frames[9].complete).to eq true + end + + it 'updates the current frame index correctly after frame is completed' do + score_card = ScoreCard.new + + score_card.roll_current_frame(10) + + expect(score_card.current_frame_index).to eq 1 + end + + it 'will not update the current frame index after tenth frame is completed' do + score_card = ScoreCard.new + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + + score_card.roll_current_frame(10) + score_card.roll_current_frame(10) + score_card.roll_current_frame(10) + + expect(score_card.current_frame_index).to eq 9 + end + + it 'can enter subsequent rolls into correct "regular" frames' do + score_card = ScoreCard.new + + score_card.roll_current_frame(2) + score_card.roll_current_frame(2) + expect(score_card.frames[0].complete).to eq true + expect(score_card.current_frame_index).to eq 1 + + score_card.roll_current_frame(5) + score_card.roll_current_frame(2) + score_card.frames[1].calculate_frame_score + + expect(score_card.frames[1].rolls).to eq [5, 2] + expect(score_card.frames[1].frame_score).to eq 7 + expect(score_card.current_frame_index).to eq 2 + end + + it 'after strike, can enter subsequent rolls into correct "regular" frames' do + score_card = ScoreCard.new + + score_card.roll_current_frame(10) + expect(score_card.frames[0].complete).to eq true + expect(score_card.current_frame_index).to eq 1 + + score_card.roll_current_frame(5) + score_card.roll_current_frame(2) + expect(score_card.current_frame_index).to eq 2 + end + + it 'calculates the bonus score for a regular spare frame' do + score_card = ScoreCard.new + + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + expect(score_card.frames[0].is_spare?).to eq true + + score_card.roll_current_frame(1) + score_card.roll_current_frame(2) + + score_card.calculate_bonus_scores + expect(score_card.frames[0].bonus_score).to eq 1 + + score_card.frames[0].calculate_total_score + expect(score_card.frames[0].frame_score).to eq 10 + expect(score_card.frames[0].total_score).to eq 11 + end + + it 'calculates the bonus score for a ninth spare frame' do + score_card = ScoreCard.new + + # 8 frames of zeros + 16.times do + score_card.roll_current_frame(0) + end + + # ninth frame is a spare + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + expect(score_card.frames[8].is_spare?).to eq true + + #rolls of tenth frame + score_card.roll_current_frame(1) + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + expect(score_card.frames[8].bonus_score).to eq 1 + + score_card.frames[8].calculate_total_score + expect(score_card.frames[8].frame_score).to eq 10 + expect(score_card.frames[8].total_score).to eq 11 + end + + it 'calculates the bonus score for a regular strike frame (when next roll not a strike)' do + score_card = ScoreCard.new + # frame 1: strike + score_card.roll_current_frame(10) + # frame 2: two regular rolls scoring 5 + score_card.roll_current_frame(2) + score_card.roll_current_frame(3) + + score_card.calculate_bonus_scores + # frame 1: bonus score of 5 + expect(score_card.frames[0].bonus_score).to eq 5 + + score_card.frames[0].calculate_total_score + expect(score_card.frames[0].frame_score).to eq 10 + expect(score_card.frames[0].total_score).to eq 15 + end + + it 'calculates the bonus score for a regular strike frame (when next roll is a strike)' do + score_card = ScoreCard.new + # frame 1 strike + score_card.roll_current_frame(10) + # frame 2 strike + score_card.roll_current_frame(10) + # frame 3 regular rolls + score_card.roll_current_frame(1) + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + + expect(score_card.frames[0].bonus_score).to eq 11 + expect(score_card.frames[1].bonus_score).to eq 2 + + score_card.frames[0].calculate_total_score + expect(score_card.frames[0].frame_score).to eq 10 + expect(score_card.frames[0].total_score).to eq 21 + + score_card.frames[1].calculate_total_score + expect(score_card.frames[1].frame_score).to eq 10 + expect(score_card.frames[1].total_score).to eq 12 + end + + it 'calculates the bonus score for a ninth frame spare' do + score_card = ScoreCard.new + + # 8 frames of zeros + 16.times do + score_card.roll_current_frame(0) + end + # 9th frame spare + score_card.roll_current_frame(9) + score_card.roll_current_frame(1) + # 10th frame rolls + score_card.roll_current_frame(1) + score_card.roll_current_frame(1) + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + + expect(score_card.frames[8].bonus_score).to eq 1 + end + + it 'calculates the bonus score for a ninth frame strike' do + score_card = ScoreCard.new + + # 8 frames of zeros + 16.times do + score_card.roll_current_frame(0) + end + # 9th frame strike + score_card.roll_current_frame(10) + # 10th frame rolls + score_card.roll_current_frame(1) + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + + expect(score_card.frames[8].bonus_score).to eq 2 + end + + + it 'calculates the bonus score for a tenth frame spare' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + # 10th frame spare + score_card.roll_current_frame(9) + score_card.roll_current_frame(1) + # bonus roll + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + + expect(score_card.frames[9].bonus_score).to eq 1 + end + + it 'calculates the bonus score for a tenth frame strike (regular bonus rolls)' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + # 10th frame strike + score_card.roll_current_frame(10) + # bonus rolls + score_card.roll_current_frame(1) + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + + expect(score_card.frames[9].bonus_score).to eq 2 + end + + it 'calculates the bonus score for a tenth frame strike (bonus: one strike, one regular)' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + # 10th frame strike + score_card.roll_current_frame(10) + # bonus rolls + score_card.roll_current_frame(10) + score_card.roll_current_frame(1) + + score_card.calculate_bonus_scores + + expect(score_card.frames[9].bonus_score).to eq 11 + end + + it 'calculates the bonus score for a tenth frame strike (two bonus strikes)' do + score_card = ScoreCard.new + + # 9 frames of zeros + 18.times do + score_card.roll_current_frame(0) + end + # 10th frame strike + score_card.roll_current_frame(10) + # bonus rolls + score_card.roll_current_frame(10) + score_card.roll_current_frame(10) + + score_card.calculate_bonus_scores + + expect(score_card.frames[9].bonus_score).to eq 20 + end + + it 'calculates game score of 0 for a Gutter Game' do + score_card = ScoreCard.new + + 20.times{ score_card.roll_current_frame(0) } + + expect(score_card.calculate_game_score).to eq(0) + end + + it 'calculates game score of 90 when each roll is 1' do + score_card = ScoreCard.new + + 20.times{ score_card.roll_current_frame(1) } + + expect(score_card.calculate_game_score).to eq(20) + end + + it 'calculates correct game score when an "average" game' do + score_card = ScoreCard.new + + score_card.roll_current_frame(5) + score_card.roll_current_frame(4) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(4) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + + score_card.roll_current_frame(10) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + + score_card.roll_current_frame(3) + score_card.roll_current_frame(5) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(4) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(4) + + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + score_card.roll_current_frame(5) + + expect(score_card.calculate_game_score).to eq(127) + end + + it 'returns game score of 300 for a Perfect Game' do + score_card = ScoreCard.new + + 12.times{ score_card.roll_current_frame(10) } + + expect(score_card.calculate_game_score).to eq(300) + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..c37e78f3 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,104 @@ +require 'simplecov' +require 'simplecov-cobertura' +SimpleCov.start + +SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter + +# 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