4

I am writing a C# 3D chess game in Unity. I am looking for a chess engine that can

  1. Take in an FEN string
  2. Return the best move to make given this FEN
  3. Check if the board given this FEN is in checkmate

Does anyone know of a chess engine that can do this, preferably in C#? I have tried Stockfish and other chess engines, but they all work from the command line. If anyone knows how I can program the game using an engine that works from the command line, that would also work. Thanks!

2 Answers 2

1

Unfortunately, chess engine doesn't work like what you expect. In particular, there is no such thing as API in the chess engine world. That's because calculating a chess move takes time, and it'll be insane to block your function call until you can get the move.

Chess engine almost always do the works in its own thread or process. You might need to learn how to interact with a thread/process. You'll also need to learn UCI chess protocol. I don't think there is a wrapper for you, but maybe there is just that I don't know.

Stockfish is a C++ engine, so you might need to wrap it with a managed C++ layer for C#. There is a Stockfish port on C#, but it's not as good as the original. However, you don't even need a source code to do your works. You could also take a closed engine such as Houdini.

General steps:

  1. Download a compiled binary (*.exe). It can be anything like Houdini, Komodo or Stockfish. You don't have to compile the source code yourself.
  2. Use Process.Start in C# to start a process of the chess engine
  3. Talk with the child process like here
  4. Try to send a UCI message like this:

isready

The isready UCI command is like pinging the chess engine. You should hear something back if your implementation works. Please take a look at the UCI documentation, you'll find it on Google. Once you know your code works. Try this:

ucinewgame

go move time 1000

This gives the engine one second to make a move. You will get something like this:

bestmove e2e4 ponder e7e5

2
  • I can give you more details if that was confusing at all. Again, thank you for your help! I really appreciate it.
    – Payal
    Jul 1, 2015 at 17:11
  • UCI is an API in a broad sense. Do you have a more specific definition of API?
    – qwr
    Aug 28, 2022 at 21:52
0

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.

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.

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.