Skip to content

Latest commit

 

History

History
184 lines (140 loc) · 3.32 KB

035-pokemon.livemd

File metadata and controls

184 lines (140 loc) · 3.32 KB

35 - Batalla Pokémon

BATALLA POKÉMON

Enunciado

Crea un programa que calcule el daño de un ataque durante una batalla Pokémon.

  • La fórmula será la siguiente: daño = 50 * (ataque / defensa) * efectividad
  • Efectividad: x2 (súper efectivo), x1 (neutral), x0.5 (no es muy efectivo)
  • Sólo hay 4 tipos de Pokémon: Agua, Fuego, Planta y Eléctrico (buscar su efectividad)
  • El programa recibe los siguientes parámetros:
    • Tipo del Pokémon atacante.
    • Tipo del Pokémon defensor.
    • Ataque: Entre 1 y 100.
    • Defensa: Entre 1 y 100.

Solución

defmodule TypeIds do
  def water do
    0
  end

  def fire do
    1
  end

  def plant do
    2
  end

  def electric do
    3
  end
end
{:module, TypeIds, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:electric, 0}}
defmodule Type do
  defstruct ~w(id weakness vulnerable)a

  def water do
    %__MODULE__{
      id: TypeIds.water(),
      weakness: [TypeIds.water()],
      vulnerable: [TypeIds.electric()]
    }
  end

  def fire do
    %__MODULE__{id: TypeIds.fire(), weakness: [TypeIds.fire()], vulnerable: [TypeIds.water()]}
  end

  def plant do
    %__MODULE__{id: TypeIds.plant(), weakness: [TypeIds.plant()], vulnerable: [TypeIds.fire()]}
  end

  def electric do
    %__MODULE__{
      id: TypeIds.electric(),
      weakness: [TypeIds.electric()],
      vulnerable: [TypeIds.plant()]
    }
  end
end
{:module, Type, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:electric, 0}}
defmodule Effectivity do
  @supereffective 2
  @effective 1
  @noteffective 0.5

  def get(attacker, opponent) do
    cond do
      Enum.member?(opponent.vulnerable, attacker.id) -> @supereffective
      Enum.member?(opponent.weakness, attacker.id) -> @effective
      true -> @noteffective
    end
  end
end
{:module, Effectivity, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:get, 2}}
defmodule Attack do
  @amount 50
  def damage(attacker, opponent) do
    @amount * (attacker.power / opponent.power) * Effectivity.get(attacker.type, opponent.type)
  end
end
{:module, Attack, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:damage, 2}}
defmodule Pokemon do
  defstruct ~w(type power)a

  def new(type, power) do
    %__MODULE__{type: type, power: power}
  end

  def water(power) do
    new(Type.water(), power)
  end

  def fire(power) do
    new(Type.fire(), power)
  end

  def plant(power) do
    new(Type.plant(), power)
  end

  def electric(power) do
    new(Type.electric(), power)
  end
end
{:module, Pokemon, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:electric, 1}}
defmodule Solution do
  def run() do
    Attack.damage(Pokemon.water(10), Pokemon.plant(20))
  end
end
{:module, Solution, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:run, 0}}
Solution.run()
12.5