Skip to content
Daniel M edited this page Feb 12, 2022 · 9 revisions

chess

A python chess library with legal move generation, custom chess variant support, move making and unmaking, PGN/FEN loading/generating, and many other features

Requirements

Python 2.7 or Python 3

Installation

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

Basic Features

See the features section of the wiki for a list of features

Creating a new game

>>> 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")

Printing the board

To generate a unicode representation of the board, use the Game.visualized() function:

>>> print(game.visualized())
---------------------------------
|||||||||
---------------------------------
|||||||||
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|||||||||
---------------------------------
|||||||||
---------------------------------

Making a move

To make a move, use the Game.move() function:

>>> game.move("e4")
e4

Print the new board:

>>> print(game.visualized())
---------------------------------
|||||||||
---------------------------------
|||||||||
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   ||   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|||||   ||||
---------------------------------
|||||||||
---------------------------------

Takeback

To take back a move, use the Game.takeback() function:

>>> game.takeback()

Print the new board:

>>> print(game.visualized())
---------------------------------
|||||||||
---------------------------------
|||||||||
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|   |   |   |   |   |   |   |   |
---------------------------------
|||||||||
---------------------------------
|||||||||
---------------------------------
Clone this wiki locally