Skip to content

Cubix1729/python-chess-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Python Chess Engine

Lichess bullet rating Lichess blitz rating Lichess rapid rating Lichess classical rating

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.

Usage

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.

Implementation details

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:

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 thoughts

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).

Contributing

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.

About

A Python chess engine experiment with a UCI interface, made for fun

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages