class Fortune::Odds

win

Attributes

k[RW]
p[RW]
s[RW]
type[RW]

Public Class Methods

equal() click to toggle source
# File lib/fortune/odds.rb, line 87
def self.equal
  Odds.new({:s => 1, :k => 1})
end
human(h) click to toggle source
# File lib/fortune/odds.rb, line 91
def self.human(h)
  Odds.p_human(h).values
end
new(h = {}) click to toggle source
# File lib/fortune/odds.rb, line 8
def initialize(h = {})
  # TODO: check p or (s and k) exists
  h[:s] ||= h[:win]
  h[:k] ||= h[:lose]
  [:win, :lose].each{|k| h.delete(k)}

  h.each{|k,v| instance_variable_set("@#{k}", v)}
  self.p = P.new(s, s + k).value unless self.p
  self.calc_s_k if !self.k || !self.s

  self.to_win
end

Private Class Methods

add_fractions(p_odds, h) click to toggle source
# File lib/fortune/odds.rb, line 130
def self.add_fractions(p_odds, h)
  p_odds_addons = {}
  p_odds.each{|key, odd|
    next if odd.k == h[:k]
    Odds.fillup_p_human(p_odds_addons, odd.s, odd.k + 0.5)
  }
  p_odds_addons.merge(p_odds)
end
fillup_p_human(p_odds, s, k) click to toggle source
# File lib/fortune/odds.rb, line 123
def self.fillup_p_human(p_odds, s, k)
  odd = Odds.new({:s => s, :k => k})
  odd_dup = odd.dup.revert
  p_odds[odd.p] = odd if not p_odds.has_key?(odd.p)
  p_odds[odd_dup.p] = odd_dup if not p_odds.has_key?(odd_dup.p)
end
p_human(h) click to toggle source
# File lib/fortune/odds.rb, line 112
def self.p_human(h)
  p_odds = {Odds.equal.p => Odds.equal}
  (1..h[:k]).each{|k|
    (0..k-1).each{|s|
      Odds.fillup_p_human(p_odds, s, k)
    }
  }

  h[:fractions] ? Odds.add_fractions(p_odds, h) : p_odds
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/fortune/odds.rb, line 21
def <=>(other)
  if self.p < other.p
    -1
  elsif self.p > other.p
    1
  else
    0
  end
end
is_on_lose?() click to toggle source
# File lib/fortune/odds.rb, line 61
def is_on_lose?
  self.type == :on_lose
end
is_on_win?() click to toggle source
# File lib/fortune/odds.rb, line 57
def is_on_win?
  self.type == :on_win
end
n_all() click to toggle source
# File lib/fortune/odds.rb, line 36
def n_all
  s + k
end
Also aliased as: variants
p_obj() click to toggle source
# File lib/fortune/odds.rb, line 99
def p_obj
  P.new(self.p)
end
revert() click to toggle source
# File lib/fortune/odds.rb, line 81
def revert
  self.s_to_k
  self.p = 1 - self.p
  self
end
s_to_k() click to toggle source
# File lib/fortune/odds.rb, line 71
def s_to_k
  self.s, self.k = self.k, self.s
end
to_human(h = {:k => 7}) click to toggle source

h = {:k => int, [:fractions => true]}

# File lib/fortune/odds.rb, line 42
def to_human(h = {:k => 7})
  min_delta = nil
  closest_odd = nil

  Odds.human(h).each{|odd|
    delta = (self.p - odd.p).abs
    if !min_delta || delta < min_delta
      min_delta = delta
      closest_odd = odd
    end
  }

  closest_odd
end
to_i() click to toggle source
# File lib/fortune/odds.rb, line 31
def to_i
  self.s, self.k = self.s.to_i, self.k.to_i
  self
end
to_lose() click to toggle source
# File lib/fortune/odds.rb, line 75
def to_lose
  self.s_to_k if self.is_on_win?
  self.type = :on_lose
  self
end
to_s() click to toggle source
# File lib/fortune/odds.rb, line 95
def to_s
  "#{self.s}:#{self.k} #{self.type} (p: #{p_obj.to_percent_string})"
end
to_win() click to toggle source
# File lib/fortune/odds.rb, line 65
def to_win
  self.s_to_k if self.is_on_lose?
  self.type = :on_win
  self
end
variants()
Alias for: n_all

Protected Instance Methods

calc_s_k() click to toggle source
# File lib/fortune/odds.rb, line 105
def calc_s_k
  self.s = 1
  self.k = s.to_f/p.to_f - s.to_f
end