ExChess.Square (ExChess v0.1.0)
View SourceThe 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
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
@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
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}
@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}
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)
trueUnequal 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)
falseNon-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
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}
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?()
trueToo high
iex> ExChess.Square.new(0, 8) |> ExChess.Square.valid?()
falseNegative
iex> ExChess.Square.new(-1, 7) |> ExChess.Square.valid?()
false