-
Notifications
You must be signed in to change notification settings - Fork 0
Home
A python chess library with legal move generation, custom chess variant support, move making and unmaking, PGN/FEN loading/generating, and many other features
Python 2.7 or Python 3
The library can be installed directly from github using pip: pip install git+https://github.com/DanielMiao1/chess
.
For Python versions 2.5 and 2.6, the package cannot be installed using pip, so the source must be downloaded and placed within any project using it.
To import the package in Python after installation, use import chess
or from chess import SUBMODULE_OR_FEATURE
See the features section of the wiki for a list of features
>>> game = chess.Game()
To load a custom FEN, use the fen argument:
>>> game = chess.Game(fen="fen text")
Or, use the loadFEN
function to load a FEN after the game is initialized:
>>> game.loadFEN("fen text")
To generate a unicode representation of the board, use the Game.visualized()
function:
>>> print(game.visualized())
---------------------------------
| ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
---------------------------------
| ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ |
---------------------------------
| ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
---------------------------------
To make a move, use the Game.move()
function:
>>> game.move("e4")
e4
Print the new board:
>>> print(game.visualized())
---------------------------------
| ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
---------------------------------
| ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | ♙ | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| ♙ | ♙ | ♙ | ♙ | | ♙ | ♙ | ♙ |
---------------------------------
| ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
---------------------------------
To take back a move, use the Game.takeback()
function:
>>> game.takeback()
Print the new board:
>>> print(game.visualized())
---------------------------------
| ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
---------------------------------
| ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ |
---------------------------------
| ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
---------------------------------