Sunfish is a simple, but strong chess engine, written in Python, mostly for teaching purposes. Without tables and its simple interface, it takes up just 111 lines of code!
The clarity of the Sunfish code provides a great platform for experimenting, be it with evaluation functions, search extensions or anything. Fork it today and see what you can do!
My move: g8f6
8 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
7 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
6 · · · · · · · ·
5 · · · · · · · ·
4 · · · · ♟ · · ·
3 · · · · · · · ·
2 ♟ ♟ ♟ ♟ · ♟ ♟ ♟
1 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
a b c d e f g h
Your move:
Sunfish is self contained in the sunfish.py
file from the repository. I recommend running it with pypy
for optimal performance.
It is also possible to run Sunfish as an XBoard/CECP engine in PyChess, Arena or your chess interface of choice. Just add the command pypy -u xboard.py
. Ruxy Sylwyka has a note on making it all work on Windows.
- Built around the simple, but deadly efficient MTD-bi search algorithm.
- Filled with classic as well as modern 'chess engine tricks' for simpler and faster code.
- Easily adaptive evaluation function through Piece Square Tables.
- Uses standard Python collections and data structures for clarity and efficiency.
Sunfish supports castling, en passant, and promotion. It doesn't however do minor promotion or draws of any kind. All input must be done in simple 'two coordinate' notation, as shown in the screenshot.
On the technical side there are a lot of features that could be interesting to add to Sunfish. For performance, the most important might be a piecelist to save the enumeration of all board squares at every move generation. Other performance optimizations could include dedicated check detection, zobrist hashing and a mutable board representation - perhaps based on bitboards.
The evaluation in Sunfish is not very sophisticated. E.g. we don't distinguish between midgame and endgame. Not much selective deepening is done, no threat detection and the like. Finally Sunfish might benefit from a more advanced move ordering, including such things as killer move and SEE.
The name Sunfish actually refers to the Pygmy Sunfish, which is among the very few fish to start with the letters 'Py'. The use of a fish is in the spirit of great engines such as Stockfish, Zappa and Rybka.
In terms of Heritage, Sunfish borrows much more from Micro-Max by Geert Muller and PyChess.