View Source Islands.State (Islands State v0.1.24)

A state struct and functions for the Game of Islands.

The state struct contains the fields game_state, player1_state and player2_state for implementing a state machine in the Game of Islands.

Based on the book Functional Web Development by Lance Halvorsen.

Summary

Types

State machine event

Game state

Player state

t()

A state struct for the Game of Islands

Functions

Decides whether to permit the state/event combination. Also decides whether to transition to a new state. Returns {:ok, new_state} if the combination is permissible. Returns :error if it is not.

Callback implementation for Access.fetch/2.

Creates a new state struct.

Callback implementation for Access.pop/2.

Types

@type event() ::
  :add_player
  | {:position_island, Islands.PlayerID.t()}
  | {:position_all_islands, Islands.PlayerID.t()}
  | {:set_islands, Islands.PlayerID.t()}
  | {:guess_coord, Islands.PlayerID.t()}
  | {:stop, Islands.PlayerID.t()}
  | {:win_check, :no_win | :win}

State machine event

@type game_state() ::
  :initialized | :players_set | :player1_turn | :player2_turn | :game_over

Game state

@type player_state() :: :islands_not_set | :islands_set

Player state

@type t() :: %Islands.State{
  game_state: game_state(),
  player1_state: player_state(),
  player2_state: player_state()
}

A state struct for the Game of Islands

Functions

@spec check(t(), event()) :: {:ok, t()} | :error

Decides whether to permit the state/event combination. Also decides whether to transition to a new state. Returns {:ok, new_state} if the combination is permissible. Returns :error if it is not.

Callback implementation for Access.fetch/2.

Link to this function

get_and_update(state, key, fun)

View Source

Callback implementation for Access.get_and_update/3.

@spec new() :: t()

Creates a new state struct.

Examples

iex> Islands.State.new()
%Islands.State{
  game_state: :initialized,
  player1_state: :islands_not_set,
  player2_state: :islands_not_set
}

Callback implementation for Access.pop/2.