ChessDotNet.Pieces.King.CanCastle C# (CSharp) Method

CanCastle() protected method

protected CanCastle ( Position origin, Position destination, ChessGame game ) : bool
origin Position
destination Position
game ChessGame
return bool
        protected virtual bool CanCastle(Position origin, Position destination, ChessGame game)
        {
            if (!HasCastlingAbility) return false;
            if (Owner == Player.White)
            {
                if (origin.File != File.E || origin.Rank != 1)
                    return false;
                if (game.IsInCheck(Player.White))
                    return false;
                if (destination.File == File.C)
                {
                    if (!game.CanWhiteCastleQueenSide || game.GetPieceAt(File.D, 1) != null
                        || game.GetPieceAt(File.C, 1) != null
                        || game.GetPieceAt(File.B, 1) != null
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.D, 1), Player.White), Player.White)
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.C, 1), Player.White), Player.White))
                        return false;
                }
                else
                {
                    if (!game.CanWhiteCastleKingSide || game.GetPieceAt(File.F, 1) != null
                        || game.GetPieceAt(File.G, 1) != null
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.F, 1), Player.White), Player.White)
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.G, 1), Player.White), Player.White))
                        return false;
                }
            }
            else
            {
                if (origin.File != File.E || origin.Rank != 8)
                    return false;
                if (game.IsInCheck(Player.Black))
                    return false;
                if (destination.File == File.C)
                {
                    if (!game.CanBlackCastleQueenSide || game.GetPieceAt(File.D, 8) != null
                        || game.GetPieceAt(File.C, 8) != null
                        || game.GetPieceAt(File.B, 8) != null
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.D, 8), Player.Black), Player.Black)
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.C, 8), Player.Black), Player.Black))
                        return false;
                }
                else
                {
                    if (!game.CanBlackCastleKingSide || game.GetPieceAt(File.F, 8) != null
                        || game.GetPieceAt(File.G, 8) != null
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.F, 8), Player.Black), Player.Black)
                        || game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.G, 8), Player.Black), Player.Black))
                        return false;
                }
            }
            return true;
        }