Class: GamesDice::RerollRule

Inherits:
Object
  • Object
show all
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”.

Examples:

A rule for “exploding” dice

rule = GamesDice::RerollRule.new( 6, :<=, :reroll_add )
# Test whether the rule applies . . .
rule.applies? 4   # => false
rule.applies? 6   # => true
rule.type         # => :reroll_add

A rule for re-rolling and taking best value if first attempt is lower than a threshold

rule = GamesDice::RerollRule.new( 11, :>, :reroll_use_best, 1 )
# Test whether the rule applies . . .
rule.applies? 4   # => true
rule.applies? 14  # => false
rule.type         # => :reroll_use_best

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • trigger_value (Integer, Range<Integer>, Object)

    Any object is allowed, but typically an Integer

  • trigger_op (Symbol)

    A method of trigger_value that takes an Integer param and returns Boolean

  • type (Symbol)

    The type of reroll

  • limit (Integer) (defaults to: 1000)

    Maximum number of times this rule can be applied to a single 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

#limitInteger (readonly)

Maximum to number of times that this rule can be applied to a single die.

Returns:

  • (Integer)

    A number of rolls.



68
69
70
# File 'lib/games_dice/reroll_rule.rb', line 68

def limit
  @limit
end

#trigger_opSymbol (readonly)

Trigger operation. How the rule is assessed against #trigger_value.

Returns:

  • (Symbol)

    Method name to be sent to #trigger_value



50
51
52
# File 'lib/games_dice/reroll_rule.rb', line 50

def trigger_op
  @trigger_op
end

#trigger_valueInteger, ... (readonly)

Trigger value. An object that will use #trigger_op to assess a die result for a reroll.

Returns:

  • (Integer, Range, Object)

    Object that receives (#trigger_op, die_result)



54
55
56
# File 'lib/games_dice/reroll_rule.rb', line 54

def trigger_value
  @trigger_value
end

#typeSymbol (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

Returns:

  • (Symbol)

    A category for the re-roll, declares how it should be processed



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.

Parameters:

  • test_value (Integer)

    Value that is result of rolling a single die.

Returns:

  • (Boolean)

    Whether the rule applies.



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