Created
January 4, 2011 17:33
-
-
Save xaviuzz/765078 to your computer and use it in GitHub Desktop.
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) | |
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 |
pues si, aunque si se hace generico y con menos codigo al mismo tiempo ... ;-)
le he dado una vuelta y le he quitado unas cuantas lineas mas, haciendolo para n triples de paso
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
El ejercicio dice que viene 5 número nada más si no recuerdo mal. Yo lo hice para más triples, pero si el enunciado no te lo marca es tontería hacer código de más