Class: GamesDice::RerollRule
- Inherits:
-
Object
- Object
- GamesDice::RerollRule
- Defined in:
- lib/games_dice/reroll_rule.rb
Overview
This class models a variety of game rules that cause dice to be re-rolled.
An object of the class represents a single rule, such as "re-roll a result of 1 and use the new value".
Instance Attribute Summary collapse
-
#limit ⇒ Integer
readonly
Maximum to number of times that this rule can be applied to a single die.
-
#trigger_op ⇒ Symbol
readonly
Trigger operation.
-
#trigger_value ⇒ Integer, ...
readonly
Trigger value.
-
#type ⇒ Symbol
readonly
The reroll behaviour that this rule triggers.
Instance Method Summary collapse
-
#applies?(test_value) ⇒ Boolean
Assesses the rule against a die result value.
-
#initialize(trigger_value, trigger_op, type, limit = 1000) ⇒ GamesDice::RerollRule
constructor
Creates new instance of GamesDice::RerollRule.
Constructor Details
#initialize(trigger_value, trigger_op, type, limit = 1000) ⇒ GamesDice::RerollRule
Creates new instance of GamesDice::RerollRule. The rule will be assessed as trigger_value.send( trigger_op, x ) where x is the Integer value shown on a die.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/games_dice/reroll_rule.rb', line 32 def initialize(trigger_value, trigger_op, type, limit = 1000) unless trigger_value.respond_to?(trigger_op) raise ArgumentError, "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}" end raise ArgumentError, "Unrecognised reason for a re-roll #{type}" unless GamesDice::REROLL_TYPES.key?(type) @trigger_value = trigger_value @trigger_op = trigger_op @type = type @limit = limit ? Integer(limit) : 1000 @limit = 1 if @type == :reroll_subtract end |
Instance Attribute Details
#limit ⇒ Integer (readonly)
Maximum to number of times that this rule can be applied to a single die.
67 68 69 |
# File 'lib/games_dice/reroll_rule.rb', line 67 def limit @limit end |
#trigger_op ⇒ Symbol (readonly)
Trigger operation. How the rule is assessed against #trigger_value.
49 50 51 |
# File 'lib/games_dice/reroll_rule.rb', line 49 def trigger_op @trigger_op end |
#trigger_value ⇒ Integer, ... (readonly)
Trigger value. An object that will use #trigger_op to assess a die result for a reroll.
53 54 55 |
# File 'lib/games_dice/reroll_rule.rb', line 53 def trigger_value @trigger_value end |
#type ⇒ Symbol (readonly)
The reroll behaviour that this rule triggers.
The following values are supported:
:reroll_add:: add result of reroll to running total, and ignore :reroll_subtract for this die
reroll_subtractsubtract result of reroll from running total, reverse sense of any further :reroll_add results
:reroll_replace:: use the new value in place of existing value for the die
:reroll_use_best:: use the new value if it is higher than the existing value
:reroll_use_worst:: use the new value if it is higher than the existing value
63 64 65 |
# File 'lib/games_dice/reroll_rule.rb', line 63 def type @type end |
Instance Method Details
#applies?(test_value) ⇒ Boolean
Assesses the rule against a die result value.
72 73 74 |
# File 'lib/games_dice/reroll_rule.rb', line 72 def applies?(test_value) @trigger_value.send(@trigger_op, test_value) ? true : false end |