rudzen / ChessLib

C# chess library containing a complete data structure and move generation.
MIT License
81 stars 22 forks source link

== operator by Move comparison #49

Closed JMSSVLC closed 2 years ago

JMSSVLC commented 2 years ago

I was watching your project and it occurs to me to find the legal moves for the position described for the following FEN "r3kb1r/p3pppp/p1n2n2/2pp1Q2/3P1B2/2P1PN2/Pq3PPP/RN2K2R w KQkq - 0 9" so that I could understand the "magic" of the "Magic Bitboards" and I found that in the following snippet

  MoveList ml = pos.GenerateMoves();
  ml.Contains(new Move(new Square(Ranks.Rank1, Files.FileE), new Square(Ranks.Rank1, Files.FileG), MoveTypes.Castling))

Among the legal moves there is a short-castling, but it seems that the internal data are different because of the == operator in the following function of MoveList class

    public bool Contains(Move item)
    {
        for (var i = 0; i < _last; ++i)
            if (_moves[i].Move == item)
                return true;
        return false;
    }

using the debugger I found that left._data = 49415 and right._data = 49414, although both moves are recognized as "0-0"

the rest of moves are all OK with the == operator. I think te problem is with castling

but I don't understand your code enough to find what is wrong. Maybe I am the one who makes something wrong

I hope it helps you. It is a very interesting project

rudzen commented 2 years ago

At first glance I can't seem to find any problem.

The illegal move removal code is correctly "removing" e1e2 from the list (by placing it beyond a "empty" move). Any move after the first ("none") move is not applicable and it's done this way for performance reasons.

Let me know if there should be something I've missed.