Class: FifthedSim::MultiNode

Inherits:
DiceExpression show all
Defined in:
lib/fifthed_sim/nodes/multi_node.rb

Overview

This class models the result of a roll of multiple dice. It is filled with the actual result of randomly-rolled dice, but contains methods to enable the calculation of average values.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DiceExpression

#*, #+, #-, #/, #difference_from_average, #max, #min, #or_greater, #or_least, #percentile, #range, #test_then, #to_dice_expression, #to_f, #to_i

Constructor Details

#initialize(array) ⇒ MultiNode

Generally, don’t calculate this yourself



38
39
40
41
42
43
44
45
46
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 38

def initialize(array)
  unless array.is_a?(Array) && ! array.empty?
    raise ArgumentError, "Not a valid array"
  end
  unless array.all?{|elem| elem.is_a?(RollNode) }
    raise ArgumentError, "Not all die rolls"
  end
  @array = array
end

Class Method Details

.d(num, type) ⇒ Object



32
33
34
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 32

def self.d(num, type)
  self.new(num.times.map{RollNode.roll(type)})
end

Instance Method Details

#averageObject

What is the theoretical average value when we roll this many dice?



66
67
68
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 66

def average
  @array.map(&:average).inject(:+)
end

#dice_typeObject

What kind of dice did we roll?



84
85
86
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 84

def dice_type
  @array.first.type
end

#distributionObject

Obtain a probability distribution for when we roll this many dice. This is an instnace of the Distribution class.



103
104
105
106
107
108
109
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 103

def distribution
  total_possible = (dice_type ** roll_count)
  mapped = min_value.upto(max_value).map do |k|
    [k, (occurences(k) / total_possible.to_f)]
  end
  Distribution.new(Hash[mapped])
end

#expression_equationObject



117
118
119
120
121
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 117

def expression_equation
  "(" + @array.map do |a|
    a.expression_equation
  end.join(", ") + ")"
end

#has_crit?Boolean

Did any of our dice crit?

Returns:

  • (Boolean)


54
55
56
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 54

def has_crit?
  @array.any?(&:crit?)
end

#has_critfail?Boolean

Did any of our dice critically fail?

Returns:

  • (Boolean)


60
61
62
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 60

def has_critfail?
  @array.any?(&:critfail?)
end

#max_valueObject

The maximum value we could have rolled



96
97
98
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 96

def max_value
  dice_type * roll_count
end

#min_valueObject

The minimum value we could have rolled



90
91
92
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 90

def min_value
  roll_count
end

#rerollObject



48
49
50
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 48

def reroll
  self.class.new(@array.map(&:reroll))
end

#roll_countObject

How many dice did we roll?



78
79
80
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 78

def roll_count
  @array.count
end

#valueObject

Calculate the value of these dice



72
73
74
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 72

def value
  @array.map(&:value).inject(:+)
end

#value_equation(terminal: false) ⇒ Object



111
112
113
114
115
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 111

def value_equation(terminal: false)
  "(" + @array.map do |a|
    a.value_equation(terminal: terminal)
  end.join(", ") + ")"
end