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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/games_dice/reroll_rule.rb', line 31 def initialize trigger_value, trigger_op, type, limit = 1000 if ! trigger_value.respond_to?( trigger_op ) raise ArgumentError, "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}" end unless GamesDice::REROLL_TYPES.has_key?(type) raise ArgumentError, "Unrecognised reason for a re-roll #{type}" end @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.
68 69 70 |
# File 'lib/games_dice/reroll_rule.rb', line 68 def limit @limit end |
#trigger_op ⇒ Symbol (readonly)
Trigger operation. How the rule is assessed against #trigger_value.
50 51 52 |
# File 'lib/games_dice/reroll_rule.rb', line 50 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.
54 55 56 |
# File 'lib/games_dice/reroll_rule.rb', line 54 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_subtract
-
subtract result of reroll from running total, and 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
64 65 66 |
# File 'lib/games_dice/reroll_rule.rb', line 64 def type @type end |
Instance Method Details
#applies?(test_value) ⇒ Boolean
Assesses the rule against a die result value.
73 74 75 |
# File 'lib/games_dice/reroll_rule.rb', line 73 def applies? test_value @trigger_value.send( @trigger_op, test_value ) ? true : false end |