15

I am looking for a chess engine which provides a normal API which I can use with some high level programming language like C# or python. I went through the list of leaders (Houdini, Komodo, Stockfish and Rybka) and was not able to find any signs that these engines provides API (which surprised me and suggested that I am missing something).

To be less vague, here is what I need from this API:

  • be able to load specific game into the engine
  • be able to evaluate position of the game
  • be able to make a move

So, does anyone knows an engine which provides a clear API to it's functions?

2
  • 2
    UCI is a API which gets command via command line switches and standard input. You can write a wrapper around those commands and call commands by functions.
    – masoud
    Commented Nov 24, 2013 at 11:08
  • Yes, UCI is exactly what you are looking for. All of those programs support UCI, which is how they are integrated into programs like ChessBase.
    – dfan
    Commented Nov 24, 2013 at 14:24

7 Answers 7

10

As others have said, UCI is the API you want. The full specifications of the protocol is here (the zip file extracts to a text file): http://download.shredderchess.com/div/uci.zip

It's actually very straight forward and simple, a UCI engine must respond to and reply in plain text through stdin, stdout and stderr. In fact, you should be able to launch the executable binary of any UCI engine on your platform and issue the UCI commands manually.

UCI facilitates instructions from a GUI or other engine to tell the specific engine to do whatever it is that you want it to do, including analyse a specific PGN file (the loading of it is through the GUI or similar front-end, the raw moves are then fed to the engine in extended algebraic form, such as e4 becoming e2e4). The second and third points of your requirements are standard with UCI, but it is up to each engine developer to support them (given how lean those requirements are, that should be most of them).

Here's an example of UCI in action on the command line:

bash-3.2$ ./stockfish-5-64 
Stockfish 5 64 by Tord Romstad, Marco Costalba and Joona Kiiski
uci
id name Stockfish 5 64
id author Tord Romstad, Marco Costalba and Joona Kiiski

option name Write Debug Log type check default false
option name Write Search Log type check default false
option name Search Log Filename type string default SearchLog.txt
option name Book File type string default book.bin
option name Best Book Move type check default false
option name Contempt Factor type spin default 0 min -50 max 50
option name Mobility (Midgame) type spin default 100 min 0 max 200
option name Mobility (Endgame) type spin default 100 min 0 max 200
option name Pawn Structure (Midgame) type spin default 100 min 0 max 200
option name Pawn Structure (Endgame) type spin default 100 min 0 max 200
option name Passed Pawns (Midgame) type spin default 100 min 0 max 200
option name Passed Pawns (Endgame) type spin default 100 min 0 max 200
option name Space type spin default 100 min 0 max 200
option name Aggressiveness type spin default 100 min 0 max 200
option name Cowardice type spin default 100 min 0 max 200
option name Min Split Depth type spin default 0 min 0 max 12
option name Threads type spin default 1 min 1 max 128
option name Hash type spin default 32 min 1 max 16384
option name Clear Hash type button
option name Ponder type check default true
option name OwnBook type check default false
option name MultiPV type spin default 1 min 1 max 500
option name Skill Level type spin default 20 min 0 max 20
option name Emergency Move Horizon type spin default 40 min 0 max 50
option name Emergency Base Time type spin default 60 min 0 max 30000
option name Emergency Move Time type spin default 30 min 0 max 5000
option name Minimum Thinking Time type spin default 20 min 0 max 5000
option name Slow Mover type spin default 80 min 10 max 1000
option name UCI_Chess960 type check default false
uciok
isready
readyok
quit
bash-3.2$ 

The commands I issued were uci, isready and quit.

To utilise the UCI commands you essentially have two main choices; you can use an existing interface (e.g. ChessX, Arena and a host of others) or you can write your own code to issue instructions to an engine or group of engines. I believe there may already be Python wrappers for accessing UCI engines, but I'd need to double-check. There's definitely python-chess (import chess) and pychess (import pychess), I just can't recall the extent of their functions. I can't recall where pychess is lurking (probably github or google code), but python-chess can be installed with pip.

5
  • UCI doesn't seem to provide an API for setting the value of the pieces.
    – Snowcrash
    Commented Nov 23, 2016 at 14:02
  • That sort of thing ought to be programmed into the engine itself and chess engines will use standard values. You'd only need to change it for some other game (e.g. faerie chess) and chances are you'll need to make a lot more changes than just the piece values then.
    – Ben
    Commented Nov 23, 2016 at 14:18
  • I disagree. There aren't standard values.
    – Snowcrash
    Commented Nov 26, 2016 at 10:18
  • Then you'd probably need to fork an engine code base to redesign it with variable values instead. Then set a separate configuration for the forked and recompiled engine to use the new values. Then continue to interact via UCI as normal. Alternatively look at the other engine language format that GNU Chess uses, I forget the details of it, though.
    – Ben
    Commented Dec 14, 2016 at 13:29
  • @Snowcrash : all those option lines you see are specific to Stockfish, the engine used for this example. The UCI protocol does not deal with the content of the options and it is up to the engine to define and use them.
    – AlefSin
    Commented Sep 28, 2020 at 16:20
4

python-chess provides a useful API and supports UCI, apparently:

http://python-chess.readthedocs.io/en/v0.15.3/uci.html?highlight=engine

The above link gives a 'stockfish' example.

1
  • Python-chess + Stockfish executable is really easy to work with! Commented Dec 16, 2021 at 7:25
3

I've googled on "chess engine api" but have not seen any real API's. As suggested in the comments of your question I would go with UCI. This is also the way GUI's communicate with chess engines.

If you're really a hardcore programmer you could always check out the open source engine Stockfish. It could very well be that there is some sort of API built in it, or you could make a clone and build your own API for it.

http://stockfishchess.org/

6
  • 1
    The API for talking to Stockfish is also UCI.
    – dfan
    Commented Nov 24, 2013 at 20:11
  • 1
    Indeed it is. But using UCI or diving into the code will depend on how the engine is going to be used and not for what. The question was not clear on that.
    – Rafiek
    Commented Nov 24, 2013 at 20:38
  • I think I mentioned what exactly I need to do with the engine. Load a particular PNG and evaluate after each move. Commented Nov 24, 2013 at 21:06
  • It is perfectly clear what you want to do with the engine. How you will be using it is left in the open. In what setting will you be using the engine? It could be that UCI may not be the optimal way to communicate with the engine. I suggested another option if UCI is not sufficient for you.
    – Rafiek
    Commented Nov 25, 2013 at 10:08
  • Sorry, but I can not understand what do you mean by how I am going to use the engine. Can you please clarify this? Commented Nov 25, 2013 at 20:28
1

Batch First is a JIT compiled chess engine written in Python, compiled using Numba, which would allow easy C++ interfacing and even easier Python interfacing.

While Batch First itself is an engine not API, it heavily uses and relies upon the Python-Chess package, which is an awesome API.

The JIT compilation of Batch First, and use of NumPy arrays for representing boards, allows a more practical Python interface (at least an order of magnitude faster) for actual use in a chess engine.

The core of the engine is a zero-window k-best-first search algorithm used to evaluate ANNs in batches.

Full disclosure, I am the author of the Batch First engine.

1

I know of two APIs by which you can access Stockfish. The source code is available for both to implement yourself. In one case, the API is also hosted and can be queried freely.

(This answer is essentially the same as this answer that I gave to a similar, later question.)

noobpwnftw/chessdb

I discovered this API through Fabian Fichter’s answer to this similar, later question.

The front-end is a PHP script and uses in addition MongoDB. It requires a Redis-compatible server for data storage. See the source code on GitHub.

The API is actively hosted and can be freely queried. For example, here’s a position from the Fried Liver Attack, with the FEN: “r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R w KQkq - 1 6”

r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R w KQkq - 1 6

For example, to find just the best move, form the query like:

https://www.chessdb.cn/cdb.php?action=querybest&board=FEN_STRING_WITH_BLANKS_ESCAPED

In this case:

https://www.chessdb.cn/cdb.php?action=querybest&board=r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R%20w%20KQkq%20-%201%206

which gives you this result:

move:c4b5

which translates to Bb5+, agreeing with theory.

See the GitHub link for a little additional documentation, such as using querypv to get continuations.

Ilya Zhelyabuzhsky’s Python wrapper for Stockfish

See Ilya Zhelyabuzhsky’s Python wrapper (a) on GitHub and (b) available for installation from PyPI.

This requires that Stockfish be installed on the server.

This Python package implements a class to integrate Stockfish with Python.

For example, you can define a position from a FEN and then request the best move:

stockfish.set_fen_position("rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
stockfish.get_best_move()

See either of the above links for additional documentation.

0

Here there's a port version of Stockfish in .Net: https://github.com/bpfliegel/Portfish

To use this engine (once it's compiled), you'll nedd a GUI like Arena http://wwww.playwitharena.com or ScidVSPC http://scidvspc.sourceforge.net. These applications are used to provide a nice interface and allow you to load games/positions, analyze them (I'm pretty sure you can load a game on pgn and analyze it by an engine with criteria like "n seconds per move" or "n plies per move"), run matches between engines, etc

In (almost) every chess engine you should find something like a uci.cs or an xboard.cs file which is commited to this communication task.

(A "chess engine" is a program that is capable of playing chess but has no graphical interface. The way it communicates to applications like Arena or ScidVSPC is by the UCI or xboard protocols. Most of the chess programs are written this way, so the developer can forget about the GUI stuff and focus on the "brain").

0

I was looking around for something similar and came across Joe's Chess API:

http://chess-api.herokuapp.com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.