Created
December 10, 2009 20:12
-
-
Save aherrman/253634 to your computer and use it in GitHub Desktop.
First attempt at ruby koans' Greed score calculator
This file contains hidden or 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) | |
count_dice_types(dice).each_pair.inject(0) { |score, pair| | |
value, count = pair | |
score + score_dice_value(value, count) | |
} | |
end | |
def score_dice_value(value, count) | |
return 0 if count == 0 | |
return tripple_score(value) + score_dice_value(value, count - 3) if count > 2 | |
return 100 * count if value == 1 | |
return 50 * count if value == 5 | |
0 | |
end | |
def tripple_score(value) | |
return 1000 if value == 1 | |
value * 100 | |
end | |
def count_dice_types(dice) | |
dice.inject({}) { |counts, value| | |
counts[value] = 0 if counts[value].nil? | |
counts[value] += 1 | |
counts | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment