This project is a small Python chess engine with a UCI interface, made for fun.
This program is very minimalistic and its level isn't very high. Its purpose is only to be a simple and fun project.
You can play games against the engine on lichess.org. In order to do that, just challenge @SomePythonBot, and enjoy the game! Currently, it is rated at around 1600~1700 elo. Note that the bot does not accept correspondence games.
If you are curious, you can also clone the repo, install python-chess, and run the engine locally,
e.g. with a GUI like Arena or CuteChess.
The goal was to make a tiny, single-file chess AI in Python. I used the library python-chess, which allowed me not to
have to implement the board and pieces representation, the move generation logic, etc...
It uses a simple evaluation function, which considers:
- Material: standard piece value evaluation
- Piece-square tables (mostly taken from the Simplified Evaluation Function presented in the Chess Programming Wiki)
- King safety using “attack units”, as described here in the Chess Programming Wiki
- Activity/mobility of the pieces: number of legal moves, with weights for piece type
- Center control: points for the control of the center squares
- Pawn structure:
- Negative points for doubled pawns
- Negative points for isolated pawns
- Bonuses for passed pawns
Regarding the search logic, it uses:
- Negamax search with alpha-beta pruning
- Quiescence search for captures, checks and promotions
- A transposition table for caching previous search results
- Null-move pruning
- Late move reductions
- Move ordering:
- Captures sorting with MVV-LVA (Most Valuable Victim - Least Valuable Attacker)
- Killer move heuristic
- History heuristic
- Transposition table move first
- A bonus for checks
- Iterative deepening, as the main search loop
The engine implements a basic UCI interface, but with no configuration options. However, you can still change manually the constants defined in the first few lines of the file.
By default, the program uses no opening book, but you can specify one by setting PATH_TO_OPENING_BOOK
to some opening book in the Polyglot format. The version running on Lichess uses one called komobo.bin,
taken from this repo.
My bot cannot compare with the vast majority of other bots, as it is quite weak. So what could be improved?
I think the main issue is the depth it searches at on average, which is only about 3~6 for rapid or classical games. This is very low, and causes the engine to miss most long-term strategies.
According to me, the main two reasons the depth is quite low are:
- The lack of pruning: techniques like Aspiration Search or Principal variation search (PVS) could probably increase the search efficiency
- The lack of optmisation: Python isn't particularly fast (even if the Lichess version runs on PyPy), and the engine does not use efficient techniques like bitboards
Still, I am happy with my engine. After all, a level around 1600 elo isn't so bad. Anyway, it destroys me whenever I play against it (even if that's not significant given my chess level).
I know this is only a small personal project, but if you find a bug, have an idea or make an improvement, I'd appreciate it if you opened an issue or a pull request.