Class: FifthedSim::MultiNode
- Inherits:
-
DiceExpression
- Object
- DiceExpression
- FifthedSim::MultiNode
- 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
-
#average ⇒ Object
What is the theoretical average value when we roll this many dice?.
-
#dice_type ⇒ Object
What kind of dice did we roll?.
-
#distribution ⇒ Object
Obtain a probability distribution for when we roll this many dice.
- #expression_equation ⇒ Object
-
#has_crit? ⇒ Boolean
Did any of our dice crit?.
-
#has_critfail? ⇒ Boolean
Did any of our dice critically fail?.
-
#initialize(array) ⇒ MultiNode
constructor
Generally, don’t calculate this yourself.
-
#max_value ⇒ Object
The maximum value we could have rolled.
-
#min_value ⇒ Object
The minimum value we could have rolled.
- #reroll ⇒ Object
-
#roll_count ⇒ Object
How many dice did we roll?.
-
#value ⇒ Object
Calculate the value of these dice.
- #value_equation(terminal: false) ⇒ Object
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
Instance Method Details
#average ⇒ Object
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_type ⇒ Object
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 |
#distribution ⇒ Object
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_equation ⇒ Object
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?
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?
60 61 62 |
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 60 def has_critfail? @array.any?(&:critfail?) end |
#max_value ⇒ Object
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_value ⇒ Object
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 |
#reroll ⇒ Object
48 49 50 |
# File 'lib/fifthed_sim/nodes/multi_node.rb', line 48 def reroll self.class.new(@array.map(&:reroll)) end |
#roll_count ⇒ Object
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 |
#value ⇒ Object
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 |