Skip to content

Instantly share code, notes, and snippets.

@calavera
Forked from xaviuzz/greed.rb
Created January 5, 2011 16:43
Show Gist options
  • Save calavera/766566 to your computer and use it in GitHub Desktop.
Save calavera/766566 to your computer and use it in GitHub Desktop.
def score(dice)
compute_singles(dice) + compute_triples(dice)
end
def compute_singles(diceRoll)
diceRoll.inject(0) do |result, roll|
result += single_value(roll)
end
end
SINGLE_VALUES = { 1 => 100 , 5 => 50}
def single_value(roll)
SINGLE_VALUES[roll] || 0
end
def compute_triples(diceRoll)
(1..6).inject(0) do |result, points|
result += calculate_triple_score(points) if diceRoll.count(points) >= 3
result
end
end
TRIPLE_FACTOR = 100
SPECIAL_TRIPLE_SCORES = {1=>1000}
def calculate_triple_score(points)
tripleScore = SPECIAL_TRIPLE_SCORES[points] || ( points * TRIPLE_FACTOR )
tripleScore - score_of_triple_as_singles(points)
end
def score_of_triple_as_singles(points)
single_value(points) * 3
end
@alberto
Copy link

alberto commented Jan 6, 2011

se supone que sólo hay 5 dados, así que sólo puede haber un trío ;)

@jneira
Copy link

jneira commented Jan 10, 2011

la cosa se extiende: en python usando solo lambdas y reduces: https://gist.github.com/772470

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