Queem.Core.ChessBoard.PlayerBoard.GetKingMoves C# (CSharp) Method

GetKingMoves() public method

public GetKingMoves ( PlayerBoard opponent, ulong mask, MovesMask movesMask ) : List
opponent PlayerBoard
mask ulong
movesMask MovesMask
return List
        public List<Move[]> GetKingMoves(PlayerBoard opponent, ulong mask, MovesMask movesMask)
        {
            var otherFigures = opponent.allFigures | this.allFigures;
            var list = this.moveGenerators[(int)Figure.King].GetMoves(otherFigures, mask);

            // ---------------------------------------------

            // castlings stuff

            // check king can move
            if (this.King.AlreadyMoved != 0)
                return list;

            var rooks = this.Rooks;
            // if rooks cannot move
            if (rooks.GetInnerProperty() == 0)
                return list;

            if (movesMask == MovesMask.Attacks)
                return list;

            var allMasks = KingBitBoardHelper.CastlingMasks;
            ulong[] castlingMasks = allMasks[(int)this.color][(int)this.position];

            bool leftIsEmpty = (castlingMasks[0] & otherFigures) == 0;
            bool rightIsEmpty = (castlingMasks[1] & otherFigures) == 0;

            // check other figures
            if (!(leftIsEmpty || rightIsEmpty))
                return list;

            bool noCheck;
            var squares = KingBitBoardHelper.CastlingSquares[(int)this.color][(int)this.position];
            int resultIndex = -1;
            // check checks
            if (leftIsEmpty && (rooks.LeftNotMoved != 0))
            {
                noCheck = true;
                for (int i = 0; i < squares[0].Length; ++i)
                {
                    if (this.IsUnderAttack(squares[0][i], opponent))
                    {
                        noCheck = false;
                        break;
                    }
                }
                if (noCheck)
                    resultIndex = 0;
            }

            if (rightIsEmpty && (rooks.RightNotMoved != 0))
            {
                noCheck = true;
                for (int i = 0; i < squares[1].Length; ++i)
                {
                    if (this.IsUnderAttack(squares[1][i], opponent))
                    {
                        noCheck = false;
                        break;
                    }
                }
                if (noCheck)
                    resultIndex += 2;
            }

            if (resultIndex != -1)
                list.Add(KingBitBoardHelper.CastlingMoves[(int)this.color][(int)this.position][resultIndex]);

            return list;
        }