Skip to content

Instantly share code, notes, and snippets.

@albert-chang0
Created July 30, 2011 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save albert-chang0/1115885 to your computer and use it in GitHub Desktop.
Save albert-chang0/1115885 to your computer and use it in GitHub Desktop.
greed score in ruby
#!/usr/bin/env ruby
# 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)
# You need to write this method
keeper = Array.new(6, 0)
score = 0
dice[0, 5].each do
raise ArgumentError, "Invalid die face #{val}" if val < 1 || val > 6
|val| keeper[val - 1] += 1
end
score += 1000 if keeper[0] >= 3
score += 100 * (keeper[0] % 3)
score += 50 * (keeper[4] % 3)
keeper.each_with_index do |tally, val|
score += (val + 2) * 100 if tally >= 3
end
score
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment