Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# gem "rails"

gem 'rspec', '~> 3.12'

gem 'rubocop', '~> 1.51'

gem 'simplecov', '~> 0.22.0'

gem 'simplecov-cobertura', '~> 2.1'
61 changes: 61 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
diff-lcs (1.5.0)
docile (1.4.0)
json (2.6.3)
parallel (1.23.0)
parser (3.2.2.1)
ast (~> 2.4.1)
rainbow (3.1.1)
regexp_parser (2.8.0)
rexml (3.2.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.51.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.1)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-cobertura (2.1.0)
rexml
simplecov (~> 0.19)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
unicode-display_width (2.4.2)

PLATFORMS
arm64-darwin-21

DEPENDENCIES
rspec (~> 3.12)
rubocop (~> 1.51)
simplecov (~> 0.22.0)
simplecov-cobertura (~> 2.1)

BUNDLED WITH
2.4.13
164 changes: 164 additions & 0 deletions design/class_recipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@

# Multi-Class Planned Design Recipe

## 1. Describe the Problem

As a user
I want to be able to see
a scorecard of my bowling game
for one player

As a user
I want to see results
frame by frame in a 10
frame game of bowling

As a user
I will inputthe score of each roll,
of each frame

As a user
I want to see a running
total score of all the points
I have total frame by frame

As a user
I want to acurrately
calcuate a strike so I get
10 points for knocking down all pins
and x amount of extra points, x being
the number of pins knocked down in the
BOTH rolls of next frame

As a user
I want to acurrately
calcuate a spare 10 points for knocking down all pins
over two rolls, and x amount of extra points, x being
the number of pins knocked down in the
FIRST roll of next frame

As a user
if i roll a strike on the tenth frame
i want to be able to roll twice more
for my bonus points

As a user
if I roll a spare on the tenth frame
i want to be able to roll once more
for my bonus points

## 2. Design the Class System

nouns:

frame, scorecard, points, rolls, strike, spare, tenth frame, bonus points

```ruby

class Frame
def initialize
@final = []
end

def result(first_roll, second_roll)
if first_roll == 10
@final << first_roll, 'X'
return @final
else
@final << first_roll, second_roll
return @final
end
end

def strike?
@final.first == 10
end

def spare?
!strike? && @final.sum == 10
end

def final
return @final
end
end


class ScoreCard
def initialize
@frame_count = 0
@all_frames = []
end

def frame_input(frame)
if @frame_count == 9
return tenth_frame
else
@all_frames << frame.final
@frame_count += 1
return "#{all_frames} - #{10 - @frame_count} frames left"
end
end

def calculate_total

end

def tenth_frame

end

def final_format
return "Your game isn't over yet!" if @frame_count < 10
end

private

def spare_bonus_roll

end

def stike_bonus_rolls

end
end
```

## 3. Create Examples as Integration Tests

```ruby
RSpec.describe 'integration' do
context 'Inputting a Frame' do
it 'correctly inputs one Frame object into the total array' do
scorecard = Scorecard.new
frame_one = Frame.new
frame_one.result(3, 5)

expect(scorecard..frame_input(frame_one)).to eq "[3, 5] - 9 frames left"
end
end
end
```

## 4. Create Examples as Unit Tests

Create examples, where appropriate, of the behaviour of each relevant class at
a more granular level of detail.

Encode one of these as a test and move to step 5.

## 5. Implement the Behaviour

For each example you create as a test, implement the behaviour that allows the
class to behave according to your example.

Then return to step 3 until you have addressed the problem you were given. You
may also need to revise your design, for example if you realise you made a
mistake earlier.

Copy and fill out [this template](../resources/multi_class_recipe_template.md)
for each of the below exercises.

## Demonstration

[A video demonstration](https://www.youtube.com/watch?v=CkyhW3pNTUY&t=0s)
27 changes: 27 additions & 0 deletions lib/frame.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class Frame
def initialize
@final = []
end

def result(first_roll, second_roll)
@final << first_roll
@final << if first_roll == 10
0
else
second_roll
end
@final
end

def strike?
@final.first == 10
end

def spare?
!strike? && @final.sum == 10
end

attr_reader :final
end
86 changes: 86 additions & 0 deletions lib/scorecard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# frozen_string_literal: true

require_relative 'frame'

class ScoreCard
def initialize
@frame_count = 10
@all_frames = []
@final_frames = []
@tenth_frame = false
@total = nil
end

def frame_input(frame)
@frame_count -= 1
if @all_frames.empty?
@all_frames << frame
calculate_total
elsif @all_frames.last.strike? == true
strike_bonus_rolls(frame)
elsif @all_frames.last.spare? == true
spare_bonus_roll(frame)
else
@all_frames << frame
calculate_total
end
end

def calculate_total
@final_frames = final_arr_mapping
@total = @final_frames.compact.flatten.reduce(:+)
return tenth_frame if @frame_count == 0 && @tenth_frame == false
"Current Total: #{@total} - #{@frame_count} frames left\n#{@final_frames}"
end

def tenth_frame
@tenth_frame = true
if @all_frames.last.strike? == true
'strike tenth frame bonus roll functionality needed'
elsif @all_frames.last.spare? == true
'spare tenth frame bonus roll functionality needed'
else
final_format
end
end

def final_format
return "Your game isn't over yet! You have #{@frame_count} frames left" if @frame_count.positive?
@final_frames = final_arr_mapping
@total = @final_frames.compact.flatten.reduce(:+)
return "Final Total: #{@total} - Frames:\n#{@final_frames}"
end

private

def spare_bonus_roll(frame)
@all_frames.delete_at(@all_frames.length - 1)
total = frame.final.first + 10
spare_bonus = [total, 0]
@all_frames << spare_bonus
@all_frames << frame
calculate_total
end

def strike_bonus_rolls(frame)
@all_frames.delete_at(@all_frames.length - 1)
total = frame.final.sum + 10
strike_bonus = [total, 0]
@all_frames << strike_bonus
@all_frames << frame
calculate_total
end

def final_arr_mapping
result = @all_frames.map do |frame|
if frame.is_a?(Array)
frame
else
frame.final
end
end

return result
end

end
34 changes: 34 additions & 0 deletions spec/frame_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'frame'

RSpec.describe Frame do
context 'Constructing a Frame Object' do
it 'should create a normal Frame' do
frame = Frame.new

expect(frame.result(3, 5)).to eq [3, 5]
expect(frame.final).to eq [3, 5]
end

it 'should construct a Stike Frame object' do
frame = Frame.new

expect(frame.result(10, 3)).to eq [10, 0]
expect(frame.final).to eq [10, 0]
expect(frame.strike?).to eq true
end

it 'should construct a Spare Frame object' do
frame = Frame.new
frame_two = Frame.new

expect(frame.result(7, 3)).to eq [7, 3]
expect(frame.final).to eq [7, 3]
expect(frame.spare?).to eq true
expect(frame_two.result(4, 6)).to eq [4, 6]
expect(frame_two.final).to eq [4, 6]
expect(frame_two.spare?).to eq true
end
end
end
Loading