Islands.Island (Islands Island v0.1.25) View Source
An island
struct and functions for the Game of Islands.
The island
struct contains the fields type, origin, coords and hits
representing the characteristics of an island in the Game of Islands.
Based on the book Functional Web Development by Lance Halvorsen.
Link to this section Summary
Types
A set of squares
Grid cell e.g. "a1" or "c3"
A map representing a CSS grid position
An island struct for the Game of Islands
Island types
Functions
Checks if all the squares of island
have been hit.
Converts island
's origin into a CSS grid position.
Returns {:hit, updated_island}
, where updated_island is island
consequently updated if guess
was a hit, or :miss
otherwise.
Returns a list of hit "cells" relative to the island
's origin.
Returns {:ok, island}
or {:error, reason}
if given an invalid type
or
origin
.
Returns an island
struct or raises if given an invalid type
or origin
.
Checks if new_island
overlaps island
.
Link to this section Types
Specs
coords() :: MapSet.t(Islands.Coord.t())
A set of squares
Specs
grid_cell() :: <<_::16>>
Grid cell e.g. "a1" or "c3"
Specs
grid_position() :: %{ gridColumnStart: Islands.Coord.col(), gridRowStart: Islands.Coord.row() }
A map representing a CSS grid position
Specs
t() :: %Islands.Island{ coords: coords(), hits: coords(), origin: Islands.Coord.t(), type: type() }
An island struct for the Game of Islands
Specs
type() :: :atoll | :dot | :l_shape | :s_shape | :square
Island types
Link to this section Functions
Specs
Checks if all the squares of island
have been hit.
Specs
grid_position(t()) :: grid_position()
Converts island
's origin into a CSS grid position.
Examples
iex> alias Islands.{Coord, Island}
iex> {:ok, origin} = Coord.new(2, 3)
iex> {:ok, atoll} = Island.new(:atoll, origin)
iex> Island.grid_position(atoll)
%{gridRowStart: 2, gridColumnStart: 3}
Specs
guess(t(), Islands.Coord.t()) :: {:hit, t()} | :miss
Returns {:hit, updated_island}
, where updated_island is island
consequently updated if guess
was a hit, or :miss
otherwise.
Specs
Returns a list of hit "cells" relative to the island
's origin.
Examples
iex> alias Islands.{Coord, Island}
iex> {:ok, origin} = Coord.new(2, 2)
iex> {:ok, atoll} = Island.new(:atoll, origin)
iex> {:ok, a1} = Coord.new(2, 2)
iex> {:ok, b1} = Coord.new(2, 3)
iex> {:ok, a3} = Coord.new(4, 2)
iex> {:hit, atoll} = Island.guess(atoll, a1)
iex> {:hit, atoll} = Island.guess(atoll, b1)
iex> {:hit, atoll} = Island.guess(atoll, a3)
iex> Island.hit_cells(atoll) |> Enum.sort()
["a1", "a3", "b1"]
Specs
new(type(), Islands.Coord.t()) :: {:ok, t()} | {:error, atom()}
Returns {:ok, island}
or {:error, reason}
if given an invalid type
or
origin
.
Examples
iex> alias Islands.{Coord, Island}
iex> {:ok, origin} = Coord.new(1, 1)
iex> {:ok, island} = Island.new(:dot, origin)
iex> %Island{origin: ^origin, coords: coords, hits: hits} = island
iex> {coords, hits}
{MapSet.new([origin]), MapSet.new()}
Specs
new!(type(), Islands.Coord.t()) :: t()
Returns an island
struct or raises if given an invalid type
or origin
.
Examples
iex> alias Islands.{Coord, Island}
iex> origin = Coord.new!(1, 1)
iex> %Island{coords: coords, hits: hits} = Island.new!(:dot, origin)
iex> {coords, hits}
{MapSet.new([origin]), MapSet.new()}
iex> alias Islands.{Coord, Island}
iex> origin = Coord.new!(10, 9)
iex> Island.new!(:square, origin)
** (ArgumentError) cannot create island, reason: :invalid_island_location
iex> alias Islands.Island
iex> origin = %{row: 10, col: 9}
iex> Island.new!(:square, origin)
** (ArgumentError) cannot create island, reason: :invalid_island_args
Specs
Checks if new_island
overlaps island
.
Examples
iex> alias Islands.{Coord, Island}
iex> square_origin = Coord.new!(1, 1)
iex> atoll_origin = Coord.new!(2, 2)
iex> square = Island.new!(:square, square_origin)
iex> atoll = Island.new!(:atoll, atoll_origin)
iex> Island.overlaps?(atoll, square)
true