Islands.Coord (Islands Coord v0.1.26) View Source

A coordinates struct and functions for the Game of Islands.

The coordinates struct contains the fields row and col representing the coordinates of a square in the Game of Islands.

Based on the book Functional Web Development by Lance Halvorsen.

Link to this section Summary

Types

Column number

Row number

Square number: (row - 1) * 10 + col

t()

A coordinates struct for the Game of Islands

Functions

Compares two coordinates structs based on their square numbers.

Returns {:ok, coord} or {:error, reason} if given an invalid square.

Returns {:ok, coord} or {:error, reason} if given an invalid row or col.

Returns a coordinates struct or raises if given an invalid square.

Returns a coordinates struct or raises if given an invalid row or col.

Returns "<row> <col>" or {:error, reason} if given an invalid coord.

Returns a square number or {:error, reason} if given an invalid coord.

Link to this section Types

Specs

col() :: 1..10

Column number

Specs

row() :: 1..10

Row number

Specs

square() :: 1..100

Square number: (row - 1) * 10 + col

Specs

t() :: %Islands.Coord{col: col(), row: row()}

A coordinates struct for the Game of Islands

Link to this section Functions

Specs

compare(t(), t()) :: :lt | :eq | :gt

Compares two coordinates structs based on their square numbers.

Examples

iex> alias Islands.Coord
iex> Coord.compare(Coord.new!(4, 7), Coord.new!(5, 7))
:lt

Specs

new(square()) :: {:ok, t()} | {:error, atom()}

Returns {:ok, coord} or {:error, reason} if given an invalid square.

Examples

iex> alias Islands.Coord
iex> Coord.new(99)
{:ok, %Coord{row: 10, col: 9}}

Specs

new(row(), col()) :: {:ok, t()} | {:error, atom()}

Returns {:ok, coord} or {:error, reason} if given an invalid row or col.

Examples

iex> alias Islands.Coord
iex> Coord.new(10, 10)
{:ok, %Coord{col: 10, row: 10}}

Specs

new!(square()) :: t()

Returns a coordinates struct or raises if given an invalid square.

Examples

iex> alias Islands.Coord
iex> Coord.new!(99)
%Coord{row: 10, col: 9}

iex> alias Islands.Coord
iex> Coord.new!(101)
** (ArgumentError) cannot create coord, reason: :invalid_square_number

Specs

new!(row(), col()) :: t()

Returns a coordinates struct or raises if given an invalid row or col.

Examples

iex> alias Islands.Coord
iex> Coord.new!(10, 10)
%Coord{row: 10, col: 10}

iex> alias Islands.Coord
iex> Coord.new!(0, 1)
** (ArgumentError) cannot create coord, reason: :invalid_coordinates

Specs

to_row_col(t()) :: String.t() | {:error, atom()}

Returns "<row> <col>" or {:error, reason} if given an invalid coord.

Examples

iex> alias Islands.Coord
iex> {:ok, coord} = Coord.new(2, 9)
iex> Coord.to_row_col(coord)
"2 9"

Specs

to_square(t()) :: square() | {:error, atom()}

Returns a square number or {:error, reason} if given an invalid coord.

Examples

iex> alias Islands.Coord
iex> {:ok, coord} = Coord.new(2, 9)
iex> Coord.to_square(coord)
19