Skip to content

Instantly share code, notes, and snippets.

@yock
Created September 15, 2010 00:40
Show Gist options
  • Save yock/580045 to your computer and use it in GitHub Desktop.
Save yock/580045 to your computer and use it in GitHub Desktop.
def score(dice)
return 0 if dice.empty?
counts = Hash.new(0)
dice.each do |die|
counts[die] += 1
end
a_set = (counts.select {|k, v| v > 2})
set = a_set[0][0] if a_set.empty? == false
counts[set] -= 3 if set != nil
score = 0
if set == 1
score += 1000
score += credit_ones_and_fives(counts)
elsif set == 5
score += 500
score += credit_ones_and_fives(counts)
elsif set != nil
score += set * 100
score += credit_ones_and_fives(counts)
else
score += credit_ones_and_fives(counts)
end
end
def credit_ones_and_fives(counts)
score = 0
score += counts[1] * 100
score += counts[5] * 50
end
@yock
Copy link
Author

yock commented Sep 23, 2010

Refactored to use a hash and a helper method for the common math

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment