Answers.Body: <p>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.</p>
  <p>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.</p>
  <p>Stockfish is a C++ engine, so you might need to wrap it with a managed C++ layer for C#. There is a <a href="https://github.com/bpfliegel/Portfish" rel="nofollow noreferrer">Stockfish port</a> 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.</p>
  <p>General steps:</p>
  <ol>
  <li>Download a compiled binary (*.exe). It can be anything like Houdini, Komodo or Stockfish. You don't have to compile the source code yourself.</li>
  <li>Use Process.Start in C# to start a process of the chess engine</li>
  <li>Talk with the child process like <a href="https://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro">here</a></li>
  <li>Try to send a UCI message like this:</li>
  </ol>
  <blockquote>
  <p>isready</p>
  </blockquote>
  <p>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:</p>
  <blockquote>
  <p>ucinewgame</p>
  <p>go move time 1000</p>
  </blockquote>
  <p>This gives the engine one second to make a move. You will get something like this:</p>
  <blockquote>
  <p>bestmove e2e4 ponder e7e5</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-01T17:11:40.177Z
Answers.Text: I can give you more details if that was confusing at all. Again, thank you for your help! I really appreciate it.
Answers.UserDisplayName: Payal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-28T21:52:16.330Z
Answers.Text: UCI is an API in a broad sense. Do you have a more specific definition of API?
Answers.UserDisplayName: qwr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-30T23:14:36.217Z
Answers.LastActivityDate: 2015-06-30T23:20:54.047Z
Answers.LastEditDate: 2020-06-17T09:59:59.497Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>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.</p>
  <h1>noobpwnftw/chessdb</h1>
  <p>I discovered this API through <a href="https://chess.stackexchange.com/a/24853/17203">Fabian Fichter’s answer</a> to <a href="https://chess.stackexchange.com/questions/13958/is-there-an-online-chess-evaluation-engine-with-a-rest-api">this similar, later question</a>.</p>
  <p>The front-end is a PHP script and uses in addition MongoDB. It requires a Redis-compatible server for data storage. See the <a href="https://github.com/noobpwnftw/chessdb" rel="nofollow noreferrer">source code on GitHub</a>.</p>
  <p>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”</p>
  <pre><code>r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R w KQkq - 1 6
  </code></pre>
  <p>For example, to find just the best move, form the query like:</p>
  <pre><code>https://www.chessdb.cn/cdb.php?action=querybest&amp;board=FEN_STRING_WITH_BLANKS_ESCAPED
  </code></pre>
  <p>In this case:</p>
  <pre><code>https://www.chessdb.cn/cdb.php?action=querybest&amp;board=r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R%20w%20KQkq%20-%201%206
  </code></pre>
  <p>which gives you <a href="https://www.chessdb.cn/cdb.php?action=querybest&amp;board=r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R%20w%20KQkq%20-%201%206" rel="nofollow noreferrer">this result</a>:</p>
  <pre><code>move:c4b5
  </code></pre>
  <p>which translates to Bb5+, agreeing with theory.</p>
  <p>See the GitHub link for a little additional documentation, such as using <code>querypv</code> to get continuations.</p>
  <h2>Ilya Zhelyabuzhsky’s Python wrapper for Stockfish</h2>
  <p>See Ilya Zhelyabuzhsky’s Python wrapper (a) <a href="https://github.com/zhelyabuzhsky/stockfish" rel="nofollow noreferrer">on GitHub</a> and (b) available for installation <a href="https://pypi.org/project/stockfish/" rel="nofollow noreferrer">from PyPI</a>.</p>
  <p>This requires that Stockfish be installed on the server.</p>
  <p>This Python package implements a class to integrate Stockfish with Python.</p>
  <p>For example, you can define a position from a FEN and then request the best move:</p>
  <pre><code>stockfish.set_fen_position(&quot;rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2&quot;)
  stockfish.get_best_move()
  </code></pre>
  <p>See either of the above links for additional documentation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-27T06:25:06.847Z
Answers.LastActivityDate: 2022-08-27T16:09:45.177Z
Answers.LastEditDate: 2022-08-27T16:09:45.177Z
Answers.UserDisplayName: Jim Ratliff
Body: <p>I am writing a C# 3D chess game in Unity. I am looking for a chess engine that can</p>
  
  <ol>
  <li>Take in an FEN string </li>
  <li>Return the best move to make given this FEN</li>
  <li>Check if the board given this FEN is in checkmate</li>
  </ol>
  
  <p>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!</p>
  
Tags: <engines><fen>
Title: Chess Engine API
ThreadText: # Chess Engine API
  
  (Asked by: username_0 on 2015-06-30)
  
  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!
  
  **Tags:** engines, fen
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-06-30. Score: 1 | Accepted answer
  
  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](https://github.com/bpfliegel/Portfish) 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](https://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro)
  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
  
  > **Comments:**
  > **username_0**: I can give you more details if that was confusing at all. Again, thank you for your help! I really appreciate it.  
  > **username_2**: UCI is an API in a broad sense. Do you have a more specific definition of API?  
  
  ---
  
  ### Answer by username_3 on 2022-08-27. Score: 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](https://chess.stackexchange.com/a/24853/17203) to [this similar, later question](https://chess.stackexchange.com/questions/13958/is-there-an-online-chess-evaluation-engine-with-a-rest-api).
  
  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](https://github.com/noobpwnftw/chessdb).
  
  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](https://www.chessdb.cn/cdb.php?action=querybest&board=r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R%20w%20KQkq%20-%201%206):
  
  ```
  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](https://github.com/zhelyabuzhsky/stockfish) and (b) available for installation [from PyPI](https://pypi.org/project/stockfish/).
  
  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.
  
  
  ---
  
  