Class: BagOfHolding::Dice::DieRoller

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_rollObject

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

#dieObject

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

Returns:

  • (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

Returns:

  • (Boolean)


38
39
40
# File 'lib/bag_of_holding/dice/die_roller.rb', line 38

def reroll?
  die.reroll && current_roll <= die.reroll
end

#resultObject



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

#rngObject



46
47
48
# File 'lib/bag_of_holding/dice/die_roller.rb', line 46

def rng
  1 + rand(die.sides)
end

#rollObject



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