Skip to main content

A pure Python chess AI (minimax algorithm) integrated with a PyGame to interact with a chess board.

Project description

Chess AI

This a very basic chess game with an ai implementation using the minimax algorithm; built in python and pygame.

Install

Install and use uv to create Python virtual environment.

curl -LsSf https://astral.sh/uv/install.sh | sh
uv venv -p 3.12
source .venv/bin/activate
uv pip install chess-minimax
chess-minimax

Alternatively you can just install and run it in your default Python environment.

pip install chess-minimax
chess_minimax

Architecture

The game mode must be equal to 0 or 1 and it's the color of the chosen pieces (zero for whites and one for blacks). The depth is the number of times that the computer "runs a simulation of the game". For example, if the depth is 2 and it is the computers turn, the computer will simulate every possible move it has available, and for every one of those moves, it will also play every possible move of their opponenent. To run the simulation the computer always assumes that the player plays the move in their best interest. The return value is a list containing (piece, move) tuples with the highest score.


Board

The Board class consists of a simple 2D list to store the chess pieces and a number of required methods like make_move(), unmake_move() (which is necessary for the minimax algorithm), methods to check if the game is finished, and an evaluation method which returns the total score of the caller's pieces minus the total score of the opponent's pieces (also required for the ai).


Chess Piece

The ChessPiece class is an abstract class and it is used as a parent for every piece. It consists of a method for moves filtering (prevent illegal moves like exposing the king) and some methods to keep the previous state of the chess piece intact (required by the ai when calling unmake_move()). Every chess piece is equipped with a get_score() function that is used when evaluating the board. The scores are 10 points for the pawns, 20 for knights, 30 for bishops and rooks, 240 for the queen and 1000 for the king.


Computer

The Computer class is a static class and it is used to get a move from the computer. There is a method that returns a random move and a method that returns an ai move using the minimax alogirthm.


AI

The minimax algorithm starts by making every possible (valid) move, and for each one of those moves, it simulates the opponents move that it's in their best interest. Note that since the goal of the game is not to "eat" the king, but instead "trap" him, the computer assumes that the player can make illegal moves. This is a recursive algorithm that stops when there are no more moves available, or the board is terminal.


Logging

By setting log=True in the board constructor parameters, the Logger.py script will produce a .txt file that contains the minimax tree of the current ai move decision.

For example in the following state

Screenshot_1

The AI will evaluate higher these moves (depth=1)

Screenshot_22

The following .txt file will be produced

Screenshot_4

You can clearly see the evaluation of 240 points by killing the queen and the evaluation of 10 points by killing the pawn.

Make sure to open the .txt file using a jetbrains ide (i.e. pycharm) in order to view the board properly due to inconsistencies in the sizes of the unicode characters.

Chess assets by John Pablok CC-BY-SA 3.0

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_minimax-0.1.5.tar.gz (120.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chess_minimax-0.1.5-py3-none-any.whl (127.1 kB view details)

Uploaded Python 3

File details

Details for the file chess_minimax-0.1.5.tar.gz.

File metadata

  • Download URL: chess_minimax-0.1.5.tar.gz
  • Upload date:
  • Size: 120.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for chess_minimax-0.1.5.tar.gz
Algorithm Hash digest
SHA256 4229fe8b8c2b454bbb29c6dc3e0e8cc496f52175700ca54bc500c44370b2b775
MD5 9c2c7d0a924e4ed249c049e0a8ccd463
BLAKE2b-256 68e82963475ba407a4fee26b7b6768d33a1760018241026a8a1739b1a3c1b4b9

See more details on using hashes here.

File details

Details for the file chess_minimax-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for chess_minimax-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4c6f6e8dbdc0e1e5bccac23fa5c83826c059abe7483ba58f31d87e951552827e
MD5 e531558061dd222b868af53f244d5a6d
BLAKE2b-256 f7c50f671e11ab8137bf3825fa46d5a241438d47d54be876f610a3ca38b5dbe6

See more details on using hashes here.

Supported by

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