View Source Islands.Game (Islands Game v0.1.45)
A game struct and functions for the Game of Islands.
The game struct contains the fields name
, player1
, player2
, request
,
response
and state
representing the characteristics of a game in the
Game of Islands.
Based on the book Functional Web Development by Lance Halvorsen.
Summary
Types
Game name
A game overview map
A game overview player map
A game struct for the Game of Islands
Functions
Callback implementation for Access.fetch/2
.
Callback implementation for Access.get_and_update/3
.
Returns a unique, URL-friendly name such as "bold-frog-8249".
Creates a game struct from name
, player1_name
, gender
and pid
.
Sends the game state to a player's process.
Returns a player's opponent ID.
Returns the game overview map of game
.
Returns a player's board struct.
Callback implementation for Access.pop/2
.
Returns a random name of 4 to 10 characters.
Updates a player's board struct with board
.
Updates a player's guesses struct using hit_or_miss
and guess
.
Updates a player struct using name
, gender
and pid
.
Updates the request tuple.
Updates the response tuple.
Updates the state struct.
Types
@type name() :: String.t()
Game name
@type overview() :: %{ game_name: name(), player1: overview_player(), player2: overview_player() }
A game overview map
@type overview_player() :: %{ name: Islands.Player.name(), gender: Islands.Player.gender() }
A game overview player map
@type t() :: %Islands.Game{ name: name(), player1: Islands.Player.t(), player2: Islands.Player.t(), request: Islands.Request.t(), response: Islands.Response.t(), state: Islands.State.t() }
A game struct for the Game of Islands
Functions
Callback implementation for Access.fetch/2
.
Callback implementation for Access.get_and_update/3
.
@spec haiku_name() :: name()
Returns a unique, URL-friendly name such as "bold-frog-8249".
@spec new(name(), Islands.Player.name(), Islands.Player.gender(), pid()) :: t() | {:error, atom()}
Creates a game struct from name
, player1_name
, gender
and pid
.
Examples
iex> alias Islands.{Game, Player}
iex> {player_name, gender, pid} = {"James", :m, self()}
iex> game = Game.new("Sky Fall", player_name, gender, pid)
iex> %Game{name: name, player1: player1} = game
iex> %Player{name: ^player_name, gender: ^gender, pid: ^pid} = player1
iex> {name, is_struct(player1, Player), is_struct(game.player2, Player)}
{"Sky Fall", true, true}
iex> alias Islands.Game
iex> {player_name, gender, pid} = {"James", :m, self()}
iex> Game.new('Sky Fall', player_name, gender, pid)
{:error, :invalid_game_args}
@spec notify_player(t(), Islands.PlayerID.t()) :: t()
Sends the game state to a player's process.
@spec opponent_id(Islands.PlayerID.t()) :: Islands.PlayerID.t()
Returns a player's opponent ID.
Returns the game overview map of game
.
@spec player_board(t(), Islands.PlayerID.t()) :: Islands.Board.t()
Returns a player's board struct.
Callback implementation for Access.pop/2
.
@spec random_name() :: name()
Returns a random name of 4 to 10 characters.
@spec update_board(t(), Islands.PlayerID.t(), Islands.Board.t()) :: t()
Updates a player's board struct with board
.
@spec update_guesses( t(), Islands.PlayerID.t(), Islands.Guesses.type(), Islands.Coord.t() ) :: t()
Updates a player's guesses struct using hit_or_miss
and guess
.
@spec update_player( t(), Islands.PlayerID.t(), Islands.Player.name(), Islands.Player.gender(), pid() ) :: t()
Updates a player struct using name
, gender
and pid
.
@spec update_request(t(), Islands.Request.t()) :: t()
Updates the request tuple.
@spec update_response(t(), Islands.Response.t()) :: t()
Updates the response tuple.
@spec update_state(t(), Islands.State.t()) :: t()
Updates the state struct.