ExChess.Square (ExChess v0.1.0)

View Source

The ExChess.Square module encapsulates the struct and functions used to create and work with squares.

The functions in this module do not interact with the ExChess.Board module at all and are instead focused entirely on the square struct.

Summary

Types

t()

A square with it's :file (x) and :rank (y) coordinates.

Functions

Returns the file_shift and rank_shift needed to ExChess.Square.shift/3 a square from it's current position to that of another square.

Creates a new ExChess.Square with the specified file and rank.

Evaluates whether both squares are on the same location.

Returns a new ExChess.Square with the updated coordinates by adding the file_shift and rank_shift to the current file and rank.

Evaluates whether both the coordinates of a ExChess.Square are in the 0..7 range.

Types

t()

@type t() :: %ExChess.Square{file: non_neg_integer(), rank: non_neg_integer()}

A square with it's :file (x) and :rank (y) coordinates.

The file is in range 0..7, mapping to files a through h in order. The rank is in range 0..7, mapping to ranks 1..8 on a chess board.

Functions

compare(from, to)

@spec compare(t(), t()) :: {integer(), integer()}

Returns the file_shift and rank_shift needed to ExChess.Square.shift/3 a square from it's current position to that of another square.

Examples

Equal squares

iex> first_square = ExChess.Square.new(3, 4)
iex> second_square = ExChess.Square.new(3, 4)
iex> ExChess.Square.compare(first_square, second_square)
{0, 0}

Unequal squares

iex> first_square = ExChess.Square.new(3, 4)
iex> second_square = ExChess.Square.new(4, 3)
iex> ExChess.Square.compare(first_square, second_square)
{1, -1}

new(file, rank)

@spec new(non_neg_integer(), non_neg_integer()) :: t()

Creates a new ExChess.Square with the specified file and rank.

Examples

iex> ExChess.Square.new(0, 7)
%ExChess.Square{file: 0, rank: 7}

same_location?(arg1, arg2)

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

Evaluates whether both squares are on the same location.

This is currently equivalent to checking whether both arguments are of type ExChess.Square and equal.

Examples

Equal squares

iex> first_square = ExChess.Square.new(3, 4)
iex> second_square = ExChess.Square.new(3, 4)
iex> ExChess.Square.same_location?(first_square, second_square)
true

Unequal squares

iex> first_square = ExChess.Square.new(3, 4)
iex> second_square = ExChess.Square.new(4, 3)
iex> ExChess.Square.same_location?(first_square, second_square)
false

Non-squares

iex> square = ExChess.Square.new(3, 4)
iex> ExChess.Square.same_location?(square, nil)
false
iex> ExChess.Square.same_location?(nil, square)
false

iex> ExChess.Square.same_location?(nil, nil)
false

shift(square, file_shift, rank_shift)

@spec shift(t(), integer(), integer()) :: t()

Returns a new ExChess.Square with the updated coordinates by adding the file_shift and rank_shift to the current file and rank.

Examples

Shifting from the bottom-left to the top-right corners

iex> square = ExChess.Square.new(0, 0)
iex> ExChess.Square.shift(square, 7, 7)
%ExChess.Square{file: 7, rank: 7}

Shifting from the top-right to the bottom-left corners

iex> square = ExChess.Square.new(7, 7)
iex> ExChess.Square.shift(square, -7, -7)
%ExChess.Square{file: 0, rank: 0}

valid?(square)

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

Evaluates whether both the coordinates of a ExChess.Square are in the 0..7 range.

Examples

Valid

iex> ExChess.Square.new(0, 7) |> ExChess.Square.valid?()
true

Too high

iex> ExChess.Square.new(0, 8) |> ExChess.Square.valid?()
false

Negative

iex> ExChess.Square.new(-1, 7) |> ExChess.Square.valid?()
false