Skip to content

Instantly share code, notes, and snippets.

@arafatm
Last active August 26, 2015 01:08
Show Gist options
  • Save arafatm/34eebc2da38be746a9e4 to your computer and use it in GitHub Desktop.
Save arafatm/34eebc2da38be746a9e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# USAGE: dnd.rb +0 0d0[+0] [0]
# e.g. dnd.rb +0 2d6
# or dnd.rb +5 2d6+6 2 (for 2 attacks per round e.g. flurry)
if ARGV.length < 2
puts "ARGS = attk dmg"
puts "e.g. +5 2d6+2"
exit
end
atk = ARGV[0]
dmg = ARGV[1]
apr = ARGV[2].to_i # attacks per round
apr = 1 if apr == 0
unless atk =~ /[+-][0-9]+/
puts "Cannot understand atk mod. Try something like +5 or -2"
exit
end
unless dmg =~ /[0-9]+d[0-9]+([+-][0-9]+)*/
puts "Cannot understand dmg mod. Try something like 2d6 or 3d4+6"
exit
end
hitdie = 10.5 + atk.to_f
acs = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
print "AC |"
acs.each do |ac|
print "#{ac.to_s.rjust(6)} |"
end
puts
def hit_prob(atk, ac)
(21 + atk.to_f - ac) / 20
end
print "HIT |"
acs.each do |ac|
hitprob = hit_prob(atk, ac)
if hitprob > 0
print "#{sprintf('%.2f', hitprob).rjust(6)} |"
end
end
puts
dien = dmg[/[0-9]+/].to_f
dice = dmg[/d[0-9]+/][1..-1].to_f
dmod = dmg[/[+-][0-9]+/].to_f
dmg = (dien * (dice.to_i / 2 + 0.5) + dmod) * apr
print "D/R |"
acs.each do |ac|
hitprob = hit_prob(atk, ac)
if hitprob > 0
print "#{sprintf('%-.2f', hitprob*dmg.to_f).rjust(6)} |"
else
print " |"
end
end
puts
puts
puts "On a hit, avg dmg = #{dmg / apr}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment