Created
January 5, 2011 16:59
-
-
Save alberto/766590 to your computer and use it in GitHub Desktop.
Correcciones de greed para xaviuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score(dice) | |
compute_singles(dice) + compute_triples(dice) | |
end | |
def compute_singles(diceRoll) | |
diceRoll.each.inject(0) {|result, roll| result + single_value(roll) } | |
end | |
SINGLE_VALUES = { 1 => 100 , 5 => 50} | |
def single_value(roll) | |
SINGLE_VALUES.keys.include?(roll) ? SINGLE_VALUES[roll] : 0 | |
end | |
def compute_triples(diceRoll) | |
(1..6).each.inject(0) { |result, roll| result + calculate_triple_score(diceRoll, roll)} | |
end | |
TRIPLE_FACTOR = 100 | |
SPECIAL_TRIPLE_SCORES = {1=>1000} | |
def calculate_triple_score(diceRoll, points) | |
ocurrences = diceRoll.count(points) | |
return 0 if (ocurrences < 3) | |
score_for_a_set_of_three(points) - score_of_triple_as_singles(points) | |
end | |
def score_for_a_set_of_three points | |
SPECIAL_TRIPLE_SCORES.keys.include?(points) ? SPECIAL_TRIPLE_SCORES[points] : ( points * TRIPLE_FACTOR ) | |
end | |
def score_of_triple_as_singles(points) | |
single_value(points) * 3 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No tienen por qué gustarme todos los patrones :) Con nombre o sin él, a mi personalmente no me convence, y ya digo que lo hago de vez en cuando (si no encuentro otra forma más limpia)