DEV Community

Discussion on: Daily Challenge #170 - Pokemon Damage Calculator

Collapse
 
savagepixie profile image
SavagePixie • Edited

Pattern matching for the win!

I didn't know what to do with the base_power thing, so I left it as one.

Elixir

defmodule Pokemon do
  def calculate(effectiveness, attack, defence) do
    1 * (attack / defence) * effectiveness
  end

  def total_damage(a, a, att, def), do: calculate(0.5, att, def)
  def total_damage("electric", "water", att, def), do: calculate(2, att, def)
  def total_damage("fire", "grass", att, def), do: calculate(2, att, def)
  def total_damage("fire", "water", att, def), do: calculate(0.5, att, def)
  def total_damage("grass", "fire", att, def), do: calculate(0.5, att, def)
  def total_damage("grass", "water", att, def), do: calculate(2, att, def)
  def total_damage("water", "electric", att, def), do: calculate(0.5, att, def)
  def total_damage("water", "fire", att, def), do: calculate(2, att, def)
  def total_damage("water", "grass", att, def), do: calculate(0.5, att, def)
  def total_damage(_, _, att, def), do: calculate(1, att, def)
end