ExChess.Piece (ExChess v0.1.0)

View Source

This 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.

t()

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

color()

@type color() :: :white | :black

The piece color.

t()

@type t() :: %ExChess.Piece{color: color(), type: type()}

A struct representing a chess piece. Each piece abides to a different set of rules when moving.

type()

@type type() :: :p | :r | :n | :b | :q | :k

The piece type (pawn, rook, knight, bishop, queen, king).

Functions

flip_color(color)

@spec flip_color(color()) :: color()

Returns the opposing color of the passed in color.

Examples

Flip white to black

iex> ExChess.Piece.flip_color(:white)
:black

Flip black to white

iex> ExChess.Piece.flip_color(:black)
:white

new(type, color)

@spec new(type(), color()) :: t()

Creates a new chess piece.

Examples

iex>ExChess.Piece.new(:p, :white)
%ExChess.Piece{type: :p, color: :white}

same_color?(arg1, arg2)

@spec same_color?(t() | nil, t() | nil) :: boolean()

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)
true

Two 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)
false

Non-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