High-performance Python chess library in C: legal move generation, SAN/UCI parsing, and NumPy tensor export
Project description
fastchess
A high-performance chess library written in C with Python bindings. Designed as a fast drop-in replacement for python-chess in data pipelines and machine learning workflows.
Installation
pip install fastchess
Requires a C compiler and NumPy. On most systems pip will handle this automatically.
Usage
import fastchess
board = fastchess.Board() # start position
board = fastchess.Board("rnbqkbnr/...") # from FEN
# Move generation
moves = board.legal_moves_uci() # ['e2e4', 'd2d4', ...]
# Apply moves
board.push_uci("e2e4")
board.push_san("e5")
# Board state
print(board.fen())
print(board.turn) # fastchess.WHITE or fastchess.BLACK
print(board.is_check())
# Tensor for ML (18 x 8 x 8 numpy array)
tensor = board.to_tensor(canonical=True)
# Mirroring
mirrored = board.mirror()
API
fastchess.Board(fen=None)
| Method | Description |
|---|---|
copy() |
Return a copy of the board |
mirror() |
Return a vertically mirrored copy with swapped colors |
push_uci(uci) |
Apply a UCI move (e.g. "e2e4") |
push_san(san) |
Apply a SAN move; returns the UCI string |
legal_moves_uci() |
List all legal moves as UCI strings |
piece_at(sq) |
(piece_type, color) at square index, or None |
is_check() |
True if the side to move is in check |
has_kingside_castling_rights(color) |
Castling rights query |
has_queenside_castling_rights(color) |
Castling rights query |
has_legal_en_passant() |
True if a legal en passant capture exists |
to_tensor(canonical=False) |
Board as (18, 8, 8) numpy array |
fen() |
Current FEN string |
Constants
WHITE, BLACK, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastchess-0.1.0.tar.gz
(13.2 kB
view details)
File details
Details for the file fastchess-0.1.0.tar.gz.
File metadata
- Download URL: fastchess-0.1.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43922fee7ed2352de8b9a6a2c352126c8e95b57ac8c1cbe03c400f7eda331b2a
|
|
| MD5 |
733a68c361b684c47067e17ba2f7dc40
|
|
| BLAKE2b-256 |
077d611183a1a7587ba0e087d44f7933f310d99d4063965ad8d5813b04e85464
|