ExChess.Board (ExChess v0.1.0)
View SourceExposes a set of types and functions to work with the game's chess board.
Summary
Types
A map with ExChess.Squares as keys and ExChess.Pieces as values.
Functions
Returns an empty board.
Gets the piece from square of board. Returns the ExChess.Piece or nil when not found.
Returns a list of all pieces of color on board.
Creates a new chess board in the starting position: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
Returns an updated board with square's value set to piece.
Returns true if square of board has no piece on it, otherwise false.
Returns an updated board where the square and it's piece are unset.
Types
@type t() :: %{required(ExChess.Square.t()) => ExChess.Piece.t()}
A map with ExChess.Squares as keys and ExChess.Pieces as values.
If a square on the board is occupied by no piece, it should not appear in the map's keys at all.
This implementation is quite primitive and not very performant, so in later versions it will be replaced with a different board representation, probably bitboards.
Functions
@spec empty() :: t()
Returns an empty board.
Currently not very useful, but the client code should not care about the fact the board is a map.
Examples
iex> ExChess.Board.empty()
...> |> ExChess.Fen.from_board()
"8/8/8/8/8/8/8/8"
@spec get(t(), ExChess.Square.t()) :: ExChess.Piece.t() | nil
Gets the piece from square of board. Returns the ExChess.Piece or nil when not found.
Examples
Piece found
iex> white_king_square = ExChess.Square.new(4, 0)
iex> ExChess.Board.new()
...> |> ExChess.Board.get(white_king_square)
%ExChess.Piece{type: :k, color: :white}Piece not found
iex> ExChess.Board.empty()
...> |> ExChess.Board.get(ExChess.Square.new(0, 0))
nil
@spec get_pieces_by_color(t(), ExChess.Piece.color()) :: [ {ExChess.Square.t(), ExChess.Piece.t()} ]
Returns a list of all pieces of color on board.
The returned element is a list of tuples where the first element is the ExChess.Square, and the second element is the ExChess.Piece.
Examples
Empty board
iex> ExChess.Board.empty()
...> |> ExChess.Board.get_pieces_by_color(:white)
[]Board with pieces
iex> ExChess.Fen.to_board("k7/1p6/8/8/8/8/8/8")
...> |> ExChess.Board.get_pieces_by_color(:black)
[
{%ExChess.Square{file: 0, rank: 7}, %ExChess.Piece{type: :k, color: :black}},
{%ExChess.Square{file: 1, rank: 6}, %ExChess.Piece{type: :p, color: :black}}
]
@spec new() :: t()
Creates a new chess board in the starting position: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
Examples
iex> ExChess.Board.new()
...> |> ExChess.Fen.from_board()
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
@spec set(t(), ExChess.Square.t(), ExChess.Piece.t()) :: t()
Returns an updated board with square's value set to piece.
Examples
Set piece on empty square
iex> ExChess.Board.empty()
...> |> ExChess.Board.set(ExChess.Square.new(0, 0), ExChess.Piece.new(:k, :white))
...> |> ExChess.Fen.from_board()
"8/8/8/8/8/8/8/K7"Set piece on occupied square
iex> square = ExChess.Square.new(0, 0)
iex> board = ExChess.Board.empty()
...> |> ExChess.Board.set(square, ExChess.Piece.new(:k, :white))
iex> ExChess.Fen.from_board(board)
"8/8/8/8/8/8/8/K7"
iex> ExChess.Board.set(board, square, ExChess.Piece.new(:n, :black))
...> |> ExChess.Fen.from_board()
"8/8/8/8/8/8/8/n7"
@spec square_empty?(t(), ExChess.Square.t()) :: boolean()
Returns true if square of board has no piece on it, otherwise false.
Empty
iex> ExChess.Board.empty()
...> |> ExChess.Board.square_empty?(ExChess.Square.new(0, 0))
trueNot empty
iex> white_king_square = ExChess.Square.new(4, 0)
iex> ExChess.Board.new()
...> |> ExChess.Board.square_empty?(white_king_square)
false
@spec unset(t(), ExChess.Square.t()) :: t()
Returns an updated board where the square and it's piece are unset.
Examples
Unset piece from occupied square
iex> square = ExChess.Square.new(0, 0)
iex> board = ExChess.Board.empty()
...> |> ExChess.Board.set(square, ExChess.Piece.new(:k, :white))
iex> ExChess.Fen.from_board(board)
"8/8/8/8/8/8/8/K7"
iex> ExChess.Board.unset(board, square)
...> |> ExChess.Fen.from_board()
"8/8/8/8/8/8/8/8"Unset piece from empty square
iex> ExChess.Board.empty()
...> |> ExChess.Board.unset(ExChess.Square.new(0, 0))
...> |> ExChess.Fen.from_board()
"8/8/8/8/8/8/8/8"