Hangman.Game (Hangman Game v0.1.4) View Source

A game struct and functions for the Hangman Game.

The game struct contains the fields game_name, turns_left, game_state, letters and used representing the characteristics of a game in the Hangman Game.

Based on the course Elixir for Programmers by Dave Thomas.

Link to this section Summary

Types

Letter between a and z or _

Game name

Game state

t()

A game struct for the Hangman Game

A tally struct for the Hangman Game

Functions

Makes a move by guessing a letter.

Returns a game struct given a game_name and a word to be guessed.

Returns a random name of 4 to 10 characters.

Returns a tally struct externalizing game.

Link to this section Types

Specs

letter() :: String.codepoint()

Letter between a and z or _

Specs

name() :: String.t()

Game name

Specs

state() ::
  :initializing | :good_guess | :bad_guess | :already_used | :lost | :won

Game state

Specs

t() :: %Hangman.Game{
  game_name: name(),
  game_state: state(),
  letters: [letter()],
  turns_left: turns_left(),
  used: used()
}

A game struct for the Hangman Game

Specs

tally() :: %{
  game_state: state(),
  turns_left: turns_left(),
  letters: [letter() | charlist()],
  guesses: [letter()]
}

A tally struct for the Hangman Game

Specs

turns_left() :: 0..7

Specs

used() :: MapSet.t(letter())

Link to this section Functions

Specs

make_move(t(), guess :: letter()) :: t()

Makes a move by guessing a letter.

Examples

iex> alias Hangman.Game
iex> game = Game.random_name() |> Game.new()
iex> Game.make_move(game, "a").game_state in [:good_guess, :bad_guess]
true
Link to this function

new(game_name, word \\ Dictionary.random_word())

View Source

Specs

new(name(), String.t()) :: t()

Returns a game struct given a game_name and a word to be guessed.

Examples

iex> alias Hangman.Game
iex> Game.new("Mr Smith").game_state
:initializing

Specs

random_name() :: name()

Returns a random name of 4 to 10 characters.

Examples

iex> alias Hangman.Game
iex> for _ <- 0..99, uniq: true do
iex>   length = Game.random_name() |> String.length()
iex>   length in 4..10
iex> end
[true]

Specs

tally(t()) :: tally()

Returns a tally struct externalizing game.

Examples

iex> alias Hangman.Game
iex> game = Game.random_name() |> Game.new()
iex> game = Game.make_move(game, "a")
iex> Game.tally(game).turns_left in 6..7
true