Skip to content

thomasahle/sunfish

Repository files navigation

Sunfish logo

Introduction

Sunfish is a simple, but strong chess engine, written in Python. With its simple UCI interface, and removing comments and whitespace, it takes up just 131 lines of code! (build/clean.sh sunfish.py | wc -l). Yet it plays at ratings above 2000 at Lichess.

Because Sunfish is small and strives to be simple, the code provides a great platform for experimenting. People have used it for testing parallel search algorithms, experimenting with evaluation functions, and developing deep learning chess programs. Fork it today and see what you can do!

Play against sunfish!

The simplest way to run sufish is through the "fancy" terminal interface:

$ tools/fancy.py -cmd ./sunfish.py
Playing against sunfish 2023.
Do you want to be white or black? black
  1 ♖ ♘ ♗ ♔ ♕ ♗ ♘ ♖
  2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
  3
  4
  5
  6
  7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
  8 ♜ ♞ ♝ ♚ ♛ ♝ ♞ ♜
    h g f e d c b a

Score: 23, nodes: 11752, nps: 13812, time: 0.9
 My move: d4
  1 ♖ ♘ ♗ ♔ ♕ ♗ ♘ ♖
  2 ♙ ♙ ♙ ♙   ♙ ♙ ♙
  3
  4         ♙
  5
  6
  7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
  8 ♜ ♞ ♝ ♚ ♛ ♝ ♞ ♜
    h g f e d c b a

Your move (e.g. c6 or g8h6): Nf6

Note this requires the python-chess package. For a true minimalist experience, first we can "pack" sunfish into a compressed executable (less than 3KB!) and run it directly:

$ build/pack.sh sunfish.py packed.sh
Total length: 2953
$ ./packed.sh
go wtime 1000 btime 1000 winc 1000 binc 1000
info depth 1 score cp 0 pv d2d4
bestmove d2d4

(See the UCI specification for the full set of commands.)

Playing with a graphical interface

It is also possible to run Sunfish with a graphical interface, such as PyChess or Arena.

Finally you can play sunfish now on Lichess or play against Recursing's Rust port, also on Lichess, which is about 100 ELO stronger.

NNUE version

There is an experimental version using an Efficiently updatable neural network. You can test it using the fancy terminal interface as above:

$ tools/fancy.py -cmd "./sunfish_nnue.py nnue/models/tanh.pickle"
...

In contrast to the large NNUE in say, Stockfish, this network is only 1207 bytes! That makes sure sunfish NNUE can still be packed into less than 4KB. Using NNUE, sunfish will play better positionally, but worse tactically, since the implementation is still not fast enough.

Features

  1. Built around the simple, but efficient MTD-bi search algorithm, also known as C*.
  2. Filled with classic "chess engine tricks" for simpler and faster code.
  3. Efficiently updatedable evaluation function through Piece Square Tables.
  4. Uses standard Python collections and data structures for clarity and efficiency.

Limitations

Sunfish supports all chess rules, except the 50 moves draw rule.

There are many ways in which you may try to make Sunfish stronger. First you could change from a board representation to a mutable array and add a fast way to enumerate pieces. Then you could implement dedicated capture generation, check detection and check evasions. You could also move everything to bitboards, implement parts of the code in C or experiment with parallel search!

The other way to make Sunfish stronger is to give it more knowledge of chess. The current evaluation function only uses piece square tables - it doesn't even distinguish between midgame and endgame. You can also experiment with more pruning - currently only null move is done - and extensions - currently none are used. Finally Sunfish might benefit from a more advanced move ordering, MVV/LVA and SEE perhaps?

An easy way to get a strong Sunfish is to run with with the PyPy Just-In-Time intepreter. In particular the python2.7 version of pypy gives a 250 ELO boost compared to the cpython (2 or 3) intepreters at fast time controls:

Rank Name                    Elo     +/-   Games   Score   Draws
   1 pypy2.7 (7.1)           166      38     300   72.2%   19.7%
   2 pypy3.6 (7.1)            47      35     300   56.7%   21.3%
   3 python3.7               -97      36     300   36.3%   20.7%
   4 python2.7              -109      35     300   34.8%   24.3%

Why Sunfish?

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.

License

GNU GPL v3