Let us say that I input a wrong move in Stockfish. What answer will Stockfish send me back? Or is there a function to check if a move is bad?

People, I found a solution by myself.

  • 1
    Why on earth would you want to do that? Can't you tell if a move is legal without resort to a computer? – David Richerby Sep 3 '14 at 8:57
  • 2
    We are using stockfish for a project. The computer needs to know if the move is legal. We are using a python program to check this along with stockfish. – MikhailTal Sep 3 '14 at 8:59

Stockfish assumes that all FEN positions you feed it are legal positions. If you feed it an illegal position and ask it to evaluate it, it will likely crash:

Stockfish 5 64 by Tord Romstad, Marco Costalba and Joona Kiiski
position fen 4k3/4p3/8/8/8/8/8/3KP3
go depth 14
Segmentation fault: 11

But you could use a Python library such as Chessnut to validate moves. Chessnut returns all legal moves with get_moves() and raises an InvalidMove exception if you pass an illegal move to apply_move(), as shown in their example:

from Chessnut import Game

chessgame = Game()
print chessgame  # 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

print chessgame.get_moves()
['a2a3', 'a2a4', 'b2b3', 'b2b4', 'c2c3', 'c2c4', 'd2d3', 'd2d4', 'e2e3', 
 'e2e4', 'f2f3', 'f2f4', 'g2g3', 'g2g4', 'h2h3', 'h2h4', 'b1c3', 'b1a3', 
 'g1h3', 'g1f3']

chessgame.apply_move('e2e4')  # succeeds!
print chessgame  # 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'

chessgame.apply_move('e2e4')  # fails! (raises InvalidMove exception)

You could update the game each move with Chessnut, and only pass moves to Stockfish for evaluation if Chessnut says they're valid.

  • I have used a very big code do do everything. It is correct. I will keep this in mind if I need it. – MikhailTal Sep 4 '14 at 7:31
  • +1 Correct solution. One should never use a chess engine to validate a move. – SmallChess Sep 4 '14 at 7:52
  • 1
    @Phonon I've not used it, but Python Chess has is_check(), is_checkmate(), is_stalemate(), and other move validation functions. – Nick Feb 2 '15 at 23:22

Sorry, I had to downvote your question, because you tried something that you weren't supposed to. Stockfish is a chess engine; it always and always assume a move given is legal. If you give it an illegal move, the behavior is undefined - from a crash to jumping king. Anything could happen if you give a move as illegal.

Nobody would use an engine to verify a move's legality. This is done by the GUI. You'll need to incorporate a move generator into your project.

  • And that is what I did. Please try it by yourself, by supplying moves instead of a FEN. Put your last move wrong. You will see that the response will be the same as if you hadn't placed it at all. – MikhailTal Sep 4 '14 at 7:33
  • 1
    I'm not saying your approach wouldn't work. It just doesn't make any sense. Chess engine is never a tool for validating a chess move. What you did is a hack, a hack that might break in the future version of Stockfish. A correct approach is to use a legal move generator. This is how everybody does it. – SmallChess Sep 4 '14 at 7:52
  • Well, excuse me. We will use for our project only that version of stockfish, for once. Also since it works on every UCI protocol, it means that unless the protocol stops getting used, it will keep working. – MikhailTal Sep 4 '14 at 8:06
  • NO and a big NO. If you think your approach is correct, then do it. I'm not in a position to force you. But the UCI protocol doesn't say anything like validating chess moves. Most of the other engines would crash given an illegal move. If you don't believe me, it's fine, but I'm an experienced chess engine author so I do know what I'm talking about. – SmallChess Sep 4 '14 at 8:10
  • So you're the one who asked on the Stockfish support forum? I can help but please post a new question and I'll answer. I know exactly what to do. – SmallChess Jan 31 '15 at 5:17

I have found through self experimenting what was the best choice.

I send the pgn of the entire game each time. Then I send the pgn minus the last move. If they are the same, that means that the last move wasn't correct at all. If they aren't, the move is correct. This works because I use the same depth for both tries, and therefore the answer must be the same. Hope it helps!

  • This might work but its very doggy. – SmallChess Sep 4 '14 at 6:29
  • Also your approach is not compatible with the future versions of Stockfish. There is zero guarantee that an illegal move to Stockfish wouldn't lead to a crash. – SmallChess Sep 4 '14 at 6:30
  • This works with the latest version at least. Also since stockfish uses UCI protocol, it will work with every version of stockfish or other UCI engines – MikhailTal Sep 4 '14 at 7:29
  • No it won't. You must have confused what a UCI protocol is.... – SmallChess Sep 4 '14 at 7:54
  • Please do try it. – MikhailTal Sep 4 '14 at 8:04

Chess engines such as stockfish cannot be used to validates legal chess moves. It will crash or if you get lucky sometimes it won't. But go ahead whatever it is you are doing and good luck


It doesn't seem Stockfish says anything. If it's illegal, it doesn't seem to accept it, and a suggestion is generated on a board without that move.

Illegal move

Position startpos moves e2e7
Info depth 1 seldepth 1 score cp 72 nodes 28 nps 0 time 0 multipv 1 pv g1f3
bestmove g1f3 ponder (none)

OK move

Position startpos moves e2e4
Go Info depth 1 seldepth 1 score cp 16 nodes 43 nps 43000 time 1 multipv 1 pv b8c6 bestmove b8c6 ponder (none)

  • I tried that. But the problem is that I need a response of sorts, because I need to know if the move I receive is right or wrong, for a project. – MikhailTal Aug 4 '14 at 12:47

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.