class DiceBox::Dice
Representation of a dice
Attributes
@!attribute [r] result @return [Integer] the result from the previous roll
@!attribute [r] result @return [Integer] the result from the previous roll
@!attribute [r] rolled_side
@return [Dice::Side] the last rolled Side
@!attribute [r] result @return [Integer] the result from the previous roll
@!attribute [r] sides @return [Array] the Array of Sides of the Dice
Public Class Methods
@param sides_number [Integer] the number of Sides this Dice
should have
# File lib/dice_box/dice.rb, line 19 def initialize(sides_number) @sides = build_sides(sides_number) @rolled_side = nil @rolled = nil end
Rolls multiple dices with the same number of sides @param sides_number [Integer] the number of sides of the dices @param amount [Integer] the number of dices rolled @return [Integer] the total roll result
# File lib/dice_box/dice.rb, line 29 def self.roll(sides_number, amount = 1) return 0 if amount.nil? || amount <= 0 amount.times.map do Random.new.rand(1..sides_number) end.reduce(&:+) end
Public Instance Methods
Determines if all Sides of the Dice
have the same weight @return [Boolean]
# File lib/dice_box/dice.rb, line 62 def balanced? !crooked? end
Returns the highest value the Dice
can roll @return [Integer] maximum roll value
# File lib/dice_box/dice.rb, line 48 def maximum sides.map(&:value).max end
Returns the lowest value the Dice
can roll @return [Integer] minimum roll value
# File lib/dice_box/dice.rb, line 55 def minimum sides.map(&:value).min end
Rolls the dice @note Sets rolled_side
to the rolled Side
@note Sets rolled_value
to the rolled Side’s value @return [Integer] the value of the rolled Side
# File lib/dice_box/dice.rb, line 41 def roll @rolled_side = balanced? ? sides.sample : weighted_roll @result = rolled_side.value end
Private Instance Methods
Instantiates multiple Sides @param sides_number [Integer] the number of Sides to instantiate @return [Array] the Array of instantiated Sides
# File lib/dice_box/dice.rb, line 85 def build_sides(sides_number) sides_number.times.map do |number| Side.new(number + 1) end end