ExChess.Piece (ExChess v0.1.0)
View SourceThis module contains the types used to represent a piece on the chess board, as well as some utility functions to create and work with those types.
Summary
Types
The piece color.
A struct representing a chess piece. Each piece abides to a different set of rules when moving.
The piece type (pawn, rook, knight, bishop, queen, king).
Functions
Returns the opposing color of the passed in color.
Creates a new chess piece.
Returns true when both parameters are a ExChess.Piece struct with the same :color, otherwise returns false.
Types
@type color() :: :white | :black
The piece color.
A struct representing a chess piece. Each piece abides to a different set of rules when moving.
@type type() :: :p | :r | :n | :b | :q | :k
The piece type (pawn, rook, knight, bishop, queen, king).
Functions
Returns the opposing color of the passed in color.
Examples
Flip white to black
iex> ExChess.Piece.flip_color(:white)
:blackFlip black to white
iex> ExChess.Piece.flip_color(:black)
:white
Creates a new chess piece.
Examples
iex>ExChess.Piece.new(:p, :white)
%ExChess.Piece{type: :p, color: :white}
Returns true when both parameters are a ExChess.Piece struct with the same :color, otherwise returns false.
Examples
Two pieces of same color
iex> first_piece = ExChess.Piece.new(:p, :white)
iex> second_piece = ExChess.Piece.new(:n, :white)
iex> ExChess.Piece.same_color?(first_piece, second_piece)
trueTwo pieces of different color
iex> first_piece = ExChess.Piece.new(:p, :white)
iex> second_piece = ExChess.Piece.new(:p, :black)
iex> ExChess.Piece.same_color?(first_piece, second_piece)
falseNon-piece
iex> piece = ExChess.Piece.new(:n, :white)
iex> ExChess.Piece.same_color?(piece, nil)
false
iex> ExChess.Piece.same_color?(nil, piece)
false
iex> ExChess.Piece.same_color?(nil, nil)
false