Answers.Body: <p>Let's use Stockfish to find out the moves for this position</p>
  
  <pre><code>[fen "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34"]
  </code></pre>
  
  <p>The FEN string is: "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34". You should copy-and-paste my commands to experience yourself.</p>
  
  <p>Load the position by "position fen ..." and then use "d" to print the position. Please note that "d" is a Stockfish-only command.</p>
  
  <pre><code>tedwong@:~$ stockfish
  Stockfish 6 64 by Tord Romstad, Marco Costalba and Joona Kiiski
  position fen N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34        
  d
  
   +---+---+---+---+---+---+---+---+
   | N |   |   |   |   |   |   |   |
   +---+---+---+---+---+---+---+---+
   | P |   |   |   | p | k |   | p |
   +---+---+---+---+---+---+---+---+
   |   |   |   | p |   |   | p |   |
   +---+---+---+---+---+---+---+---+
   | r |   |   |   |   | p |   |   |
   +---+---+---+---+---+---+---+---+
   |   |   |   |   |   |   |   |   |
   +---+---+---+---+---+---+---+---+
   |   |   |   |   | b |   |   | B |
   +---+---+---+---+---+---+---+---+
   |   |   |   |   | P |   | K | P |
   +---+---+---+---+---+---+---+---+
   |   | R |   |   |   |   |   |   |
   +---+---+---+---+---+---+---+---+
  
  Fen: N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34
  Key: 4E025494EA4A7523
  Checkers: 
  </code></pre>
  
  <p>Ask the engine to think for 30s and set the number of principal variations to 3.</p>
  
  <pre><code>setoption name MultiPV value 3
  go movetime 30000
  </code></pre>
  
  <p>You'll see a bunch of engine outputs. In particular, you should something like:</p>
  
  <pre><code>info depth 5 seldepth 5 multipv 1 score cp -5 nodes 1540 nps 54 tbhits 0 time 28098 pv a8b6 e3b6 b1b6 a5a7 e2e3
  info depth 5 seldepth 5 multipv 2 score cp -8 nodes 1540 nps 54 tbhits 0 time 28098 pv a8c7 e3a7 c7b5 h7h6 b5a7
  info depth 4 seldepth 5 multipv 3 score cp -10 nodes 1540 nps 54 tbhits 0 time 28098 pv g2f3 a5a7 a8b6 a7a3
  info depth 5 currmove g2f3 currmovenumber 3
  info nodes 1618 time 30049
  bestmove a8b6 ponder e3b6
  </code></pre>
  
  <p>Here, Stockfish gives three possible lines: Nb6, Nc7 and Kf3. The lines are sorted in terms of the advantage for the side to move.</p>
  
  <p>Let's check what if your friend blunders a rook by Ra1... We expect Stockfish to report ...Rxa1 with a winning advantage for Black. I use <a href="http://analysis.cpuchess.com" rel="noreferrer">http://analysis.cpuchess.com</a> to make the move and get a new FEN.</p>
  
  <pre><code>[fen "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/R7 b - - 1 34"]
  </code></pre>
  
  <p>The new FEN is: "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/R7 b - - 1 34"</p>
  
  <pre><code>position fen "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/R7 b - - 1 34"
  go move time 30000
  </code></pre>
  
  <p>You should see something like:</p>
  
  <pre><code>info depth 25 seldepth 38 multipv 1 score cp 692 nodes 34732679 nps 1391812 tbhits 0 time 24955 pv a5a1 a8c7 e3a7 g2g3 a1a3 g3g2 a3a5 e2e4 a7b8 c7d5 f5e4 d5c3 d6d5 h3c8 b8e5 c3d1 a5a2 g2f1 e5h2 c8b7 e7e6 d1e3 a2b2 b7a8 b2b5 f1f2 h2f4 e3d1 b5c5 a8b7 c5c2 f2e1 f7f6
  info depth 25 seldepth 38 multipv 2 score cp 62 nodes 34732679 nps 1391812 tbhits 0 time 24955 pv a5a7 a1a7 e3a7 g2f1 d6d5 h3g2 e7e6 h2h3 f7e7 a8c7 a7b6 c7a6 e7d6 a6b4 b6d4 b4c2 d4c5 f1e1 g6g5 g2f3 d6e5 e2e3 f5f4 e1e2 f4e3 c2e3
  info depth 24 seldepth 38 multipv 3 score cp -480 nodes 34732679 nps 1391812 tbhits 0 time 24955 pv e3a7 a1a5 a7d4 a5a4 d4e3 a8c7 e3b6 c7d5 b6c5 e2e4 e7e6 d5c7 f7f6 a4a6 f6e5 e4f5 e6f5 g2g3 c5d4 h3f1 h7h6 c7e8 d4c5 a6a5 e5e6 e8c7 e6d7 c7d5 c5d4 g3f3 d4e5 f1b5 d7e6 d5f4 e6f7 a5a8
  bestmove a5a1 ponder a8c7
  </code></pre>
  
  <p>As can you can see the evaluation drops to "cp 692". It was "cp -5" before our move. Therefore the move Ra1 must have been a gross blunder.</p>
  
  <p>Please read the UCI specification for more details.</p>
  
  <p><strong>Your job will be:</strong></p>
  
  <ol>
  <li><p>Connect an engine to your favourite programming language. You can do it by inter-process communication. There are lots of examples on Google.</p></li>
  <li><p>Issue UCI commands like how I did.</p></li>
  <li><p>Parse the commands. You might want to use like a string tokeniser.</p></li>
  <li><p>Make a move (like how I did with Ra1) to get an evaluation of the move.</p></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-12T00:12:06.720Z
Answers.LastActivityDate: 2015-10-12T00:12:06.720Z
Answers.UserDisplayName: SmallChess
Body: <p>I'm currently writing small application for a chess interested friend. The application is supposed to automatically annotate his chess games. When giving the engine a position to analyze, and a depth (or time) for doing it, I'm looking to get the following info in return:
  1. The best three (or any other given number) moves and their scores in centipawns.
  2. An evaluation of a given move (ie his move). 
  I would really appreciate if someone could tell me how to accomplish this. I guess my lack of knowledge about chess and chess engines makes it harder for me than it actually is.</p>
  
  <p>/Tom</p>
  
Tags: <engines><programming><uci>
Title: Working with UCI protocol (coding)
ThreadText: # Working with UCI protocol (coding)
  
  (Asked by: username_0 on 2015-10-11)
  
  I'm currently writing small application for a chess interested friend. The application is supposed to automatically annotate his chess games. When giving the engine a position to analyze, and a depth (or time) for doing it, I'm looking to get the following info in return:
  1. The best three (or any other given number) moves and their scores in centipawns.
  2. An evaluation of a given move (ie his move).
  I would really appreciate if someone could tell me how to accomplish this. I guess my lack of knowledge about chess and chess engines makes it harder for me than it actually is.
  
  /Tom
  
  **Tags:** engines, programming, uci
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-12. Score: 12 | Accepted answer
  
  Let's use Stockfish to find out the moves for this position
  
  ```
  [fen "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34"]
  
  ```
  
  The FEN string is: "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34". You should copy-and-paste my commands to experience yourself.
  
  Load the position by "position fen ..." and then use "d" to print the position. Please note that "d" is a Stockfish-only command.
  
  ```
  tedwong@:~$ stockfish
  Stockfish 6 64 by Tord Romstad, Marco Costalba and Joona Kiiski
  position fen N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34        
  d
  
   +---+---+---+---+---+---+---+---+
   | N |   |   |   |   |   |   |   |
   +---+---+---+---+---+---+---+---+
   | P |   |   |   | p | k |   | p |
   +---+---+---+---+---+---+---+---+
   |   |   |   | p |   |   | p |   |
   +---+---+---+---+---+---+---+---+
   | r |   |   |   |   | p |   |   |
   +---+---+---+---+---+---+---+---+
   |   |   |   |   |   |   |   |   |
   +---+---+---+---+---+---+---+---+
   |   |   |   |   | b |   |   | B |
   +---+---+---+---+---+---+---+---+
   |   |   |   |   | P |   | K | P |
   +---+---+---+---+---+---+---+---+
   |   | R |   |   |   |   |   |   |
   +---+---+---+---+---+---+---+---+
  
  Fen: N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/1R6 w - - 0 34
  Key: 4E025494EA4A7523
  Checkers: 
  
  ```
  
  Ask the engine to think for 30s and set the number of principal variations to 3.
  
  ```
  setoption name MultiPV value 3
  go movetime 30000
  
  ```
  
  You'll see a bunch of engine outputs. In particular, you should something like:
  
  ```
  info depth 5 seldepth 5 multipv 1 score cp -5 nodes 1540 nps 54 tbhits 0 time 28098 pv a8b6 e3b6 b1b6 a5a7 e2e3
  info depth 5 seldepth 5 multipv 2 score cp -8 nodes 1540 nps 54 tbhits 0 time 28098 pv a8c7 e3a7 c7b5 h7h6 b5a7
  info depth 4 seldepth 5 multipv 3 score cp -10 nodes 1540 nps 54 tbhits 0 time 28098 pv g2f3 a5a7 a8b6 a7a3
  info depth 5 currmove g2f3 currmovenumber 3
  info nodes 1618 time 30049
  bestmove a8b6 ponder e3b6
  
  ```
  
  Here, Stockfish gives three possible lines: Nb6, Nc7 and Kf3. The lines are sorted in terms of the advantage for the side to move.
  
  Let's check what if your friend blunders a rook by Ra1... We expect Stockfish to report ...Rxa1 with a winning advantage for Black. I use <http://analysis.cpuchess.com> to make the move and get a new FEN.
  
  ```
  [fen "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/R7 b - - 1 34"]
  
  ```
  
  The new FEN is: "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/R7 b - - 1 34"
  
  ```
  position fen "N7/P3pk1p/3p2p1/r4p2/8/4b2B/4P1KP/R7 b - - 1 34"
  go move time 30000
  
  ```
  
  You should see something like:
  
  ```
  info depth 25 seldepth 38 multipv 1 score cp 692 nodes 34732679 nps 1391812 tbhits 0 time 24955 pv a5a1 a8c7 e3a7 g2g3 a1a3 g3g2 a3a5 e2e4 a7b8 c7d5 f5e4 d5c3 d6d5 h3c8 b8e5 c3d1 a5a2 g2f1 e5h2 c8b7 e7e6 d1e3 a2b2 b7a8 b2b5 f1f2 h2f4 e3d1 b5c5 a8b7 c5c2 f2e1 f7f6
  info depth 25 seldepth 38 multipv 2 score cp 62 nodes 34732679 nps 1391812 tbhits 0 time 24955 pv a5a7 a1a7 e3a7 g2f1 d6d5 h3g2 e7e6 h2h3 f7e7 a8c7 a7b6 c7a6 e7d6 a6b4 b6d4 b4c2 d4c5 f1e1 g6g5 g2f3 d6e5 e2e3 f5f4 e1e2 f4e3 c2e3
  info depth 24 seldepth 38 multipv 3 score cp -480 nodes 34732679 nps 1391812 tbhits 0 time 24955 pv e3a7 a1a5 a7d4 a5a4 d4e3 a8c7 e3b6 c7d5 b6c5 e2e4 e7e6 d5c7 f7f6 a4a6 f6e5 e4f5 e6f5 g2g3 c5d4 h3f1 h7h6 c7e8 d4c5 a6a5 e5e6 e8c7 e6d7 c7d5 c5d4 g3f3 d4e5 f1b5 d7e6 d5f4 e6f7 a5a8
  bestmove a5a1 ponder a8c7
  
  ```
  
  As can you can see the evaluation drops to "cp 692". It was "cp -5" before our move. Therefore the move Ra1 must have been a gross blunder.
  
  Please read the UCI specification for more details.
  
  **Your job will be:**
  
  1. Connect an engine to your favourite programming language. You can do it by inter-process communication. There are lots of examples on Google.
  2. Issue UCI commands like how I did.
  3. Parse the commands. You might want to use like a string tokeniser.
  4. Make a move (like how I did with Ra1) to get an evaluation of the move.
  
  
  ---
  
  