Run the file with:
$ iex digipet.ex
The create a pet via:
iex> pet = DigiPet.new "Franklin"
Available commands are:
iex> DigiPet.show pet
iex> DigiPet.feed pet
iex> DigiPet.clean pet
iex> DigiPet.drink pet
iex> DigiPet.play pet
defmodule DigiPet do | |
def new(name \\ "George"), do: _new(name) | |
defp _new(name), do: spawn(__MODULE__, :start, [%{ name: name, age: 0, hunger: 0, dirtyness: 0, thirst: 0, sadness: 0 }]) | |
def show(pet), do: send(pet, { self, :show }) | |
def feed(pet), do: send(pet, { self, :feed }) | |
def clean(pet), do: send(pet, { self, :clean }) | |
def drink(pet), do: send(pet, { self, :drink }) | |
def play(pet), do: send(pet, { self, :play }) | |
def start(pet) do | |
receive do | |
{sender, msg} -> | |
pet = _update(pet, msg) | |
send sender, { :ok, _pet_state(pet) } | |
start(pet) | |
after 500 -> | |
_step(pet) | |
|> start | |
end | |
end | |
defp _pet_state(pet), do: _state(pet.name, pet.age, pet.hunger, pet.dirtyness, pet.thirst, pet.sadness) | |
# Happyness States | |
defp _state(name, _a, _h, _d, _t, sadness) when sadness > 60, do: _render "#{name} is inconsolable." | |
defp _state(name, _a, _h, _d, _t, sadness) when sadness > 40, do: _render "#{name} is sad." | |
defp _state(name, _a, _h, _d, _t, sadness) when sadness > 20, do: _render "#{name} is a a little upset." | |
defp _state(name, _a, _h, _d, _t, sadness) when sadness < 0, do: _render "#{name} is a happy." | |
defp _state(name, _a, _h, _d, _t, sadness) when sadness < -20, do: _render "#{name} is a extatic." | |
# Thirst States | |
defp _state(name, _a, _h, _d, thirst, _s) when thirst > 35, do: _render "#{name} is parched." | |
defp _state(name, _a, _h, _d, thirst, _s) when thirst > 15, do: _render "#{name} is thirsty." | |
# Hunger States | |
defp _state(name, _a, hunger, _d, _t, _s) when hunger > 100, do: _render "#{name} is starving." | |
defp _state(name, _a, hunger, _d, _t, _s) when hunger > 50, do: _render "#{name} is hungry." | |
defp _state(name, _a, hunger, _d, _t, _s) when hunger > 25, do: _render "#{name} is peckish." | |
defp _state(name, _a, hunger, _d, _t, _s) when hunger < 0, do: _render "#{name} is full." | |
# Age States | |
defp _state(name, age, _h, _d, _t, _s) when age < 50, do: _render "#{name} is a baby." | |
defp _state(name, age, _h, _d, _t, _s) when age < 250, do: _render "#{name} is a child." | |
defp _state(name, age, _h, _d, _t, _s) when age < 450, do: _render "#{name} is a teenager." | |
defp _state(name, age, _h, _d, _t, _s) when age < 750, do: _render "#{name} is an adult." | |
defp _state(name, age, _h, _d, _t, _s) when age < 950, do: _render "#{name} is getting old." | |
defp _state(name, _a, _h, _d, _t, _s), do: _render "#{name} is fine." | |
defp _update(pet, :show), do: pet | |
defp _update(pet, :feed), do: %{ name: pet.name, | |
age: pet.age, | |
hunger: pet.hunger - 30, | |
dirtyness: pet.dirtyness, | |
thirst: pet.thirst, | |
sadness: pet.sadness} | |
defp _update(pet, :clean), do: %{ name: pet.name, | |
age: pet.age, | |
hunger: pet.hunger, | |
dirtyness: pet.dirtyness - 20, | |
thirst: pet.thirst, | |
sadness: pet.sadness} | |
defp _update(pet, :drink), do: %{ name: pet.name, | |
age: pet.age, | |
hunger: pet.hunger, | |
dirtyness: pet.dirtyness, | |
thirst: pet.thirst - 25, | |
sadness: pet.sadness} | |
defp _update(pet, :play), do: %{ name: pet.name, | |
age: pet.age, | |
hunger: pet.hunger, | |
dirtyness: pet.dirtyness, | |
thirst: pet.thirst, | |
sadness: pet.sadness - 18} | |
defp _step(pet), do: %{ name: pet.name, | |
age: pet.age + 1, | |
hunger: pet.hunger + 1, | |
dirtyness: pet.dirtyness + 1, | |
thirst: pet.thirst + 1, | |
sadness: pet.sadness + 1} | |
defp _render(text), do: IO.puts "\n\n#{text}\n\n" | |
end |