View Source Hangman.Game (Hangman Game v0.1.51)
A game struct and functions for the Hangman Game.
The game struct contains the fields game_name
, game_state
, turns_left
,
letters
and 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
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
@type letter() :: <<_::8>>
Letter from a
to z
@type name() :: String.t()
Game name
@type state() ::
:initializing | :good_guess | :bad_guess | :already_used | :lost | :won
Game state
@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
@type tally() :: %{ game_state: state(), turns_left: turns_left(), letters: [letter() | underline() | [letter()]], guesses: [letter()] }
A tally map for the Hangman Game
@type turns_left() :: 0..7
Turns left from 7 to 0
@type underline() :: <<_::8>>
Underline: _
A set of used (guessed) letters
@type word() :: String.t()
A word with letters from a
to z
Functions
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
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
@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]
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]}
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]}