# 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!

# 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.

#

#

# Examples:

#

# 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.

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

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)

···

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

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!

# 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.

#

#

# Examples:

#

# 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.

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

#============================================================================
# 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