class Dice::PolyhedronSet

Class represents a PolyhedronsSet

Public Class Methods

new(arr) click to toggle source
# File lib/theory_of_probability.rb, line 106
def initialize(arr)
  @pons = parse_pons(arr).sort_by{|item| -item.csides}
  @percentage = count_chance_sum
end

Public Instance Methods

make_graph_by_plotter(x = percentage.size * 10, y = percentage.size * 10) click to toggle source

creating a graph representing chances of getting points

# File lib/theory_of_probability.rb, line 138
def make_graph_by_plotter(x = percentage.size * 10, y = percentage.size * 10)
  filename = 'tmp/percentage.png'
  File.delete(filename) if File.exist?(filename)
  plotter = Image.new(x, y)
  plotter.bar_chart(percentage, 1, color('red @ 1.0'))
  plotter.export(filename)
end
percentage() click to toggle source

hash with chances of getting definite score

# File lib/theory_of_probability.rb, line 112
def percentage
  @percentage
end
throw() click to toggle source

ability to throw a polyhedron set using hash of chances

# File lib/theory_of_probability.rb, line 124
def throw
  sum = 0
  r = rand
  @percentage.each do |item|
    sum += item[1]
    if sum >= r
      item[0]
      break
    end
  end
end
to_s() click to toggle source

returns array of polyhedrons

# File lib/theory_of_probability.rb, line 118
def to_s
  @pons.map {|item| item.to_s}
end

Private Instance Methods

count_chance_sum() click to toggle source
# File lib/theory_of_probability.rb, line 195
def count_chance_sum
  h = {}
  @pons[0].sides.each { |item| h[item] = 1 }
  arr3 = @pons[0].sides
  for i in 1..@pons.size - 1
    arr1 = arr3
    arr3 = []
    arr2 = @pons[i].sides
    h1 = count_chance_sum_chances_step(arr1, arr2, arr3, h)
    h = h1
  end
  res = {}
  fchance = @pons.inject(1) { |mult, item| mult * item.csides }
  arr3.each {|item| res[item] = Float(h[item]) / fchance}
  res
end
count_chance_sum_chances_step(arr1, arr2, arr3, h) click to toggle source
# File lib/theory_of_probability.rb, line 160
def count_chance_sum_chances_step(arr1, arr2, arr3, h)
  n = 0
  m = 0
  sum = 0
  q = Queue.new
  h1 = {}
  while m < arr2.size
    sum = m_0([sum, n, m], q, h, arr1)
    sum -= q.pop if q.size > arr2.size || m > 0
    h1[arr1[n] + arr2[m]] = sum
    arr3 << (arr1[n] + arr2[m])
    nmarr = n_less_arr1_size(n, arr1, m)
    n, m = nmarr[0], nmarr[1]
  end
  h1
end
m_0(arr, q, h, arr1) click to toggle source
# File lib/theory_of_probability.rb, line 177
def m_0(arr, q, h, arr1)
  if arr[2] == 0
    a = h[arr1[arr[1]]]
    q << a
    arr[0] += a
  end
  arr[0]
end
n_less_arr1_size(n, arr1, m) click to toggle source
# File lib/theory_of_probability.rb, line 186
def n_less_arr1_size(n, arr1, m)
  if n < arr1.size - 1
    n += 1
  else
    m += 1
  end
  [n,m]
end
parse_pons(arr) click to toggle source
# File lib/theory_of_probability.rb, line 148
def parse_pons(arr)
  res = []
  arr.each do |item|
    if item.is_a?(Integer) or item.is_a?(Array)
      res << Polyhedron.new(item)
    elsif item.is_a?(Polyhedron)
      res << item
    end
  end
  res
end