-
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.
- Loading branch information
miguel.camba
committed
Aug 3, 2011
1 parent
61e448a
commit feed1c4
Showing
2 changed files
with
24 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.