niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.35k stars 512 forks source link
chess epd fen gaviota pgn polyglot syzygy uci xboard

python-chess: a chess library for Python

.. image:: https://github.com/niklasf/python-chess/workflows/Test/badge.svg :target: https://github.com/niklasf/python-chess/actions :alt: Test status

.. image:: https://badge.fury.io/py/chess.svg :target: https://pypi.python.org/pypi/chess :alt: PyPI package

.. image:: https://readthedocs.org/projects/python-chess/badge/?version=latest :target: https://python-chess.readthedocs.io/en/latest/ :alt: Docs

.. image:: https://badges.gitter.im/python-chess/community.svg :target: https://gitter.im/python-chess/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge :alt: Chat on Gitter

Introduction

python-chess is a chess library for Python, with move generation, move validation, and support for common formats. This is the Scholar's mate in python-chess:

.. code:: python

>>> import chess

>>> board = chess.Board()

>>> board.legal_moves  # doctest: +ELLIPSIS
<LegalMoveGenerator at ... (Nh3, Nf3, Nc3, Na3, h3, g3, f3, e3, d3, c3, ...)>
>>> chess.Move.from_uci("a8a1") in board.legal_moves
False

>>> board.push_san("e4")
Move.from_uci('e2e4')
>>> board.push_san("e5")
Move.from_uci('e7e5')
>>> board.push_san("Qh5")
Move.from_uci('d1h5')
>>> board.push_san("Nc6")
Move.from_uci('b8c6')
>>> board.push_san("Bc4")
Move.from_uci('f1c4')
>>> board.push_san("Nf6")
Move.from_uci('g8f6')
>>> board.push_san("Qxf7")
Move.from_uci('h5f7')

>>> board.is_checkmate()
True

>>> board
Board('r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 4')

Installing

Requires Python 3.8+. Download and install the latest release:

::

pip install chess

Documentation <https://python-chess.readthedocs.io/en/latest/>__

Features

Selected projects

If you like, share interesting things you are using python-chess for, for example:

+------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/syzygy.png?raw=true | https://syzygy-tables.info/ | | :height: 64 | | | :width: 64 | | | :target: https://syzygy-tables.info/ | A website to probe Syzygy endgame tablebases | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/maia.png?raw=true | https://maiachess.com/ | | :height: 64 | | | :width: 64 | | | :target: https://maiachess.com/ | A human-like neural network chess engine | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/clente-chess.png?raw=true | clente/chess <https://github.com/clente/chess> | | :height: 64 | | | :width: 64 | | | :target: https://github.com/clente/chess | Oppinionated wrapper to use python-chess from the R programming language | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/crazyara.png?raw=true | https://crazyara.org/ | | :height: 64 | | | :width: 64 | | | :target: https://crazyara.org/ | Deep learning for Crazyhouse | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/jcchess.png?raw=true | http://johncheetham.com <http://johncheetham.com/projects/jcchess/> | | :height: 64 | | | :width: 64 | | | :target: http://johncheetham.com/projects/jcchess/ | A GUI to play against UCI chess engines | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/pettingzoo.png?raw=true | https://pettingzoo.farama.org <https://pettingzoo.farama.org/environments/classic/chess/> | | :width: 64 | | | :height: 64 | | | :target: https://pettingzoo.farama.org/environments/classic/chess/ | A multi-agent reinforcement learning environment | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+ | .. image:: https://github.com/niklasf/python-chess/blob/master/docs/images/cli-chess.png?raw=true | cli-chess <https://github.com/trevorbayless/cli-chess> | | :width: 64 | | | :height: 64 | | | :target: https://github.com/trevorbayless/cli-chess | A highly customizable way to play chess in your terminal | +------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+

Prior art

Thanks to the Stockfish authors and thanks to Sam Tannous for publishing his approach to avoid rotated bitboards with direct lookup (PDF) <http://arxiv.org/pdf/0704.3773.pdf> alongside his GPL2+ engine Shatranj <https://github.com/stannous/shatranj>. Some move generation ideas are taken from these sources.

Thanks to Ronald de Man for his Syzygy endgame tablebases <https://github.com/syzygy1/tb>_. The probing code in python-chess is very directly ported from his C probing code.

Thanks to Kristian Glass <https://github.com/doismellburning>_ for transferring the namespace chess on PyPI.

License

python-chess is licensed under the GPL 3 (or any later version at your option). Check out LICENSE.txt for the full text.