Class: Pbw::Utils::Dice
- Inherits:
-
Object
- Object
- Pbw::Utils::Dice
- Includes:
- Comparable
- Defined in:
- lib/pbw/utils/dice.rb
Instance Attribute Summary collapse
-
#d ⇒ Object
Returns the value of attribute d.
-
#m ⇒ Object
Returns the value of attribute m.
-
#n ⇒ Object
Returns the value of attribute n.
Class Method Summary collapse
Instance Method Summary collapse
- #+(d) ⇒ Object
- #-(d) ⇒ Object
- #<(d) ⇒ Object
- #<=>(d) ⇒ Object
- #==(d) ⇒ Object
- #>(d) ⇒ Object
- #average ⇒ Object
- #blank? ⇒ Boolean
-
#initialize(n = 0, d = 0, m = 0) ⇒ Dice
constructor
A new instance of Dice.
- #max ⇒ Object
- #min ⇒ Object
- #mongoize ⇒ Object
- #probabilities ⇒ Object
- #roll ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(n = 0, d = 0, m = 0) ⇒ Dice
Returns a new instance of Dice.
25 26 27 28 29 |
# File 'lib/pbw/utils/dice.rb', line 25 def initialize(n = 0, d = 0, m = 0) @n = n @d = d @m = m end |
Instance Attribute Details
#d ⇒ Object
Returns the value of attribute d.
5 6 7 |
# File 'lib/pbw/utils/dice.rb', line 5 def d @d end |
#m ⇒ Object
Returns the value of attribute m.
5 6 7 |
# File 'lib/pbw/utils/dice.rb', line 5 def m @m end |
#n ⇒ Object
Returns the value of attribute n.
5 6 7 |
# File 'lib/pbw/utils/dice.rb', line 5 def n @n end |
Class Method Details
.demongoize(object) ⇒ Object
35 36 37 |
# File 'lib/pbw/utils/dice.rb', line 35 def self.demongoize(object) read(object.to_s) end |
.evolve(object) ⇒ Object
43 44 45 |
# File 'lib/pbw/utils/dice.rb', line 43 def self.evolve(object) object.mongoize if object.is_a?(::Pbw::Utils::Dice) end |
.mongoize(object) ⇒ Object
39 40 41 |
# File 'lib/pbw/utils/dice.rb', line 39 def self.mongoize(object) object.mongoize if object.is_a?(::Pbw::Utils::Dice) end |
.read(s) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pbw/utils/dice.rb', line 7 def self.read(s) return nil if s.blank? begin a = s.include?("+") ? s.split("+") : (s.include?("-") ? s.split("-") : [s]) b = a[0].split("d") n = b[0].to_i d = b[1].to_i if a.length > 1 m = s.include?("+") ? m = a[1].to_i : m = 0 - a[1].to_i else m = 0 end ::Pbw::Utils::Dice.new(n,d,m) rescue raise "Invalid dice notation" end end |
Instance Method Details
#+(d) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pbw/utils/dice.rb', line 75 def +(d) if d.respond_to?(:roll) if d.d == self.d ::Pbw::Utils::Dice.new((self.n + d.n), self.d, (self.m + d.m)) else ::Pbw::Utils::Dice.new((self.n), self.d, (self.m + d.average)) end else ::Pbw::Utils::Dice.new((self.n), self.d, (self.m + d)) end end |
#-(d) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pbw/utils/dice.rb', line 87 def -(d) if d.respond_to?(:roll) if d.d == self.d ::Pbw::Utils::Dice.new((self.n - d.n), self.d, (self.m - d.m)) else ::Pbw::Utils::Dice.new((self.n), self.d, (self.m - d.average)) end else ::Pbw::Utils::Dice.new((self.n), self.d, (self.m - d)) end end |
#<(d) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/pbw/utils/dice.rb', line 51 def <(d) if d.is_a?(Dice) (self.average < d.average) else (self.average < d.to_f) end end |
#<=>(d) ⇒ Object
71 72 73 |
# File 'lib/pbw/utils/dice.rb', line 71 def <=>(d) self.average <=> d.average end |
#==(d) ⇒ Object
67 68 69 |
# File 'lib/pbw/utils/dice.rb', line 67 def ==(d) (self.d == d.d) && (self.n == d.n) && (self.m == d.m) end |
#>(d) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/pbw/utils/dice.rb', line 59 def >(d) if d.is_a?(Dice) (self.average > d.average) else (self.average > d.to_f) end end |
#average ⇒ Object
99 100 101 102 |
# File 'lib/pbw/utils/dice.rb', line 99 def average apd = sum_possibilities / d.to_f ((apd * n) + m) end |
#blank? ⇒ Boolean
47 48 49 |
# File 'lib/pbw/utils/dice.rb', line 47 def blank? n == 0 || d == 0 end |
#max ⇒ Object
104 105 106 |
# File 'lib/pbw/utils/dice.rb', line 104 def max (n * d) + m end |
#min ⇒ Object
108 109 110 |
# File 'lib/pbw/utils/dice.rb', line 108 def min n + m end |
#mongoize ⇒ Object
31 32 33 |
# File 'lib/pbw/utils/dice.rb', line 31 def mongoize to_s end |
#probabilities ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/pbw/utils/dice.rb', line 122 def probabilities return @probabilities if @probabilities @probabilities = {} min = n max = n * d combinations = 1 * d**n polys = [] (1..n).each do |r| polys.push(::Pbw::Utils::Polynomial.mkroll(d)) end biggun = ::Pbw::Utils::Polynomial.new([1]) #identity polys.each do |p| biggun *= p end (min..max).each do |deg| coeff = biggun.coefficients[deg] if !(coeff.nil? or coeff == 0) px = coeff.to_f / combinations.to_f @probabilities[(deg + m)] = px end end @probabilities end |
#roll ⇒ Object
112 113 114 115 116 |
# File 'lib/pbw/utils/dice.rb', line 112 def roll t = 0 n.times{ t += (rand(d) + 1)} t + m end |
#to_s ⇒ Object
118 119 120 |
# File 'lib/pbw/utils/dice.rb', line 118 def to_s m == 0 ? "#{self.n}d#{self.d}" : (m > 0 ? "#{self.n}d#{self.d}+#{self.m}" : "#{self.n}d#{self.d}-#{self.m.abs}") end |