Skip to content

Instantly share code, notes, and snippets.

@MonkeyIsNull
Last active August 29, 2015 14:10
Show Gist options
  • Save MonkeyIsNull/ce4897ac35a735c45f9e to your computer and use it in GitHub Desktop.
Save MonkeyIsNull/ce4897ac35a735c45f9e to your computer and use it in GitHub Desktop.
War - Battle System
defmodule Die do
def init(), do: :random.seed(:erlang.now)
def d6(), do: :random.uniform(6)
def d100(), do: :random.uniform(100)
end
defmodule War do
def create_player(name, skill, hp) do
[ name: name, skill: skill, hp: hp ]
end
def name(p), do: p[:name]
def skill(p), do: p[:skill]
def hp(p), do: p[:hp]
def to_s(p), do: "Name: #{name(p)} Skill: #{skill(p)} HP: #{hp(p)}"
def pr_hit(p1, p2, roll), do: IO.puts "#{name(p1)} has hit #{name(p2)} with a roll of #{roll}"
def pr_miss(p1, _, roll), do: IO.puts "#{name(p1)} missed with a roll of #{roll}"
def attack([p1, p2]) do
roll = Die.d100()
if roll <= skill(p1) do
pr_hit p1, p2, roll
[p1, p2, true]
else
pr_miss p1, p2, roll
[p1, p2, false]
end
end
def defend([p1, p2, result_att]) do
if result_att do
roll = Die.d100()
if roll <= skill(p2) do
IO.puts "#{name(p2)} has defended with a roll of #{roll}"
[p1, p2, result_att, true]
else
IO.puts "#{name(p2)} has FAILED to defend with a roll of #{roll}"
[p1, p2, result_att, false]
end
else
[p1, p2, result_att, false]
end
end
def calcdmg([p1, p2, result_att, result_dfnd]) do
if result_att and not result_dfnd do
IO.puts "\tCalculating damage"
IO.puts "\t#{name(p2)} was hit!"
dmg = Die.d6()
newHP = hp(p2) - dmg
IO.puts "\tDamage: #{dmg}"
p2 = War.create_player(p2[:name], p2[:skill], newHP)
IO.puts "\tNew charpoints: #{to_s(p2)}"
end
[p2, p1]
end
def is_dead(someone) do
if hp(someone) < 0 do
true
else
false
end
end
def war_loop([p1, p2]) do
if is_dead(p1) or is_dead(p2) do
:ok
else
[p1, p2] |> War.attack |> War.defend |> War.calcdmg |> War.war_loop
end
end
end
Die.init()
conan = War.create_player("Conan", 70, 20)
urg = War.create_player("Urg", 65, 25)
IO.puts War.to_s(conan)
IO.puts War.to_s(urg)
#kickoff the loop
War.war_loop([conan, urg])
IO.puts "The end"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment