public Board DoMoveIfKingSafe(Board board, Move move)
        {
            var afterMoveBitBoards     = board.DoMove(move);
            var enemyAttackedAfterMove = AttacksService.GetAllAttacked(afterMoveBitBoards);
            var myKings = board.WhiteToMove ? afterMoveBitBoards.BitBoard[ChessPiece.WhiteKing] : afterMoveBitBoards.BitBoard[ChessPiece.BlackKing];
            var isSafe  = (enemyAttackedAfterMove & myKings) == 0;

            return(isSafe ? afterMoveBitBoards : null);
        }
        private void GetPotentialCastlingMoves(Board board, Move[] moves, ref int moveCount)
        {
            var      isWhite = board.WhiteToMove;
            Position kingPos;
            ulong    queenSideCastleMask;
            ulong    kingSideCastleMask;
            ulong    queenSideCastleAttackMask;
            ulong    kingSideCastleAttackMask;
            Piece    piece;
            bool     castlingPermissionQueenSide;
            bool     castlingPermissionKingSide;

            if (isWhite)
            {
                castlingPermissionQueenSide = (board.CastlingPermissions & CastlingPermission.WhiteQueen) != CastlingPermission.None;
                castlingPermissionKingSide  = (board.CastlingPermissions & CastlingPermission.WhiteKing) != CastlingPermission.None;
                //kingPos = board.KingPositions[ChessPiece.White];
                kingPos                   = board.BitBoard[ChessPiece.WhiteKing].BitScanForward();
                queenSideCastleMask       = BitboardConstants.WhiteQueenSideCastleMask;
                kingSideCastleMask        = BitboardConstants.WhiteKingSideCastleMask;
                queenSideCastleAttackMask = BitboardConstants.WhiteQueenSideCastleAttackMask;
                kingSideCastleAttackMask  = BitboardConstants.WhiteKingSideCastleAttackMask;
                piece = ChessPiece.WhiteKing;
            }
            else
            {
                castlingPermissionQueenSide = (board.CastlingPermissions & CastlingPermission.BlackQueen) != CastlingPermission.None;
                castlingPermissionKingSide  = (board.CastlingPermissions & CastlingPermission.BlackKing) != CastlingPermission.None;
                //kingPos = board.KingPositions[ChessPiece.Black];
                kingPos                   = board.BitBoard[ChessPiece.BlackKing].BitScanForward();
                queenSideCastleMask       = BitboardConstants.BlackQueenSideCastleMask;
                kingSideCastleMask        = BitboardConstants.BlackKingSideCastleMask;
                queenSideCastleAttackMask = BitboardConstants.BlackQueenSideCastleAttackMask;
                kingSideCastleAttackMask  = BitboardConstants.BlackKingSideCastleAttackMask;
                piece = ChessPiece.BlackKing;
            }

            var canMaybeCastleQueenSide = castlingPermissionQueenSide && ((board.AllPieces & queenSideCastleMask) == 0);
            var canMaybeCastleKingSide  = castlingPermissionKingSide && (board.AllPieces & kingSideCastleMask) == 0;

            if (canMaybeCastleQueenSide | canMaybeCastleKingSide)
            {
                var attackedByEnemy = _attacksService.GetAllAttacked(board, !board.WhiteToMove);
                //if (canMaybeCastleQueenSide && !AttacksService.IsBitboardAttacked(board, queenSideCastleAttackMask, !board.WhiteToMove))
                if (canMaybeCastleQueenSide && ((attackedByEnemy & queenSideCastleAttackMask) == 0))
                {
                    moves[moveCount++] = new Move(kingPos, (Position)(kingPos - 2), piece, ChessPiece.Empty, false, true);
                }
                //if (canMaybeCastleKingSide && !AttacksService.IsBitboardAttacked(board, kingSideCastleAttackMask, !board.WhiteToMove))
                if (canMaybeCastleKingSide && ((attackedByEnemy & kingSideCastleAttackMask) == 0))
                {
                    moves[moveCount++] = new Move(kingPos, (Position)(kingPos + 2), piece, ChessPiece.Empty, false, true);
                }
            }
        }
        public IList <Move> GetPotentialCastlingMoves(Board board)
        {
            var   castlingMoves = new List <Move>();
            var   isWhite       = board.WhiteToMove;
            int   kingPos;
            ulong queenSideCastleMask;
            ulong kingSideCastleMask;
            ulong queenSideCastleAttackMask;
            ulong kingSideCastleAttackMask;
            int   piece;
            bool  castlingPermissionQueenSide;
            bool  castlingPermissionKingSide;

            if (isWhite)
            {
                castlingPermissionQueenSide = board.CastlingPermissions[CastlePermission.WhiteQueenSide];
                castlingPermissionKingSide  = board.CastlingPermissions[CastlePermission.WhiteKingSide];
                kingPos                   = board.BitBoard[ChessPiece.WhiteKing].BitScanForward();
                queenSideCastleMask       = Board.WhiteQueenSideCastleMask;
                kingSideCastleMask        = Board.WhiteKingSideCastleMask;
                queenSideCastleAttackMask = Board.WhiteQueenSideCastleAttackMask;
                kingSideCastleAttackMask  = Board.WhiteKingSideCastleAttackMask;
                piece = ChessPiece.WhiteKing;
            }
            else
            {
                castlingPermissionQueenSide = board.CastlingPermissions[CastlePermission.BlackQueenSide];
                castlingPermissionKingSide  = board.CastlingPermissions[CastlePermission.BlackKingSide];
                kingPos                   = board.BitBoard[ChessPiece.BlackKing].BitScanForward();
                queenSideCastleMask       = Board.BlackQueenSideCastleMask;
                kingSideCastleMask        = Board.BlackKingSideCastleMask;
                queenSideCastleAttackMask = Board.BlackQueenSideCastleAttackMask;
                kingSideCastleAttackMask  = Board.BlackKingSideCastleAttackMask;
                piece = ChessPiece.BlackKing;
            }

            var canMaybeCastleQueenSide = castlingPermissionQueenSide && ((board.AllPieces & queenSideCastleMask) == 0);
            var canMaybeCastleKingSide  = castlingPermissionKingSide && (board.AllPieces & kingSideCastleMask) == 0;

            if (canMaybeCastleQueenSide | canMaybeCastleKingSide)
            {
                var attackedByEnemy = AttacksService.GetAllAttacked(board, !board.WhiteToMove);
                if (canMaybeCastleQueenSide && ((attackedByEnemy & queenSideCastleAttackMask) == 0))
                {
                    castlingMoves.Add(new Move(kingPos, kingPos - 2, piece));
                }
                if (canMaybeCastleKingSide && ((attackedByEnemy & kingSideCastleAttackMask) == 0))
                {
                    castlingMoves.Add(new Move(kingPos, kingPos + 2, piece));
                }
            }

            return(castlingMoves);
        }