Ejemplo n.º 1
0
        /*  IsLegalMove() for the king also calls into the ChessBoard to see if
         *  making that move would put it in check.
         */

        public MovementResult IsLegalMove(int newX, int newY)
        {
            if (!ChessBoard.IsLegalBoardPosition(newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That board position does not exist."
                       }
            }
            ;

            if ((Math.Abs(newX - XCoordinate) > 1) || (Math.Abs(newY - YCoordinate) > 1))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That is not a valid move for this piece."
                       }
            }
            ;

            if (!ChessBoard.IsPositionSafeForKing(this, newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "A king cannot move into check."
                       }
            }
            ;

            if (ChessBoard.IsPieceAt(newX, newY))
            {
                if (ChessBoard.PieceAt(newX, newY).PieceColor == PieceColor)
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = false,
                               ReasonForFailure = "There is a piece of the same color already there."
                           }
                }
            }
            ;

            return(new MovementResult()
            {
                WasSuccessful = true,
                ReasonForFailure = ""
            });
        }