Ejemplo n.º 1
0
        public bool PerformCastlingIfCastlingMove(ChessMove chessMove,
                                                  IEnumerable <IChessPiece> pieces)
        {
            if (chessMove.PawnPromotion != ChessPieceType.Pawn)
            {
                return(false);
            }

            var movedPiece = pieces
                             .First(p => p.Position == chessMove.StartingPosition);

            if (movedPiece.PieceType != ChessPieceType.King)
            {
                return(false);
            }
            if (CalculateDifference(chessMove.StartingPosition, chessMove.FinishedPosition) == 1)
            {
                return(false);
            }

            var rook = pieces
                       .First(p => p.Position == chessMove.FinishedPosition);

            rook.Position = CalculateRookPositionAfterCastling(rook.Position,
                                                               movedPiece.Position);
            movedPiece.Position = CalculateKingPositionAfterCastling(rook.Position,
                                                                     movedPiece.Position);

            rook.IncrementMoveCounter();
            movedPiece.IncrementMoveCounter();
            history.Add(chessMove);
            return(true);
        }
Ejemplo n.º 2
0
        public IChessPiece PerformEnPassantIfApplicable(ChessMove chessMove,
                                                        IEnumerable <IChessPiece> pieces)
        {
            if (chessMove.PawnPromotion != ChessPieceType.Pawn)
            {
                return(null);
            }
            var pieceToMove = pieces
                              .First(p => p.Position == chessMove.StartingPosition);

            if (pieceToMove.PieceType != ChessPieceType.Pawn)
            {
                return(null);
            }
            if (!IsDiagonalMove(chessMove))
            {
                return(null);
            }
            if (IsFinishedPositionTaken(chessMove, pieces))
            {
                return(null);
            }

            pieceToMove.IncrementMoveCounter();
            pieceToMove.Position = chessMove.FinishedPosition;
            history.Add(chessMove.ReturnWithCaptureAsTrue());

            return(FindPawnToRemove(chessMove, pieces));
        }
Ejemplo n.º 3
0
        public IChessPiece Move(ChessMove chessMove, IEnumerable <IChessPiece> pieces)
        {
            if (piecePromoter.PromoteIfPromotionMove(chessMove, pieces))
            {
                return(null);
            }
            if (castlingMover.PerformCastlingIfCastlingMove(chessMove, pieces))
            {
                return(null);
            }

            var pieceToRemove = enPassantMover.PerformEnPassantIfApplicable(chessMove, pieces);

            if (pieceToRemove != null)
            {
                return(pieceToRemove);
            }

            var pieceToMove = pieces
                              .First(p => p.Position == chessMove.StartingPosition);

            pieceToRemove = pieces
                            .FirstOrDefault(p => p.Position == chessMove.FinishedPosition);
            pieceToMove.Position = chessMove.FinishedPosition;
            pieceToMove.IncrementMoveCounter();
            if (pieceToRemove != null)
            {
                chessMove = chessMove.ReturnWithCaptureAsTrue();
            }
            history.Add(chessMove);
            return(pieceToRemove);
        }