Hey everyone! I’m a long-time lurker, and this is my first post. I’m
working through the Edgecase Ruby Koans (http://www.rubykoans.com/), and
have written a solution to scoring the game of dice called “greed.”
However, while it passes all the assertion tests, I am wondering if my
is sufficiently “Rubyesque”? I appreciate any and all feedback!
Greed is a dice game where you roll up to five dice to accumulate
points. The following “score” function will be used calculate the
score of a single roll of the dice.
A greed roll is scored as follows:
* 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.
score([1,1,1,5,1]) => 1150 points
score([2,3,4,6,2]) => 0 points
score([3,4,5,3,3]) => 350 points
score([1,5,1,2,4]) => 250 points
More scoring examples are given in the tests below:
Your goal is to write the score method.
total = 0
dice.uniq.each do |num|
if num == 1 if dice.count(1) >= 3 total += 1000 total += 100 * (dice.count(1) - 3) else total += 100 * dice.count(1) end elsif num == 5 if dice.count(5) >= 3 total += 5 * 100 total += 50 * (dice.count(5) - 3) else total += 50 * dice.count(5) end else total += num * 100 if dice.count(num) >= 3 end