A chess library with bindings to libchess.
Project description
Introduction
This is the scholars mate in python-chess:
pos = chess.Position() pos.make_move_from_san("e4") pos.make_move_from_san("e5") pos.make_move_from_san("Qh5") pos.make_move_from_san("Nc6") pos.make_move_from_san("Bc4") pos.make_move_from_san("Nf6") pos.make_move_from_san("Qxf7") assert pos.is_checkmate()
Features
Legal move generator and move validation. This includes all castling rules and en-passant captures.
assert not chess.Move.from_uci("a8a1") in pos.get_legal_moves()
Detects checkmates, stalemates and draws by insufficient material. Has a half-move clock.
assert not pos.is_stalemate() assert not pos.is_insufficient_material() assert pos.is_game_over()
Detects checks and can enumerate attackers and defenders of a square.
assert pos.is_check() assert chess.Square("f7") in pos.get_attackers("w", chess.Square("e8"))
Parses and creates SAN representation of moves.
pos = chess.Position() assert "e4" == pos.make_move(chess.Move("e2e4")).san
Parses and creates FENs.
assert pos.fen == "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" pos = chess.Position("8/8/8/2k5/4K3/8/8/8 w - - 4 45") assert pos["c5"] == chess.Piece("k")
Read Polyglot opening books.
book = chess.PolyglotOpeningBook("data/opening-books/performance.bin") pos = chess.Position() for entry in book.get_entries_for_position(pos): assert chess.Move.from_uci("e2e4") == entry.move break
Peformance
python-chess is not intended to be used by chess engines where performance is critical. The goal is rather to create a simple and highlevel library.
However parts like move generation are in C++ (see the libchess directory) to improve the performance over pure Python code.
Building
libboost-regex-dev and libboost-python-dev are required.
With easy_install:
sudo easy_install python-chess
From current source code:
python setup.py build sudo python setup.py install
License
python-chess is licensed under the GPL3. See the LICENSE file for the full copyright and license information.
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
Built Distribution
Hashes for python-chess-0.0.6.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7c7348c5244241d5bb2ac8fcecf53002dadc48d938db728f50ae6c0903f12c5 |
|
MD5 | 6be1fd33e966a2d0ec623450e9ddebfc |
|
BLAKE2b-256 | 3abf114db9509ce8d88695c6486832249c3b97617d223275a02461ef4e13675f |