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

[BUG] Undoing a move does not decrease `Board.depth` #424

Closed rokbok closed 11 months ago

rokbok commented 11 months ago

Board.depth is increased in MakeMove, but not decreased in UndoMove. This leads to issues, at the very least in the following situation

This could be related to #170. The reason I created a separate bug is that it is not clear that this is the only cause for #170.

Draeggon commented 11 months ago

I encountered the same problem. Here is a minimal code snippet to reproduce behavior, if you play b1a3, a1b1, b1a1, a1b1 b1a1 it does the trick.

using ChessChallenge.API;
using System;

public class MyBot : IChessBot
{
    int[] pieceValues = { 0, 100, 300, 300, 500, 900, 10000 };

    public Move Think(Board board, Timer timer)
    {
        bool a = board.IsDraw();
        Move moveToPlay = ThinkHarder(board, board.IsWhiteToMove);
        bool b = board.IsDraw();

        if (a != b) // log if weirdness happen
        {
            Console.WriteLine(board.GetFenString());
            Console.WriteLine(moveToPlay);
            Console.WriteLine(board.IsInStalemate());
            Console.WriteLine(board.IsRepeatedPosition());
            Console.WriteLine(board.IsInsufficientMaterial());
            Console.WriteLine(board.IsFiftyMoveDraw());
        }

        return moveToPlay;
    }

    Move ThinkHarder(Board board, bool color)
    {
        Move[] allMoves = board.GetLegalMoves();
        Move moveToPlay = allMoves[0];

        foreach (Move move in allMoves)
        {
            board.MakeMove(move);
            board.UndoMove(move);
        }
        return moveToPlay;
    }
}
SebLague commented 11 months ago

Thank you, I've fixed the bug with the depth counter. Sorry about that!

rokbok commented 11 months ago

Absolutely no worries and thank you for the fix