Answers.Body: <p>Stockfish assumes that all FEN positions you feed it are legal positions. If you feed it an <a href="http://www.chessvideos.tv/bimg/5rs712zf8j6g.png">illegal position</a> and ask it to evaluate it, it will likely crash:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But you could use a Python library such as <a href="https://github.com/cgearhart/Chessnut">Chessnut</a> to validate moves. Chessnut returns all legal moves with <code>get_moves()</code> and raises an InvalidMove exception if you pass an illegal move to <code>apply_move()</code>, as shown in their example:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>You could update the game each move with Chessnut, and only pass moves to Stockfish for evaluation if Chessnut says they're valid.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-02T23:22:12.170Z
Answers.Text: @Phonon I've not used it, but [Python Chess](https://pypi.python.org/pypi/python-chess) has `is_check()`, `is_checkmate()`, `is_stalemate()`, and other move validation functions.
Answers.UserDisplayName: Nick
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:31:47.757Z
Answers.Text: I have used a very big code do do everything. It is correct. I will keep this in mind if I need it.
Answers.UserDisplayName: MikhailTal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:52:46.507Z
Answers.Text: +1 Correct solution. One should never use a chess engine to validate a move.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:25:15.723Z
Answers.LastActivityDate: 2014-09-04T07:25:15.723Z
Answers.UserDisplayName: Nick
Answers.Body: <p>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.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:33:20.927Z
Answers.Text: 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.
Answers.UserDisplayName: MikhailTal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:52:01.030Z
Answers.Text: 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.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T08:06:20.900Z
Answers.Text: 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.
Answers.UserDisplayName: MikhailTal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T08:10:05.407Z
Answers.Text: 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.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-31T05:17:17.743Z
Answers.Text: 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.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-09-04T06:27:48.030Z
Answers.LastActivityDate: 2018-12-03T11:27:37.307Z
Answers.LastEditDate: 2018-12-03T11:27:37.307Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>I have found through self experimenting what was the best choice.</p>
  
  <p>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!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T06:29:25.130Z
Answers.Text: This might work but its very doggy.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T06:30:09.293Z
Answers.Text: 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.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:29:58.997Z
Answers.Text: 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
Answers.UserDisplayName: MikhailTal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T07:54:08.473Z
Answers.Text: No it won't. You must have confused what a UCI protocol is....
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T08:04:17.823Z
Answers.Text: Please do try it.
Answers.UserDisplayName: MikhailTal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T08:08:06.813Z
Answers.Text: I don't want to force you to accept my views. Just last point, something that works doesn't mean it's good. Even it works, you would have missed the right concept and understanding. Chess engine is not designed to validate moves, Nick's answer is perfect.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-03T08:30:22.617Z
Answers.LastActivityDate: 2014-09-03T08:30:22.617Z
Answers.UserDisplayName: MikhailTal
Answers.Body: <p>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.</p>
  <h3>Illegal move</h3>
  <p>Position <strong>startpos moves e2e7</strong> <br/>
  Go <br/>
  Info depth 1 seldepth 1 score cp 72 nodes 28 nps 0 time 0 multipv 1 pv g1f3 <br/>
  <strong>bestmove g1f3</strong> ponder (none)</p>
  <h3>OK move</h3>
  <p>Position <strong>startpos moves e2e4</strong> <br/>
  Go
  Info depth 1 seldepth 1 score cp 16 nodes 43 nps 43000 time 1 multipv 1 pv b8c6
  <strong>bestmove b8c6</strong> ponder (none)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-04T12:47:34.330Z
Answers.Text: 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.
Answers.UserDisplayName: MikhailTal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-08-04T07:39:50.943Z
Answers.LastActivityDate: 2018-12-03T11:40:43.457Z
Answers.LastEditDate: 2020-06-17T09:59:59.497Z
Answers.UserDisplayName: user2371
Answers.Body: <p>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</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-06T12:59:37.953Z
Answers.LastActivityDate: 2015-09-06T12:59:37.953Z
Answers.UserDisplayName: daparic
Body: <p>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?</p>
  
  <p>People, I found a solution by myself.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-15T10:36:48.663Z
Comments.Text: Perhaps, you need to distinguish between `bad` or `illegal` move. A `bad` move is legal but it is `bad` but still is a `legal` move. An `illegal` move is a move not allowed in the rule of chess, such as moving your queen like a knight which is illegal.
Comments.UserDisplayName: daparic
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-03T08:57:50.933Z
Comments.Text: Why on earth would you want to do that? Can't you tell if a move is legal without resort to a computer?
Comments.UserDisplayName: David Richerby
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-03T08:59:39.040Z
Comments.Text: 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.
Comments.UserDisplayName: MikhailTal
Tags: <engines><stockfish><programming><uci>
Title: How do I check if a move is legal using Stockfish?
ThreadText: # How do I check if a move is legal using Stockfish?
  
  (Asked by: username_0 on 2014-08-02)
  
  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.
  
  **Tags:** engines, stockfish, programming, uci
  
  > **Comments:**
  > **username_2**: Why on earth would you want to do that? Can't you tell if a move is legal without resort to a computer?  
  > **username_0**: 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.  
  > **username_1**: Perhaps, you need to distinguish between `bad` or `illegal` move. A `bad` move is legal but it is `bad` but still is a `legal` move. An `illegal` move is a move not allowed in the rule of chess, such as moving your queen like a knight which is illegal.  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_3 on 2014-09-04. Score: 12
  
  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.
  
  > **Comments:**
  > **username_0**: I have used a very big code do do everything. It is correct. I will keep this in mind if I need it.  
  > **username_4**: +1 Correct solution. One should never use a chess engine to validate a move.  
  > **username_3**: @Phonon I've not used it, but [Python Chess](https://pypi.python.org/pypi/python-chess) has `is_check()`, `is_checkmate()`, `is_stalemate()`, and other move validation functions.  
  
  ---
  
  ### Answer by username_4 on 2014-09-04. Score: 2
  
  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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_4**: 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.  
  > **username_0**: 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.  
  > **username_4**: 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.  
  > **username_4**: 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.  
  
  ---
  
  ### Answer by username_0 on 2014-09-03. Score: 1
  
  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!
  
  > **Comments:**
  > **username_4**: This might work but its very doggy.  
  > **username_4**: 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.  
  > **username_0**: 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  
  > **username_4**: No it won't. You must have confused what a UCI protocol is....  
  > **username_0**: Please do try it.  
  > **username_4**: I don't want to force you to accept my views. Just last point, something that works doesn't mean it's good. Even it works, you would have missed the right concept and understanding. Chess engine is not designed to validate moves, username_3's answer is perfect.  
  
  ---
  
  ### Answer by username_5 on 2014-08-04. Score: 1
  
  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**   
  Go   
  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)
  
  > **Comments:**
  > **username_0**: 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.  
  
  ---
  
  ### Answer by username_1 on 2015-09-06. Score: 1
  
  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
  
  
  ---
  
  