# Greed Dice Game Scoring from Ruby Koans

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
code
is sufficiently “Rubyesque”? I appreciate any and all feedback!

# Your goal is to write the score method.

def score(dice)

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
``````

end

total

end

On 09/12/11 09:11, Nate Lindstrom wrote:

# score([1,5,1,2,4]) => 250 points

``````     total += 1000

end
``````

end

This is quite a fun exercise, and interesting to see what people come up
with. The only suggestion I have is that you can gain a lot of economy
by ‘baking’ the score values from the getgo. If the rules of the game
ever changed (you could roll more dice, or the score values changed),
there would be a lot less stuff to have to fiddle. This seems to pass
the tests.

def score(dice)

#============================================================================

# This bit bakes the scores on first invocation

#============================================================================
@greed_scores ||= (Hash.new { |h, k|
h[k] = {:singles=>0, :triples=>k*100}
}).merge!({1=>{:singles=>100, :triples=>1000}, 5=>{:singles=>50,
:triples=>500}})

#============================================================================

# This bit does the actual scoring

#============================================================================
score = 0
dice.uniq.each { |n|
triples, singles = dice.count(n).divmod(3)
score += (@greed_scores[n][:singles] *
singles)+(@greed_scores[n][:triples] * triples)
}
score

#============================================================================
end

Sam