ExChess (ExChess v0.1.0)
View SourceExChess is a, although still primitive, comprehensive implementation of the chess game rules in Elixir.
Examples
Starting a game
iex> ExChess.start_game()
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"Starting a game using FEN
iex> ExChess.start_game("rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2")
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2"Making a move
iex> ExChess.start_game()
...> |> ExChess.move("Nf3")
...> |> ExChess.move("Nf6")
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2"Getting a piece from the board
iex> game = ExChess.start_game()
iex> square = ExChess.Square.new(0, 0)
iex> ExChess.Board.get(game.board, square)
%ExChess.Piece{type: :r, color: :white}Getting legal moves for a piece
iex> ExChess.start_game()
...> |> ExChess.list_legal_moves(ExChess.Square.new(1, 1))
[%ExChess.Square{file: 1, rank: 2}, %ExChess.Square{file: 1, rank: 3}]
Summary
Functions
Allows the active player to claim a draw in case of either
Lists all available target squares for the piece situated on from_square.
Returns the updated ExChess.Game when the move is valid, and :error otherwise.
Returns the update ExChess.Game with the current active color's side having resigned and the opponent is declared the winner.
Instantiates a new ExChess.Game. An optional fen string can be passed in to start the game at a particular position.
Functions
@spec claim_draw(ExChess.Game.t()) :: ExChess.Game.t() | :error
Allows the active player to claim a draw in case of either:
- 50 move rule
- threefold repetition
Examples
50 move rule
iex> ExChess.start_game("knn5/8/8/8/8/8/8/KNN5 w - - 101 90") # 101 halfmoves
...> |> ExChess.claim_draw()
...> |> ExChess.Visualization.status()
"1/2-1/2"Threefold repetition
iex> ExChess.start_game() # first occurence
...> |> ExChess.move("Nc3") |> ExChess.move("Nc6")
...> |> ExChess.move("Nb1") |> ExChess.move("Nb8") # second occurence
...> |> ExChess.move("Nc3") |> ExChess.move("Nc6")
...> |> ExChess.move("Nb1") |> ExChess.move("Nb8") # third occurence
...> |> ExChess.claim_draw()
...> |> ExChess.Visualization.status()
"1/2-1/2"Draw unclaimable
iex> ExChess.start_game()
...> |> ExChess.claim_draw()
:errorGame already complete
iex> ExChess.start_game()
...> |> ExChess.resign()
...> |> ExChess.claim_draw()
:error
@spec list_legal_moves(ExChess.Game.t(), ExChess.Square.t()) :: [ExChess.Square.t()]
Lists all available target squares for the piece situated on from_square.
Examples
Getting legal moves for a piece
iex> ExChess.start_game()
...> |> ExChess.list_legal_moves(ExChess.Square.new(1, 1))
[%ExChess.Square{file: 1, rank: 2}, %ExChess.Square{file: 1, rank: 3}]
@spec move(ExChess.Game.t(), ExChess.Move.t() | ExChessCore.San.t()) :: ExChess.Game.t() | :error
Returns the updated ExChess.Game when the move is valid, and :error otherwise.
Examples
Making a move using SAN
iex> ExChess.start_game()
...> |> ExChess.move("Nf3")
...> |> ExChess.move("Nf6")
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2"Making a move using structs
iex> first_move = ExChess.Move.new(ExChess.Square.new(6, 0), ExChess.Square.new(5, 2))
...> second_move = ExChess.Move.new(ExChess.Square.new(6, 7), ExChess.Square.new(5, 5))
...> ExChess.start_game()
...> |> ExChess.move(first_move)
...> |> ExChess.move(second_move)
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2"Making an invalid move
iex> ExChess.start_game()
...> |> ExChess.move("a2b3")
:errorGame already complete
iex> ExChess.start_game()
...> |> ExChess.resign()
...> |> ExChess.move("h3")
:error
@spec resign(ExChess.Game.t()) :: ExChess.Game.t() | :error
Returns the update ExChess.Game with the current active color's side having resigned and the opponent is declared the winner.
If the game is already complete, this returns :error.
Examples
White resigns
iex> ExChess.start_game()
...> |> ExChess.resign()
...> |> ExChess.Visualization.status()
"0-1"Black resigns
iex> ExChess.start_game()
...> |> ExChess.move("a3")
...> |> ExChess.resign()
...> |> ExChess.Visualization.status()
"1-0"Game already complete
iex> ExChess.start_game()
...> |> ExChess.resign()
...> |> ExChess.resign()
:error
@spec start_game(ExChess.Fen.t() | nil) :: ExChess.Game.t()
Instantiates a new ExChess.Game. An optional fen string can be passed in to start the game at a particular position.
Examples
Starting a game
iex> ExChess.start_game()
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"Starting a game using FEN
iex> ExChess.start_game("rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2")
...> |> ExChess.Visualization.game()
"STATUS: * | FEN: rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2"