View Source Islands.Grid (Islands Grid v0.1.38)

A grid (map of maps) and functions for the Game of Islands.

Inspired by the book Functional Web Development by Lance Halvorsen.

Summary

Types

t()

A grid (map of maps) allowing the grid[row][col] syntax

Function creating a tile from a cell value

Functions

Creates an "empty" grid.

Converts a board or guesses struct into a grid.

Converts a board or guesses struct into a grid and then into a list of maps. Function tile_fun converts each grid cell value into a colored tile (with embedded ANSI escapes). The default for tile_fun is function Islands.Grid.Tile.new/1.

Types

@type t() :: %{
  required(Islands.Coord.row()) => %{required(Islands.Coord.col()) => atom()}
}

A grid (map of maps) allowing the grid[row][col] syntax

@type tile_fun() :: (atom() -> IO.chardata())

Function creating a tile from a cell value

Functions

@spec new() :: t()

Creates an "empty" grid.

Examples

iex> alias Islands.Grid
iex> grid = Grid.new()
iex> {grid[1][1], grid[10][10]}
{nil, nil}

iex> alias Islands.Grid
iex> grid = Grid.new()
iex> for row <- 1..10 do
iex>   for col <- 1..10, uniq: true do
iex>     grid[row][col]
iex>   end
iex> end
[[nil], [nil], [nil], [nil], [nil], [nil], [nil], [nil], [nil], [nil]]

iex> Islands.Grid.new()
%{
  1 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  2 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  3 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  4 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  5 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  6 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  7 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  8 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  9 => %{ 1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil},
  10 => %{1 => nil, 2 => nil, 3 => nil, 4 => nil,  5 => nil,
          6 => nil, 7 => nil, 8 => nil, 9 => nil, 10 => nil}
}
@spec new(Islands.Board.t() | Islands.Guesses.t()) :: t()

Converts a board or guesses struct into a grid.

Link to this function

to_maps(board_or_guesses, tile_fun \\ &Islands.Grid.Tile.new/1)

View Source
@spec to_maps(Islands.Board.t() | Islands.Guesses.t(), tile_fun()) :: [map()]

Converts a board or guesses struct into a grid and then into a list of maps. Function tile_fun converts each grid cell value into a colored tile (with embedded ANSI escapes). The default for tile_fun is function Islands.Grid.Tile.new/1.