Hangman.Engine (Hangman Engine v0.1.72)
View SourceModels 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
@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
@spec make_move(Hangman.Game.name(), Hangman.Game.letter()) :: Hangman.Game.tally()
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
@spec new_game(Hangman.Game.name()) :: Supervisor.on_start_child()
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
@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
@spec tally(Hangman.Game.name()) :: Hangman.Game.tally()
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