Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Consumable Protocol

deprecated_consumable_protocol.livemd

Consumable Protocol

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 Math With ProtocolsBattle Map

Overview

You’re creating a (RPG) Role Playing Game where there are Character structs which store information about a character.

Define a Character struct with the following keys and default values.

classDiagram
    class Character {
        strength: 10
        dexterity: 10
        constitution: 10
        wisdom: 10
        intelligence: 10
        charisma: 10
    }

Example Solution

defmodule Character do
  defstruct [strength: 10, dexterity: 10, constitution: 10, wisdom: 10, intelligence: 10, charisma: 10]
end

Enter your solution below.

Our game has Consumable items which modify a character’s base stats. For example, we’ll have a StrengthPotion struct which will increase a Character‘s strength based on the :level key.

Define item structs with the following keys and default values.

classDiagram
    class StrengthPotion {
      level: 1
    }
    class DexterityPotion {
      level: 1
    }
    class ConstitutionPotion {
      level: 1
    }
    class WisdomPotion {
      level: 1
    }
    class IntelligencePotion {
      level: 1
    }
    class CharismaPotion {
      level: 1
    }

Example Solution

defmodule StrengthPotion do
  defstruct [level: 1]
end

defmodule DexterityPotion do
  defstruct [level: 1]
end

defmodule ConstitutionPotion do
  defstruct [level: 1]
end

defmodule WisdomPotion do
  defstruct [level: 1]
end

defmodule IntelligencePotion do
  defstruct [level: 1]
end

defmodule CharismaPotion do
  defstruct [level: 1]
end

Enter your solution below.

To make our system extendable, we’ll have a Consumable protocol, and separate implementations for structs such as the StrengthPotion.

Create a Consumable protocol with a consume/2 function which accepts an item and a Character struct. We’ll apply the items affects and return a newly updated character.

Also create Consumable implementations for the StrengthPotion, DexterityPotion, ConstitutionPotion, WisdomPotion, IntelligencePotion, and CharismaPotion structs.

When consumed, the item should increase the associated Character struct key by the :level of the Consumable item.

Consumable.consume(%StrengthPotion{level: 1}, %Character{})
%Character{strength: 11, dexterity: 10, constitution: 10, wisdom: 10, intelligence: 10, charisma: 10}

Consumable.consume(%DexterityPotion{level: 2}, %Character{})
%Character{strength: 10, dexterity: 12, constitution: 10, wisdom: 10, intelligence: 10, charisma: 10}

Example Solution

defprotocol Consumable do
  def consume(item, character)
end

defimpl Consumable, for: StrengthPotion do
  def consume(potion, character) do
    %{character | strength: character.strength + potion.level}
  end
end

defimpl Consumable, for: DexterityPotion do
  def consume(potion, character) do
    %{character | dexterity: character.dexterity + potion.level}
  end
end

defimpl Consumable, for: ConstitutionPotion do
  def consume(potion, character) do
    %{character | constitution: character.constitution + potion.level}
  end
end

defimpl Consumable, for: WisdomPotion do
  def consume(potion, character) do
    %{character | wisdom: character.wisdom + potion.level}
  end
end

defimpl Consumable, for: IntelligencePotion do
  def consume(potion, character) do
    %{character | intelligence: character.intelligence + potion.level}
  end
end

defimpl Consumable, for: CharismaPotion do
  def consume(potion, character) do
    %{character | charisma: character.charisma + potion.level}
  end
end

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 Consumable Protocol 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 Math With ProtocolsBattle Map