Skip to main content

Unit tests Pylint License: MIT

Chess Engine

This is a small chess engine written in Python. It currently supports basic moves and captures for all chess pieces (this does not include en-passant and castling). The supported game endings are checkmate and stalemate.

Chess AI

The game currently uses a simple minimax algorithm with alpha-beta pruning to decide on its best move. This works fairly well, and at recursion depth 4, the engine starts to make pretty sensible moves, such as getting hold of the center, developing major pieces, forking pieces, controlling open and semi-open files with rooks, controlling more space by playing pawn a4 -> a5, etc...

The AI is quite positionally minded, without any hard-coded square weights or similar methods. This is achieved by using a simple evaluation function in the minimax algorithm that rates the amount of legal moves the allied side can make, versus the moves of the enemy side:

len(team.compute_all_moves(board)) - len(enemy.compute_all_moves(board))

This naturally encourages a lot of activity in the center and positional play, as well as rating pieces correctly depending on the game state (e.g. a blocked rook is worthless because it can make no moves, but a rook on a semi-open file controls a lot of space and is worth a lot), without the shortcomings of a hand-crafted or hard-coded approach.

Example game

An example game of the chess AI playing against itself can be found here.

Chess artificial intelligence playing a game

Getting started

Install the package using pip, then run it using:

python -m pip install chess_ng
python -m chess_ng

Documentation

CLI

The command line options for the chess_ng package are the following:

usage: chess_ng [-h] [--depth DEPTH] [--mode {cli,auto}] [--player {1,2}] [--fen FEN] [--eval-algorithm {moves,move-distance}]
                [--resign-threshold RESIGN_THRESHOLD] [--max-moves MAX_MOVES] [--seed SEED] [--log-folder LOG_FOLDER]
                [--log-filename-suffix LOG_FILENAME_SUFFIX] [--disable-logs]

A Python chess engine

optional arguments:
  -h, --help            show this help message and exit
  --depth DEPTH, -d DEPTH
                        The minimax depth to use
  --mode {cli,auto}, -m {cli,auto}
                        The player mode
  --player {1,2}, -p {1,2}
                        The player colour
  --fen FEN, -f FEN     The FEN string with which to initialise the game
  --eval-algorithm {moves,move-distance}, -e {moves,move-distance}
                        The evaluation algorithm to use in minimax
  --resign-threshold RESIGN_THRESHOLD, -r RESIGN_THRESHOLD
                        The position rating at which to surrender
  --max-moves MAX_MOVES, --max MAX_MOVES
                        The maximum amount of moves to play
  --seed SEED, -s SEED  The random seed to be used
  --log-folder LOG_FOLDER
                        The folder to which logfiles are written
  --log-filename-suffix LOG_FILENAME_SUFFIX
                        The name suffix for logfiles
  --disable-logs        Disables log files from being written

To print the help message, run python -m chess_ng -h.

Graphical chess board

To render a graphical chess board using the chess_ng.renderer.ImageRenderer, the class expects images of size 60x60 in the following tree structure at the root of the repository:

\---media/images
    +---black
    |       bishop.png
    |       king.png
    |       knight.png
    |       pawn.png
    |       queen.png
    |       rook.png
    |
    \---white
            bishop.png
            king.png
            knight.png
            pawn.png
            queen.png
            rook.png

Images with a creative commons license can be downloaded from e.g. here.

Contributions

All contributions are welcome! All details can be found in the contribution guidelines.

Python

Written in Python 3.8.3.

License

This repository is open-source software available under the MIT license.

Contact

Please raise an issue for code changes. To reach out, please send an email to richard@baltrusch.net.

Metadata

Release files for chess-ng 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chess-ng 1.1.1
File Size Uploaded
chess_ng-1.1.1.tar.gz 23.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chess-ng 1.1.1
File Interpreter ABI Platform
chess_ng-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 51.0 kB

Release files / chess_ng-1.1.1.tar.gz

Download URL chess_ng-1.1.1.tar.gz
Size 23.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d7aad7245e877ce0d45ac4f854a0353fb15b2d5ee9afb6bbf134870b7e91e16b
BLAKE2b-256 checksum
How to use checksums
d24ea98deafc066feff7007a921edb44c057c809551ed7765dd6a116f8569b6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.9

Release files / chess_ng-1.1.1-py3-none-any.whl

Download URL chess_ng-1.1.1-py3-none-any.whl
Size 27.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b4a5c87c7600487403d9a875e940101ea7e36afc0558aad447ff54d7b763aca
BLAKE2b-256 checksum
How to use checksums
d1b84f0d19e36ce47fbaf2bbd4455821cf2c68d9340684b8ca616e09dc28d024
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.9

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page