Hangman.Game (Hangman Game v0.1.54)

View Source

A game struct and functions for the Hangman Game.

The game struct contains the fields:

  • game_name
  • game_state
  • turns_left
  • letters
  • used

representing the properties of a game in the Hangman Game.

Based on the course Elixir for Programmers by Dave Thomas.

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

A word with letters from a to z

Functions

Makes a move by guessing a letter.

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

Returns a random name of 4 to 10 characters.

Resigns game.

Returns a tally map externalizing game.

Types

letter()

@type letter() :: <<_::8>>

Letter from a to z

name()

@type name() :: String.t()

Game name

state()

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

Game state

t()

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

A game struct for the Hangman Game

tally()

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

A tally map for the Hangman Game

turns_left()

@type turns_left() :: 0..7

Turns left from 7 to 0

underline()

@type underline() :: <<_::8>>

Underline: _

used()

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

A set of used (guessed) letters

word()

@type word() :: String.t()

A word with letters from a to z

Functions

make_move(game, guess)

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

Makes a move by guessing a letter.

Examples

iex> alias Hangman.Game
iex> game = Game.new("wibble")
iex> Game.make_move(game, "a").game_state
:bad_guess

iex> alias Hangman.Game
iex> game = Game.new("wibble")
iex> Game.make_move(game, "B")
** (ArgumentError) guess 'B' not a-z

new(word, game_name \\ random_name())

@spec new(word(), name()) :: t()

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

Using function Hangman.Dictionary.random_word/0 of app :hangman_dictionary to provide the default value for word would cause app :hangman_dictionary to run on each client node as opposed to only on the engine node (see function Hangman.Engine.GameServer.init/1 of app :hangman_engine).

Examples

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

iex> alias Hangman.Game
iex> Game.new("José")
** (ArgumentError) some characters of 'José' not a-z

random_name()

@spec 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]

iex> alias Hangman.Game
iex> for _ <- 0..99, uniq: true do
iex>   Game.random_name() =~ ~r/^[a-zA-Z0-9_-]{4,10}$/
iex> end
[true]

resign(game)

@spec resign(t()) :: t()

Resigns game.

Examples

iex> alias Hangman.Game
iex> game = Game.new("anaconda")
iex> game = Game.make_move(game, "a")
iex> game = Game.make_move(game, "n")
iex> lost_game = Game.resign(game)
iex> tally = Game.tally(lost_game)
iex> {tally.game_state, tally.turns_left, tally.letters, tally.guesses}
{:lost, 7, ["a", "n", "a", ["c"], ["o"], "n", ["d"], "a"], ~W[a n]}

tally(game)

@spec tally(t()) :: tally()

Returns a tally map externalizing game.

Examples

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