Created
August 28, 2010 21:31
-
-
Save defeated/555603 to your computer and use it in GitHub Desktop.
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
# Ruby Koans - Greed Game - scoring method | |
# from http://rubykoans.com http://edgecase.com | |
# * A set of three ones is 1000 points | |
# * A set of three numbers (other than ones) is worth 100 times the | |
# number. (e.g. three fives is 500 points). | |
# * A one (that is not part of a set of three) is worth 100 points. | |
# * A five (that is not part of a set of three) is worth 50 points. | |
# * Everything else is worth 0 points. | |
def score(dice) | |
score = 0 | |
dice.sort!.each_with_index do |v, i| | |
if dice.count(v) >= 3 then | |
if v == 1 then | |
score += 1000 | |
else | |
score += v * 100 | |
end | |
2.times { dice.delete_at i } | |
elsif v == 1 then | |
score += 100 | |
elsif v == 5 then | |
score += 50 | |
end | |
end | |
score | |
end |
well .. that's the problem I'm attempting to solve ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Re: (but I feel like it's still very procedural and could be improved?)
Any chance you can eliminate the if else statements?