Class: DiceBox::Dice

Inherits:
Object
  • Object
show all
Defined in:
lib/dice_box/dice.rb,
lib/dice_box/dice/side.rb

Overview

Representation of a dice

Defined Under Namespace

Classes: Side

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sides_number) ⇒ Dice

Returns a new instance of Dice.

Parameters:

  • sides_number (Integer)

    the number of Sides this Dice should have



19
20
21
22
23
# File 'lib/dice_box/dice.rb', line 19

def initialize(sides_number)
  @sides = build_sides(sides_number)
  @rolled_side = nil
  @rolled = nil
end

Instance Attribute Details

#resultInteger (readonly) Also known as: rolled, rolled_value

Returns the result from the previous roll.

Returns:

  • (Integer)

    the result from the previous roll



14
15
16
# File 'lib/dice_box/dice.rb', line 14

def result
  @result
end

#rolled_sideDice::Side (readonly)

Returns the last rolled Side.

Returns:



10
11
12
# File 'lib/dice_box/dice.rb', line 10

def rolled_side
  @rolled_side
end

#sidesArray (readonly)

Returns the Array of Sides of the Dice.

Returns:

  • (Array)

    the Array of Sides of the Dice



6
7
8
# File 'lib/dice_box/dice.rb', line 6

def sides
  @sides
end

Class Method Details

.roll(sides_number, amount = 1) ⇒ Integer

Rolls multiple dices with the same number of sides

Parameters:

  • sides_number (Integer)

    the number of sides of the dices

  • amount (Integer) (defaults to: 1)

    the number of dices rolled

Returns:

  • (Integer)

    the total roll result



29
30
31
32
33
34
35
# 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

Instance Method Details

#balanced?Boolean

Determines if all Sides of the Dice have the same weight

Returns:

  • (Boolean)


62
63
64
# File 'lib/dice_box/dice.rb', line 62

def balanced?
  !crooked?
end

#crooked?Boolean

Determines if at least one Side has a different weight than any other Side of the Dice

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/dice_box/dice.rb', line 68

def crooked?
  sides.map(&:weight).any? do |weight|
    weight != sides.first.weight
  end
end

#maximumInteger Also known as: max

Returns the highest value the Dice can roll

Returns:

  • (Integer)

    maximum roll value



48
49
50
# File 'lib/dice_box/dice.rb', line 48

def maximum
  sides.map(&:value).max
end

#minimumInteger Also known as: min

Returns the lowest value the Dice can roll

Returns:

  • (Integer)

    minimum roll value



55
56
57
# File 'lib/dice_box/dice.rb', line 55

def minimum
  sides.map(&:value).min
end

#rollInteger

Note:

Sets #rolled_side to the rolled Side

Note:

Sets #rolled_value to the rolled Side’s value

Rolls the dice

Returns:

  • (Integer)

    the value of the rolled Side



41
42
43
44
# File 'lib/dice_box/dice.rb', line 41

def roll
  @rolled_side = balanced? ? sides.sample : weighted_roll
  @result = rolled_side.value
end

#weightFloat

The weight of the Dice, sum of all Sides weights

Returns:

  • (Float)

    the weight of the Dice



76
77
78
# File 'lib/dice_box/dice.rb', line 76

def weight
  sides.map(&:weight).reduce(&:+)
end