View Source Islands.Island (Islands Island v0.1.31)
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.
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 type
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
.
Types
@type coords() :: MapSet.t(Islands.Coord.t())
A set of squares
@type grid_cell() :: <<_::16>>
Grid cell e.g. "a1" or "c3"
@type grid_position() :: %{ gridColumnStart: Islands.Coord.col(), gridRowStart: Islands.Coord.row() }
A map representing a CSS grid position
@type t() :: %Islands.Island{ coords: coords(), hits: coords(), origin: Islands.Coord.t(), type: type() }
An island struct for the Game of Islands
@type type() :: :atoll | :dot | :l_shape | :s_shape | :square
Island type
Functions
Checks if all the squares of island
have been hit.
@spec 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}
@spec 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.
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"]
@spec 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()}
@spec 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
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