SebLague / Chess-Challenge

Create your own tiny chess bot!
https://www.youtube.com/watch?v=Ne40a5LkK6A
MIT License
1.78k stars 1.07k forks source link

board.MakeMove() creating NullReferenceException #434

Closed ComplexSymbol closed 11 months ago

ComplexSymbol commented 11 months ago

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at ChessChallenge.Chess.Board.MovePiece(Int32 piece, Int32 startSquare, Int32 targetSquare) in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/Framework/Chess/Board/Board.cs:line 113 at ChessChallenge.Chess.Board.MakeMove(Move move, Boolean inSearch) in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/Framework/Chess/Board/Board.cs:line 142 at ChessChallenge.API.Board.MakeMove(Move move) in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/API/Board.cs:line 83 at MyBot.IsMoveCheckmate(Board board, Move move) in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/My Bot/MyBot.cs:line 77 at MyBot.<Think>g__SearchForBestMove|1_0(Board board, <>c__DisplayClass1_0& ) in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/My Bot/MyBot.cs:line 23 at MyBot.Think(Board board, Timer timer) in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/My Bot/MyBot.cs:line 70 at ChessChallenge.Application.ChallengeController.GetBotMove() in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs:line 150 --- End of stack trace from previous location --- at ChessChallenge.Application.ChallengeController.Update() in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs:line 378 at ChessChallenge.Application.Program.Main() in /Users/whoami/Downloads/Chess-Challenge-main/Chess-Challenge/src/Framework/Application/Core/Program.cs:line 40

I haven't a clue why this is happening, all of my MakeMoves have an UndoMove right after them, and I have made multiple function and exception breakpoints to see what is going on, and the issue seems to be a function called IsMoveCheckmate()

Here is the code for that function: bool IsMoveCheckmate(Board board, Move move) { board.MakeMove(move); bool isMate = board.IsInCheckmate(); board.UndoMove(move); return isMate; }

I don't see anything wrong, yet the last call from my program in the stacktrace is the board.MakeMove function... My move that I fed into MakeMove was gathered from legalMoves = board.GetLegalMoves(), and I even verified in immediate that legalMoves.Contains(move) and it returned true.

Can someone help me? Thanks!

ComplexSymbol commented 11 months ago

Make sure you have UndoMove()'s folks, I was misled by the stacktrace to this function, but the root cause was a missing UndoMove() in a different function entirely!