Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

The Greed game, from Ruby Koans, where you roll up to 5 dice:

A set of three ones is 1000 points. A set of three numbers (other than ones) is worth 100 times the number.

A one (that is not part of a set of three) is worth 100 points. A five (that is not part of a set of three) is worth 50 points. Everything else is worth 0 points.

I broke my solution into four methods.

def score(dice)
 (1..6).reduce(0) { |sum, n| sum + points(dice, n) }
end
def points(dice, num)
 ((dice.count(num) / 3) * triple(num)) + ((dice.count(num) % 3) * bonus(num))
end
def triple(num)
 num == 1 ? 1000 : (num * 100)
end
def bonus(num)
 [1, 5].include?(num) ? 50 + (50 * (num % 5)) : 0
end

I suspect there's a way to avoid having to pass the dice array into points(), but I can't figure it out. Maybe using a yield somewhere?

The Greed game, from Ruby Koans, where you roll up to 5 dice:

A set of three ones is 1000 points. A set of three numbers (other than ones) is worth 100 times the number.

A one (that is not part of a set of three) is worth 100 points. A five (that is not part of a set of three) is worth 50 points. Everything else is worth 0 points.

I broke my solution into four methods.

def score(dice)
 (1..6).reduce(0) { |sum, n| sum + points(dice, n) }
end
def points(dice, num)
 ((dice.count(num) / 3) * triple(num)) + ((dice.count(num) % 3) * bonus(num))
end
def triple(num)
 num == 1 ? 1000 : (num * 100)
end
def bonus(num)
 [1, 5].include?(num) ? 50 + (50 * (num % 5)) : 0
end

I suspect there's a way to avoid having to pass the dice array into points(), but I can't figure it out. Maybe using a yield somewhere?

The Greed game, from Ruby Koans, where you roll up to 5 dice:

A set of three ones is 1000 points. A set of three numbers (other than ones) is worth 100 times the number.

A one (that is not part of a set of three) is worth 100 points. A five (that is not part of a set of three) is worth 50 points. Everything else is worth 0 points.

I broke my solution into four methods.

def score(dice)
 (1..6).reduce(0) { |sum, n| sum + points(dice, n) }
end
def points(dice, num)
 ((dice.count(num) / 3) * triple(num)) + ((dice.count(num) % 3) * bonus(num))
end
def triple(num)
 num == 1 ? 1000 : (num * 100)
end
def bonus(num)
 [1, 5].include?(num) ? 50 + (50 * (num % 5)) : 0
end

I suspect there's a way to avoid having to pass the dice array into points(), but I can't figure it out. Maybe using a yield somewhere?

edited tags
Link
RubberDuck
  • 31.1k
  • 6
  • 73
  • 176
Tweeted twitter.com/#!/StackCodeReview/status/459973992993267712
Source Link
ivan
  • 805
  • 6
  • 11

RubyKoans: Greed dice scoring

The Greed game, from Ruby Koans, where you roll up to 5 dice:

A set of three ones is 1000 points. A set of three numbers (other than ones) is worth 100 times the number.

A one (that is not part of a set of three) is worth 100 points. A five (that is not part of a set of three) is worth 50 points. Everything else is worth 0 points.

I broke my solution into four methods.

def score(dice)
 (1..6).reduce(0) { |sum, n| sum + points(dice, n) }
end
def points(dice, num)
 ((dice.count(num) / 3) * triple(num)) + ((dice.count(num) % 3) * bonus(num))
end
def triple(num)
 num == 1 ? 1000 : (num * 100)
end
def bonus(num)
 [1, 5].include?(num) ? 50 + (50 * (num % 5)) : 0
end

I suspect there's a way to avoid having to pass the dice array into points(), but I can't figure it out. Maybe using a yield somewhere?

lang-rb

AltStyle によって変換されたページ (->オリジナル) /