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

SquareIsAttackedByOpponent is not always Working #416

Open PetePete opened 11 months ago

PetePete commented 11 months ago

I make board.MakeMove and then board.SquareIsAttackedByOpponent in order to check if a Square is safe to go to, but this does not always work correctly, leading my bot to make silly moves....

Also, for debugging purposes, a forward/backward-functionality would be very helpful in order to analyse certain behaviour.

draeg82 commented 11 months ago

Are you understanding board.SquareIsAttackedByOpponent() returns whether your bot is attacking the square after board.MakeMove()?

PetePete commented 11 months ago

Hi draeg, I do, I tried it otherwise without move first, but with similiar mixed results. But it is no problem, I created my own check-Method based on the opponents legal capturing moves targeting my position, this way it is working fine. I suspect a problem with the frameworks BitBoard logic but it could be me as well misunderstandig the methods purpose

SebLague commented 11 months ago

Hi, could you maybe provide some code where the function is not doing what you expect?

MarcusOtter commented 9 months ago

EDIT: The white rook is probably blocking the attack from the rook on h8.

Workaround for this case (good bye tokens 😉)

public MyBot()
{
    var board = Board.CreateBoardFromFEN("2kr3r/ppp5/2nb1n1R/4p1p1/Q2p2b1/3P2P1/PPP1PPP1/RNK2BN1 w - - 1 16");

    var move = new Move("h6h1", board);
    board.MakeMove(move);
    board.TrySkipTurn();
    var isAttacked = board.SquareIsAttackedByOpponent(new Square("h1"));
    board.UndoSkipTurn();
    board.UndoMove(move);

    Console.WriteLine(isAttacked); // True
}

I am also having bugs with board.SquareIsAttackedByOpponent(). Here's a minimal repro:

public MyBot()
{
    var board = Board.CreateBoardFromFEN("2kr3r/ppp5/2nb1n1R/4p1p1/Q2p2b1/3P2P1/PPP1PPP1/RNK2BN1 w - - 1 16");
    Console.WriteLine(board.SquareIsAttackedByOpponent(new Square("h1"))); // False
}

White to move. Is h1 attacked by black in this position?

image

The method seems to think that it is not. Maybe I am misunderstanding something about how this method is supposed to work?

I know that the API will not change, but I thought I'd still report it)