-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bread And Potions.ex
46 lines (37 loc) · 961 Bytes
/
Bread And Potions.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# Add code to define the protocol and its implementations below here...
defprotocol Edible do
def eat(item, character)
end
defimpl Edible, for: [RPG.LoafOfBread, RPG.Character] do
def eat(_, character) do
# [nil, character]
{nil, %{character | health: character.health + 5}}
end
end
defimpl Edible, for: [RPG.ManaPotion, RPG.Character] do
def eat(potion, character) do
{%RPG.EmptyBottle{}, %{character | mana: character.mana + potion.strength}}
end
end
defimpl Edible, for: [RPG.Poison, RPG.Character] do
def eat(_, character) do
{%RPG.EmptyBottle{}, %{character | health: 0}}
end
end
end