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.
34 35 36 37 38 39 40 |
# File 'lib/games_dice/dice.rb', line 34 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.
53 54 55 |
# File 'lib/games_dice/dice.rb', line 53 def bunch_multipliers @bunch_multipliers end |
#bunches ⇒ Array<GamesDice::Bunch> (readonly)
Bunches of dice that are components of the object
48 49 50 |
# File 'lib/games_dice/dice.rb', line 48 def bunches @bunches end |
#explain_result ⇒ String? (readonly)
Returns Explanation of result, or nil if no call to #roll yet.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/games_dice/dice.rb', line 104 def explain_result return nil unless @result explanations = @bunches.map { |bunch| bunch.label + ": " + bunch.explain_result } if explanations.count == 0 return @offset.to_s end if explanations.count == 1 if @offset !=0 return explanations[0] + '. ' + array_to_sum( [ @bunches[0].result, @offset ] ) else return explanations[0] end end bunch_values = @bunch_multipliers.zip(@bunches).map { |m,b| m * b.result } bunch_values << @offset if @offset != 0 explanations << array_to_sum( bunch_values ) return explanations.join('. ') end |
#max ⇒ Integer (readonly)
Maximum possible result from a call to #roll
79 80 81 |
# File 'lib/games_dice/dice.rb', line 79 def max @max ||= @offset + bunches_weighted_sum( :max ) end |
#min ⇒ Integer (readonly)
Minimum possible result from a call to #roll
72 73 74 |
# File 'lib/games_dice/dice.rb', line 72 def min @min ||= @offset + bunches_weighted_sum( :min ) end |
#minmax ⇒ Array<Integer> (readonly)
Convenience method, same as [dice.min, dice.max]
86 87 88 |
# File 'lib/games_dice/dice.rb', line 86 def minmax [min,max] end |
#name ⇒ String (readonly)
Name to help identify dice
44 45 46 |
# File 'lib/games_dice/dice.rb', line 44 def name @name end |
#offset ⇒ Integer (readonly)
Fixed offset added to sum of all bunches.
57 58 59 |
# File 'lib/games_dice/dice.rb', line 57 def offset @offset end |
#result ⇒ Integer? (readonly)
Result of most-recent roll, or nil if no roll made yet.
61 62 63 |
# File 'lib/games_dice/dice.rb', line 61 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.
94 95 96 97 98 99 100 |
# File 'lib/games_dice/dice.rb', line 94 def probabilities return @probabilities if @probabilities probs = @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
65 66 67 |
# File 'lib/games_dice/dice.rb', line 65 def roll @result = @offset + bunches_weighted_sum( :roll ) end |