Class: GamesDice::Dice
- Inherits:
-
Object
- Object
- GamesDice::Dice
- Defined in:
- lib/games_dice/dice.rb
Overview
This class models a combination of GamesDice::Bunch objects plus a fixed offset.
An object of this class is a dice "recipe" that specifies the numbers and types of dice that can be rolled to generate an integer value.
Instance Attribute Summary collapse
-
#bunch_multipliers ⇒ Array<Integer>
readonly
Multipliers for each bunch of identical dice.
-
#bunches ⇒ Array<GamesDice::Bunch>
readonly
Bunches of dice that are components of the object.
-
#explain_result ⇒ String?
readonly
Explanation of result, or nil if no call to #roll yet.
-
#max ⇒ Integer
readonly
Maximum possible result from a call to #roll.
-
#min ⇒ Integer
readonly
Minimum possible result from a call to #roll.
-
#minmax ⇒ Array<Integer>
readonly
Convenience method, same as [dice.min, dice.max].
-
#name ⇒ String
readonly
Name to help identify dice.
-
#offset ⇒ Integer
readonly
Fixed offset added to sum of all bunches.
-
#result ⇒ Integer?
readonly
Result of most-recent roll, or nil if no roll made yet.
Instance Method Summary collapse
-
#initialize(bunches, offset = 0, name = '') ⇒ GamesDice::Dice
constructor
The first parameter is an array of values that are passed to GamesDice::Bunch constructors.
-
#probabilities ⇒ GamesDice::Probabilities
Calculates the probability distribution for the dice.
-
#roll ⇒ Integer
Simulates rolling dice.
Constructor Details
#initialize(bunches, offset = 0, name = '') ⇒ GamesDice::Dice
The first parameter is an array of values that are passed to GamesDice::Bunch constructors.
40 41 42 43 44 45 46 |
# File 'lib/games_dice/dice.rb', line 40 def initialize(bunches, offset = 0, name = '') @name = name @offset = offset @bunches = bunches.map { |b| GamesDice::Bunch.new(b) } @bunch_multipliers = bunches.map { |b| b[:multiplier] || 1 } @result = nil end |
Instance Attribute Details
#bunch_multipliers ⇒ Array<Integer> (readonly)
Multipliers for each bunch of identical dice. Typically 1 or -1 to represent groups of dice that are either added or subtracted from the total.
59 60 61 |
# File 'lib/games_dice/dice.rb', line 59 def bunch_multipliers @bunch_multipliers end |
#bunches ⇒ Array<GamesDice::Bunch> (readonly)
Bunches of dice that are components of the object
54 55 56 |
# File 'lib/games_dice/dice.rb', line 54 def bunches @bunches end |
#explain_result ⇒ String? (readonly)
Returns Explanation of result, or nil if no call to #roll yet.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/games_dice/dice.rb', line 111 def explain_result return nil unless @result explanations = @bunches.map { |bunch| "#{bunch.label}: #{bunch.explain_result}" } return @offset.to_s if explanations.none? return simple_explanation(explanations.first) if explanations.one? multi_explanations(explanations) end |
#max ⇒ Integer (readonly)
Maximum possible result from a call to #roll
85 86 87 |
# File 'lib/games_dice/dice.rb', line 85 def max @max ||= @offset + bunches_weighted_sum(:max) end |
#min ⇒ Integer (readonly)
Minimum possible result from a call to #roll
78 79 80 |
# File 'lib/games_dice/dice.rb', line 78 def min @min ||= @offset + bunches_weighted_sum(:min) end |
#minmax ⇒ Array<Integer> (readonly)
Convenience method, same as [dice.min, dice.max]
92 93 94 |
# File 'lib/games_dice/dice.rb', line 92 def minmax [min, max] end |
#name ⇒ String (readonly)
Name to help identify dice
50 51 52 |
# File 'lib/games_dice/dice.rb', line 50 def name @name end |
#offset ⇒ Integer (readonly)
Fixed offset added to sum of all bunches.
63 64 65 |
# File 'lib/games_dice/dice.rb', line 63 def offset @offset end |
#result ⇒ Integer? (readonly)
Result of most-recent roll, or nil if no roll made yet.
67 68 69 |
# File 'lib/games_dice/dice.rb', line 67 def result @result end |
Instance Method Details
#probabilities ⇒ GamesDice::Probabilities
Calculates the probability distribution for the dice. When the dice include components with open-ended re-roll rules, there are some arbitrary limits imposed to prevent large amounts of recursion.
100 101 102 103 104 105 106 107 |
# File 'lib/games_dice/dice.rb', line 100 def probabilities return @probabilities if @probabilities @bunch_multipliers.zip(@bunches).inject(GamesDice::Probabilities.new([1.0], @offset)) do |probs, mb| m, b = mb GamesDice::Probabilities.add_distributions_mult(1, probs, m, b.probabilities) end end |
#roll ⇒ Integer
Simulates rolling dice
71 72 73 |
# File 'lib/games_dice/dice.rb', line 71 def roll @result = @offset + bunches_weighted_sum(:roll) end |