View Source Islands.Coord (Islands Coord v0.1.32)
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.
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
@type col() :: 1..10
Column number
@type row() :: 1..10
Row number
@type square() :: 1..100
Square number: (row - 1) * 10 + col
A coordinates struct for the Game of Islands
Functions
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
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}}
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}}
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
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
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"
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