mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
40 lines
833 B
Elixir
40 lines
833 B
Elixir
defmodule RPG do
|
|
defmodule Character do
|
|
defstruct health: 100, mana: 0
|
|
end
|
|
|
|
defmodule LoafOfBread do
|
|
defstruct []
|
|
end
|
|
|
|
defmodule ManaPotion do
|
|
defstruct strength: 10
|
|
end
|
|
|
|
defmodule Poison do
|
|
defstruct []
|
|
end
|
|
|
|
defmodule EmptyBottle do
|
|
defstruct []
|
|
end
|
|
|
|
defprotocol Edible do
|
|
def eat(item, character)
|
|
end
|
|
|
|
defimpl Edible, for: LoafOfBread do
|
|
def eat(_, %Character{health: health} = character),
|
|
do: {nil, %{character | health: health + 5}}
|
|
end
|
|
|
|
defimpl Edible, for: ManaPotion do
|
|
def eat(%ManaPotion{strength: strength}, %Character{mana: mana} = character),
|
|
do: {%EmptyBottle{}, %{character | mana: mana + strength}}
|
|
end
|
|
|
|
defimpl Edible, for: Poison do
|
|
def eat(_, %Character{} = character),
|
|
do: {%EmptyBottle{}, %{character | health: 0}}
|
|
end
|
|
end
|