Skip to content

Instantly share code, notes, and snippets.

@xaviuzz
Created January 4, 2011 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xaviuzz/765078 to your computer and use it in GitHub Desktop.
Save xaviuzz/765078 to your computer and use it in GitHub Desktop.
def score(dice)
compute_singles(dice) + compute_triples(dice)
end
def compute_singles(diceRoll)
result = 0
diceRoll.each do |roll|
result+= single_value(roll)
end
result
end
SINGLE_VALUES = { 1 => 100 , 5 => 50}
def single_value(roll)
return SINGLE_VALUES[roll] if SINGLE_VALUES.keys.include?(roll)
0
end
def compute_triples(diceRoll)
result = 0
asString = diceRoll.sort.to_s
for points in (1..6) do
ocurrences=diceRoll.count(points)
if ( ocurrences>=3 )
result += calculate_triple_score(points)
end
end
result
end
TRIPLE_FACTOR = 100
SPECIAL_TRIPLE_SCORES = {1=>1000}
def calculate_triple_score(points)
tripleScore = ( points * TRIPLE_FACTOR )
if SPECIAL_TRIPLE_SCORES.keys.include?(points)
tripleScore = SPECIAL_TRIPLE_SCORES[points]
end
tripleScore - score_of_triple_as_singles(points)
end
def score_of_triple_as_singles(points)
single_value(points) * 3
end
@jneira
Copy link

jneira commented Jan 7, 2011

clojure 1 - ruby 0 :-P https://gist.github.com/766918

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