Skip to content

Instantly share code, notes, and snippets.

@defeated
Created August 28, 2010 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defeated/555603 to your computer and use it in GitHub Desktop.
Save defeated/555603 to your computer and use it in GitHub Desktop.
# 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
@sl4m
Copy link

sl4m commented Aug 29, 2010

Re: (but I feel like it's still very procedural and could be improved?)

Any chance you can eliminate the if else statements?

@defeated
Copy link
Author

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