Class: BagOfHolding::Dice::DieRoller
- Inherits:
-
Object
- Object
- BagOfHolding::Dice::DieRoller
- Defined in:
- lib/bag_of_holding/dice/die_roller.rb
Overview
Contain all the logic around rolling a die given it’s options
Instance Attribute Summary collapse
-
#current_roll ⇒ Object
Returns the value of attribute current_roll.
-
#die ⇒ Object
Returns the value of attribute die.
Class Method Summary collapse
Instance Method Summary collapse
- #explode? ⇒ Boolean
-
#initialize(die:) ⇒ DieRoller
constructor
A new instance of DieRoller.
- #reroll? ⇒ Boolean
- #result ⇒ Object
- #rng ⇒ Object
- #roll ⇒ Object
Constructor Details
#initialize(die:) ⇒ DieRoller
Returns a new instance of DieRoller.
11 12 13 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 11 def initialize(die:) self.die = die end |
Instance Attribute Details
#current_roll ⇒ Object
Returns the value of attribute current_roll.
9 10 11 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 9 def current_roll @current_roll end |
#die ⇒ Object
Returns the value of attribute die.
9 10 11 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 9 def die @die end |
Class Method Details
.roll(die) ⇒ Object
5 6 7 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 5 def self.roll(die) new(die: die).roll end |
Instance Method Details
#explode? ⇒ Boolean
42 43 44 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 42 def explode? die.explode && current_roll >= die.explode end |
#reroll? ⇒ Boolean
38 39 40 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 38 def reroll? die.reroll && current_roll <= die.reroll end |
#result ⇒ Object
32 33 34 35 36 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 32 def result @result ||= BagOfHolding::Dice::DieResult.new( value: 0, rolls: [], die: self ) end |
#rng ⇒ Object
46 47 48 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 46 def rng 1 + rand(die.sides) end |
#roll ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bag_of_holding/dice/die_roller.rb', line 15 def roll loop do self.current_roll = rng result.add_roll current_roll next if reroll? result.value += current_roll next if explode? break end result end |