예제 #1
0
        private void UndoMovePiece(Position origin, Position destination, Piece capturedPiece)
        {
            Piece movingPiece = board.PickUpPiece(destination);

            movingPiece.DecreaseMovementAmount();
            if (capturedPiece != null)
            {
                capturedPiece.DecreaseMovementAmount();
                board.PlacePiece(capturedPiece, destination);
                capturedPiece.DecreaseMovementAmount();
                capturedPieces.Remove(capturedPiece);
            }
            board.PlacePiece(movingPiece, origin);
            movingPiece.DecreaseMovementAmount();

            // # Special move: castling
            if (movingPiece is King)
            {
                // Castling king side
                if (destination.column == origin.column + 2)
                {
                    Position rookOrigin      = new Position(origin.line, origin.column + 3);
                    Position rookDestination = new Position(origin.line, origin.column + 1);
                    Piece    rook            = board.PickUpPiece(rookDestination);
                    rook.DecreaseMovementAmount();
                    board.PlacePiece(rook, rookOrigin);
                    rook.DecreaseMovementAmount();
                }

                // Castling queen side
                if (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.PickUpPiece(rookDestination);
                    rook.DecreaseMovementAmount();
                    board.PlacePiece(rook, rookOrigin);
                    rook.DecreaseMovementAmount();
                }
            }

            // # Special move: en passant
            if (movingPiece is Pawn)
            {
                if (origin.column != destination.column && capturedPiece == enPassantVulnerable)
                {
                    Piece    pawn = board.PickUpPiece(destination);
                    Position pawnPos;
                    if (movingPiece.color == Color.White)
                    {
                        pawnPos = new Position(3, destination.column);
                    }
                    else
                    {
                        pawnPos = new Position(4, destination.column);
                    }
                    board.PlacePiece(pawn, pawnPos);
                    pawn.DecreaseMovementAmount();
                }
            }
        }