SebLague / Chess-Challenge

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

Board reports legal moves after drawn position #295

Open WorstWizard opened 1 year ago

WorstWizard commented 1 year ago

To reproduce, use the code snippet below for MyBot and play g1 to g3, then shuffle the rook back and forth. The computer should do the same in the opposite corner, and a repetition draw will (correctly) occur after a few moves back and forth.

public class MyBot : IChessBot
{
    public Move Think(Board board, Timer timer)
    {
        Move[] moves = board.GetLegalMoves();
        Move moveToPlay = moves[0];

        board.MakeMove(moveToPlay);
        if (board.IsDraw())
        {
            System.Console.WriteLine(
                "Number of legal positions from drawn position: {0}",
                board.GetLegalMoves().Length
            );
        }
        board.UndoMove(moveToPlay);

        return moveToPlay;
    }
}

Doing so produces the following output:

Launching Chess-Challenge version 1.13
Number of legal positions from drawn position: 19
Number of legal positions from drawn position: 20
Number of legal positions from drawn position: 19
Game Over: Repetition

There should be no legal moves following a draw, as the game has ended. Further, the bot misreports that its next move will be a draw twice too early, but I think https://github.com/SebLague/Chess-Challenge/issues/170 deals with this already.

mcthouacbb commented 1 year ago

What's wrong with this behavior? Move generators don't concern themselves with the 50 move rule or repetitions, that's not what they're made for. You would get this same behavior if you modified stockfish to perform this same test.

lucasmeneghin commented 1 year ago

What's wrong with this behavior? Move generators don't concern themselves with the 50 move rule or repetitions, that's not what they're made for. You would get this same behavior if you modified stockfish to perform this same test.

A drawn game has no legal moves, afaik, because it has ended.

WorstWizard commented 1 year ago

A simple tree search (the way I've implemented it for instance) will usually only evaluate the state of the board at the leaf nodes. The easiest way to do this is to get a list of legal moves, and then recursively evaluate on the board state following those moves.

With the current behaviour, you would have to check whether the board is a draw at each internal node if you don't want to search an invalid state space, rather than just the leaves. Since we're constrained on token space in this challenge, this absolutely matters. Whether or not it is intended behaviour is up to @SebLague of course, but the current behaviour seems unintuitive, it's certainly not documented.

Move generators don't concern themselves with the 50 move rule or repetitions, that's not what they're made for.

The Board class includes a check for whether the current state is a draw, because it knows whether or not it is (or at least it should). So it should already know that the moves it's generating are bogus. It's not just a move generator, it already tracks this information, just a matter of including that in its knowledge of move legality.

mcthouacbb commented 1 year ago

There's absolutely 0 point checking for drawn games in the move generator. If an engine isn't already checking for draws, it could mistake the draw for a checkmate, since an absence of legal moves can be counted as checkmate. If it is already checking for draws, then you're just pointlessly checking for the 50 move rule.

Also, a tree search is not a game. You can search in whatever way you want. Returning no legal moves in technically drawn positions is just hindering creativity.

Lastly, like I said previously, every major chess engine will have the exact same behavior. Stockfish doesn't check for draws in move generation. Neither does Leela Chess Zero or Ethereal, and I'm certain that Komodo doesn't either.

Zadmur commented 1 year ago

Note that by the chess rules 'In chess, the threefold repetition rule states that a player may claim a draw if the same position occurs three times during the game'. They may claim, but they don't have to. It's perfectly legal for them to continue. Thus it's valid to return legal moves.

WorstWizard commented 1 year ago

That's a perfectly valid point! Wasn't aware that it was optional per actual rules. Within this version of the game, it is an instant draw, so I'll leave the issue open for now as it could still be a convenience feature.