games_dice 0.3.12 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,77 +1,76 @@
1
- # This class models a variety of game rules that cause dice to be re-rolled.
2
- #
3
- # An object of the class represents a single rule, such as "re-roll a result of 1
4
- # and use the new value".
5
- #
6
- # @example A rule for "exploding" dice
7
- # rule = GamesDice::RerollRule.new( 6, :<=, :reroll_add )
8
- # # Test whether the rule applies . . .
9
- # rule.applies? 4 # => false
10
- # rule.applies? 6 # => true
11
- # rule.type # => :reroll_add
12
- #
13
- # @example A rule for re-rolling and taking best value if first attempt is lower than a threshold
14
- # rule = GamesDice::RerollRule.new( 11, :>, :reroll_use_best, 1 )
15
- # # Test whether the rule applies . . .
16
- # rule.applies? 4 # => true
17
- # rule.applies? 14 # => false
18
- # rule.type # => :reroll_use_best
19
- #
20
-
21
- class GamesDice::RerollRule
22
-
23
- # Creates new instance of GamesDice::RerollRule. The rule will be assessed as
24
- # trigger_value.send( trigger_op, x )
25
- # where x is the Integer value shown on a die.
26
- # @param [Integer,Range<Integer>,Object] trigger_value Any object is allowed, but typically an Integer
27
- # @param [Symbol] trigger_op A method of trigger_value that takes an Integer param and returns Boolean
28
- # @param [Symbol] type The type of reroll
29
- # @param [Integer] limit Maximum number of times this rule can be applied to a single die
30
- # @return [GamesDice::RerollRule]
31
- def initialize trigger_value, trigger_op, type, limit = 1000
32
-
33
- if ! trigger_value.respond_to?( trigger_op )
34
- raise ArgumentError, "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}"
35
- end
36
-
37
- unless GamesDice::REROLL_TYPES.has_key?(type)
38
- raise ArgumentError, "Unrecognised reason for a re-roll #{type}"
39
- end
40
-
41
- @trigger_value = trigger_value
42
- @trigger_op = trigger_op
43
- @type = type
44
- @limit = limit ? Integer( limit ) : 1000
45
- @limit = 1 if @type == :reroll_subtract
46
- end
47
-
48
- # Trigger operation. How the rule is assessed against #trigger_value.
49
- # @return [Symbol] Method name to be sent to #trigger_value
50
- attr_reader :trigger_op
51
-
52
- # Trigger value. An object that will use #trigger_op to assess a die result for a reroll.
53
- # @return [Integer,Range,Object] Object that receives (#trigger_op, die_result)
54
- attr_reader :trigger_value
55
-
56
- # The reroll behaviour that this rule triggers.
57
- # @return [Symbol] A category for the re-roll, declares how it should be processed
58
- # The following values are supported:
59
- # +:reroll_add+:: add result of reroll to running total, and ignore :reroll_subtract for this die
60
- # +reroll_subtract+:: subtract result of reroll from running total, and reverse sense of any further :reroll_add results
61
- # +:reroll_replace+:: use the new value in place of existing value for the die
62
- # +:reroll_use_best+:: use the new value if it is higher than the existing value
63
- # +:reroll_use_worst+:: use the new value if it is higher than the existing value
64
- attr_reader :type
65
-
66
- # Maximum to number of times that this rule can be applied to a single die.
67
- # @return [Integer] A number of rolls.
68
- attr_reader :limit
69
-
70
- # Assesses the rule against a die result value.
71
- # @param [Integer] test_value Value that is result of rolling a single die.
72
- # @return [Boolean] Whether the rule applies.
73
- def applies? test_value
74
- @trigger_value.send( @trigger_op, test_value ) ? true : false
75
- end
76
-
77
- end # class RerollRule
1
+ # frozen_string_literal: true
2
+
3
+ module GamesDice
4
+ # This class models a variety of game rules that cause dice to be re-rolled.
5
+ #
6
+ # An object of the class represents a single rule, such as "re-roll a result of 1
7
+ # and use the new value".
8
+ #
9
+ # @example A rule for "exploding" dice
10
+ # rule = GamesDice::RerollRule.new( 6, :<=, :reroll_add )
11
+ # # Test whether the rule applies . . .
12
+ # rule.applies? 4 # => false
13
+ # rule.applies? 6 # => true
14
+ # rule.type # => :reroll_add
15
+ #
16
+ # @example A rule for re-rolling and taking best value if first attempt is lower than a threshold
17
+ # rule = GamesDice::RerollRule.new( 11, :>, :reroll_use_best, 1 )
18
+ # # Test whether the rule applies . . .
19
+ # rule.applies? 4 # => true
20
+ # rule.applies? 14 # => false
21
+ # rule.type # => :reroll_use_best
22
+ #
23
+ class RerollRule
24
+ # Creates new instance of GamesDice::RerollRule. The rule will be assessed as
25
+ # trigger_value.send( trigger_op, x )
26
+ # where x is the Integer value shown on a die.
27
+ # @param [Integer,Range<Integer>,Object] trigger_value Any object is allowed, but typically an Integer
28
+ # @param [Symbol] trigger_op A method of trigger_value that takes an Integer param and returns Boolean
29
+ # @param [Symbol] type The type of reroll
30
+ # @param [Integer] limit Maximum number of times this rule can be applied to a single die
31
+ # @return [GamesDice::RerollRule]
32
+ def initialize(trigger_value, trigger_op, type, limit = 1000)
33
+ unless trigger_value.respond_to?(trigger_op)
34
+ raise ArgumentError,
35
+ "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}"
36
+ end
37
+
38
+ raise ArgumentError, "Unrecognised reason for a re-roll #{type}" unless GamesDice::REROLL_TYPES.key?(type)
39
+
40
+ @trigger_value = trigger_value
41
+ @trigger_op = trigger_op
42
+ @type = type
43
+ @limit = limit ? Integer(limit) : 1000
44
+ @limit = 1 if @type == :reroll_subtract
45
+ end
46
+
47
+ # Trigger operation. How the rule is assessed against #trigger_value.
48
+ # @return [Symbol] Method name to be sent to #trigger_value
49
+ attr_reader :trigger_op
50
+
51
+ # Trigger value. An object that will use #trigger_op to assess a die result for a reroll.
52
+ # @return [Integer,Range,Object] Object that receives (#trigger_op, die_result)
53
+ attr_reader :trigger_value
54
+
55
+ # The reroll behaviour that this rule triggers.
56
+ # @return [Symbol] A category for the re-roll, declares how it should be processed
57
+ # The following values are supported:
58
+ # +:reroll_add+:: add result of reroll to running total, and ignore :reroll_subtract for this die
59
+ # +reroll_subtract+:: subtract result of reroll from running total, reverse sense of any further :reroll_add results
60
+ # +:reroll_replace+:: use the new value in place of existing value for the die
61
+ # +:reroll_use_best+:: use the new value if it is higher than the existing value
62
+ # +:reroll_use_worst+:: use the new value if it is higher than the existing value
63
+ attr_reader :type
64
+
65
+ # Maximum to number of times that this rule can be applied to a single die.
66
+ # @return [Integer] A number of rolls.
67
+ attr_reader :limit
68
+
69
+ # Assesses the rule against a die result value.
70
+ # @param [Integer] test_value Value that is result of rolling a single die.
71
+ # @return [Boolean] Whether the rule applies.
72
+ def applies?(test_value)
73
+ @trigger_value.send(@trigger_op, test_value) ? true : false
74
+ end
75
+ end
76
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GamesDice
2
- VERSION = "0.3.12"
4
+ VERSION = '0.4.0'
3
5
  end
data/lib/games_dice.rb CHANGED
@@ -1,19 +1,21 @@
1
- require "games_dice/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'games_dice/version'
2
4
  begin
3
- require "games_dice/games_dice"
5
+ require 'games_dice/games_dice'
4
6
  rescue LoadError
5
- require "games_dice/probabilities"
7
+ require 'games_dice/probabilities'
6
8
  end
7
- require "games_dice/constants"
8
- require "games_dice/die"
9
- require "games_dice/die_result"
10
- require "games_dice/reroll_rule"
11
- require "games_dice/map_rule"
12
- require "games_dice/complex_die"
13
- require "games_dice/bunch"
14
- require "games_dice/dice"
15
- require "games_dice/parser"
16
- require "games_dice/marshal"
9
+ require 'games_dice/constants'
10
+ require 'games_dice/die'
11
+ require 'games_dice/die_result'
12
+ require 'games_dice/reroll_rule'
13
+ require 'games_dice/map_rule'
14
+ require 'games_dice/complex_die'
15
+ require 'games_dice/bunch'
16
+ require 'games_dice/dice'
17
+ require 'games_dice/parser'
18
+ require 'games_dice/marshal'
17
19
 
18
20
  module GamesDice
19
21
  # @!visibility private
@@ -24,11 +26,9 @@ module GamesDice
24
26
  # @param [#rand] prng Optional random number generator, default is to use Ruby's built-in #rand()
25
27
  # @return [GamesDice::Dice] A new dice object.
26
28
  #
27
- def self.create dice_description, prng = nil
28
- parsed = @@parser.parse( dice_description )
29
- if prng
30
- parsed[:bunches].each { |bunch| bunch.merge!( :prng => prng ) }
31
- end
32
- GamesDice::Dice.new( parsed[:bunches], parsed[:offset] )
29
+ def self.create(dice_description, prng = nil)
30
+ parsed = @@parser.parse(dice_description)
31
+ parsed[:bunches].each { |bunch| bunch.merge!(prng: prng) } if prng
32
+ GamesDice::Dice.new(parsed[:bunches], parsed[:offset])
33
33
  end
34
34
  end