-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This push will be done in May because: I don't have internet in this flight
- Loading branch information
1 parent
b72c579
commit e075552
Showing
6 changed files
with
220 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Frame | ||
attr_accessor :first_roll, :second_roll, :number | ||
|
||
AllPoints = 10 | ||
FirstRegularFrame = 1 | ||
FramesNumberIncrement = 1 | ||
LastRegularFrame = 10 | ||
NoPoints = 0 | ||
|
||
def initialize | ||
@number = FirstRegularFrame | ||
end | ||
|
||
def next_frame= next_frame | ||
@next_frame = next_frame | ||
next_frame.number = number + FramesNumberIncrement | ||
end | ||
|
||
def total_points | ||
return NoPoints if extra? | ||
return strike_points if strike? | ||
return spare_points if spare? | ||
roll_points | ||
end | ||
|
||
def spare_extra_points | ||
return AllPoints if strike? | ||
first_roll.to_i | ||
end | ||
|
||
def strike_extra_points | ||
return AllPoints if spare? | ||
return spare_points if strike? | ||
roll_points | ||
end | ||
|
||
def roll_points | ||
@first_roll.to_i + @second_roll.to_i | ||
end | ||
|
||
def spare_points | ||
AllPoints + @next_frame.spare_extra_points | ||
end | ||
|
||
def strike_points | ||
AllPoints + @next_frame.strike_extra_points | ||
end | ||
|
||
def extra? | ||
@number > LastRegularFrame | ||
end | ||
|
||
def spare? | ||
@second_roll == "/" | ||
end | ||
|
||
def strike? | ||
@first_roll == "X" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
require "frame" | ||
|
||
class ScoreCard | ||
attr_reader :notation, :frames | ||
|
||
def notation= notation | ||
@notation = notation | ||
end | ||
|
||
def notation | ||
@notation | ||
@frames = [] | ||
last_frame = nil | ||
rolls = notation.split(//) | ||
while (!rolls.empty?) do | ||
frame = Frame.new | ||
frame.first_roll = rolls.shift | ||
frame.second_roll = rolls.shift unless rolls.empty? || frame.strike? | ||
last_frame.next_frame = frame unless last_frame.nil? | ||
@frames << frame | ||
last_frame = frame | ||
end | ||
end | ||
|
||
def total_score | ||
@notation.split(//).inject(0) do |sum,roll| | ||
sum += roll.to_i | ||
@frames.inject(0) do |sum,frame| | ||
sum += frame.total_points | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require "spec_helper" | ||
|
||
describe Frame do | ||
|
||
let(:frame) { Frame.new } | ||
let(:simple_frame) { Frame.new.tap { |frame| frame.first_roll, frame.second_roll = "4", "3" } } | ||
let(:spare_frame) { Frame.new.tap { |frame| frame.first_roll, frame.second_roll = "3", "/" } } | ||
let(:strike_frame) { Frame.new.tap { |frame| frame.first_roll = "X" } } | ||
|
||
|
||
it "should be extra after number 10" do | ||
frame.number = 11 | ||
frame.extra?.should be true | ||
end | ||
|
||
it "should be numbered at creation" do | ||
frame.number.should == 1 | ||
end | ||
|
||
it "should be renumbered after been added as next frame" do | ||
frame.next_frame = strike_frame | ||
strike_frame.number.should == 2 | ||
end | ||
|
||
describe "simple points" do | ||
it "should be the sum where are two numbers" do | ||
frame.first_roll, frame.second_roll = "3", "2" | ||
frame.total_points.should == 5 | ||
end | ||
it "should sum 0 for misses" do | ||
frame.first_roll, frame.second_roll = "3", "-" | ||
frame.total_points.should == 3 | ||
end | ||
end | ||
|
||
describe "spares" do | ||
it "should be when second roll is /" do | ||
frame.first_roll, frame.second_roll = "3", "/" | ||
frame.spare?.should be true | ||
end | ||
it "should add the first roll of next frame" do | ||
frame.first_roll, frame.second_roll = "3", "/" | ||
frame.next_frame = simple_frame | ||
frame.total_points.should == 14 | ||
end | ||
it "should add 10 when next roll is strike" do | ||
frame.first_roll, frame.second_roll = "3", "/" | ||
frame.next_frame = strike_frame | ||
frame.total_points.should == 20 | ||
end | ||
end | ||
|
||
describe "strikes" do | ||
it "should add the following two rolls" do | ||
frame.first_roll = "X" | ||
next_frame = Frame.new | ||
next_frame.first_roll, next_frame.second_roll = "4", "3" | ||
frame.next_frame = next_frame | ||
frame.total_points.should == 17 | ||
end | ||
it "should add the following two rolls including spares" do | ||
frame.first_roll = "X" | ||
spare_frame.next_frame = simple_frame | ||
frame.next_frame = spare_frame | ||
frame.total_points.should == 20 | ||
end | ||
it "should add the following two rolls including strikes" do | ||
frame.first_roll = "X" | ||
next_frame = strike_frame | ||
strike_frame.next_frame = simple_frame | ||
frame.next_frame = next_frame | ||
frame.total_points.should == 24 | ||
end | ||
it "should sum 30 for 3 consecutive strikes" do | ||
frame.first_roll = "X" | ||
frame.next_frame = strike_frame | ||
strike_frame.next_frame = strike_frame | ||
frame.total_points.should == 30 | ||
end | ||
it "should not count rolls for extra frames" do | ||
simple_frame.number = 11 | ||
simple_frame.total_points.should == 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters