This is a simple python chess engine with all the essentials - moving generation, move searching, nega-max algorithm - and some more advanced features such as piece table evaluation function and move ordering. A UI is also included which includes move highlighting and a UI for choosing which colour(s) the AI plays.
As this project is entirely built in python3, please make sure that you have Python 3 installed on your device. You can download it from the following link if you have not done so.
To install the engine, download the file directly from Git Hub or create a fork of the repository using the forked repository.
After installing the folder, open the terminal and change the directory to this file. Then, run the following to install all the extensions used:
pip install -r requirements.txt
Thereafter, simply run the file chess_game.py, either from the terminal or using a code editor. A main menu should pop up as shown below:
By default, you will be playing the white pieces and the engine will be playing black. To change this, simply click on options, which will bring up the options menu:
Simply click on the options that you desire. The options selected is automatically saved and you can simply exit back to the main menu.
Important
This program supports human vs human and ai vs ai. However, do note that if you choose ai vs ai, it is not possible to stop the program other than terminating it or waiting till the game is over.
After clicking on "play", a similar screen to this will greet you (of course, this screenshot has been played out to demonstrate the UI).
Note the following:
- The piece marked yellow is the last move made.
- The piece marked blue is the current piece selected.
- The squares marked red are the squares that the selected piece can move to.
- The pgn of the game is avaliable on the right hand side.
- To undo a move, press the
left arrowkey. To restart the game, press thePgDnkey.
When the game is over, a screen similar to this will show:
Important
Do not undo a move if it was a move made by the AI. As this engine is largely deterministic in nature as the evaluation function usually leads to only 1 best move, undoing a move will only make the engine think again and play the same move. Spamming the button will not work either but will only cause a backlog of undo moves, creating more lag.
Note
Note that the number of positions searched by the AI as well as the evaluation of the move played is printed in the terminal. Do note that the evaluation is always positive for the AI, i.e. no matter the colour, the higher the number, the better the AI thinks the move is. If the AI sees mate, the evaluation will either be 100000 (if it is mating) or -100000 (if it is getting mated). To translate the evaluation to our what we commonly use, simply divide the number by 100.
Special thanks to the kind people who has put up the following resources, without which I wouldn't have been able to complete this:
- The Chess Programming Wiki for all of the amazing information about creating a chess engine.
- The PESTO evaluation function which I edited slightly to create the evaluation function used.
- The Chess Engine in Python Series by Eddie Sharick which I took reference from whenever I am stuck or was looking for inspiration.



