Skip to content

Instantly share code, notes, and snippets.

@brenfrow
Created June 29, 2012 19:33
Show Gist options
  • Save brenfrow/3020142 to your computer and use it in GitHub Desktop.
Save brenfrow/3020142 to your computer and use it in GitHub Desktop.
dice game
# You need to write this method
score = 0
map = {}
#create map of { dice_num => times_rolled }
dice.each do |num|
if map.has_key? num
map[num] += 1
else
map[num] = 1
end
end
#find one if one is greater than 3
triple = map.detect {|k,v| v >= 3}
unless triple.nil?
#score the triple
dice_value = triple[0]
if dice_value == 1
score += 1000
else
score += 100 * dice_value
end
#remove triple from array
i = 3
dice.delete_if do |num|
if( num == dice_value && i > 0)
i -= 1
return true
else
return false
end
end
end
score += dice.select { |x| x == 5}.count * 50
score += dice.select { |x| x == 1}.count * 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment