Example #1
0
        private string ValidateCastleling(string error)
        {
            Span <Player>           players = stackalloc Player[] { Player.White, Player.Black };
            Span <CastlelingRights> crs     = stackalloc CastlelingRights[] { CastlelingRights.None, CastlelingRights.None };

            foreach (var c in players)
            {
                crs[0] = CastlelingRights.KingSide.MakeCastlelingRights(c);
                crs[1] = CastlelingRights.QueenSide.MakeCastlelingRights(c);

                var ourRook = PieceTypes.Rook.MakePiece(c);
                foreach (var cr in crs)
                {
                    if (!_pos.CanCastle(cr))
                    {
                        continue;
                    }

                    var rookSq = _pos.CastlingRookSquare(cr);

                    if (_board.PieceAt(rookSq) != ourRook)
                    {
                        error = AddError(error, $"rook does not appear on its position for {c}");
                    }

                    if (_pos.GetCastlelingRightsMask(rookSq) != cr)
                    {
                        error = AddError(error, $"castleling rights mask at {rookSq} does not match for player {c}");
                    }

                    if ((_pos.GetCastlelingRightsMask(_pos.GetKingSquare(c).AsInt()) & cr) != cr)
                    {
                        error = AddError(error, $"castleling rights mask at {_pos.GetKingSquare(c)} does not match for player {c}");
                    }
                }
            }

            return(error);
        }