5

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

9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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