class DicePool

Attributes

how_many[RW]
plus[RW]
results[RW]
set[R]
top_number_of_dice[RW]

Public Class Methods

inspect() click to toggle source
# File lib/dicebag.rb, line 137
def self.inspect 
  puts "<DicePool:\n  @set: #{@set.inspect}, @plus: #{plus}, @total: #{total}, @top: #{top_number_of_dice}>"
end
new(set: nil, plus: 0, top: 2) click to toggle source
# File lib/dicebag.rb, line 89
def initialize(set: nil, plus: 0, top: 2)
  @set = set.is_a?(Die) ? [set] : set
  raise 'Dice passed to DicePool must be instances of Die.' unless @set.any?{|s| s.is_a?(Die) }
  @plus = plus
  @how_many = 100_000
  @top_number_of_dice = top
end

Public Instance Methods

generate_results() click to toggle source
# File lib/dicebag.rb, line 111
def generate_results
  @results = Hash.new(0)
  how_many.times { @results[highest(top: top_number_of_dice, re_roll: true)] += 1  }
  self
end
graph(r=@results) click to toggle source
# File lib/dicebag.rb, line 117
def graph(r=@results)
  DiceGraph.new(r, how_many, top_number_of_dice).graph
end
highest(top: top_number_of_dice, re_roll: false) click to toggle source
# File lib/dicebag.rb, line 97
def highest(top: top_number_of_dice, re_roll: false)
  reroll if re_roll
  highest_results = set.sort_by{|d| d.value }.reverse[0..top-1] 
  return highest_results.map(&:to_i).inject(0){|sum,i| sum += i } + plus
end
Also aliased as: result
inspect() click to toggle source
# File lib/dicebag.rb, line 133
def inspect
  puts "<DicePool:\n  @set: #{@set.inspect}, @plus: #{plus}, @total: #{total}, @top: #{top_number_of_dice}>"
end
reroll() click to toggle source
# File lib/dicebag.rb, line 104
def reroll
  set.map(&:reroll)
  self
end
result(top: top_number_of_dice, re_roll: false)
Alias for: highest
roll() click to toggle source
# File lib/dicebag.rb, line 109
def roll; reroll; end
roll_results() click to toggle source
# File lib/dicebag.rb, line 121
def roll_results
  set.inspect
end
to_h() click to toggle source
# File lib/dicebag.rb, line 141
def to_h
  {
    top: top_number_of_dice,
    total: total,
    plus: plus,
    set: @set.map(&:to_h)
  }
end
to_json() click to toggle source
# File lib/dicebag.rb, line 150
def to_json
  to_h.to_json
end
total() click to toggle source
# File lib/dicebag.rb, line 129
def total
  highest(top: top_number_of_dice)
end