Skip to content

Commit

Permalink
100% working with samples.
Browse files Browse the repository at this point in the history
This push will be done in May because:
I don't have internet in this flight
  • Loading branch information
GermanDZ authored and 12meses12katas committed May 1, 2011
1 parent b72c579 commit e075552
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 20 deletions.
39 changes: 32 additions & 7 deletions germandz/features/player_roll.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,36 @@ Feature: player fill the score card to obtain the total points

Each roll count in represented in the score card as the number of hits, or a '/' for the spares or a 'X' for the strikes.

Scenario Outline: just a number
Scenario Outline: score card complete
Given a score card with <notation>
When I ask the total points
Then I should see <points>
Examples:
| notation | points |
| "4" | 4 |
| "6" | 6 |
When I ask the total score
Then I should see <score>

Scenarios: No rolls
| notation | score |
| "" | 0 |

Scenarios: No spares or strikes
| notation | score |
| "43" | 7 |
| "--------------------" | 0 |
| "91919191919191919191" | 100 |
| "9-9-9-9-9-9-9-9-9-9-" | 90 |

Scenarios: with spares
| notation | score |
| "5/5/5/5/5/5/5/5/5/5/5" | 150 |
| "9/9-8--/72-481-63-8-" | 92 |

Scenarios: with strikes
| notation | score |
| "XXXXXXXXXXXX" | 300 |
| "729-9-9-9/9/XX8-71" | 137 |
| "-69/X63719-5/X7/7-" | 135 |
| "1/-99-6-157-8/7-9-XX-" | 100 |
| "357-3/-333X9/8/629/6" | 112 |
| "8/X71718-9-XX9/X7-" | 157 |
| "9/8/X7/53348/-78/-/8" | 133 |
| "XX6-XX-97-7-9/9/7" | 146 |
| "9/XX7/639/X9/9/9-" | 179 |
| "349-3--95/X3--1X-7" | 89 |
2 changes: 1 addition & 1 deletion germandz/features/step_definitions/player_roll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@score_card.notation = notation
end

When /^I ask the total points$/ do
When /^I ask the total score$/ do
@total_score = @score_card.total_score
end

Expand Down
61 changes: 61 additions & 0 deletions germandz/lib/frame.rb
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
23 changes: 17 additions & 6 deletions germandz/lib/score_card.rb
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
85 changes: 85 additions & 0 deletions germandz/spec/frame_spec.rb
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
30 changes: 24 additions & 6 deletions germandz/spec/score_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,44 @@

describe "adding notations" do
it "should accept a full notation" do
score_card.notation = "7"
score_card.notation.should == "7"
score_card.notation = "73"
score_card.notation.should == "73"
end
it "should replace other notation" do
score_card.notation = "3"
score_card.notation = "43"
score_card.notation.should == "43"
end
it "should not add a second roll strike frames" do
score_card.notation = "X12"
score_card.frames[0].second_roll.should be nil
end
end

describe "score" do
describe "without strikes or spares" do
it "should be count of down when rolled one" do
score_card.notation = "3"
score_card.total_score.should be 3
end
it "should be the sum of downs when rolled many" do
score_card.notation = "34"
score_card.total_score.should be 7
end
it "should be sum 0 when is a miss" do
score_card.notation = "-2"
score_card.total_score.should be 2
end
end
describe "spares" do
it "should sum the complement to 10 in the second roll" do
score_card.notation = "3/--"
score_card.total_score.should be 10
end
it "should add the next roll" do
score_card.notation = "3/51"
score_card.total_score.should be 21
end
it "should add the extra roll" do
score_card.notation = "3/33"
score_card.total_score.should be 19
end
end
end
end

0 comments on commit e075552

Please sign in to comment.