示例#1
0
 /// <summary>
 /// Determine whether the given move is valid for this chess piece on the given chess board.
 /// </summary>
 /// <param name="chessBoard">The chess board on which the chess piece is being moved.</param>
 /// <param name="from">The board position from which the chess piece is being moved.</param>
 /// <param name="to">The board position to which the chess piece is being moved.</param>
 /// <returns></returns>
 internal virtual MoveResult ValidateMove(
     ChessBoard chessBoard,
     BoardPosition from,
     BoardPosition to,
     out ExceptionReasonDetail exceptionReasonDetail)
 {
     exceptionReasonDetail = ExceptionReasonDetail.None;
     return MoveResult.Illegal;
 }
示例#2
0
        internal override MoveResult ValidateMove(
            ChessBoard chessBoard,
            BoardPosition from,
            BoardPosition to,
            out ExceptionReasonDetail exceptionReasonDetail)
        {
            exceptionReasonDetail = ExceptionReasonDetail.None;
            MoveResult result = MoveResult.Illegal;

            int fileDiff = Math.Abs(from.InternalFile - to.InternalFile);
            int forward = (this.Colour == Colour.White) ? to.InternalRank - from.InternalRank : from.InternalRank - to.InternalRank;

            Square toSquare = chessBoard.GetSquare(to);

            if (forward == 1) {

                if (fileDiff == 0) {
                    if (toSquare.ChessPiece != null) {
                        result = MoveResult.Collision;
                    }
                    else {
                        result = MoveResult.Valid;
                    }
                }

                if (fileDiff == 1) {
                    if (toSquare.ChessPiece != null && toSquare.ChessPiece.Colour != this.Colour) {
                        result = MoveResult.Valid;
                    }
                    else {
                        exceptionReasonDetail = ExceptionReasonDetail.PawnCannotMoveDiagonallyUnlessCapturing;
                    }
                }
            }

            if (forward == 2 && fileDiff == 0) {
                bool onHomeRank = (this.Colour == Colour.White && from.Rank == 2) || (this.Colour == Colour.Black && from.Rank == 7);
                if (onHomeRank && chessBoard.Round == 1) {
                    int inbetweenFile = from.InternalFile;
                    int inbetweenRank = (this.Colour == Colour.White) ? from.InternalRank + 1 : from.InternalRank - 1;
                    Square inbetweenSquare = chessBoard.GetSquare(new BoardPosition(inbetweenFile, inbetweenRank));
                    if (inbetweenSquare.ChessPiece != null) {
                        result = MoveResult.Collision;
                    }
                    else {
                        if (toSquare.ChessPiece == null) {
                            result = MoveResult.Valid;
                        }
                    }
                }
                else {
                    exceptionReasonDetail = ExceptionReasonDetail.PawnCannotMoveTwoSpacesAtThisTime;
                }
            }

            return result;
        }
示例#3
0
        internal override MoveResult ValidateMove(
             ChessBoard chessBoard,
             BoardPosition from,
             BoardPosition to,
             out ExceptionReasonDetail exceptionReasonDetail)
        {
            exceptionReasonDetail = ExceptionReasonDetail.None;
             MoveResult result = MoveResult.Illegal;

             int fileDiff = Math.Abs(from.InternalFile - to.InternalFile);
             int rankDiff = Math.Abs(from.InternalRank - to.InternalRank);

             if ((fileDiff == 2 && rankDiff == 1) || (fileDiff == 1 && rankDiff == 2)) {
                 Square toSquare = chessBoard.GetSquare(to);
                 if (toSquare.ChessPiece == null || toSquare.ChessPiece.Colour != this.Colour) {
                     result = MoveResult.Valid;
                 }
             }

             return result;
        }
 public ChessBoardException(ExceptionReason exceptionReason, ExceptionReasonDetail exceptionReasonDetail, Exception innerException)
     : base(MessageFromExceptionReason(exceptionReason), innerException)
 {
     this.ExceptionReason = exceptionReason;
     this.ExceptionReasonDetail = exceptionReasonDetail;
 }
 public ChessBoardException(ExceptionReason exceptionReason, ExceptionReasonDetail exceptionReasonDetail)
     : this(exceptionReason, exceptionReasonDetail, null)
 {
 }