


class HiddenHands 

attr :hands, :card_count 

def initialize( left , right, card_count ) 
   @hands = [ left, right ]
   @card_count = card_count 
end


def possibilities 
   pos = (card_count + 1).times.collect{ | left | [ left, card_count - left ]  } 
   pos = pos.reject{ | opt | opt[0] > hands[0].hidden || opt[1] > hands[1].hidden }   
   pos = pos.collect do |opt| 
     card_count = opt[0] + opt[1] 
     slots = hands[0].hidden + hands[1].hidden
   # get count of combinations possible 
     card_combos = permutations(card_count, card_count)
   # allow for spaces in left hand 
     left_free_combos = combinations(hands[0].hidden, opt[0]) 
   # allow for spaces in right hand 
     right_free_combos = combinations(hands[1].hidden, opt[1]) 
     all_combos = card_combos * (left_free_combos * right_free_combos)  
     [opt, all_combos]  
   end 
   tot = pos.inject(0){|sum, opt| sum += opt[1]}
   pos = pos.collect { |opt| opt << (100.0 * opt[1] / tot).round(2)} 

end 


def permutations(n,k) 
     # fact(n)/(fact(k)*fact(n-k))    
     vals = [n,k]
     max = vals.max 
     min = vals.min
     # fact(max)/(fact(min))    
     # Permuatations is cards cmbos * spaces left combos * spaces right combos  
     fact(max)/(fact(max-min))    
end

def combinations(n, k) 
   k > 0 ? fact(n)/(fact(k)*fact(n-k)) : 1 
end  

def fact(i) 
   ret = 1
   i.times{ |n| ret = ret  * (n+1) } 
   ret 
end 


#sample code 

def samples 
  l = Hand.new('Left', 4)  
  l.allocate(2)
  r = Hand.new('Right', 4) 
  hh=HiddenHands.new(l,r,3)   
  hh.possibilities
end 

end 
