class Object
Public Instance Methods
bonus_yahtzee_line()
click to toggle source
# File lib/scoresheet.rb, line 116 def bonus_yahtzee_line bonus_string, yahtzee_string = justify_score("Bonus", upper_score_bonus.to_s), format_score(LowerScores, LowerScores.length - 1) ssl bonus_string, yahtzee_string end
Also aliased as: byl
cap_label(score_label)
click to toggle source
@param score_label [String] @return [String] Capitalize each letter of each word only if the score label has two or more words Else only capitalize the first letter of the score label
end¶ ↑
# File lib/scoresheet.rb, line 144 def cap_label(score_label) if score_label.split.length >= 2 score_label = score_label.split.map(&:capitalize)*' ' else score_label.capitalize! end end
format_score(score_region, index)
click to toggle source
@return [String] the formatted string @param index [Fixnum] @param score_region [String, Array<String>] Replace underscores with spaces
end¶ ↑
# File lib/scoresheet.rb, line 127 def format_score(score_region, index) score_label = score_region[index].to_s.tr(?_, " ") cap_label score_label score_field = @sheet[score_region[index]] return justify_score(score_label, "#{score_field[1]? score_field[0]:?-}") end
freq(dice)
click to toggle source
# File lib/scoring.rb, line 132 def freq(dice) dice.inject(Hash.new(0)) { |h,v| h[v] += 1; h } end
justify_score(label, score)
click to toggle source
# File lib/scoresheet.rb, line 134 def justify_score(label, score) label.ljust(20) + score.rjust(3) end
modal_frequency(dice)
click to toggle source
# File lib/scoring.rb, line 136 def modal_frequency(dice) freq(dice).max_by{|k,v| v}[1] end
of_a_kind(dice, limit)
click to toggle source
# File lib/scoring.rb, line 140 def of_a_kind(dice, limit) if modal_frequency(dice) >= limit dice.reduce :+ else 0 end end
print_score_sheet_line(i)
click to toggle source
# File lib/scoresheet.rb, line 110 def print_score_sheet_line(i); upper, lower = format_score(UpperScores, i), format_score(LowerScores, i) ssl upper, lower end
score_sheet_line(left_val, right_val)
click to toggle source
# File lib/scoresheet.rb, line 105 def score_sheet_line(left_val, right_val) (left_val + "\t\t" + right_val).center(68) + ?\n end
Also aliased as: ssl
straight(dice, limit, score)
click to toggle source
@param dice [Fixnum] the dice to be tested @param limit [Fixnum] = 4 for small straight @param limit [Fixnum] = 5 for large straight common code for both small straight (SS) and large straight (LS) @param score [Fixnum] is the score to return @return [Fixnum] score
end¶ ↑
# File lib/scoring.rb, line 156 def straight(dice, limit, score) (1..6).each_cons(limit).each do |i| #each_cons is generating every possible value for a straight of length limit if (i - dice).empty? # Asking if i is a subset of dice return score if (i - dice) end end return 0 end