- Elixir 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
All checks were successful
Elixir CI / Build and test (push) Successful in 2m11s
PGN writes the promotion piece in upper case for both colours, while a Black promotion move holds the lower case piece (:q, :r, :b, :n). The parser compared them case-sensitively, so a legal "b1=Q" was rejected with "no legal move matches", and the writer emitted the non-standard "b1=q". Compare promotion letters case-insensitively when matching a token, and write the promotion piece in upper case for both sides. Add SAN and PGN tests that parse, write and round-trip a Black promotion. |
||
| .forgejo/workflows | ||
| .github/workflows | ||
| .zed | ||
| doc | ||
| lib | ||
| test | ||
| .formatter.exs | ||
| .gitignore | ||
| AGENTS.md | ||
| FORGEJO_CI.md | ||
| LICENSE | ||
| mix.exs | ||
| README.md | ||
| TODO.md | ||
chess_ex
ChessEx is an Elixir project implementing tools related to Chess, including an engine and support for various notations.
This is a foundational project that has no real UI. The related chess_ex_ui project offers a web interface to actual play against the chess_ex engine.
A live demo can be found at https://chess.stfx.eu Completed games are recorded anonymously and will appear in a history for replay or download.
usage
Play interactively in IEx with Chess.Repl:
iex> {:ok, game} = Chess.Repl.new
New game, initial position
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Evaluation: +0.00 for White (material +0.00, positional +0.00)
White to move.
{:ok, #Chess.Game<>}
iex> {:ok, game} = game |> Chess.Repl.move("e4")
1. e4
white pawn from e2 to e4, advances two squares
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
Evaluation: +0.00 for White (material +0.00, positional +0.00)
Black to move.
{:ok, #Chess.Game<>}
iex> {:ok, game} = game |> Chess.Repl.move # engine move
2. e5
black pawn from e7 to e5, advances two squares
r n b q k b n r
p p p p . p p p
. . . . . . . .
. . . . p . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
Evaluation: +0.00 for White (material +0.00, positional +0.00)
White to move.
{:ok, #Chess.Game<>}
iex> Chess.Repl.clear() # restore full game inspection
:ok
move/1 plays the :search strategy, so the reply comes from the opening
book (one of e5, c5, e6 or c6 after 1. e4) and can differ between
games. move/2 also takes engine options, for example
Chess.Repl.move(game, strategy: :best) for a quick 1-ply move or
Chess.Repl.move(game, depth: 2) for a shallower search.
The board of the starting position:
iex> Chess.Board.initial |> Chess.Board.print
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Pass extended: true to Chess.Board.to_ascii/2 (or Chess.Board.print/2) to add
file and rank labels and a border around the board:
iex> Chess.Board.initial |> Chess.Board.print(extended: true)
a b c d e f g h
+-----------------+
8 | r n b q k b n r | 8
7 | p p p p p p p p | 7
6 | . . . . . . . . | 6
5 | . . . . . . . . | 5
4 | . . . . . . . . | 4
3 | . . . . . . . . | 3
2 | P P P P P P P P | 2
1 | R N B Q K B N R | 1
+-----------------+
a b c d e f g h
A game read from a PGN file. The moves are the moves of the game, in the order they were played, and the game itself is the state after the last move:
iex> pgn = Chess.PGN.parse_file("test/fixtures/The_Opera_game.pgn")
iex> pgn.result
"1-0"
iex> length(pgn.moves)
33
iex> Chess.Board.print(pgn.game.board)
. n . R k b . r
p . . . . p p p
. . . . q . . .
. . . . p . B .
. . . . P . . .
. . . . . . . .
P P P . . P P P
. . K . . . . .
A game written back as PGN:
iex> IO.puts(Chess.PGN.to_pgn(pgn))
[Event "Paris"]
[Site "Paris FRA"]
...
1. e4 e5 2. Nf3 d6 3. d4 Bg4 4. dxe5 Bxf3 5. Qxf3 dxe5 6. Bc4 Nf6 7. Qb3 Qe7 8.
Nc3 c6 9. Bg5 b5 10. Nxb5 cxb5 11. Bxb5+ Nbd7 12. O-O-O Rd8 13. Rxd7 Rxd7 14.
Rd1 Qe6 15. Bxd7+ Nxd7 16. Qb8+ Nxb8 17. Rd8#
1-0
A position read from a FEN record. The record holds the placement of the pieces, the side to move, the castling rights, the en passant target and the two counters of the game:
iex> game = Chess.FEN.parse("r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3")
iex> Chess.Board.print(game.board)
r . b q k b n r
p p p p . p p p
. . n . . . . .
. . . . p . . .
. . . . P . . .
. . . . . N . .
P P P P . P P P
R N B Q K B . R
iex> game.side_to_move
:white
The position is written back as a record:
iex> IO.puts(Chess.FEN.to_fen(game))
r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3
A PGN game may start from a position of its own. The position then stands in
the tag pair FEN, and the moves of the game are read and written against
it.
A game described step by step. Every ply holds the move in algebraic notation, the move in words, the board it leads to and the strength of the position, and the text ends with how the game stopped:
iex> pgn |> Chess.Narrator.print
Paul Morphy (White) vs Duke Karl / Count Isouard (Black)
Initial position
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Evaluation: +0.00 for White (material +0.00, positional +0.00)
1. e4
white pawn from e2 to e4, advances two squares
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
Evaluation: +0.00 for White (material +0.00, positional +0.00)
Captures, castling, promotions and checks are spelled out as they happen:
19. Nxb5
white knight from c3 to b5, captures black pawn on b5
23. O-O-O
white king castles queen side from e1 to c1, rook from a1 to d1
33. Rd8#
white rook from d1 to d8, black is checkmated
Chess.Narrator.print/1 writes the whole description out; the text ends with
a sentence about how the game stopped, as in black is checkmated. White wins (1-0).
How a game stands, and who won it:
iex> game = Chess.PGN.parse_file("test/fixtures/The_Opera_game.pgn").game
iex> Chess.Game.status(game)
:checkmate
iex> Chess.Game.winner(game)
:white
iex> Chess.Game.over?(game)
true
A game that is still running is :ongoing; a drawn game is one of
:draw_repetition, :draw_fivefold_repetition, :draw_fifty_moves,
:draw_seventy_five_moves or :draw_insufficient_material, and a drawn game
has no winner.
Playing a game automatically with the engine:
iex> game = Chess.Game.new()
iex> {:ok, game, move} = Chess.Engine.play(game)
iex> move in [{:b1, :c3}, {:g1, :f3}]
true
iex> {:ok, game, _move} = Chess.Engine.play(game, strategy: :random)
iex> game.side_to_move
:white
iex> Chess.Engine.best_move(Chess.Game.new(), randomize: false)
{:ok, {:b1, :c3}}
Chess.Engine.play/2 applies the move and returns {:ok, game, move}. It uses
1-ply evaluation by default (:best strategy): the two knight developments
score best and one of them is picked randomly. It can also pick a :random
move. When the game is over, it returns {:error, reason} with the game
status.
The :search strategy plays a move found by a depth-limited search:
game = Chess.Game.new()
{:ok, game, move} = Chess.Engine.play(game, strategy: :search, depth: 3)
{:ok, reply} = Chess.Engine.search_move(game, depth: 2, randomize: false)
depth counts plies (half-moves), so depth: 4 looks at a move, the reply,
another move and its reply; the default is 4. Chess.Search explains the
search:
it uses negamax (minimax with a single side-to-move score and a sign flip
per move) with alpha-beta pruning, which skips lines that cannot change
the result. Captures are ordered first by MVV-LVA (Most Valuable Victim,
Least Valuable Attacker). Material and piece-square tables from
Chess.Evaluation score quiet positions. A small compile-time opening book
(Chess.Book) is consulted before the search and yields a move with score 0;
pass book: false to search the position itself. There is no endgame
tablebase; deeper searches are stronger but slower.
UCI interface
The UCI engine is an escript that speaks the protocol on stdin/stdout, the way a GUI expects it:
mix escript.build
./chess_ex
Point a GUI at ./chess_ex, or run a session by hand:
printf 'uci\nisready\nposition startpos moves e2e4\ngo depth 2\n' | ./chess_ex
id name ChessEx 0.1.2
id author ChessEx contributors
uciok
readyok
info depth 1 score cp 0 time 3 pv c7c6
info depth 2 score cp 0 time 4 pv c7c6
bestmove c7c6
c7c6 (the Caro-Kann Defence) is a book move: replies to 1. e4 come
straight from Chess.Book, so they are reported with score cp 0 without
searching.
The supported commands are uci, isready, ucinewgame, position
(startpos or fen, with an optional moves list), go, stop and quit.
go understands depth, movetime, infinite, and the clock options
wtime, btime, winc, binc and movestogo; the clock budget is shared
out over the moves left and the search deepens until the budget is used. An
automatic search stops at depth 4; go depth N can go deeper, and an explicit
depth wins over any time limit. infinite deepens to the automatic maximum
unless stopped. The search does not advertise UCI_LimitStrength or
UCI_Elo, because the engine has no strength setting.
A go runs in a process of its own, so isready, stop and quit are
answered while the engine is thinking. stop answers with the best move of
the last completed iteration. The search itself has no time check inside a
single iteration, so a time limit is soft: a deeper iteration that has already
started can overshoot it. Nothing but UCI lines is written to stdout.
references
- https://en.wikipedia.org/wiki/Chess
- https://en.wikipedia.org/wiki/Chess_notation
- https://en.wikipedia.org/wiki/Forsyth–Edwards_Notation
- https://en.wikipedia.org/wiki/Portable_Game_Notation
- https://en.wikipedia.org/wiki/Algebraic_notation_(chess)
- https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode
- https://en.wikipedia.org/wiki/Chess_engine
- https://en.wikipedia.org/wiki/Universal_Chess_Interface