class DiceBox::Dice

Representation of a dice

Attributes

result[R]

@!attribute [r] result @return [Integer] the result from the previous roll

rolled[R]

@!attribute [r] result @return [Integer] the result from the previous roll

rolled_side[R]

@!attribute [r] rolled_side @return [Dice::Side] the last rolled Side

rolled_value[R]

@!attribute [r] result @return [Integer] the result from the previous roll

sides[R]

@!attribute [r] sides @return [Array] the Array of Sides of the Dice

Public Class Methods

new(sides_number) click to toggle source

@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
roll(sides_number, amount = 1) click to toggle source

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

balanced?() click to toggle source

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
crooked?() click to toggle source

Determines if at least one Side has a different weight than any other Side of the Dice @return [Boolean]

# File lib/dice_box/dice.rb, line 68
def crooked?
  sides.map(&:weight).any? do |weight|
    weight != sides.first.weight
  end
end
max()
Alias for: maximum
maximum() click to toggle source

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
Also aliased as: max
min()
Alias for: minimum
minimum() click to toggle source

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
Also aliased as: min
roll() click to toggle source

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
weight() click to toggle source

The weight of the Dice, sum of all Sides weights @return [Float] the weight of the Dice

# File lib/dice_box/dice.rb, line 76
def weight
  sides.map(&:weight).reduce(&:+)
end

Private Instance Methods

build_sides(sides_number) click to toggle source

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
weighted_roll() click to toggle source

Rolls the Dice taking Sides weights into account @return [Side] the rolled Side

# File lib/dice_box/dice.rb, line 93
def weighted_roll
  num = rand(0..weight)

  sides.each do |side|
    return side if side.weight > num
    num = num - side.weight
  end
end