A grid (map of maps) and functions for the Game of Islands.
Inspired by the book Functional Web Development by Lance Halvorsen.
Summary
Types
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.
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.
Examples
iex> alias Islands.{Board, Coord, Grid, Island}
iex> {:ok, atoll_origin} = Coord.new(1, 1)
iex> {:ok, atoll_hit} = Coord.new(1, 2)
iex> {:ok, atoll} = Island.new(:atoll, atoll_origin)
iex> board = Board.new() |> Board.position_island(atoll)
iex> {:hit, :none, :no_win, board} = Board.guess(board, atoll_hit)
iex> grid = Grid.new(board)
iex> row_1 = {grid[1][1], grid[1][2], grid[1][3]}
iex> row_2 = {grid[2][1], grid[2][2], grid[2][3]}
iex> row_3 = {grid[3][1], grid[3][2], grid[3][3]}
iex> {row_1, row_2, row_3}
{{:atoll, :atoll_hit, nil}, {nil, :atoll, nil}, {:atoll, :atoll, nil}}
@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 should convert each grid cell value into a colored tile
(with embedded ANSI escape sequences). The default for tile_fun is function
Islands.Grid.Tile.new/1.
Examples
iex> alias Islands.{Board, Coord, Grid, Island}
iex> {:ok, atoll_origin} = Coord.new(1, 1)
iex> {:ok, atoll_hit} = Coord.new(1, 2)
iex> {:ok, atoll} = Island.new(:atoll, atoll_origin)
iex> board = Board.new() |> Board.position_island(atoll)
iex> {:hit, :none, :no_win, board} = Board.guess(board, atoll_hit)
iex> [row_1 | _other_9_maps] = Grid.to_maps(board, & &1)
iex> Map.take(row_1, ["row", 1, 2, 3, 10])
%{"row" => 1, 1 => :atoll, 2 => :atoll_hit, 3 => nil, 10 => nil}