class ScoreSheet

Constants

LowerScores
UpperScores

Attributes

dice[R]
num_yahtzees[R]
sheet[R]

Public Class Methods

new(custom_dice=Array.new(5) {Dice.new.instance_eval "new_dice"}) click to toggle source

@param custom_dice [Array<Fixnum>] custom dice for testing

end

# File lib/scoresheet.rb, line 17
def initialize(custom_dice=Array.new(5) {Dice.new.instance_eval "new_dice"})
        @sheet, @dice, @num_yahtzees = Hash.new, Dice.new(custom_dice), 0
        Array.new(UpperScores).concat(LowerScores).each {|s| @sheet[s] = [0, false]}
end

Public Instance Methods

enter_score(field) click to toggle source

@param field [Symbol] @return [void] @raise ArgumentError

end

# File lib/scoresheet.rb, line 27
def enter_score(field)
        if field == :yahtzee && ((available :yahtzee) || @num_yahtzees > 0)
                @sheet[field] = yahtzee, true
        elsif available field                 
                @sheet[field] = send(field, @dice.values), true
        else
                raise ArgumentError, "Score already entered."
        end
end
filled?() click to toggle source

@return [true] if the score sheet is completely filled and no legal moves remain @return [false] if the score sheet is not completely filled and there are still legal moves to be made

end

# File lib/scoresheet.rb, line 41
def filled?
        @sheet.collect{|k,v| v[1]}.reduce{|r,e| r && e}
end
lower_score_total() click to toggle source
# File lib/scoresheet.rb, line 52
def lower_score_total  # @return [Integer] The total score of the lower part of the ScoreSheet
        @sheet.select{|x| LowerScores.include? x }.collect{|k,v| v[0]}.reduce :+
end
raw_upper() click to toggle source
# File lib/scoresheet.rb, line 45
def raw_upper  # @return [Fixnum]
        @sheet.select{|x| UpperScores.include? x }.collect{|k,v| v[0]}.reduce :+
end
to_s() click to toggle source
@todo Find a less complex way to create final string
@return [String]

end

# File lib/scoresheet.rb, line 87
def to_s
        ss = String.new
        ss += %Q( S C O R E  S H E E T ).center(80, ?=) + "\n\n"
        (0..(UpperScores.length - 1)).each { |i| ss += print_score_sheet_line(i) }
        ss += bonus_yahtzee_line
        ss += "\n\n"
        ss += "Total Score: #{total}".center(80) + ?\n
        ss += (?= * 80) + ?\n
        return ss
end
total() click to toggle source
# File lib/scoresheet.rb, line 55
def total      # @return [Integer] the grand total
        lower_score_total + upper_score_total
end
upper_score_bonus() click to toggle source

Checks if upper score bonus can be awarded @return [Fixnum] 0 if raw_upper < 63 @return [Fixnum] 35 if raw_upper >= 63

end

# File lib/scoresheet.rb, line 64
def upper_score_bonus
        if raw_upper >= 63 then 35
        else
                0
        end
end
upper_score_total() click to toggle source
# File lib/scoresheet.rb, line 49
def upper_score_total  # @return [Fixnum] the total score of the upper part of the ScoreSheet, including bonuses
        raw_upper + upper_score_bonus
end
yahtzee() click to toggle source

@return [Integer] Checks to see if you have all the of the same dice

end

# File lib/scoresheet.rb, line 74
def yahtzee
        if dice.values.all? {|x| x == dice.values[0]}
                @num_yahtzees += 1
                return sheet[:yahtzee][0] + 50 * 2 ** (@num_yahtzees - 1)
        else
                return 0
        end
end