Created
July 15, 2012 00:43
-
-
Save redsquirrel/3114133 to your computer and use it in GitHub Desktop.
In which I realize that the scoring logic for strikes and spares are the same. (3 consecutive balls)
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
require 'test/unit' | |
def score(*frames) | |
score = roll_count = 0 | |
rolls = frames.flatten | |
frames.each do |frame| | |
if sum(frame) == 10 | |
score += sum(next_three(rolls, roll_count)) | |
else | |
score += sum(frame) | |
end | |
roll_count += frame.size | |
end | |
score | |
end | |
def next_three(rolls, roll_count) | |
rolls[roll_count, 3] | |
end | |
def sum(rolls) | |
rolls.inject{|sum,roll|sum+roll} | |
end | |
class BowlingTest < Test::Unit::TestCase | |
def test_1 | |
assert_equal 9, score([5, 4]) | |
end | |
def test_2 | |
assert_equal 10, score([5, 4], [1, 0]) | |
end | |
def test_spare | |
assert_equal 25, score([5, 5], [6, 3]) | |
end | |
def test_strike | |
assert_equal 28, score([10], [6, 3]) | |
end | |
def test_double_strike | |
assert_equal 60, score([3, 3], [10], [10], [6, 3]) | |
end | |
def test_max_rolls | |
assert_equal 30, score(*(1..9).map{[0,0]}+[[10,10,10]]) | |
end | |
def test_almost_perfect | |
assert_equal 279, score(*(1..9).map{[10]}+[[9,1,10]]) | |
end | |
def test_perfect | |
assert_equal 300, score(*(1..9).map{[10]}+[[10,10,10]]) | |
end | |
end |
That looks like it was a good time! If you're interested, now that you have
the tests passing, keep playing around with it and see what you can cut out
of this. Such as: empty else statements, unnecessary return
statements, unnecessary @instance variables. Also, see if there are better
intention-revealing names that you can use for your variables.
…On Mon, Jul 23, 2012 at 6:14 AM, Daniel < ***@***.*** > wrote:
Great challenge Dave. Made me aware of how much I could DRY my code out.
My RQQKIE-version here: git://gist.github.com/3163121.git Danny
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/3114133
great catches Dave. reposted and working on drying out with each_cons
enumerable.
Danny
On Mon, Jul 23, 2012 at 8:44 AM, Dave Hoover <
reply@reply.github.com
… wrote:
That looks like it was a good time! If you're interested, now that you have
the tests passing, keep playing around with it and see what you can cut out
of this. Such as: empty else statements, unnecessary return
statements, unnecessary @instance variables. Also, see if there are better
intention-revealing names that you can use for your variables.
On Mon, Jul 23, 2012 at 6:14 AM, Daniel <
***@***.***
> wrote:
>
> Great challenge Dave. Made me aware of how much I could DRY my code out.
> My RQQKIE-version here: git://gist.github.com/3163121.git
> Danny
> ---
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/3114133
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/3114133
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great challenge Dave. Made me aware of how much I could DRY my code out. My response here: git://gist.github.com/3163121.git Danny