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.
Chess Stack Exchange is a question and answer site for serious players and enthusiasts of chess. It only takes a minute to sign up.
Sign up to join this communityStockfish 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.
is_check()
, is_checkmate()
, is_stalemate()
, and other move validation functions.
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.
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!
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.
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)
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)
bad
orillegal
move. Abad
move is legal but it isbad
but still is alegal
move. Anillegal
move is a move not allowed in the rule of chess, such as moving your queen like a knight which is illegal.