All Projects → thomasahle → Sunfish

thomasahle / Sunfish

Licence: gpl-3.0
Sunfish: a Python Chess Engine in 111 lines of code

Programming Languages

python
139335 projects - #7 most used programming language
Roff
2310 projects

Projects that are alternatives of or similar to Sunfish

Walleye
A chess engine written from scratch in Rust ♞
Stars: ✭ 82 (-96.36%)
Mutual labels:  chess-engine, chess, chess-ai
Bit-Genie
UCI chess engine in C++
Stars: ✭ 19 (-99.16%)
Mutual labels:  chess-engine, chess, chess-ai
fastchess
Predicts the best chess move with 27.5% accuracy by a single matrix multiplication
Stars: ✭ 75 (-96.67%)
Mutual labels:  chess-engine, chess, chess-ai
magic-bits
A C++ header-only library for efficient move generation in Chess using "magic bitboards" technique
Stars: ✭ 26 (-98.85%)
Mutual labels:  chess-engine, chess
php-grandmaster
Chess engine written in PHP
Stars: ✭ 100 (-95.56%)
Mutual labels:  chess-engine, chess-ai
renpy-chess
A chess GUI built with Ren'Py, python-chess, and Stockfish. Version 2.0 of https://github.com/RuolinZheng08/renpy-chess-engine
Stars: ✭ 36 (-98.4%)
Mutual labels:  chess-engine, chess
elephantfish
elephantfish: 一个只有124行的中国象棋引擎
Stars: ✭ 129 (-94.28%)
Mutual labels:  chess-engine, chess
stockfish-chess-web-gui
Responsive chess web GUI to play against the Stockfish 10 chess engine. Multiple web GUI implementations have also been included.
Stars: ✭ 21 (-99.07%)
Mutual labels:  chess-engine, chess
ChessVisionBot
Chessbot using computer vision to play on any chess website
Stars: ✭ 32 (-98.58%)
Mutual labels:  chess, chess-ai
liground
A free, open-source and modern Chess Variant Analysis GUI for the 21st century
Stars: ✭ 41 (-98.18%)
Mutual labels:  chess-engine, chess
zahak
A UCI compatible chess AI in Go
Stars: ✭ 20 (-99.11%)
Mutual labels:  chess-engine, chess
OpenChess
A cross-platform chess game.
Stars: ✭ 18 (-99.2%)
Mutual labels:  chess-engine, chess
Batch-First
A JIT compiled chess engine which traverses the search tree in batches in a best-first manner, allowing for neural network batching, asynchronous GPU use, and vectorized CPU computations.
Stars: ✭ 27 (-98.8%)
Mutual labels:  chess, chess-ai
Realtime-OpenCV-Chess
♔ Chess-playing with Open-CV [Human vs AI (Stockfish engine)]
Stars: ✭ 18 (-99.2%)
Mutual labels:  chess-engine, chess
rustic
Rustic is a chess engine. It is written from scratch, in the Rust programming language.
Stars: ✭ 68 (-96.98%)
Mutual labels:  chess-engine, chess
littlewing
Chess engine written in Rust ♛
Stars: ✭ 27 (-98.8%)
Mutual labels:  chess-engine, chess
Pleco
A Rust-based re-write of the Stockfish Chess Engine
Stars: ✭ 137 (-93.92%)
Mutual labels:  ai, chess
ConvChess
Convolutional Neural Networks learns to play chess moves
Stars: ✭ 14 (-99.38%)
Mutual labels:  chess-engine, chess
uci
A thin wrapper on a uci chess engine
Stars: ✭ 33 (-98.54%)
Mutual labels:  chess-engine, chess
bot-o-tron
Try out lichess' bot interface
Stars: ✭ 36 (-98.4%)
Mutual labels:  chess-engine, chess

Sunfish logo

Introduction

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! (see compressed.py) 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!

Screenshot

My move: g8f6

  8 ♖ ♘ ♗ ♕ ♔ ♗ · ♖
  7 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
  6 · · · · · ♘ · ·
  5 · · · · · · · ·
  4 · · · · ♟ · · ·
  3 · · · · · · · ·
  2 ♟ ♟ ♟ ♟ · ♟ ♟ ♟
  1 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
    a b c d e f g h


Your move:

Run it!

Sunfish is self contained in the sunfish.py file from the repository. I recommend running it with pypy or pypy3 for optimal performance.

If you want a more "fancy" terminal experience, run fancy.py instead. (Note this requires the python-chess package.)

It is also possible to run Sunfish with a graphical interface, such as PyChess, Arena or your chess interface of choice. Sunfish' can communicate through the XBoard/CECP protocol by the command pypy -u xboard.py. Ruxy Sylwyka has a note on making it all work on Windows.

You can play sunfish now on Lichess (requires log in) or play against Recursing's Rust port, also on Lichess, which is about 100 ELO stronger.

Features

  1. Built around the simple, but deadly efficient MTD-bi search algorithm.
  2. Filled with classic as well as modern 'chess engine tricks' for simpler and faster code.
  3. Easily adaptive evaluation function through Piece Square Tables.
  4. Uses standard Python collections and data structures for clarity and efficiency.

Limitations

Sunfish supports castling, en passant, and promotion. It doesn't however do minor promotions to rooks, knights or bishops - all input must be done in simple 'two coordinate' notation, as shown in the screenshot.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].