私は Ruby Koans に取り組んでいますが、私が書いたメソッドの何が問題なのかを理解するのに少し苦労しています。私は about_scoring_project.rb にいて、サイコロ ゲームのスコア メソッドを書きました。
def score(dice)
return 0 if dice == []
sum = 0
rolls = dice.inject(Hash.new(0)) { |result, element| result[element] += 1; result; }
rolls.each { |key, value|
# special condition for rolls of 1
if key == 1
sum += 1000 | value -= 3 if value >= 3
sum += 100*value
next
end
sum += 100*key | value -= 3 if value >= 3
sum += 50*value if key == 5 && value > 0
}
return sum
end
運動に慣れていない人のために:
グリードはサイコロを5つ振ってポイントを貯めるサイコロゲームです。次の「score」関数は、サイコロの 1 回のロールのスコアを計算するために使用されます。
グリード ロールは次のように採点されます。
3個セットで1000ポイント
3 つの数字 (1 を除く) のセットは、その数字の 100 倍の価値があります。(例: スリーファイブは 500 ポイントです)。
1 つ (3 つのセットの一部ではない) は 100 ポイントの価値があります。
5 (3 つのセットの一部ではない) は 50 ポイントの価値があります。
それ以外はすべて0点です。
例:
スコア([1,1,1,5,1]) => 1150ポイント スコア([2,3,4,6,2]) => 0ポイント スコア([3,4,5,3,3]) => 350 ポイント score([1,5,1,2,4]) => 250 ポイント
以下のテストでは、その他のスコアリングの例が示されています。
あなたの目標は、score メソッドを書くことです。
ファイル内の最後のテストを実行しようとすると、問題が発生します。assert_equal 550, score([5,5,5,5])
何らかの理由で、550 ではなく 551 を返します。ご協力ありがとうございます。