Skip to content

Instantly share code, notes, and snippets.

@jubishop
Created January 7, 2009 19:22
Show Gist options
  • Save jubishop/44373 to your computer and use it in GitHub Desktop.
Save jubishop/44373 to your computer and use it in GitHub Desktop.
class RiskProbability
@@odds = {
[1,1] => {[1,0] => 41.67, [0,1] => 58.33},
[2,1] => {[1,0] => 57.87, [0,1] => 42.13},
[3,1] => {[1,0] => 65.97, [0,1] => 34.03},
[1,2] => {[1,0] => 25.46, [0,1] => 74.54},
[2,2] => {[2,0] => 22.76, [0,2] => 44.83, [1,1] => 32.41},
[3,2] => {[2,0] => 37.17, [0,2] => 29.26, [1,1] => 33.58}
}
attr_reader :attackers, :defenders, :probs
def initialize(attackers, defenders)
@probs = Hash.new(0)
@attackers = attackers
@defenders = defenders
calc_probability
end
def calc_probability(attackers=@attackers, defenders=@defenders, cur_prob = 1)
if (attackers == 0 || defenders == 0)
@probs[[attackers, defenders]] += cur_prob
else
cur_attackers = [attackers, 3].min
cur_defenders = [defenders, 2].min
outcomes = @@odds[[cur_attackers, cur_defenders]]
outcomes.each_pair {|outcome, chance|
calc_probability(attackers - outcome[0], defenders - outcome[1],
cur_prob * chance/100)
}
end
end
end
if (ARGV.length != 2)
raise "Usage: ruby probs.rb <attackers> <defenders>"
end
result = RiskProbability.new(ARGV.first.to_i, ARGV.last.to_i)
result.probs.each_pair{|outcome, chance|
puts outcome.join(",") + ": " + chance.to_s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment