# View Source Islands.Coord(Islands Coord v0.1.30)

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.

# Summary

## Types

Column number

Row number

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

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

# Types

Link to this type

# col()

View Source
`@type col() :: 1..10`

Column number

Link to this type

# row()

View Source
`@type row() :: 1..10`

Row number

Link to this type

# square()

View Source
`@type square() :: 1..100`

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

Link to this type

# t()

View Source
`@type t() :: %Islands.Coord{col: col(), row: row()}`

A coordinates struct for the Game of Islands

# Functions

Link to this function

# compare(coord1, coord2)

View Source
`@spec 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``````
Link to this function

# new(square)

View Source
`@spec 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}}``````
Link to this function

# new(row, col)

View Source
`@spec 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}}``````
Link to this function

# new!(square)

View Source
`@spec 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``````
Link to this function

# new!(row, col)

View Source
`@spec 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``````
Link to this function

# to_row_col(coord)

View Source
`@spec 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"``````
Link to this function

# to_square(coord)

View Source
`@spec 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``````