View Source Hangman.Game (Hangman Game v0.1.35)
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 characteristics 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
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
.
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
Functions
Makes a move by guessing a letter.
Examples
iex> alias Hangman.Game
iex> game = Game.new()
iex> Game.make_move(game, "a").game_state in [:good_guess, :bad_guess]
true
new(game_name \\ random_name(), word \\ Hangman.Dictionary.random_word())
View SourceCreates 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]}
@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]
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]}