Islands.Island (Islands Island v0.1.37)

View Source

An island struct and functions for the Game of Islands.

The island struct contains the fields:

  • type
  • origin
  • coords
  • hits

representing the properties 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

t()

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

coords()

@type coords() :: MapSet.t(Islands.Coord.t())

A set of squares

grid_cell()

@type grid_cell() :: <<_::16>>

Grid cell e.g. "a1" or "c3"

grid_position()

@type grid_position() :: %{
  gridColumnStart: Islands.Coord.col(),
  gridRowStart: Islands.Coord.row()
}

A map representing a CSS grid position

t()

@type t() :: %Islands.Island{
  coords: coords(),
  hits: coords(),
  origin: Islands.Coord.t(),
  type: type()
}

An island struct for the Game of Islands

type()

@type type() :: :atoll | :dot | :l_shape | :s_shape | :square

Island type

Functions

forested?(island)

@spec forested?(t()) :: boolean()

Checks if all the squares of island have been hit.

grid_position(island)

@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}

guess(island, guess)

@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.

hit_cells(island)

@spec hit_cells(t()) :: [grid_cell()]

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"]

new(type, origin)

@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()}

new!(type, origin)

@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

overlaps?(new_island, island)

@spec overlaps?(t(), t()) :: boolean()

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