Islands Engine v0.1.9 Islands.Engine.Grid View Source

Converts a player’s board/guesses to a grid (map of maps). Also converts a grid to a list of maps.

Link to this section Summary

Functions

Returns an “empty” grid

Converts a board map or a guesses struct to a grid

Converts a grid to a list of maps

Link to this section Types

Link to this type t() View Source
t() :: %{
  optional(Islands.Engine.Coord.row()) => %{
    optional(Islands.Engine.Coord.col()) => atom()
  }
}

Link to this section Functions

Returns an “empty” grid.

Examples

iex> alias Islands.Engine.Grid
iex> %{1 => row_1} = Grid.new()
iex> row_1
%{
  1 => nil, 2 => nil, 3 => nil, 4 => nil, 5  => nil,
  6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil
}
Link to this function new(board_or_guesses) View Source
new(Islands.Engine.Board.t() | Islands.Engine.Guesses.t()) :: t()

Converts a board map or a guesses struct to a grid.

Examples

iex> {:ok, atoll_coord} = Coord.new(1, 1)
iex> {:ok, atoll_hit} = Coord.new(1, 2)
iex> {:ok, atoll} = Island.new(:atoll, atoll_coord)
iex>
iex> {:hit, :none, :no_win, board} =
iex>   Board.new()
iex>   |> Board.position_island(atoll)
iex>   |> Board.guess(atoll_hit)
iex>
iex> %{1 => row_1} = Grid.new(board)
iex> row_1
%{
  1 => :atoll, 2 => :atoll_hit, 3 => nil, 4 => nil, 5  => nil,
  6 => nil   , 7 => nil       , 8 => nil, 9 => nil, 10 => nil
}

iex> {:ok, atoll_coord} = Coord.new(1, 1)
iex> {:ok, atoll_hit} = Coord.new(1, 2)
iex>
iex> guesses =
iex>   Guesses.new()
iex>   |> Guesses.add(:miss, atoll_coord)
iex>   |> Guesses.add(:hit, atoll_hit)
iex>
iex> %{1 => row_1} = Grid.new(guesses)
iex> row_1
%{
  1 => :miss, 2 => :hit, 3 => nil, 4 => nil, 5  => nil,
  6 => nil  , 7 => nil , 8 => nil, 9 => nil, 10 => nil
}
Link to this function to_maps(grid, fun \\ &Format.for/1) View Source
to_maps(t(), (atom() -> IO.ANSI.Plus.ansidata())) :: [map()]

Converts a grid to a list of maps.

Examples

iex> {:ok, atoll_coord} = Coord.new(1, 1)
iex> {:ok, atoll_hit} = Coord.new(1, 2)
iex> {:ok, atoll} = Island.new(:atoll, atoll_coord)
iex>
iex> {:hit, :none, :no_win, board} =
iex>   Board.new()
iex>   |> Board.position_island(atoll)
iex>   |> Board.guess(atoll_hit)
iex>
iex> [row_1 | _other_rows] = board |> Grid.new() |> Grid.to_maps(& &1)
iex> row_1
%{"row" => 1,
  1 => :atoll, 2 => :atoll_hit, 3 => nil, 4 => nil, 5  => nil,
  6 => nil   , 7 => nil       , 8 => nil, 9 => nil, 10 => nil
}