Class: GamesDice::ComplexDie
- Inherits:
-
Object
- Object
- GamesDice::ComplexDie
- Defined in:
- lib/games_dice/complex_die.rb,
lib/games_dice/complex_die_helpers.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
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/games_dice/complex_die.rb', line 45 def initialize(sides, = {}) @basic_die = GamesDice::Die.new(sides, [:prng]) @rerolls = construct_rerolls([:rerolls]) @maps = construct_maps([:maps]) @total = nil @result = nil @probabilities_complete = true end |
Instance Attribute Details
#basic_die ⇒ GamesDice::Die (readonly)
The simple component used by this complex one
59 60 61 |
# File 'lib/games_dice/complex_die.rb', line 59 def basic_die @basic_die end |
#explain_result ⇒ String? (readonly)
Returns Explanation of result, or nil if no call to #roll yet.
85 86 87 |
# File 'lib/games_dice/complex_die.rb', line 85 def explain_result @result.explain_value end |
#maps ⇒ Array<GamesDice::MapRule>? (readonly)
Returns Sequence of map rules, or nil if mapping is not required.
65 66 67 |
# File 'lib/games_dice/complex_die.rb', line 65 def maps @maps end |
#max ⇒ Integer (readonly)
Returns Maximum possible result from a call to #roll.
102 103 104 105 106 107 |
# File 'lib/games_dice/complex_die.rb', line 102 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.
93 94 95 96 97 98 |
# File 'lib/games_dice/complex_die.rb', line 93 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.
75 76 77 |
# File 'lib/games_dice/complex_die.rb', line 75 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.
62 63 64 |
# File 'lib/games_dice/complex_die.rb', line 62 def rerolls @rerolls end |
#result ⇒ GamesDice::DieResult? (readonly)
Returns Result of last call to #roll, nil if no call made yet.
68 69 70 |
# File 'lib/games_dice/complex_die.rb', line 68 def result @result end |
#sides ⇒ Integer (readonly)
Returns Number of sides.
79 80 81 |
# File 'lib/games_dice/complex_die.rb', line 79 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.
113 114 115 |
# File 'lib/games_dice/complex_die.rb', line 113 def probabilities @probabilities ||= calculate_probabilities end |
#roll(reason = :basic) ⇒ GamesDice::DieResult
Simulates rolling the die
120 121 122 123 124 125 |
# File 'lib/games_dice/complex_die.rb', line 120 def roll(reason = :basic) @result = GamesDice::DieResult.new(@basic_die.roll, reason) roll_apply_rerolls roll_apply_maps @result end |