Skip to main content

Chess engine including minimax AI

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chess_ng-1.1.1.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

chess_ng-1.1.1-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file chess_ng-1.1.1.tar.gz.

File metadata

  • Download URL: chess_ng-1.1.1.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for chess_ng-1.1.1.tar.gz
Algorithm Hash digest
SHA256 d7aad7245e877ce0d45ac4f854a0353fb15b2d5ee9afb6bbf134870b7e91e16b
MD5 20161cdc74bc07cacad1c0369fb66cb3
BLAKE2b-256 d24ea98deafc066feff7007a921edb44c057c809551ed7765dd6a116f8569b6b

See more details on using hashes here.

File details

Details for the file chess_ng-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: chess_ng-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for chess_ng-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b4a5c87c7600487403d9a875e940101ea7e36afc0558aad447ff54d7b763aca
MD5 0988333203f080f8f47a9ae165ae8ed0
BLAKE2b-256 d1b84f0d19e36ce47fbaf2bbd4455821cf2c68d9340684b8ca616e09dc28d024

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page