#!/usr/bin/env ruby
# LICENSE: This code is freeware.  You may use this code for any purpose you
# like so long as this license is attached.  If this code is used in a
# commercial application, you must send me an email so I feel uber-geeky.  No
# other compensation is required.  Email me at jechols@nerdbucket.com

require './HypergeometricProbabilities'

def print_basic_help
  puts %Q{
Math: http://en.wikipedia.org/wiki/Hypergeometric_distribution

Calculate permutations for hypergeometric distributions, such as keno, where
you can't pick the same number twice and order of numbers isn't important
(i.e., 123 is exactly the same as 213 or 321, and 113 isn\'t valid because
it has '1' twice), and the computer picks more items than the player.
  }.strip
end

def print_extended_help
  puts ''
  puts %Q{
To get Keno odds for a 10-spot game where range is 80 and computer picks 20:
  ruby hypergeometric_calc.rb 80 20 10

To get state lottery-style odds where range is 49 and you pick 6:
  ruby hypergeometric_calc.rb 49 6 6

To get hold \'em odds where you have a pocket pair and are looking for trips
or quads on the flop (range is 50 cards left, deal is 3, player needs
1 for trips, 2 for quads):
  ruby hypergeometric_calc.rb 50 3 2

Algorithm is based on the Urn Problem - there are black and white marbles in
an urn.  White marbles represent a "win".  Some number are chosen and set
aside.  Given a marble count, a number of picks, and the number of white
marbles, we compute the odds for getting all possibilities of white/black
combinations.
  }.strip
end

def print_syntax
  puts ''
  puts %Q{
syntax: ruby hypergeometric_calc.rb [marbles] [picks] [white]
        ruby hypergeometric_calc.rb [-h | -hh] for help
  }
end

# Figure out if the user needs help and display it if so
help = ARGV.include?('-h')
verbose_help = ARGV.include?('-hh')
help = true if ARGV.length != 3
if help || verbose_help
  print_basic_help
  print_extended_help if verbose_help
  print_syntax
  exit
end

# Grab arguments
marbles   = ARGV[0].to_i
picks     = ARGV[1].to_i
white     = ARGV[2].to_i

if marbles < picks
  puts "Marbles (#{marbles}) must be >= picks (#{picks})"
end

if marbles < white
  puts "Marbles (#{marbles}) must be >= to white marbles (#{white})"
end

# Build our object and pollute it with yummy data
hypercalc = HypergeometricProbabilities.new(
  :marbles  => marbles,
  :picks    => picks,
  :white    => white
)

# Get total permutations for various things.
white_perms   = hypercalc.get_white_permutations
picked_perms  = hypercalc.get_pick_permutations

puts "Number of ways to arrange white marbles: #{white_perms}"
puts "Number of ways to pick marbles: #{picked_perms}"

puts ''
puts "Number matched              Odds against        Probability (%)"

# Get odds for all matches and iterate
hypercalc.get_all_odds do |matched, probability|
  odds = 'N/A'
  if probability > 0
    odds = ((((1.0 / probability) + 0.09) * 10).to_i / 10.0).to_f
  end

  puts sprintf('%-28d', matched) + sprintf('%-20s', "#{odds}:1") + "#{probability * 100}"
end
