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

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 from a to z

Game name

Game state

t()

A game struct for the Hangman Game

A tally map for the Hangman Game

Turns left from 7 to 0

Underline: _

A set of used (guessed) letters

Functions

Makes a move by guessing a letter.

Creates a game struct from a game_name and a word to be guessed. The default value for game_name is provided by function random_name/0 and for word by function Hangman.Dictionary.random_word/0.

Returns a random name of 4 to 10 characters.

Returns a tally map externalizing game.

Link to this section Types

Specs

letter() :: String.codepoint()

Letter from a to z

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() | underline() | charlist()],
  guesses: [letter()]
}

A tally map for the Hangman Game

Specs

turns_left() :: 0..7

Turns left from 7 to 0

Specs

underline() :: String.codepoint()

Underline: _

Specs

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

A set of used (guessed) letters

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 \\ random_name(), word \\ Dictionary.random_word())

View Source

Specs

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

Creates a game struct from a game_name and a word to be guessed. The default value for game_name is provided by function random_name/0 and for word by function Hangman.Dictionary.random_word/0.

Examples

iex> alias Hangman.Game
iex> game = Game.new()
iex> game_name_length = String.length(game.game_name)
iex> {game.game_state, game.turns_left, game_name_length in 4..10}
{:initializing, 7, true}

iex> alias Hangman.Game
iex> game = Game.new("Mr Smith")
iex> {game.game_state, game.turns_left, game.game_name}
{:initializing, 7, "Mr Smith"}

iex> alias Hangman.Game
iex> game = Game.new("Wibble", "wibble")
iex> {game.game_state, game.turns_left, game.game_name, game.letters}
{:initializing, 7, "Wibble", ~w[w i b b l e]}

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 map externalizing game.

Examples

iex> alias Hangman.Game
iex> game = Game.random_name() |> Game.new("anaconda")
iex> game = Game.make_move(game, "a")
iex> tally = Game.tally(game)
iex> {tally.game_state, tally.turns_left, tally.letters, tally.guesses}
{:good_guess, 7, ~w[a _ a _ _ _ _ a], ~w[a]}