Class: PfrpgCore::Dice
- Inherits:
-
Object
- Object
- PfrpgCore::Dice
- Defined in:
- lib/pfrpg_utility/dice.rb
Overview
Dice class. It represents one or more homogeneous dice, with an optional integer increment and an optional integer multiplier. It is best used in conjunction with the Integer methods added at the bottom of this file.
4.d6 # => Dice.new(4, 6)
4.d6 + 2 # => Dice.new(4, 6) + 2
# => Dice.new(4, 6, 2)
4.d6 * 10 # => Dice.new(4, 6) * 10
# => Dice.new(4, 6, 0, 10)
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#increment ⇒ Object
readonly
Returns the value of attribute increment.
-
#multiplier ⇒ Object
readonly
Returns the value of attribute multiplier.
-
#sides ⇒ Object
readonly
Returns the value of attribute sides.
Instance Method Summary collapse
-
#*(n) ⇒ Object
Return a new Dice object, with the given multiplier.
-
#+(n) ⇒ Object
Return a new Dice object, with the given increment.
-
#-(n) ⇒ Object
Same as adding a negative increment.
-
#average ⇒ Object
Calculates and returns the average value (as a float) returned by rolling this dice instance.
-
#best(n, collect = false) ⇒ Object
Return the best
nof the dice rolled. -
#initialize(count, sides, increment = 0, multiplier = 1) ⇒ Dice
constructor
A new instance of Dice.
-
#max ⇒ Object
Returns the highest possible integer that may be returned by rolling this dice instance.
-
#min ⇒ Object
Returns the lowest possible integer that may be returned by rolling this dice instance.
-
#roll(collect = false) ⇒ Object
(also: #to_i)
Roll the dice.
-
#to_a ⇒ Object
Same as calling #roll with a non-false parameter.
-
#to_s ⇒ Object
(also: #inspect)
Returns a familiar representation of the dice instance.
Constructor Details
#initialize(count, sides, increment = 0, multiplier = 1) ⇒ Dice
Returns a new instance of Dice.
40 41 42 43 |
# File 'lib/pfrpg_utility/dice.rb', line 40 def initialize(count, sides, increment=0, multiplier=1) @sides, @count = sides, count @increment, @multiplier = increment, multiplier end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
38 39 40 |
# File 'lib/pfrpg_utility/dice.rb', line 38 def count @count end |
#increment ⇒ Object (readonly)
Returns the value of attribute increment.
38 39 40 |
# File 'lib/pfrpg_utility/dice.rb', line 38 def increment @increment end |
#multiplier ⇒ Object (readonly)
Returns the value of attribute multiplier.
38 39 40 |
# File 'lib/pfrpg_utility/dice.rb', line 38 def multiplier @multiplier end |
#sides ⇒ Object (readonly)
Returns the value of attribute sides.
38 39 40 |
# File 'lib/pfrpg_utility/dice.rb', line 38 def sides @sides end |
Instance Method Details
#*(n) ⇒ Object
Return a new Dice object, with the given multiplier. If the dice already have a multipler, it accumulates.
47 48 49 |
# File 'lib/pfrpg_utility/dice.rb', line 47 def *(n) PfrpgCore::Dice.new(count, sides, increment, multiplier * n) end |
#+(n) ⇒ Object
Return a new Dice object, with the given increment. If the dice already have an increment, it accumulates.
53 54 55 |
# File 'lib/pfrpg_utility/dice.rb', line 53 def +(n) PfrpgCore::Dice.new(count, sides, increment+n, multiplier) end |
#-(n) ⇒ Object
Same as adding a negative increment.
58 59 60 |
# File 'lib/pfrpg_utility/dice.rb', line 58 def -(n) self.+(-n) end |
#average ⇒ Object
Calculates and returns the average value (as a float) returned by rolling this dice instance.
104 105 106 |
# File 'lib/pfrpg_utility/dice.rb', line 104 def average (max + min) / 2.0 end |
#best(n, collect = false) ⇒ Object
Return the best n of the dice rolled. If collect is false (the default) an integer is returned, otherwise an array of the best rolls is returned.
85 86 87 88 |
# File 'lib/pfrpg_utility/dice.rb', line 85 def best(n, collect=false) list = to_a.sort.last(n) collect ? list : list.inject(0) { |s,v| s + v } end |
#max ⇒ Object
Returns the highest possible integer that may be returned by rolling this dice instance.
92 93 94 |
# File 'lib/pfrpg_utility/dice.rb', line 92 def max (count * sides + increment) * multiplier end |
#min ⇒ Object
Returns the lowest possible integer that may be returned by rolling this dice instance.
98 99 100 |
# File 'lib/pfrpg_utility/dice.rb', line 98 def min (count + increment) * multiplier end |
#roll(collect = false) ⇒ Object Also known as: to_i
Roll the dice. If collect is false (the default), returns an integer. Otherwise, it returns an array with one element for each rolled die.
Note that the multipler (if any) is applied to each die, while the increment is applied at the end. If an array is returned, the increment (times the multiplier) will be the last element of the array.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pfrpg_utility/dice.rb', line 68 def roll(collect=false) result = collect ? [] : 0 count.times do roll = (rand(sides) + 1) * multiplier result = result.send(collect ? :push : :+, roll) end if increment != 0 result = result.send(collect ? :push : :+, increment * multiplier) end result end |
#to_a ⇒ Object
Same as calling #roll with a non-false parameter.
109 110 111 |
# File 'lib/pfrpg_utility/dice.rb', line 109 def to_a roll(true) end |
#to_s ⇒ Object Also known as: inspect
Returns a familiar representation of the dice instance.
114 115 116 117 118 119 |
# File 'lib/pfrpg_utility/dice.rb', line 114 def to_s s = "#{count}d#{sides}" s << "%+d" % increment if increment != 0 s << "*%d" % multiplier if multiplier != 1 s end |