games_dice 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/games_dice.rb +5 -0
- data/lib/games_dice/constants.rb +1 -1
- data/lib/games_dice/map_rule.rb +40 -14
- data/lib/games_dice/reroll_rule.rb +47 -17
- data/lib/games_dice/version.rb +1 -1
- metadata +4 -4
data/lib/games_dice.rb
CHANGED
@@ -14,6 +14,11 @@ module GamesDice
|
|
14
14
|
# @!visibility private
|
15
15
|
@@parser = GamesDice::Parser.new
|
16
16
|
|
17
|
+
# Creates an instance of GamesDice::Dice from a string description.
|
18
|
+
# @param [String] dice_description Uses a variation of common game notation, examples: '1d6', '3d8+1d4+7', '5d10k2'
|
19
|
+
# @param [#rand] prng Optional random number generator, default is to use Ruby's built-in #rand()
|
20
|
+
# @return [GamesDice::Dice] A new dice object.
|
21
|
+
#
|
17
22
|
def self.create dice_description, prng = nil
|
18
23
|
parsed = @@parser.parse( dice_description )
|
19
24
|
GamesDice::Dice.new( parsed[:bunches], parsed[:offset], prng )
|
data/lib/games_dice/constants.rb
CHANGED
data/lib/games_dice/map_rule.rb
CHANGED
@@ -1,8 +1,32 @@
|
|
1
|
-
#
|
1
|
+
# This class models rules that convert numbers shown on a die to values used in a game. A
|
2
|
+
# common use for this is to count "successes" - dice that score a certain number or higher.
|
3
|
+
#
|
4
|
+
# An object of the class represents a single rule, such as "count a die result of 5 or more as 1
|
5
|
+
# _success_".
|
6
|
+
#
|
7
|
+
# @example A rule for counting successes
|
8
|
+
# rule = GamesDice::MapRule.new( 6, :<=, 1, 'Success' )
|
9
|
+
# # Test how the rule applies . . .
|
10
|
+
# rule.map_from 4 # => nil
|
11
|
+
# rule.map_from 6 # => 1
|
12
|
+
#
|
13
|
+
# @example A rule for counting "fumbles" which reduce total successes
|
14
|
+
# rule = GamesDice::MapRule.new( 1, :==, -1, 'Fumble' )
|
15
|
+
# # Test how the rule applies . . .
|
16
|
+
# rule.map_from 7 # => nil
|
17
|
+
# rule.map_from 1 # => -1
|
18
|
+
#
|
19
|
+
|
2
20
|
class GamesDice::MapRule
|
3
21
|
|
4
|
-
#
|
5
|
-
#
|
22
|
+
# Creates new instance of GamesDice::MapRule. The rule will be assessed as
|
23
|
+
# trigger_value.send( trigger_op, x )
|
24
|
+
# where x is the Integer value shown on a die.
|
25
|
+
# @param [Integer,Range<Integer>,Object] trigger_value Any object is allowed, but typically an Integer
|
26
|
+
# @param [Symbol] trigger_op A method of trigger_value that takes an Integer param and returns Boolean
|
27
|
+
# @param [Integer] mapped_value The value to use in place of the trigger value
|
28
|
+
# @param [String] mapped_name Name of mapped value, for use in descriptions
|
29
|
+
# @return [GamesDice::MapRule]
|
6
30
|
def initialize trigger_value, trigger_op, mapped_value=0, mapped_name=''
|
7
31
|
|
8
32
|
if ! trigger_value.respond_to?( trigger_op )
|
@@ -16,23 +40,25 @@ class GamesDice::MapRule
|
|
16
40
|
@mapped_name = mapped_name.to_s
|
17
41
|
end
|
18
42
|
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
# a valid symbol for a method, which will be called against #trigger_value with the current
|
23
|
-
# die result as a param. If the operator returns true for a specific die result, then the
|
24
|
-
# mapped_value will be used in its stead. If the operator returns nil or false, the map is not
|
25
|
-
# triggered. All other values will be returned as the result of the map (allowing you to
|
26
|
-
# specify any method that takes an integer as input and returns something else as the end result)
|
43
|
+
# Trigger operation. How the rule is assessed against #trigger_value.
|
44
|
+
# @return [Symbol] Method name to be sent to #trigger_value
|
27
45
|
attr_reader :trigger_op
|
28
46
|
|
29
|
-
#
|
47
|
+
# Trigger value. An object that will use #trigger_op to assess a die result for a reroll.
|
48
|
+
# @return [Integer,Range,Object] Object that receives (#trigger_op, die_result)
|
49
|
+
attr_reader :trigger_value
|
50
|
+
|
51
|
+
# Mapped value.
|
52
|
+
# @return [Integer]
|
30
53
|
attr_reader :mapped_value
|
31
54
|
|
32
|
-
#
|
55
|
+
# Name for mapped value.
|
56
|
+
# @return [String]
|
33
57
|
attr_reader :mapped_name
|
34
58
|
|
35
|
-
#
|
59
|
+
# Assesses the rule against a die result value.
|
60
|
+
# @param [Integer] test_value Value that is result of rolling a single die.
|
61
|
+
# @return [Integer,nil] Replacement value, or nil if this rule doesn't apply
|
36
62
|
def map_from test_value
|
37
63
|
op_result = @trigger_value.send( @trigger_op, test_value )
|
38
64
|
return nil unless op_result
|
@@ -1,9 +1,34 @@
|
|
1
|
-
#
|
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
|
+
|
2
21
|
class GamesDice::RerollRule
|
3
22
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
|
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
|
7
32
|
|
8
33
|
if ! trigger_value.respond_to?( trigger_op )
|
9
34
|
raise ArgumentError, "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}"
|
@@ -16,30 +41,35 @@ class GamesDice::RerollRule
|
|
16
41
|
@trigger_value = trigger_value
|
17
42
|
@trigger_op = trigger_op
|
18
43
|
@type = type
|
19
|
-
@limit = limit ? Integer(limit) : 1000
|
44
|
+
@limit = limit ? Integer( limit ) : 1000
|
20
45
|
@limit = 1 if @type == :reroll_subtract
|
21
46
|
end
|
22
47
|
|
23
|
-
#
|
24
|
-
#
|
48
|
+
# Trigger operation. How the rule is assessed against #trigger_value.
|
49
|
+
# @return [Symbol] Method name to be sent to #trigger_value
|
25
50
|
attr_reader :trigger_op
|
26
51
|
|
27
|
-
#
|
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)
|
28
54
|
attr_reader :trigger_value
|
29
55
|
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
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
|
36
64
|
attr_reader :type
|
37
65
|
|
38
|
-
#
|
39
|
-
#
|
66
|
+
# Maximum to number of times that this rule can be applied to a single die.
|
67
|
+
# @return [Integer] A number of rolls.
|
40
68
|
attr_reader :limit
|
41
69
|
|
42
|
-
#
|
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.
|
43
73
|
def applies? test_value
|
44
74
|
@trigger_value.send( @trigger_op, test_value ) ? true : false
|
45
75
|
end
|
data/lib/games_dice/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: games_dice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -144,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
segments:
|
146
146
|
- 0
|
147
|
-
hash: -
|
147
|
+
hash: -2482711703426352461
|
148
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
149
|
none: false
|
150
150
|
requirements:
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
segments:
|
155
155
|
- 0
|
156
|
-
hash: -
|
156
|
+
hash: -2482711703426352461
|
157
157
|
requirements: []
|
158
158
|
rubyforge_project:
|
159
159
|
rubygems_version: 1.8.24
|