Class: DiceBox::Dice
- Inherits:
-
Object
- Object
- DiceBox::Dice
- 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
-
#result ⇒ Integer
(also: #rolled, #rolled_value)
readonly
The result from the previous roll.
-
#rolled_side ⇒ Dice::Side
readonly
The last rolled Side.
-
#sides ⇒ Array
readonly
The Array of Sides of the Dice.
Class Method Summary collapse
-
.roll(sides_number, amount = 1) ⇒ Integer
Rolls multiple dices with the same number of sides.
Instance Method Summary collapse
-
#balanced? ⇒ Boolean
Determines if all Sides of the Dice have the same weight.
-
#crooked? ⇒ Boolean
Determines if at least one Side has a different weight than any other Side of the Dice.
-
#initialize(sides_number) ⇒ Dice
constructor
A new instance of Dice.
-
#maximum ⇒ Integer
(also: #max)
Returns the highest value the Dice can roll.
-
#minimum ⇒ Integer
(also: #min)
Returns the lowest value the Dice can roll.
-
#roll ⇒ Integer
Rolls the dice.
-
#weight ⇒ Float
The weight of the Dice, sum of all Sides weights.
Constructor Details
#initialize(sides_number) ⇒ Dice
Returns a new instance of Dice.
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
#result ⇒ Integer (readonly) Also known as: rolled, rolled_value
Returns the result from the previous roll.
14 15 16 |
# File 'lib/dice_box/dice.rb', line 14 def result @result end |
#rolled_side ⇒ Dice::Side (readonly)
Returns the last rolled Side.
10 11 12 |
# File 'lib/dice_box/dice.rb', line 10 def rolled_side @rolled_side end |
#sides ⇒ Array (readonly)
Returns 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
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
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
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 |
#maximum ⇒ Integer Also known as: max
Returns the highest value the Dice can roll
48 49 50 |
# File 'lib/dice_box/dice.rb', line 48 def maximum sides.map(&:value).max end |
#minimum ⇒ Integer Also known as: min
Returns the lowest value the Dice can roll
55 56 57 |
# File 'lib/dice_box/dice.rb', line 55 def minimum sides.map(&:value).min end |
#roll ⇒ Integer
Sets #rolled_side to the rolled Side
Sets #rolled_value to the rolled Side’s value
Rolls the dice
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 |
#weight ⇒ Float
The weight of the Dice, sum of all Sides weights
76 77 78 |
# File 'lib/dice_box/dice.rb', line 76 def weight sides.map(&:weight).reduce(&:+) end |