Islands.Coord (Islands Coord v0.1.21) View Source
A coord struct and functions for the Game of Islands.
The coord 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
Functions
Compares two coord 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 coord struct or raises if given an invalid square.
Returns a coord 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
Link to this section Functions
Specs
Compares two coord structs based on their square numbers.
Examples
iex> alias Islands.Coord
iex> Coord.compare(Coord.new!(4, 7), Coord.new!(5, 7))
:lt
Specs
Returns {:ok, coord} or {:error, reason} if given an invalid square.
Examples
iex> alias Islands.Coord
iex> Coord.new(99)
{:ok, %Islands.Coord{row: 10, col: 9}}
Specs
Returns {:ok, coord} or {:error, reason}
if given an invalid row or col.
Examples
iex> alias Islands.Coord
iex> Coord.new(10, 10)
{:ok, %Islands.Coord{col: 10, row: 10}}
Specs
Returns a coord struct or raises if given an invalid square.
Examples
iex> alias Islands.Coord
iex> Coord.new!(99)
%Islands.Coord{row: 10, col: 9}
iex> alias Islands.Coord
iex> Coord.new!(101)
** (ArgumentError) cannot create coord, reason: :invalid_square_number
Specs
Returns a coord struct or raises if given an invalid row or col.
Examples
iex> alias Islands.Coord
iex> Coord.new!(10, 10)
%Islands.Coord{row: 10, col: 10}
iex> alias Islands.Coord
iex> Coord.new!(0, 1)
** (ArgumentError) cannot create coord, reason: :invalid_coordinates
Specs
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
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