Class: GamesDice::MapRule

Inherits:
Object
  • Object
show all
Defined in:
lib/games_dice/map_rule.rb

Overview

This class models rules that convert numbers shown on a die to values used in a game. A common use for this is to count “successes” - dice that score a certain number or higher.

An object of the class represents a single rule, such as “count a die result of 5 or more as 1 success”.

Examples:

A rule for counting successes

rule = GamesDice::MapRule.new( 6, :<=, 1, 'Success' )
# Test how the rule applies . . .
rule.map_from 4   # => nil
rule.map_from 6   # => 1

A rule for counting “fumbles” which reduce total successes

rule = GamesDice::MapRule.new( 1, :==, -1, 'Fumble' )
# Test how the rule applies . . .
rule.map_from 7   # => nil
rule.map_from 1   # => -1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trigger_value, trigger_op, mapped_value = 0, mapped_name = '') ⇒ GamesDice::MapRule

Creates new instance of GamesDice::MapRule. 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

  • mapped_value (Integer) (defaults to: 0)

    The value to use in place of the trigger value

  • mapped_name (String) (defaults to: '')

    Name of mapped value, for use in descriptions

Raises:

  • (TypeError)

30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/games_dice/map_rule.rb', line 30

def initialize trigger_value, trigger_op, mapped_value=0, mapped_name=''

  if ! trigger_value.respond_to?( trigger_op )
    raise ArgumentError, "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}"
  end

  @trigger_value = trigger_value
  @trigger_op = trigger_op
  raise TypeError if ! mapped_value.is_a? Numeric
  @mapped_value = Integer(mapped_value)
  @mapped_name = mapped_name.to_s
end

Instance Attribute Details

#mapped_nameString (readonly)

Name for mapped value, used in explanations.

Returns:

  • (String)

57
58
59
# File 'lib/games_dice/map_rule.rb', line 57

def mapped_name
  @mapped_name
end

#mapped_valueInteger (readonly)

Value that a die will use after the value has been mapped.

Returns:

  • (Integer)

53
54
55
# File 'lib/games_dice/map_rule.rb', line 53

def mapped_value
  @mapped_value
end

#trigger_opSymbol (readonly)

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

Returns:

  • (Symbol)

    Method name to be sent to #trigger_value


45
46
47
# File 'lib/games_dice/map_rule.rb', line 45

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)


49
50
51
# File 'lib/games_dice/map_rule.rb', line 49

def trigger_value
  @trigger_value
end

Instance Method Details

#map_from(test_value) ⇒ Integer?

Assesses the rule against a die result value.

Parameters:

  • test_value (Integer)

    Value that is result of rolling a single die.

Returns:

  • (Integer, nil)

    Replacement value, or nil if this rule doesn't apply


62
63
64
65
66
67
68
69
# File 'lib/games_dice/map_rule.rb', line 62

def map_from test_value
  op_result = @trigger_value.send( @trigger_op, test_value )
  return nil unless op_result
  if op_result == true
    return @mapped_value
  end
  return op_result
end