UCI Controller Obligations

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
jshriver
Posts: 1342
Joined: Wed Mar 08, 2006 9:41 pm
Location: Morgantown, WV, USA

UCI Controller Obligations

Post by jshriver »

At what point is the engine or the GUI responsible for enforcing chess rules such as 3 fold repetition, 50 moves, stalemate, etc?

Looks like the info block does have mate 1 to signify a mate. So far stockfish seems to handle 3 fold repetition, and 50 move rule. However it will keep analyzing a draw by stalemate. Am I as the uci controller supposed to watch for that or am I overlooking something in the uci protocol.

Here is a game example, starting at move 131 its just the 2 kings yet it plays on.


[Event "Test"]
[Site "Test"]
[Date "2021-01-24"]
[Round "1"]
[White "Foo"]
[Black "Bar"]
[Result "1/2-1/2"]

1 g1f3 d7d5 2 e2e3 c7c5 3 d2d4 g8f6 4 c2c4 e7e6 5 b1c3 b8c6 6 h2h3 c8d7 7 a2a3 a8c8 8 c4d5 e6d5 9 d4c5 d7f5 10 b2b4 g7g6 11 g2g4 f5e6 12 g4g5 f6d7 13 c3b5 f8c5 14 b4c5 d7c5 15 h3h4 d5d4 16 b5d4 e8g8 17 h4h5 e6b3 18 d1d2 b3d5 19 h1h4 c6d4 20 f3d4 d8g5 21 h4h3 g5g1 22 d4f5 d5g2 23 f5e7 g8g7 24 h5h6 g7h8 25 d2e2 c8e8 26 h3g3 g1f1 27 e2f1 g2f1 28 c1b2 f7f6 29 e7g6 h7g6 30 e1f1 c5d3 31 b2d4 h8h7 32 d4a7 e8e6 33 a7d4 d3e5 34 a1b1 f8f7 35 f1g2 e6a6 36 d4c5 h7h6 37 b1b3 e5c6 38 e3e4 a6a4 39 c5b4 c6d4 40 b3b2 d4c6 41 b2b3 g6g5 42 b3b2 h6g6 43 b2b1 f7d7 44 g3c3 d7d4 45 b4c5 d4d7 46 f2f3 a4a6 47 c3b3 a6a5 48 c5e3 c6e5 49 g2f2 g5g4 50 f3f4 e5c4 51 f4f5 g6f7 52 b3b7 d7b7 53 b1b7 f7g8 54 b7b8 g8f7 55 b8b3 a5a3 56 b3a3 c4a3 57 f2g3 a3b5 58 e3g1 b5c3 59 g3g4 c3e4 60 g1h2 f7e7 61 h2b8 e7d7 62 g4f3 e4g5 63 f3e2 g5e4 64 b8f4 e4c3 65 e2f3 d7e7 66 f4b8 e7d7 67 b8f4 d7e7 68 f4b8 c3b5 69 f3f2 b5d4 70 b8a7 d4b5 71 a7c5 e7f7 72 f2f3 f7g8 73 f3e4 b5c3 74 e4f3 c3b5 75 f3e4 b5c3 76 e4f4 c3b5 77 f4f3 b5c3 78 c5d4 c3b5 79 d4c5 g8f7 80 c5g1 f7g8 81 g1c5 g8f7 82 c5g1 f7g8 83 g1c5 g8f7 84 c5g1 f7g8 85 g1c5 g8g7 86 f3f4 g7f7 87 f4f3 f7g7 88 f3f4 g7f7 89 f4f3 f7g7 90 f3f4 g7f7 91 f4f3 f7g8 92 f3e4 b5c3 93 e4f3 c3b5 94 f3e4 b5c3 95 e4f3 c3b5 96 f3e4 b5c3 97 e4f4 c3b5 98 f4f3 g8f7 99 f3g4 f7e8 100 g4h5 e8f7 101 c5b6 f7g8 102 b6c5 g8f7 103 c5f2 b5d6 104 f2e1 d6f5 105 e1f2 f5h6 106 f2h4 h6f5 107 h4f2 f5g7 108 h5h6 f6f5 109 f2g3 f5f4 110 g3f4 g7f5 111 h6g5 f5g7 112 g5g4 g7e6 113 f4h2 f7g6 114 g4f3 e6d4 115 f3g4 d4c6 116 g4g3 g6g5 117 g3g2 g5g6 118 g2h3 c6b4 119 h2b8 g6f5 120 b8a7 b4a6 121 a7f2 f5g6 122 f2g3 g6f5 123 g3f4 f5e6 124 f4d6 e6d6 125 h3h2 d6e7 126 h2h3 e7f6 127 h3g2 a6c5 128 g2f2 c5d3 129 f2f3 f6e7 130 f3e3 d3f2 131 e3f2 e7d6 132 f2g1 d6c5 133 g1h2 c5b5 134 h2g1 b5c5 135 g1h2 c5b5 136 h2g1 b5b6 137 g1h1 b6b7 138 h1g1 b7b8 139 g1g2 b8b7 140 g2f3 b7c7 141 f3f4 c7d6 142 f4f5 d6c5 143 f5g5 c5b5 144 g5f4 b5c4 145 f4f3 c4b5 146 f3g3 b5b6 147 g3g4 b6c6 148 g4f4 c6c5 149 f4f3 c5b5 150 f3g3 b5b6 151 g3g4 b6c6 152 g4f4 c6c5

Note: Not sure why the pgn tag doesnt display this properly

Here is the stockfish dump from my logs where it captures the last piece.

Fen: 8/4k3/8/8/8/4K3/5n2/8 w - -
info string NNUE evaluation using nn-82215d0fd0df.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp 0 nodes 75 nps 75000 tbhits 0 time 1 pv e3f2
info depth 2 seldepth 3 multipv 1 score cp 15 nodes 281 nps 281000 tbhits 0 time 1 pv e3f2 e7d6 f2e2
info depth 3 seldepth 4 multipv 1 score cp 0 nodes 454 nps 454000 tbhits 0 time 1 pv e3f3 f2d3 f3e3
info depth 4 seldepth 4 multipv 1 score cp 0 nodes 775 nps 775000 tbhits 0 time 1 pv e3f3 f2d3 f3e3 d3f2
info depth 5 seldepth 6 multipv 1 score cp 15 nodes 2679 nps 1339500 tbhits 0 time 2 pv e3f2 e7d7 f2g1 d7c6 g1h1
bestmove e3f2 ponder e7d7
User avatar
xr_a_y
Posts: 1871
Joined: Sat Nov 25, 2017 2:28 pm
Location: France

Re: UCI Controller Obligations

Post by xr_a_y »

3-fold is not a draw, it is only one if one player claims it
"In chess, the threefold repetition rule states that a player may claim a draw if the same position occurs three times. "
So in some kind of analysis mode, there is no reason so stop analysing after a repetition.

But I guess it shall not be the case for stale mate.
User avatar
Guenther
Posts: 4605
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: UCI Controller Obligations

Post by Guenther »

jshriver wrote: Sun Jan 24, 2021 6:33 pm At what point is the engine or the GUI responsible for enforcing chess rules such as 3 fold repetition, 50 moves, stalemate, etc?

Looks like the info block does have mate 1 to signify a mate. So far stockfish seems to handle 3 fold repetition, and 50 move rule. However it will keep analyzing a draw by stalemate. Am I as the uci controller supposed to watch for that or am I overlooking something in the uci protocol.

Here is a game example, starting at move 131 its just the 2 kings yet it plays on.


[Event "Test"]
[Site "Test"]
[Date "2021-01-24"]
[Round "1"]
[White "Foo"]
[Black "Bar"]
[Result "1/2-1/2"]

1 g1f3 d7d5 2 e2e3 c7c5 3 d2d4 g8f6 4 c2c4 e7e6 5 b1c3 b8c6 6 h2h3 c8d7 7 a2a3 a8c8 8 c4d5 e6d5 9 d4c5 d7f5 10 b2b4 g7g6 11 g2g4 f5e6 12 g4g5 f6d7 13 c3b5 f8c5 14 b4c5 d7c5 15 h3h4 d5d4 16 b5d4 e8g8 17 h4h5 e6b3 18 d1d2 b3d5 19 h1h4 c6d4 20 f3d4 d8g5 21 h4h3 g5g1 22 d4f5 d5g2 23 f5e7 g8g7 24 h5h6 g7h8 25 d2e2 c8e8 26 h3g3 g1f1 27 e2f1 g2f1 28 c1b2 f7f6 29 e7g6 h7g6 30 e1f1 c5d3 31 b2d4 h8h7 32 d4a7 e8e6 33 a7d4 d3e5 34 a1b1 f8f7 35 f1g2 e6a6 36 d4c5 h7h6 37 b1b3 e5c6 38 e3e4 a6a4 39 c5b4 c6d4 40 b3b2 d4c6 41 b2b3 g6g5 42 b3b2 h6g6 43 b2b1 f7d7 44 g3c3 d7d4 45 b4c5 d4d7 46 f2f3 a4a6 47 c3b3 a6a5 48 c5e3 c6e5 49 g2f2 g5g4 50 f3f4 e5c4 51 f4f5 g6f7 52 b3b7 d7b7 53 b1b7 f7g8 54 b7b8 g8f7 55 b8b3 a5a3 56 b3a3 c4a3 57 f2g3 a3b5 58 e3g1 b5c3 59 g3g4 c3e4 60 g1h2 f7e7 61 h2b8 e7d7 62 g4f3 e4g5 63 f3e2 g5e4 64 b8f4 e4c3 65 e2f3 d7e7 66 f4b8 e7d7 67 b8f4 d7e7 68 f4b8 c3b5 69 f3f2 b5d4 70 b8a7 d4b5 71 a7c5 e7f7 72 f2f3 f7g8 73 f3e4 b5c3 74 e4f3 c3b5 75 f3e4 b5c3 76 e4f4 c3b5 77 f4f3 b5c3 78 c5d4 c3b5 79 d4c5 g8f7 80 c5g1 f7g8 81 g1c5 g8f7 82 c5g1 f7g8 83 g1c5 g8f7 84 c5g1 f7g8 85 g1c5 g8g7 86 f3f4 g7f7 87 f4f3 f7g7 88 f3f4 g7f7 89 f4f3 f7g7 90 f3f4 g7f7 91 f4f3 f7g8 92 f3e4 b5c3 93 e4f3 c3b5 94 f3e4 b5c3 95 e4f3 c3b5 96 f3e4 b5c3 97 e4f4 c3b5 98 f4f3 g8f7 99 f3g4 f7e8 100 g4h5 e8f7 101 c5b6 f7g8 102 b6c5 g8f7 103 c5f2 b5d6 104 f2e1 d6f5 105 e1f2 f5h6 106 f2h4 h6f5 107 h4f2 f5g7 108 h5h6 f6f5 109 f2g3 f5f4 110 g3f4 g7f5 111 h6g5 f5g7 112 g5g4 g7e6 113 f4h2 f7g6 114 g4f3 e6d4 115 f3g4 d4c6 116 g4g3 g6g5 117 g3g2 g5g6 118 g2h3 c6b4 119 h2b8 g6f5 120 b8a7 b4a6 121 a7f2 f5g6 122 f2g3 g6f5 123 g3f4 f5e6 124 f4d6 e6d6 125 h3h2 d6e7 126 h2h3 e7f6 127 h3g2 a6c5 128 g2f2 c5d3 129 f2f3 f6e7 130 f3e3 d3f2 131 e3f2 e7d6 132 f2g1 d6c5 133 g1h2 c5b5 134 h2g1 b5c5 135 g1h2 c5b5 136 h2g1 b5b6 137 g1h1 b6b7 138 h1g1 b7b8 139 g1g2 b8b7 140 g2f3 b7c7 141 f3f4 c7d6 142 f4f5 d6c5 143 f5g5 c5b5 144 g5f4 b5c4 145 f4f3 c4b5 146 f3g3 b5b6 147 g3g4 b6c6 148 g4f4 c6c5 149 f4f3 c5b5 150 f3g3 b5b6 151 g3g4 b6c6 152 g4f4 c6c5

Note: Not sure why the pgn tag doesnt display this properly

This is no pgn or do you see any dots? ;-) e.g. 1. 2.
WB and I guess Xboard read all kind of crappy non standard pgn though and you can paste it there and it will spit out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Bg1
Kg8 81. Bc5 Kf7 82. Bg1 Kg8 83. Bc5 Kf7 84. Bg1 Kg8 85. Bc5 Kg7 86. Kf4 Kf7
87. Kf3 Kg7 88. Kf4 Kf7 89. Kf3 Kg7 90. Kf4 Kf7 91. Kf3 Kg8 92. Ke4 Nc3+
93. Kf3 Nb5 94. Ke4 Nc3+ 95. Kf3 Nb5 96. Ke4 Nc3+ 97. Kf4 Nb5 98. Kf3 Kf7
99. Kg4 Ke8 100. Kh5 Kf7 101. Bb6 Kg8 102. Bc5 Kf7 103. Bf2 Nd6 104. Be1
Nxf5 105. Bf2 Nh6 106. Bh4 Nf5 107. Bf2 Ng7+ 108. Kh6 f5 109. Bg3 f4 110.
Bxf4 Nf5+ 111. Kg5 Ng7 112. Kg4 Ne6 113. Bh2 Kg6 114. Kf3 Nd4+ 115. Kg4 Nc6
116. Kg3 Kg5 117. Kg2 Kg6 118. Kh3 Nb4 119. Bb8 Kf5 120. Ba7 Na6 121. Bf2
Kg6 122. Bg3 Kf5 123. Bf4 Ke6 124. Bd6 Kxd6 125. Kh2 Ke7 126. Kh3 Kf6 127.
Kg2 Nc5 128. Kf2 Nd3+ 129. Kf3 Ke7 130. Ke3 Nf2 131. Kxf2 Kd6 132. Kg1 Kc5
133. Kh2 Kb5 134. Kg1 Kc5 135. Kh2 Kb5 136. Kg1 Kb6 137. Kh1 Kb7 138. Kg1
Kb8 139. Kg2 Kb7 140. Kf3 Kc7 141. Kf4 Kd6 142. Kf5 Kc5 143. Kg5 Kb5 144.
Kf4 Kc4 145. Kf3 Kb5 146. Kg3 Kb6 147. Kg4 Kc6 148. Kf4 Kc5 149. Kf3 Kb5
150. Kg3 Kb6 151. Kg4 Kc6 152. Kf4 Kc5
*[/pgn]
https://rwbc-chess.de

trollwatch:
Chessqueen + chessica + AlexChess + Eduard + Sylwy
User avatar
jshriver
Posts: 1342
Joined: Wed Mar 08, 2006 9:41 pm
Location: Morgantown, WV, USA

Re: UCI Controller Obligations

Post by jshriver »

Guenther wrote: Sun Jan 24, 2021 7:14 pm This is no pgn or do you see any dots? ;-) e.g. 1. 2.
WB and I guess Xboard read all kind of crappy non standard pgn though and you can paste it there and it will spit out real pgn for copying back.
Oops! Thanks will update my code to add . after the move number.
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: UCI Controller Obligations

Post by Ras »

jshriver wrote: Sun Jan 24, 2021 6:33 pmAt what point is the engine or the GUI responsible for enforcing chess rules such as 3 fold repetition, 50 moves, stalemate, etc?
Always the GUI.
Looks like the info block does have mate 1 to signify a mate.
A mate-in-1, which means mate in the next move - not for a board position that is checkmate. That would be "mate 0", and the GUI shouldn't transfer positions without legal move.

My engine would answer like this to a position that is already checkmate:

Code: Select all

info score mate 0 pv 0000
bestmove 0000
And that's for stalemate:

Code: Select all

info score cp 0 pv 0000
bestmove 0000
Rasmus Althoff
https://www.ct800.net
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: UCI Controller Obligations

Post by hgm »

Ras wrote: Wed Jan 27, 2021 8:57 am..., and the GUI shouldn't transfer positions without legal move.
There is no mention of that in the specs, actually. Note the situation can also occur with an empty searchmoves list.
My engine would answer like this to a position that is already checkmate:

Code: Select all

info score mate 0 pv 0000
bestmove 0000
And that's for stalemate:

Code: Select all

info score cp 0 pv 0000
bestmove 0000
This is very good practice, and this behavior should have been made mandatory in the specs. It could also have been used for claiming rep- and 50-move draws, which are other situations where FIDE rules grant the player the right not to move.
Joerg Oster
Posts: 937
Joined: Fri Mar 10, 2006 4:29 pm
Location: Germany

Re: UCI Controller Obligations

Post by Joerg Oster »

Ras wrote: Wed Jan 27, 2021 8:57 am
jshriver wrote: Sun Jan 24, 2021 6:33 pmAt what point is the engine or the GUI responsible for enforcing chess rules such as 3 fold repetition, 50 moves, stalemate, etc?
Always the GUI.
Looks like the info block does have mate 1 to signify a mate.
A mate-in-1, which means mate in the next move - not for a board position that is checkmate. That would be "mate 0", and the GUI shouldn't transfer positions without legal move.

My engine would answer like this to a position that is already checkmate:

Code: Select all

info score mate 0 pv 0000
bestmove 0000
And that's for stalemate:

Code: Select all

info score cp 0 pv 0000
bestmove 0000
But that's a null move, isn't it?
Move format:
------------

The move format is in long algebraic notation.
A nullmove from the Engine to the GUI should be sent as 0000.
Examples: e2e4, e7e5, e1g1 (white short castling), e7e8q (for promotion)
Jörg Oster
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: UCI Controller Obligations

Post by Ras »

hgm wrote: Wed Jan 27, 2021 9:27 amThere is no mention of that in the specs, actually.
Correct, but the whole UCI design is to put the GUI in control, and if it has control, it should exert it and claim draw or mate when appropriate. Of course, that doesn't free the engine from being robust with input, and even with outright illegal positions.
Note the situation can also occur with an empty searchmoves list.
My parser ignores searchmoves entirely if not followed by moves - maybe I could change that. However, if searchmoves are given, but none is legal, the output will be like this:

Code: Select all

info string error (no legal search move)
bestmove 0000
Similarly for go mate N when there is no mate.
This is very good practice, and this behavior should have been made mandatory in the specs.
The only somewhat veiled hint is that a nullmove is specified at all, and there's not much use for that except for dealing with situations where there is no move for whatever reason.
It could also have been used for claiming rep- and 50-move draws
I actually had that implemented for a while with the same output as for stalemate. Turned out that it didn't really improve anything with correctly working UCI GUIs because they flag that as draw anyway, but Arena has a bug in its threefold recognition. If the first instance of the repeated positions is after a pawn double step without enemy pawn to possibly do the capture, Arena fails to recognise that the position is the same and hence needs four repetitions. It will flag the bestmove 0000 answer as illegal move and count it as engine loss.
Rasmus Althoff
https://www.ct800.net
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: UCI Controller Obligations

Post by Ras »

Joerg Oster wrote: Wed Jan 27, 2021 10:06 amBut that's a null move, isn't it?
Correct, and using that as bestmove is the only way a UCI engine can say "I'm done with calculating, but I don't have a move". I can't think of another situation why the engine would send a nullmove.
Rasmus Althoff
https://www.ct800.net
Joerg Oster
Posts: 937
Joined: Fri Mar 10, 2006 4:29 pm
Location: Germany

Re: UCI Controller Obligations

Post by Joerg Oster »

Ras wrote: Wed Jan 27, 2021 10:13 am
Joerg Oster wrote: Wed Jan 27, 2021 10:06 amBut that's a null move, isn't it?
Correct, and using that as bestmove is the only way a UCI engine can say "I'm done with calculating, but I don't have a move". I can't think of another situation why the engine would send a nullmove.
I agree.
But a null move is not a 'no move'.
Stockfish outputs 'none' in this case.

This is another corner case which is not really specified by the specs.
However, I wonder why any GUI would send a position with no moves to search.
Makes no sense to me.
Jörg Oster