Chess-Challenge icon indicating copy to clipboard operation
Chess-Challenge copied to clipboard

UndoMove does not place pieces back

Open megamaz opened this issue 2 years ago • 3 comments

Been doing some testing and noticed after a while that the bot was failing to see checkmate, despite having a clear if statement checking for it. After a bit I decied to output the FEN strings, and found this...

before any moves are made: rnbqkbnr/pppp1ppp/4p3/6N1/8/8/PPPPPPPP/RNBQKB1R b KQkq - 1 2
after making Move: 'e8e7' rnbq1bnr/ppppkppp/4p3/6N1/8/8/PPPPPPPP/RNBQKB1R w KQ - 2 3
after undoing Move: 'e8e7' rnbqkbnr/pppp1ppp/4p3/6N1/8/8/PPPPPPPP/RNBQKB1R b KQ - 3 3
after making Move: 'd8g5' rnb1kbnr/pppp1ppp/4p3/6q1/8/8/PPPPPPPP/RNBQKB1R w KQ - 4 4
after undoing Move: 'd8g5' rnbqkbnr/pppp1ppp/4p3/8/8/8/PPPPPPPP/RNBQKB1R b KQ - 5 4
after making Move: 'd8f6' rnb1kbnr/pppp1ppp/4pq2/8/8/8/PPPPPPPP/RNBQKB1R w KQ - 6 5
after undoing Move: 'd8f6' rnbqkbnr/pppp1ppp/4p3/8/8/8/PPPPPPPP/RNBQKB1R b KQ - 7 5

There is a white Knight on g5 that can be seen in the original FEN. When it is not taken, e8e7, the UndoMove function works fine. After it is taken, d8g5, the UndoMove function fails to place the Knight back, as the FEN string shows.

After some more testing, I found the culprit to be the board.TrySkipTurn() method. If placed twice in a row, effectively doing nothing, the UndoMove function fails to place a piece back,

megamaz avatar Jul 22 '23 05:07 megamaz

If placed twice in a row, effectively doing nothing, the UndoMove function fails to place a piece back

If starting on white, wouldn't it simply skip whites turn then skip blacks turn?

I'm having, I think, similar issues as you. The AddPieceAtSquare method is throwing "Index was out of bounds of the array." exception when I call UndoMove after calling TrySkipTurn twice in a row. I'm calling MakeMove -> TrySkipTurn -> MakeMove -> TrySkipTurn -> UndoSkipTurn -> UndoMove -> UndoSkipTurn -> UndoMove.

I'm not 100% sure if this is intended behavior and I'm just missing it or not.

QuinnCG avatar Jul 22 '23 06:07 QuinnCG

For now my solution is

Board newboard = Board.CreateBoardFromFEN(board.GetFenString());
newboard.TrySkipTurn();

though I'm not sure how compliant that is with the rules.

megamaz avatar Jul 22 '23 18:07 megamaz

For now my solution is

Board newboard = Board.CreateBoardFromFEN(board.GetFenString());
newboard.TrySkipTurn();

though I'm not sure how compliant that is with the rules.

Complies fine with the rules IMO, but will be slow, so it's not an ideal fix. Hopefully we'll get a real solution soon.

dudecon avatar Jul 23 '23 20:07 dudecon