Skip to content

Commit

Permalink
Removed spec. Kata finished
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel.camba committed Aug 3, 2011
1 parent 61e448a commit feed1c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
38 changes: 24 additions & 14 deletions cibernox/bowling.rb
@@ -1,34 +1,44 @@
class Frame

attr_accessor :index, :first, :second, :previous, :acumulative_score
attr_accessor :first, :second, :sig

def natural_score
first + second
(first || 0) + (second || 0)
end

def acumulate_score(acum)
self.acumulative_score = natural_score + acum
self.previous.acumulate_score(self.natural_score) if self.previous
def acumulative_score
return natural_score if natural_score < 10 # Normal
return natural_score + sig.natural_score if first < 10 && sig # Spare
natural_score + (sig ? sig.natural_score + (sig.sig ? sig.sig.natural_score : 0) : 0) # Strike
end

def initialize(idx, ary, prev = nil)
self.index = idx
def initialize(ary)
self.first, self.second = *ary
self.previous = prev
end

end

class Bowling
attr_accessor :frames

##
# ary contains the frames' scores.
# It can be an array^2 ([[1,2],[10,nil],[3,7],[5,5]...])
# or an array of strings where 'X' means Strike, '/' mean Spare, and "3,4" is a normal frame
# but it can't contains both types
##
def initialize(ary)
self.frames = []
raise "Invalid number of frames" if ary.size != 10
ary.each_with_index do |e, i|
prev = i == 0 ? nil : self.frames[i - 1]
self.frames << Frame.new(i, e, prev)
end
formated_ary = if ary.first.is_a? String
ary.map{ |e| e == "X" ? [10, nil] : (e == "/" ? [5,5] : e.split(',').map(&:to_i) ) }
else
ary
end
self.frames = formated_ary.map{ |e| Frame.new(e) }
self.frames.each_with_index{ |frame, index| frame.sig = self.frames[index + 1] if index <= 10 }
end

def total_score
self.frames[0..9].inject(0){|acum, frame| acum + frame.acumulative_score }
end

end
9 changes: 0 additions & 9 deletions cibernox/bowling_spec.rb

This file was deleted.

0 comments on commit feed1c4

Please sign in to comment.