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

t()

A game struct for the Game of Islands

Functions

Callback implementation for Access.fetch/2.

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.

Link to this function

get_and_update(game, key, fun)

View Source

Callback implementation for Access.get_and_update/3.

@spec haiku_name() :: name()

Returns a unique, URL-friendly name such as "bold-frog-8249".

Link to this function

new(name, player1_name, gender, pid)

View Source
@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}
Link to this function

notify_player(game, player_id)

View Source
@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.

@spec overview(t()) :: overview()

Returns the game overview map of game.

Link to this function

player_board(game, player_id)

View Source
@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.

Link to this function

update_board(game, player_id, board)

View Source
@spec update_board(t(), Islands.PlayerID.t(), Islands.Board.t()) :: t()

Updates a player's board struct with board.

Link to this function

update_guesses(game, player_id, hit_or_miss, guess)

View Source
@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.

Link to this function

update_player(game, player_id, name, gender, pid)

View Source
@spec update_player(
  t(),
  Islands.PlayerID.t(),
  Islands.Player.name(),
  Islands.Player.gender(),
  pid()
) ::
  t()

Updates a player struct using name, gender and pid.

Link to this function

update_request(game, request)

View Source
@spec update_request(t(), Islands.Request.t()) :: t()

Updates the request tuple.

Link to this function

update_response(game, response)

View Source
@spec update_response(t(), Islands.Response.t()) :: t()

Updates the response tuple.

Link to this function

update_state(game, state)

View Source
@spec update_state(t(), Islands.State.t()) :: t()

Updates the state struct.