• Stars
    star
    2,758
  • Rank 16,510 (Top 0.4 %)
  • Language
    Python
  • License
    Other
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sunfish: a Python Chess Engine in 111 lines of code

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

More Repositories

1

fastchess

Predicts the best chess move with 27.5% accuracy by a single matrix multiplication
Python
77
star
2

numberlink

Program for generating and solving numberlink / flow free puzzles
Go
62
star
3

codenames

Codenames AI using Word Vectors
Python
45
star
4

mcts-2048

A Monte Carlo Tree Search AI for the game 2048
Java
26
star
5

noisy-bayesian-optimization

Bayesian Optimization for very Noisy functions
Python
17
star
6

liars-dice

Liar's Dice AI in Pytorch: www.dudo.ai
Python
15
star
7

TrinityProject

A mindblowing trainbased game for entire family
Java
8
star
8

tinyknn

A tiny approximate K-Nearest Neighbour library in Python based on Fast Product Quantization and IVF
Python
7
star
9

learnit-

Userscripts for learnit
Python
5
star
10

snyd

Analysis of the end game of Liar's Dice / Dudo
Python
5
star
11

chess-openings-expectimax

Finding the best chess opening with expectimax search
Python
4
star
12

pystreams

Simple Python Multiprocessing implementation of Java's Streams / Hadoop RDD
Python
4
star
13

mersenne

TeX
4
star
14

tabulation

Fast implementation of Tabulation Hashing for strings
C
4
star
15

WarGame

An assembly based wargame written for ScoutFestival 2011
Python
4
star
16

icpc

Team Notebook for Lambdabamserne
C++
3
star
17

konkurrence

Python
2
star
18

supermajorities

A simple Python implementation of the Supermajorities algorithm
Python
2
star
19

basis-pursuit

Various algorithms for Compressed Sensing implemented in Python
Python
2
star
20

SokobanExam

Java
1
star
21

try_git

1
star
22

smallgrad

Some pretty small autograd implementations
Python
1
star
23

Morse

1
star
24

cqr

Clustered Compositional Embeddings
1
star
25

Hitman

Registration system (and more) for Spejdernes Lejr 2012
Java
1
star