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

MakeMove() produces illegal moves when searching #427

Open rodrigo-sotelo-sd opened 11 months ago

rodrigo-sotelo-sd commented 11 months ago

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.

loki12241224 commented 11 months ago

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.

ElectroN4ick commented 11 months ago

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

rodrigo-sotelo-sd commented 11 months ago

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

rodrigo-sotelo-sd commented 11 months ago

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

ItsBehradaYt commented 9 months ago

grr i have taht pronblem too

ItsBehradaYt commented 9 months ago

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

ryanheath commented 9 months ago

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

ItsBehradaYt commented 9 months ago

yes

ItsBehradaYt commented 9 months ago

its board.GetLegalMoves()

ryanheath commented 9 months ago

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
}
ItsBehradaYt commented 9 months ago

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: @.***>