Hangman.Engine (Hangman Engine v0.1.72)

View Source

Models the Hangman Game. Times out after 30 minutes of inactivity.

Based on the course Elixir for Programmers by Dave Thomas.

Summary

Functions

Stops a game server process normally. It won't be restarted.

Makes a move by guessing a letter.

Starts a new game server process and supervises it.

Resigns a game.

Returns the tally of a game.

Functions

end_game(game_name)

@spec end_game(Hangman.Game.name()) :: :ok

Stops a game server process normally. It won't be restarted.

Examples

iex> alias Hangman.Engine
iex> Engine.new_game("Ben")
iex> Engine.end_game("Ben")
:ok

make_move(game_name, guess)

Makes a move by guessing a letter.

Examples

iex> alias Hangman.Engine
iex> Engine.new_game("Ed")
iex> Engine.make_move("Ed", "a").game_state in [:good_guess, :bad_guess]
true

new_game(game_name)

Starts a new game server process and supervises it.

Examples

iex> alias Hangman.Engine
iex> {:ok, game_id} = Engine.new_game("Meg")
iex> {:error, {:already_started, ^game_id}} = Engine.new_game("Meg")
iex> is_pid(game_id)
true

resign(game_name)

@spec resign(Hangman.Game.name()) :: Hangman.Game.tally()

Resigns a game.

Examples

iex> alias Hangman.Engine
iex> Engine.new_game("Jay")
iex> tally = Engine.resign("Jay")
iex> %{
...>   game_state: :lost,
...>   turns_left: 7,
...>   letters: letters,
...>   guesses: []
...> } = tally
iex> Enum.all?(letters, fn [<<byte>>] -> byte in ?a..?z end)
true

tally(game_name)

Returns the tally of a game.

Examples

iex> alias Hangman.Engine
iex> Engine.new_game("Jim")
iex> tally = Engine.tally("Jim")
iex> %{
...>   game_state: :initializing,
...>   turns_left: 7,
...>   letters: letters,
...>   guesses: []
...> } = tally
iex> all_underscores? = Enum.all?(letters, & &1 == "_")
iex> is_list(letters) and length(letters) > 0 and all_underscores?
true