Exemple #1
0
        private bool Capture(Position position)
        {
            if (!ChessBoard.IsLegalBoardPosition(position))
            {
                return(false);
            }

            //Do not allow to move if no piece to capture
            if (ChessBoard.Cells[position.XCoordinate, position.YCoordinate].IsEmpty)
            {
                return(false);
            }

            return(TryMoveChessPiece(position));
        }
Exemple #2
0
        public override bool IsLegalMove(MovementType movementType, int newX, int newY)
        {
            // we need to know the board configuration first.
            if ((ChessBoard == null) || (!ChessBoard.IsLegalBoardPosition(newX, newY)))
            {
                return(false);
            }

            // Capture or move? This is the question! :)
            // Capture is not yet considered for the task, but still, we leave here an open door for future impleemntations
            // Move   : X remains the same and Y increments for White, decrements for Black
            // Capture: X varies by 1 and Y increments for White, decrements for Black, only if target pos is occupied by opponent piece
            return
                ((newY == YCoordinate + PawnMoveDirection) && (
                     (newX == XCoordinate) ||                   // Move
                     ((movementType == MovementType.Capture) && // Capture
                      (Math.Abs(newX - XCoordinate) == 1) &&
                      ((ChessBoard.GetPieceAtPosition(newX, newY) ?? this).PieceColor != this.PieceColor)
                     )
                     ));
        }
        protected bool TryMoveChessPiece(Position newPosition)
        {
            if (!ChessBoard.IsLegalBoardPosition(newPosition))
            {
                return(false);
            }

            IChessBoardCell cell = ChessBoard.Cells[newPosition.XCoordinate, newPosition.YCoordinate];

            if (!cell.IsEmpty)
            {
                if (cell.ContainingChessPiece.Color == Color)
                {
                    return(false);
                }

                cell.ContainingChessPiece.Capture();
            }

            parentCell.RemoveChessPiece();
            cell.PlaceChessPiece(this);
            return(true);
        }
        public void IsLegalBoardPosition_True_X_equals_0_Y_equals_0()
        {
            var isValidPosition = chessBoard.IsLegalBoardPosition(new Position(0, 0));

            Assert.IsTrue(isValidPosition);
        }