A portable C library for representing and manipulating chess positions, and generating legal moves. This library provides a clean API for chess engines, GUIs, and tools.
- Full representation of chess positions, moves, and pieces
- Move generation and validation
- FEN parsing and generation
- Algebraic notation parsing and formatting
- Castling, en passant, promotion, and all standard chess rules
- No dependencies required, cmocka is optionally required for testing
Include the main header in your project:
#include <chess.h>Creating a new position and generating moves
#include <chess.h>
#include <stdio.h>
int main(void) {
ChessPosition position = chess_position_new();
ChessMoves moves = chess_moves_generate(&position);
printf("There is %zu legal moves:\n", moves.count);
for (size_t i = 0; i < moves.count; i++) {
char buffer[16];
chess_move_to_algebraic(&position, moves.moves[i], buffer, sizeof(buffer));
printf("%s\n", buffer);
}
chess_position_drop(&position);
return 0;
}This library is written in standard C and should compile with any modern C compiler. It requires at least C11 support. To build using cmake:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build buildchess_position_new(): Create a new position with the standard starting positionchess_position_drop(): Destroy the position, freeing all resources held by it.chess_moves_generate(): Generate all legal moveschess_move_do(): Make a move on a positionchess_position_from_fen(),chess_position_to_fen(): convert to and from FEN*_from_algebraic(),*_to_algebraic(): convert to and from algebraic notation
See the include/chess/ headers for full API documentation.
MIT License. See LICENSE for details.