Created
April 14, 2016 17:51
-
-
Save jamie/4e0561e1a31d88312d5482ce3138ecbb to your computer and use it in GitHub Desktop.
Simple math simulation for risk legacy combat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
require 'csv' | |
def attack(attackers, defenders, defending=2) | |
while defenders > 0 and attackers > 1 | |
this_attack = [attackers-1, 3].min | |
this_defense = [defenders, defending].min | |
attack = ([0]*this_attack).map{rand(6)+1}.sort | |
defense = ([0]*this_defense).map{rand(6)+1}.sort | |
# Bunker | |
#defense[-1] = [defense[-1]+1, 6].min | |
# Ammo Shortage | |
#defense[-1] = [defense[-1]-1, 1].max | |
if attack.pop > defense.pop | |
defenders -= 1 | |
else | |
attackers -= 1 | |
end | |
if attack.any? and defense.any? | |
if (attack.pop > defense.pop) | |
defenders -= 1 | |
else | |
attackers -= 1 | |
end | |
end | |
end | |
[attackers, defenders] | |
end | |
def simulate(n, a, d, s) | |
results = Hash.new{|h,k| h[k] = 0} | |
n.times do | |
result = attack(a,d,s) | |
results[result] += 1 | |
end | |
report(results) | |
end | |
def report(results) | |
keys = results.keys.sort_by{|key| [key[0], -key[1]] } | |
max = results.values.max.to_f | |
keys.each do |key| | |
scale = (results[key] / max) * 50 | |
#puts "%2d %2d: %5d %s" % [key.first, key.last, results[key], '#' * scale] | |
puts [key.first, key.last, results[key]].to_csv | |
end | |
puts | |
end | |
simulate(100_000, 15, 5, 2) | |
simulate(100_000, 15, 5, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment