示例#1
0
        private Piece ExecuteMovement(Position origin, Position destination)
        {
            Piece pieceToMove = Board.RemovePieceFromBoard(origin);

            pieceToMove.IncrementMovementQuantity();
            Piece pieceToCapture = Board.RemovePieceFromBoard(destination);

            if (pieceToCapture != null)
            {
                piecesCaptured.Add(pieceToCapture);
            }
            Board.AddPieceToBoard(pieceToMove, destination);
            // Special Play - Castling Short
            if (pieceToMove is King && destination.Column == origin.Column + 2)
            {
                ExecuteCastlingShort(origin, pieceToMove);
            }
            // Special Play - Castling Long
            if (pieceToMove is King && destination.Column == origin.Column - 2)
            {
                ExecuteCastlingLong(origin, pieceToMove);
            }
            // Special Play - En Passant
            if (pieceToCapture == null && pieceToMove is Pawn && origin.Column != destination.Column)
            {
                pieceToCapture = ExecuteEnPassant(destination, pieceToMove, pieceToCapture);
            }
            return(pieceToCapture);
        }
示例#2
0
        private void ExecuteCastlingLong(Position origin, Piece pieceToMove)
        {
            Position rookOrigin      = new Position(origin.Line, origin.Column - 4);
            Position rookDestination = new Position(origin.Line, origin.Column - 1);
            Piece    rook            = Board.RemovePieceFromBoard(rookOrigin);

            rook.IncrementMovementQuantity();
            Board.AddPieceToBoard(rook, rookDestination);
        }
示例#3
0
        public void ExecutePlay(Position origin, Position destination)
        {
            Piece capturedPiece = ExecuteMovement(origin, destination);

            if (IsKingInCheck(CurrentPlayer))
            {
                UndoMovement(origin, destination, capturedPiece);
                throw new BoardException("You cannot place your own King in check. Your play will be undone.");
            }

            // Get a reference for the piece moved in this turn
            Piece pieceMovedInTheTurn = Board.Piece(destination);

            // Special Play - Promotion
            if (pieceMovedInTheTurn is Pawn)
            {
                if (pieceMovedInTheTurn.Color == PieceColor.White && destination.Line == 0 || pieceMovedInTheTurn.Color == PieceColor.Black && destination.Line == 7)
                {
                    pieceMovedInTheTurn = Board.RemovePieceFromBoard(destination);
                    piecesInGame.Remove(pieceMovedInTheTurn);
                    Piece queen = new Queen(pieceMovedInTheTurn.Color, Board);
                    Board.AddPieceToBoard(queen, destination);
                    piecesInGame.Add(queen);
                }
            }

            if (IsKingInCheck(FindAdversaryColor(CurrentPlayer)))
            {
                IsInCheck = true;
            }
            else
            {
                IsInCheck = false;
            }
            if (IsKingInCheckMate(FindAdversaryColor(CurrentPlayer)))
            {
                MatchIsFinished = true;
            }
            else
            {
                Turn++;
                ChangePlayer();
            }

            // Special Play - En Passant
            if (pieceMovedInTheTurn is Pawn && destination.Line == origin.Line - 2 || destination.Line == origin.Line + 2)
            {
                VulnerableToEnPassant = pieceMovedInTheTurn;
            }
            else
            {
                VulnerableToEnPassant = null;
            }
        }
示例#4
0
 private void UndoCastlingLong(Piece pieceToGoBack, Position origin, Position destination)
 {
     if (pieceToGoBack is King && destination.Column == origin.Column - 2)
     {
         Position rookOrigin      = new Position(origin.Line, origin.Column - 4);
         Position rookDestination = new Position(origin.Line, origin.Column - 1);
         Piece    rook            = Board.RemovePieceFromBoard(rookDestination);
         rook.DecrementMovementQuantity();
         Board.AddPieceToBoard(rook, rookOrigin);
     }
 }
示例#5
0
        private void UndoMovement(Position origin, Position destination, Piece capturedPiece)
        {
            Piece pieceToGoBack = Board.RemovePieceFromBoard(destination);

            pieceToGoBack.DecrementMovementQuantity();
            if (capturedPiece != null)
            {
                Board.AddPieceToBoard(capturedPiece, destination);
                piecesCaptured.Remove(capturedPiece);
            }
            Board.AddPieceToBoard(pieceToGoBack, origin);
            // Special Play - Castling Short
            UndoCastlingShort(pieceToGoBack, origin, destination);
            // Special Play - Castling Long
            UndoCastlingLong(pieceToGoBack, origin, destination);
            // Special Play - En Passant
            UndoEnPassant(pieceToGoBack, capturedPiece, origin, destination);
        }
示例#6
0
 private void UndoEnPassant(Piece pieceToGoBack, Piece capturedPiece, Position origin, Position destination)
 {
     // The UndoMovement method returns the captured Pawn to the wrong position after a EnPessant play is undone
     // The method bellow will take this Pawn and move it from the wrong position to the right position
     if (pieceToGoBack is Pawn && origin.Column != destination.Column && capturedPiece == VulnerableToEnPassant)
     {
         Piece    capturedPawn = Board.RemovePieceFromBoard(destination);
         Position capturedPawnOrigin;
         if (pieceToGoBack.Color == PieceColor.White)
         {
             capturedPawnOrigin = new Position(3, destination.Column);
         }
         else
         {
             capturedPawnOrigin = new Position(4, destination.Column);
         }
         Board.AddPieceToBoard(capturedPawn, capturedPawnOrigin);
     }
 }
示例#7
0
 private void PlaceNewPiece(char column, int line, Piece piece)
 {
     Board.AddPieceToBoard(piece, new ChessPosition(column, line).ToPosition());
     piecesInGame.Add(piece);
 }