Class: GamesDice::ComplexDie
- Inherits:
-
Object
- Object
- GamesDice::ComplexDie
- Defined in:
- lib/games_dice/complex_die.rb
Overview
This class models a die that is built up from a simpler unit by adding rules to re-roll and interpret the value shown.
An object of this class represents a single complex die. It rolls 1..#sides, with equal weighting for each value. The value from a roll may be used to trigger yet more rolls that combine together. After any re-rolls, the value can be interpretted (“mapped”) as another integer, which is used as the final result.
Instance Attribute Summary collapse
-
#basic_die ⇒ GamesDice::Die
readonly
The simple component used by this complex one.
-
#explain_result ⇒ String?
readonly
Explanation of result, or nil if no call to #roll yet.
-
#maps ⇒ Array<GamesDice::MapRule>?
readonly
Sequence of map rules, or nil if mapping is not required.
-
#max ⇒ Integer
readonly
Maximum possible result from a call to #roll.
-
#min ⇒ Integer
readonly
The minimum possible result from a call to #roll.
-
#probabilities_complete ⇒ Boolean?
readonly
Whether or not #probabilities includes all possible outcomes.
-
#rerolls ⇒ Array<GamesDice::RerollRule>?
readonly
Sequence of re-roll rules, or nil if re-rolls are not required.
-
#result ⇒ GamesDice::DieResult?
readonly
Result of last call to #roll, nil if no call made yet.
-
#sides ⇒ Integer
readonly
Number of sides.
Instance Method Summary collapse
-
#initialize(sides, options = {}) ⇒ GamesDice::ComplexDie
constructor
Creates new instance of GamesDice::ComplexDie.
-
#probabilities ⇒ GamesDice::Probabilities
Calculates the probability distribution for the die.
-
#roll(reason = :basic) ⇒ GamesDice::DieResult
Simulates rolling the die.
Constructor Details
#initialize(sides, options = {}) ⇒ GamesDice::ComplexDie
Creates new instance of GamesDice::ComplexDie
39 40 41 42 43 44 45 46 47 |
# File 'lib/games_dice/complex_die.rb', line 39 def initialize( sides, = {} ) @basic_die = GamesDice::Die.new(sides, [:prng]) @rerolls = construct_rerolls( [:rerolls] ) @maps = construct_maps( [:maps] ) @total = nil @result = nil end |
Instance Attribute Details
#basic_die ⇒ GamesDice::Die (readonly)
The simple component used by this complex one
51 52 53 |
# File 'lib/games_dice/complex_die.rb', line 51 def basic_die @basic_die end |
#explain_result ⇒ String? (readonly)
Returns Explanation of result, or nil if no call to #roll yet.
77 78 79 |
# File 'lib/games_dice/complex_die.rb', line 77 def explain_result @result.explain_value end |
#maps ⇒ Array<GamesDice::MapRule>? (readonly)
Returns Sequence of map rules, or nil if mapping is not required.
57 58 59 |
# File 'lib/games_dice/complex_die.rb', line 57 def maps @maps end |
#max ⇒ Integer (readonly)
Returns Maximum possible result from a call to #roll.
93 94 95 96 97 |
# File 'lib/games_dice/complex_die.rb', line 93 def max return @max_result if @max_result calc_minmax @max_result end |
#min ⇒ Integer (readonly)
The minimum possible result from a call to #roll. This is not always the same as the theoretical minimum, due to limits on the maximum number of rerolls.
85 86 87 88 89 |
# File 'lib/games_dice/complex_die.rb', line 85 def min return @min_result if @min_result calc_minmax @min_result end |
#probabilities_complete ⇒ Boolean? (readonly)
Whether or not #probabilities includes all possible outcomes. True if all possible results are represented and assigned a probability. Dice with open-ended re-rolls may have calculations cut short, and will result in a false value of this attribute. Even when this attribute is false, probabilities should still be accurate to nearest 1e-9.
67 68 69 |
# File 'lib/games_dice/complex_die.rb', line 67 def probabilities_complete @probabilities_complete end |
#rerolls ⇒ Array<GamesDice::RerollRule>? (readonly)
Returns Sequence of re-roll rules, or nil if re-rolls are not required.
54 55 56 |
# File 'lib/games_dice/complex_die.rb', line 54 def rerolls @rerolls end |
#result ⇒ GamesDice::DieResult? (readonly)
Returns Result of last call to #roll, nil if no call made yet.
60 61 62 |
# File 'lib/games_dice/complex_die.rb', line 60 def result @result end |
#sides ⇒ Integer (readonly)
Returns Number of sides.
71 72 73 |
# File 'lib/games_dice/complex_die.rb', line 71 def sides @basic_die.sides end |
Instance Method Details
#probabilities ⇒ GamesDice::Probabilities
Calculates the probability distribution for the die. For open-ended re-roll rules, there are some arbitrary limits imposed to prevent large amounts of recursion. Probabilities should be to nearest 1e-9 at worst.
103 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/complex_die.rb', line 103 def probabilities return @probabilities if @probabilities @probabilities_complete = true if @rerolls && @maps reroll_probs = recursive_probabilities prob_hash = {} reroll_probs.each do |v,p| add_mapped_to_prob_hash( prob_hash, v, p ) end elsif @rerolls prob_hash = recursive_probabilities elsif @maps prob_hash = {} @basic_die.probabilities.each do |v,p| add_mapped_to_prob_hash( prob_hash, v, p ) end else @probabilities = @basic_die.probabilities return @probabilities end @probabilities = GamesDice::Probabilities.from_h( prob_hash ) end |
#roll(reason = :basic) ⇒ GamesDice::DieResult
Simulates rolling the die
129 130 131 132 133 134 |
# File 'lib/games_dice/complex_die.rb', line 129 def roll( reason = :basic ) @result = GamesDice::DieResult.new( @basic_die.roll, reason ) roll_apply_rerolls roll_apply_maps @result end |