Powered by AppSignal & Oban Pro

Pokemon Battle

exercises/pokemon_battle.livemd

Pokemon Battle

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue RPG DialogueRanges

Overview

You’re going to create a pokemon battle game. If you’re not familiar with pokemon, it’s a game where elemental creatures battle.

You’re going to represent pokemon using a Pokemon struct with the keys :name, :type, :health, :attack, and :speed. Enforce that all keys must have a value.

  • :name is a string.
  • :type will be an atom of either :grass, :water, or :fire
  • :health will be an integer with a default value of 20
  • :attack will be an integer with a default value of 5
  • :speed will be an integer

You do not need to validate the data types for the keys above. These are simply the expected data types for each Pokemon struct instance.

%Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}

Then implement the Pokemon.attack/2 function according to the documented examples.

attacker = %Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}
defender = %Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}

Pokemon.attack(attacker, defender)
# Output: Subtract Attack From Health To Return An Updated Pokemon Struct
%Pokemon{name: "Bulbasaur", type: :grass, health: 15, attack: 5, speed: 20}

Pokemon.attack/2 should apply double damage if the pokemon has a type advantage, and half damage if the pokemon has a type disadvantage.

The advantages are:

  • :fire is strong against :grass
  • :grass is strong against :water
  • :water is strong against :fire
  • :fire is weak against :water
  • :grass is weak against :fire
  • :water is weak against :grass
  • the same type should deal normal damage.
attacker = %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
defender = %Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}

Pokemon.attack(attacker, defender)
# Output: Subtract Attack (doubled) From Health To Return An Updated Pokemon Struct
%Pokemon{name: "Bulbasaur", type: :grass, health: 10, attack: 5, speed: 20}
defmodule Pokemon do
  @moduledoc """
  Pokemon

  ## Examples

    iex> %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
    %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}

    iex> %Pokemon{name: "Charmander", type: :fire, speed: 20}
    %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
  """
  @enforce_keys [:name, :type, :speed]
  defstruct @enforce_keys ++ [health: 20, attack: 5]

  @doc """
  subtract the attack damage of an attacker from the defender's health.
  return an updated pokemon struct, and take type advantage into consideration.

  ## Examples

    Same Type

    iex> attacker = %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
    iex> defender = %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
    iex> Pokemon.attack(attacker, defender)
    %Pokemon{name: "Charmander", type: :fire, health: 15, attack: 5, speed: 20}

    Type Disadvantage

    iex> attacker = %Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}
    iex> defender = %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
    iex> Pokemon.attack(attacker, defender)
    %Pokemon{name: "Charmander", type: :fire, health: 17.5, attack: 5, speed: 20}

    Type Advantage

    iex> attacker = %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
    iex> defender = %Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}
    iex> Pokemon.attack(attacker, defender)
    %Pokemon{name: "Bulbasaur", type: :grass, health: 10, attack: 5, speed: 20}
  """
  def attack(attacker, defender) do
    damage =
      case {attacker.type, defender.type} do
        {:fire, :grass} -> attacker.attack * 2
        {:grass, :water} -> attacker.attack * 2
        {:water, :fire} -> attacker.attack * 2
        {:fire, :water} -> attacker.attack / 2
        {:grass, :fire} -> attacker.attack / 2
        {:water, :grass} -> attacker.attack / 2
        _ -> attacker.attack
      end

    %Pokemon{
      name: defender.name,
      type: defender.type,
      health: defender.health - damage,
      attack: defender.attack,
      speed: defender.speed
    }
  end
end

Consider using the following Elixir cell to test your solution.

Pokemon Instances

Bind three variables charmander, bulbasaur, and squirtle to an instance of a Pokemon struct with the following information.

classDiagram
  class charmander {
    name: "Charmander"
    type: :fire
    health: 20
    attack: 5
    speed: 20
  }
  class bulbasaur {
    name: "Bulbasar"
    type: :grass
    health: 20
    attack: 5
    speed: 20
  }
  class squirtle {
    name: "Squirtle"
    type: :water
    health: 20
    attack: 5
    speed: 20
  }

Use may these three structs to test your Pokemon.attack/2 function.

Example solution

charmander = %Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5, speed: 20}
bulbasaur = %Pokemon{name: "Bulbasaur", type: :grass, health: 20, attack: 5, speed: 20}
squirtle = %Pokemon{name: "Squirtle", type: :water, health: 20, attack: 5, speed: 20}

# Testing Example
Pokemon.attack(charmander, bulbasaur)

Enter your solution below.

charmander = %Pokemon{name: "Charmander", type: :fire, speed: 20}
bulbasaur = %Pokemon{name: "Bulbasar", type: :grass, speed: 20}
squirtle = %Pokemon{name: "Squirtle", type: :water, speed: 20}

IO.inspect(charmander)
IO.inspect(bulbasaur)
IO.inspect(squirtle)

charmander = Pokemon.attack(squirtle, charmander)
charmander = Pokemon.attack(squirtle, charmander)
charmander = Pokemon.attack(bulbasaur, charmander)
IO.inspect(charmander)

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Pokemon Battle exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue RPG DialogueRanges