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

MakeMove() produces illegal moves when searching

Open rodrigo-sotelo-sd opened this issue 2 years ago • 11 comments

I'm implementing a search function (very similar to the one Sebastian shows in the first coding adventure video) and I'm getting that the bot is making illegal moves on the board and the game is over on the spot.

I think that what's happening is that MakeMove() doesn't update the FEN string of the current position and when I search with depth >= 3, the bot can make moves that are impossible in the current position.

For example, if the bot is black, I play 1.e4 and it starts searching. In that process it plays 1.e5 and it evaluates after white's next move the move e1e2, but the game's FEN string shows there's a pawn there.

Maybe I'm wrong, I'm very new to C# and chess programming. If you need to look at my code I can show it.

rodrigo-sotelo-sd avatar Aug 03 '23 12:08 rodrigo-sotelo-sd

this is accurate to my experience too, yesterday i left off because even though i never feed illegal moves into the system i kept getting illegal moves sent back after a depth of around 4. it could very well still be my fault but have definitely been confused for a few days now.

loki12241224 avatar Aug 03 '23 16:08 loki12241224

MakeMove works fine to me, make sure you call UndoMove(move) each time you call MakeMove

Cupcake6 avatar Aug 03 '23 19:08 Cupcake6

I am doing that which is why i find it weird.

rodrigo-sotelo-sd avatar Aug 03 '23 22:08 rodrigo-sotelo-sd

Another weird thing is that it says it's playing illegal moves that shouldn't even be possible in any position.

"Illegal move: d1h5 in position: rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"

d1h5 moves the queen not even in the same diagonal

rodrigo-sotelo-sd avatar Aug 05 '23 16:08 rodrigo-sotelo-sd

grr i have taht pronblem too

ItsBehradaYt avatar Sep 18 '23 20:09 ItsBehradaYt

its so anoying how would you even make a bot without that feature

ItsBehradaYt avatar Sep 18 '23 20:09 ItsBehradaYt

How do you get your moves to make? Do you use the board's method GetLegalMoves()?

ryanheath avatar Sep 18 '23 20:09 ryanheath

yes

ItsBehradaYt avatar Sep 20 '23 15:09 ItsBehradaYt

its board.GetLegalMoves()

ItsBehradaYt avatar Sep 20 '23 15:09 ItsBehradaYt

Whenever you do a MakeMove you should have a corresponding UndoMove, before you can do a MakeMove of its 'siblings legalMoves'.

wrong

foreach(var m in board.GetLegalMoves())
{
    board.MakeMove(m);
    // do other stuff
}

right

foreach(var m in board.GetLegalMoves())
{
    board.MakeMove(m);
    // do other stuff
    board.UndoMove(); // undo move m
}

also wrong

foreach(var m in board.GetLegalMoves())
{
    board.MakeMove(m);
    // do other stuff
    // nested calls to GetLegalMoves()
    foreach(var m2 in board.GetLegalMoves()) // these are other legal moves than above
    {
        board.MakeMove(m2);
        // do more stuff
    }
}

also right

foreach(var m in board.GetLegalMoves())
{
    board.MakeMove(m);
    // do other stuff
    // nested calls to GetLegalMoves()
    foreach(var m2 in board.GetLegalMoves()) // these are other legal moves than above
    {
        board.MakeMove(m2);
        // do more stuff
        board.UndoMove(); // undo move m2
    }
    board.UndoMove(); // undo move m
}

ryanheath avatar Sep 20 '23 16:09 ryanheath

I did that but somehow it doesn't want to work

On Wed, 20 Sept 2023, 18:23 Ryan Heath, @.***> wrote:

Whenever you do a MakeMove you should have a corresponding UndoMove, before you can do a MakeMove of its 'siblings legalMoves'.

wrong

foreach(var m in board.GetLegalMoves()) { board.MakeMove(m); // do other stuff }

right

foreach(var m in board.GetLegalMoves()) { board.MakeMove(m); // do other stuff board.UndoMove(); // undo move m }

also wrong

foreach(var m in board.GetLegalMoves()) { board.MakeMove(m); // do other stuff // nested calls to GetLegalMoves() foreach(var m2 in board.GetLegalMoves()) // these are other legal moves than above { board.MakeMove(m2); // do more stuff } }

also right

foreach(var m in board.GetLegalMoves()) { board.MakeMove(m); // do other stuff // nested calls to GetLegalMoves() foreach(var m2 in board.GetLegalMoves()) // these are other legal moves than above { board.MakeMove(m2); // do more stuff board.UndoMove(); // undo move m2 } board.UndoMove(); // undo move m }

— Reply to this email directly, view it on GitHub https://github.com/SebLague/Chess-Challenge/issues/427#issuecomment-1728066498, or unsubscribe https://github.com/notifications/unsubscribe-auth/BCPILCSATCP2E6NUGQMPZ5LX3MKA7ANCNFSM6AAAAAA3CVHTYY . You are receiving this because you commented.Message ID: @.***>

ItsBehradaYt avatar Sep 24 '23 12:09 ItsBehradaYt