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
Science Score: 46.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
✓Academic publication links
Links to: arxiv.org -
✓Committers with academic emails
3 of 75 committers (4.0%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (7.7%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
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
Basic Info
- Host: GitHub
- Owner: niklasf
- License: gpl-3.0
- Language: Python
- Default Branch: master
- Homepage: https://python-chess.readthedocs.io/en/latest/
- Size: 12.3 MB
Statistics
- Stars: 2,642
- Watchers: 71
- Forks: 549
- Open Issues: 35
- Releases: 112
Topics
Metadata Files
README.rst
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
>>> 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 `__
--------------------------------------------------------------------
* `Core `_
* `PGN parsing and writing `_
* `Polyglot opening book reading `_
* `Gaviota endgame tablebase probing `_
* `Syzygy endgame tablebase probing `_
* `UCI/XBoard engine communication `_
* `Variants `_
* `Changelog `_
Features
--------
* Includes mypy typings.
* IPython/Jupyter Notebook integration.
`SVG rendering docs `_.
.. code:: python
>>> board # doctest: +SKIP
.. image:: https://backscattering.de/web-boardimage/board.png?fen=r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR&lastmove=h5f7&check=e8
:alt: r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR
* Chess variants: Standard, Chess960, Suicide, Giveaway, Atomic,
King of the Hill, Racing Kings, Horde, Three-check, Crazyhouse.
`Variant docs `_.
* Make and unmake moves.
.. code:: python
>>> Nf3 = chess.Move.from_uci("g1f3")
>>> board.push(Nf3) # Make the move
>>> board.pop() # Unmake the last move
Move.from_uci('g1f3')
* Show a simple ASCII board.
.. code:: python
>>> board = chess.Board("r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 4")
>>> print(board)
r . b q k b . r
p p p p . Q p p
. . n . . n . .
. . . . p . . .
. . B . P . . .
. . . . . . . .
P P P P . P P P
R N B . K . N R
* Detects checkmates, stalemates and draws by insufficient material.
.. code:: python
>>> board.is_stalemate()
False
>>> board.is_insufficient_material()
False
>>> board.outcome()
Outcome(termination=, winner=True)
* Detects repetitions. Has a half-move clock.
.. code:: python
>>> board.can_claim_threefold_repetition()
False
>>> board.halfmove_clock
0
>>> board.can_claim_fifty_moves()
False
>>> board.can_claim_draw()
False
With the new rules from July 2014, a game ends as a draw (even without a
claim) once a fivefold repetition occurs or if there are 75 moves without
a pawn push or capture. Other ways of ending a game take precedence.
.. code:: python
>>> board.is_fivefold_repetition()
False
>>> board.is_seventyfive_moves()
False
* Detects checks and attacks.
.. code:: python
>>> board.is_check()
True
>>> board.is_attacked_by(chess.WHITE, chess.E8)
True
>>> attackers = board.attackers(chess.WHITE, chess.F3)
>>> attackers
SquareSet(0x0000_0000_0000_4040)
>>> chess.G2 in attackers
True
>>> print(attackers)
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . 1 .
. . . . . . 1 .
* Parses and creates SAN representation of moves.
.. code:: python
>>> board = chess.Board()
>>> board.san(chess.Move(chess.E2, chess.E4))
'e4'
>>> board.parse_san('Nf3')
Move.from_uci('g1f3')
>>> board.variation_san([chess.Move.from_uci(m) for m in ["e2e4", "e7e5", "g1f3"]])
'1. e4 e5 2. Nf3'
* Parses and creates FENs, extended FENs and Shredder FENs.
.. code:: python
>>> board.fen()
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
>>> board.shredder_fen()
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w HAha - 0 1'
>>> board = chess.Board("8/8/8/2k5/4K3/8/8/8 w - - 4 45")
>>> board.piece_at(chess.C5)
Piece.from_symbol('k')
* Parses and creates EPDs.
.. code:: python
>>> board = chess.Board()
>>> board.epd(bm=board.parse_uci("d2d4"))
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - bm d4;'
>>> ops = board.set_epd("1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - bm Qd1+; id \"BK.01\";")
>>> ops == {'bm': [chess.Move.from_uci('d6d1')], 'id': 'BK.01'}
True
* Detects `absolute pins and their directions `_.
* Reads Polyglot opening books.
`Docs `__.
.. code:: python
>>> import chess.polyglot
>>> book = chess.polyglot.open_reader("data/polyglot/performance.bin")
>>> board = chess.Board()
>>> main_entry = book.find(board)
>>> main_entry.move
Move.from_uci('e2e4')
>>> main_entry.weight
1
>>> book.close()
* Reads and writes PGNs. Supports headers, comments, NAGs and a tree of
variations.
`Docs `__.
.. code:: python
>>> import chess.pgn
>>> with open("data/pgn/molinari-bordais-1979.pgn") as pgn:
... first_game = chess.pgn.read_game(pgn)
>>> first_game.headers["White"]
'Molinari'
>>> first_game.headers["Black"]
'Bordais'
>>> first_game.mainline() # doctest: +ELLIPSIS
>>> first_game.headers["Result"]
'0-1'
* Probe Gaviota endgame tablebases (DTM, WDL).
`Docs `__.
* Probe Syzygy endgame tablebases (DTZ, WDL).
`Docs `__.
.. code:: python
>>> import chess.syzygy
>>> tablebase = chess.syzygy.open_tablebase("data/syzygy/regular")
>>> # Black to move is losing in 53 half moves (distance to zero) in this
>>> # KNBvK endgame.
>>> board = chess.Board("8/2K5/4B3/3N4/8/8/4k3/8 b - - 0 1")
>>> tablebase.probe_dtz(board)
-53
>>> tablebase.close()
* Communicate with UCI/XBoard engines. Based on ``asyncio``.
`Docs `__.
.. code:: python
>>> import chess.engine
>>> engine = chess.engine.SimpleEngine.popen_uci("stockfish")
>>> board = chess.Board("1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - 0 1")
>>> limit = chess.engine.Limit(time=2.0)
>>> engine.play(board, limit) # doctest: +ELLIPSIS
>>> engine.quit()
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 `_ |
| :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 `_ |
| :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 `_ |
| :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 `_ |
| :width: 64 | |
| :height: 64 | |
| :target: https://github.com/trevorbayless/cli-chess | A highly customizable way to play chess in your terminal |
+------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------+
* extensions to build engines (search and evaluation) – https://github.com/Mk-Chan/python-chess-engine-extensions
* a stand-alone chess computer based on DGT board – https://picochess.com/
* a bridge between Lichess API and chess engines – https://github.com/lichess-bot-devs/lichess-bot
* a command-line PGN annotator – https://github.com/rpdelaney/python-chess-annotator
* an HTTP microservice to render board images – https://github.com/niklasf/web-boardimage
* building a toy chess engine with alpha-beta pruning, piece-square tables, and move ordering – https://healeycodes.com/building-my-own-chess-engine/
* a JIT compiled chess engine – https://github.com/SamRagusa/Batch-First
* teaching Cognitive Science – `https://jupyter.brynmawr.edu `_
* an `Alexa skill to play blindfold chess `_ – https://github.com/laynr/blindfold-chess
* a chessboard widget for PySide2 – https://github.com/H-a-y-k/hichesslib
* Django Rest Framework API for multiplayer chess – https://github.com/WorkShoft/capablanca-api
* a `browser based PGN viewer `_ written in PyScript – https://github.com/nmstoker/ChessMatchViewer
* an accessible chessboard that allows blind and visually impaired players to play chess against Stockfish – https://github.com/blindpandas/chessmart
* a web-based chess vision exercise – https://github.com/3d12/rookognition
Prior art
---------
Thanks to the Stockfish authors and thanks to Sam Tannous for publishing his
approach to `avoid rotated bitboards with direct lookup (PDF) `_
alongside his GPL2+ engine `Shatranj `_.
Some move generation ideas are taken from these sources.
Thanks to Ronald de Man for his
`Syzygy endgame tablebases `_.
The probing code in python-chess is very directly ported from his C probing code.
Thanks to `Kristian Glass `_ 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.
Owner
- Name: Niklas Fiekas
- Login: niklasf
- Kind: user
- Location: Germany
- Website: https://backscattering.de
- Repositories: 241
- Profile: https://github.com/niklasf
GitHub Events
Total
- Create event: 2
- Release event: 1
- Issues event: 31
- Watch event: 226
- Issue comment event: 51
- Push event: 13
- Pull request review comment event: 5
- Pull request review event: 5
- Pull request event: 19
- Fork event: 45
Last Year
- Create event: 2
- Release event: 1
- Issues event: 31
- Watch event: 226
- Issue comment event: 51
- Push event: 13
- Pull request review comment event: 5
- Pull request review event: 5
- Pull request event: 19
- Fork event: 45
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Niklas Fiekas | n****s@b****e | 3,440 |
| Mark Harrison | m****n@g****m | 106 |
| Boštjan Mejak | b****a@g****m | 68 |
| Manik Charan | m****1@g****m | 59 |
| Jean-Noel Avila | j****a@f****r | 12 |
| Hugo | h****k | 8 |
| Tanmay Garg | 4****4 | 8 |
| gbtami | g****i@g****m | 8 |
| TesseractA | t****z@g****m | 7 |
| Cash Costello | c****o@g****m | 6 |
| Daniel Dugovic | d****d@g****m | 4 |
| ProgramFOX | p****x@h****e | 3 |
| kraktus | 5****s | 3 |
| AInnuganti | 1****i | 3 |
| Adrien Barbaresi | b****i@b****e | 3 |
| Richard C Gerkin | r****n@a****u | 3 |
| Self-Perfection | a****t@g****m | 3 |
| Trevor Bayless | t****1@g****m | 3 |
| laniakea64 | l****4 | 3 |
| johndoknjas | j****s@s****a | 2 |
| Fabian Fichter | i****b | 2 |
| Joerg Sonnenberger | j****g@b****e | 2 |
| Michel Van den Bergh | m****h@u****e | 2 |
| Mickaël Schoentgen | c****t@t****r | 2 |
| Paul Philippov | t****p@g****m | 2 |
| mauricesvp | m****e@w****e | 2 |
| rheber | r****r@r****m | 2 |
| DonLarry | d****o@g****m | 2 |
| Alexander Lyashuk | m****h@g****m | 2 |
| salman69e27 | a****4@g****m | 2 |
| and 45 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 163
- Total pull requests: 68
- Average time to close issues: 2 months
- Average time to close pull requests: 17 days
- Total issue authors: 118
- Total pull request authors: 39
- Average comments per issue: 2.13
- Average comments per pull request: 1.72
- Merged pull requests: 46
- Bot issues: 0
- Bot pull requests: 2
Past Year
- Issues: 34
- Pull requests: 18
- Average time to close issues: 16 days
- Average time to close pull requests: 17 days
- Issue authors: 27
- Pull request authors: 10
- Average comments per issue: 1.21
- Average comments per pull request: 1.44
- Merged pull requests: 8
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- niklasf (16)
- kraktus (4)
- Torom (4)
- GabrieleBattaglia (4)
- Phillyclause89 (3)
- mooskagh (3)
- amalrajjs (3)
- HarshilPatel007 (3)
- alexlopespereira (3)
- jamesbraza (3)
- fsmosca (2)
- johndoknjas (2)
- startnow2024 (2)
- flashdens (2)
- axtrace (2)
Pull Request Authors
- MarkZH (13)
- adbar (4)
- trevorbayless (3)
- laniakea64 (3)
- dependabot[bot] (2)
- Lukasel (2)
- eyalk11 (2)
- jsonn (2)
- deepyaman (2)
- plutotree (2)
- R3dan (2)
- dubslow (2)
- shantanucoder (2)
- evidencebp (2)
- 3d12 (2)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 4
-
Total downloads:
- pypi 568,241 last-month
- Total docker downloads: 24,427,060
-
Total dependent packages: 50
(may contain duplicates) -
Total dependent repositories: 1,026
(may contain duplicates) - Total versions: 244
- Total maintainers: 2
pypi.org: chess
A chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing, and XBoard/UCI engine communication.
- Homepage: https://github.com/niklasf/python-chess
- Documentation: https://python-chess.readthedocs.io
- License: GPL-3.0+
-
Latest release: 1.11.2
published 12 months ago
Rankings
Maintainers (1)
pypi.org: python-chess
A chess library with move generation, move validation, and support for common formats.
- Homepage: https://github.com/niklasf/python-chess
- Documentation: https://python-chess.readthedocs.io
- License: GPL-3.0+
-
Latest release: 1.2.0
published over 5 years ago
Rankings
Maintainers (1)
proxy.golang.org: github.com/niklasf/python-chess
- Documentation: https://pkg.go.dev/github.com/niklasf/python-chess#section-documentation
- License: gpl-3.0
-
Latest release: v1.11.2
published 12 months ago
Rankings
pypi.org: python-chess-bughouse
A pure Python chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing and XBoard/UCI engine communication.
- Homepage: https://github.com/niklasf/python-chess
- Documentation: https://python-chess-bughouse.readthedocs.io/
- License: GPL-3.0+
-
Latest release: 0.28.0
published almost 3 years ago
Rankings
Maintainers (1)
Dependencies
- actions/checkout v3 composite
- github/codeql-action/analyze v2 composite
- github/codeql-action/init v2 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- Sphinx ==6.1.2
- chess >=1,<2