DEV Community

Discussion on: Daily Challenge #192 - Can you Survive the Zombies?

Collapse
 
savagepixie profile image
SavagePixie

Something like this should do the trick in Elixir. I'm just doubling the range and subtracting one on each iteration to avoid issues with floats and because working with integers is faster.

defmodule ZombieWars do
  def zombie_shootout(n, r, a), do: _zombie_shootout(n, r * 2, a, n)

  defp _zombie_shootout(0, _, _, s) do
    "You shot all #{s} zombies."
  end
  defp _zombie_shootout(n, 0, _a, s) do
    "You shot #{s - n} zombies before being eaten: overwhelmed."
  end
  defp _zombie_shootout(n, _r, 0, s) do
    "You shot #{s - n} zombies before being eaten: ran out of ammo."
  end
  defp _zombie_shootout(n, r, a, s) do
    if Enum.random(1..20) == 1 do
      _zombie_shootout(n, r - 1, a - 1, s)
    else
      _zombie_shootout(n - 1, r - 1, a - 1, s)
    end
  end
end